|
15 | 15 | from lib.cuckoo.common.constants import CUCKOO_ROOT |
16 | 16 | from math import log |
17 | 17 |
|
| 18 | +global PLOTTING_LIBRARIES_IMPORTED |
| 19 | +PLOTTING_LIBRARIES_IMPORTED = False |
| 20 | + |
| 21 | +if Config("cuckooml").cuckooml.plotting: |
| 22 | + try: |
| 23 | + import matplotlib.pyplot as plt |
| 24 | + import seaborn as sns |
| 25 | + PLOTTING_LIBRARIES_IMPORTED = True |
| 26 | + except ImportError, e: |
| 27 | + print >> sys.stderr, "Plotting libraries \ |
| 28 | + (matplotlib and seaborn) are not available." |
| 29 | + print >> sys.stderr, e |
| 30 | + PLOTTING_LIBRARIES_IMPORTED = False |
| 31 | + |
| 32 | + |
18 | 33 | try: |
19 | | - import matplotlib.pyplot as plt |
20 | 34 | import numpy as np |
21 | 35 | import pandas as pd |
22 | | - import seaborn as sns |
23 | 36 | from hdbscan import HDBSCAN |
24 | 37 | from sklearn import metrics |
25 | 38 | from sklearn.cluster import DBSCAN |
@@ -797,6 +810,19 @@ def filter_dataset(self, dataset=None, feature_coverage=0.1, |
797 | 810 |
|
798 | 811 | def detect_abnormal_behaviour(self, count_dataset=None, figures=True): |
799 | 812 | """Detect samples that behave significantly different than others.""" |
| 813 | + |
| 814 | + # Safety check for plotting |
| 815 | + if not PLOTTING_LIBRARIES_IMPORTED and figures: |
| 816 | + print >> sys.stderr, "Warning: plotting libraries were not imported. \n" \ |
| 817 | + "Plots wont be produced." |
| 818 | + |
| 819 | + if not Config("cuckooml").cuckooml.plotting: |
| 820 | + print >> sys.stderr, " Plotting is disabled in cuckooml config." |
| 821 | + else: |
| 822 | + print >> sys.stderr, "Plotting libraries are missing." |
| 823 | + figures = False |
| 824 | + |
| 825 | + |
800 | 826 | if count_dataset is None: |
801 | 827 | # Pull all count features |
802 | 828 | count_features = self.feature_category(":count:") |
@@ -1133,6 +1159,18 @@ def performance_metric(clustering, labels, data, noise): |
1133 | 1159 |
|
1134 | 1160 | def clustering_label_distribution(self, clustering, labels, plot=False): |
1135 | 1161 | """Get statistics about number of ground truth labels per cluster.""" |
| 1162 | + |
| 1163 | + # Safety check for plotting |
| 1164 | + if not PLOTTING_LIBRARIES_IMPORTED and plot: |
| 1165 | + print >> sys.stderr, "Warning: plotting libraries were not imported. \n" \ |
| 1166 | + "Plots wont be produced." |
| 1167 | + |
| 1168 | + if not Config("cuckooml").cuckooml.plotting: |
| 1169 | + print >> sys.stderr, " Plotting is disabled in cuckooml config." |
| 1170 | + else: |
| 1171 | + print >> sys.stderr, "Plotting libraries are missing." |
| 1172 | + plot = False |
| 1173 | + |
1136 | 1174 | cluster_ids = set(clustering["label"].tolist()) |
1137 | 1175 | labels_ids = set(labels["label"].tolist()) |
1138 | 1176 | cluster_distribution = {} |
|
0 commit comments