Example #1
0
 def displayLabel(self, barplot, label):
     label_bool = labels_tools.labelStringToBoolean(label)
     ranges = [[x for x in l if x['ground_truth_label'] == label_bool]
               for l in self.ranges]
     dataset = PlotDataset(list(map(len, ranges)), label)
     dataset.set_color(colors_tools.get_label_color(label))
     barplot.add_dataset(dataset)
Example #2
0
def getTopModelFeatures(exp_id, size, train_test, fold_id):
    exp = updateCurrentExperiment(exp_id)
    directory = exp.output_dir()
    if fold_id != 'None' and fold_id != 'all':
        directory = path.join(directory, fold_id)
    directory = path.join(directory, train_test)
    filename = path.join(directory, 'model_coefficients.csv')
    with open(filename, 'r') as f:
        coefficients_df = pd.read_csv(f,
                                      header=0,
                                      index_col=0,
                                      nrows=int(size))
        coefficients = list(coefficients_df['mean'])
        features_ids = coefficients_df.index
        tooltip_data = []
        user_ids = []
        for feature_id in features_ids:
            query = session.query(FeaturesAlchemy)
            query = query.filter(FeaturesAlchemy.id == int(feature_id))
            row = query.one()
            tooltip_data.append(row.name)
            user_ids.append(row.user_id)
        barplot = BarPlot(user_ids)
        dataset = PlotDataset(coefficients, None)
        score = exp.exp_conf.core_conf.classifier_conf.featureImportance()
        if score == 'weight':
            dataset.set_color(colors_tools.red)
        barplot.add_dataset(dataset)
        return jsonify(barplot.to_json(tooltip_data=tooltip_data))
Example #3
0
 def _gen_label_plot_dataset(self, instances, label):
     if label != 'unlabeled':
         instances = instances.getAnnotatedInstances(label=label)
     else:
         instances = instances.getUnlabeledInstances()
     values = instances.features.getValuesFromIndex(self.feature_index)
     dataset = PlotDataset(values, label)
     dataset.set_color(colors_tools.get_label_color(label))
     self.plot_datasets[label] = dataset
Example #4
0
 def _gen_binary_histogram(self):
     self.barplot = BarPlot(['0', '1'])
     for label, dataset in self.plot_datasets.items():
         if len(dataset.values) > 0:
             num_0 = sum(dataset.values == 0)
             num_1 = sum(dataset.values == 1)
             hist_dataset = PlotDataset([num_0, num_1], label)
             hist_dataset.set_color(dataset.color)
             self.barplot.add_dataset(hist_dataset)
Example #5
0
 def _gen_histogram(self):
     # 10 equal-width bins computed on all the data
     _, bin_edges = np.histogram(self.all_values, bins=10, density=False)
     x_labels = ['%.2f - %.2f' % (bin_edges[e], bin_edges[e+1])
                 for e in range(len(bin_edges) - 1)]
     self.barplot = BarPlot(x_labels)
     for label, dataset in self.plot_datasets.items():
         if len(dataset.values) > 0:
             hist, _ = np.histogram(dataset.values, bins=bin_edges,
                                    density=False)
             hist_dataset = PlotDataset(hist, label)
             hist_dataset.set_color(dataset.color)
             self.barplot.add_dataset(hist_dataset)
Example #6
0
 def executionTimeDisplay(self):
     lr = PlotDataset(None, 'Logistic Regression')
     lr.set_linestyle('dotted')
     nb = PlotDataset(None, 'Naive Bayes')
     nb.set_linestyle('dashed')
     v = [lr, nb]
     v.extend(Strategy.executionTimeDisplay(self))
     return v
Example #7
0
def getFamiliesBarplot(annotations_id, iteration, label):
    iteration = None if iteration == 'None' else int(iteration)
    family_counts = annotations_db_tools.getFamiliesCounts(session,
                                                           annotations_id,
                                                           iter_max=iteration,
                                                           label=label)
    df = pd.DataFrame({
        'families': list(family_counts.keys()),
        'counts': [family_counts[k] for k in list(family_counts.keys())]
        })
    matrix_tools.sort_data_frame(df, 'families', ascending=True, inplace=True)
    barplot = BarPlot(list(df['families']))
    dataset = PlotDataset(list(df['counts']), 'Num. Instances')
    dataset.set_color(colors_tools.get_label_color(label))
    barplot.add_dataset(dataset)
    return jsonify(barplot.to_json())
Example #8
0
 def plotEvolutionMonitoring(self, evolution_file, monitoring_dir):
     data = self.loadEvolutionMonitoring(evolution_file)
     if self.labels_families == 'labels':
         title = 'Labels Suggestions Accuracy'
     elif self.labels_families == 'families':
         title = 'Families Suggestions Accuracy'
     plot = PlotDataset(data['true_suggestions'] / data['num_suggestions'],
                        title)
     iterations = list(range(self.monitoring.iteration_number))
     plt.clf()
     max_value = 1
     plt.plot(iterations,
              plot.values,
              label=plot.label,
              color=plot.color,
              linewidth=plot.linewidth,
              marker=plot.marker)
     plt.ylim(0, max_value)
     plt.xlabel('Iteration')
     plt.ylabel('Suggestions Accuracy')
     lgd = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                      loc=3,
                      ncol=2,
                      mode='expand',
                      borderaxespad=0.,
                      fontsize='large')
     filename = '_'.join(
         [self.labels_families, self.kind, 'suggestions.png'])
     filename = path.join(monitoring_dir, filename)
     plt.savefig(filename, bbox_extra_artists=(lgd, ), bbox_inches='tight')
     plt.clf()
     self.data = data
Example #9
0
 def display(self, directory):
     labels = [
         '0-10%', '10-20%', '20-30%', '30-40%', '40-50%', '50-60%',
         '60-70%', '70-80%', '80-90%', '90-100%'
     ]
     barplot = BarPlot(labels)
     if not self.has_ground_truth:
         dataset = PlotDataset(list(map(len, self.ranges)), 'numInstances')
         dataset.set_color(colors_tools.get_label_color('all'))
         barplot.add_dataset(dataset)
     else:
         self.displayLabel(barplot, labels_tools.MALICIOUS)
         self.displayLabel(barplot, labels_tools.BENIGN)
     filename = path.join(directory, 'predictions_barplot.json')
     with open(filename, 'w') as f:
         barplot.export_json(f)
Example #10
0
def getTopWeightedFeatures(exp_id, inst_exp_id, instance_id, size, fold_id):
    if fold_id == 'all':
        return None
    instance_id = int(instance_id)
    exp = updateCurrentExperiment(exp_id)
    inst_exp = updateCurrentExperiment(inst_exp_id)
    # get the features
    features_from_exp = FeaturesFromExp(inst_exp)
    features_names, features_values = features_from_exp.get_instance(
        instance_id)
    features_values = [float(value) for value in features_values]
    # get the pipeline with scaler and logistic model
    experiment_dir = exp.output_dir()
    if fold_id != 'None':
        experiment_dir = path.join(experiment_dir, fold_id)
    pipeline = joblib.load(path.join(experiment_dir, 'model', 'model.out'))
    # scale the features
    scaled_values = pipeline.named_steps['scaler'].transform(
        np.reshape(features_values, (1, -1)))
    weighted_values = np.multiply(scaled_values,
                                  pipeline.named_steps['model'].coef_)
    features = list(
        map(lambda name, value, w_value: (name, value, w_value),
            features_names, features_values, weighted_values[0]))
    features.sort(key=lambda tup: abs(tup[2]))
    features = features[:-int(size) - 1:-1]

    features_names = [x[0] for x in features]
    features_values = [x[1] for x in features]
    features_weighted_values = [x[2] for x in features]
    labels = [str(name) for name in features_names]
    tooltips = [
        '%s (%.2f)' % (name, features_values[i])
        for i, name in enumerate(features_names)
    ]
    barplot = BarPlot(labels)
    dataset = PlotDataset(features_weighted_values, None)
    dataset.set_color(colors_tools.red)
    barplot.add_dataset(dataset)
    return jsonify(barplot.to_json(tooltip_data=tooltips))
Example #11
0
 def plotEvolutionMonitoring(self, monitoring_dir):
     iterations = list(range(1, self.monitoring.iteration_number + 1))
     plt.clf()
     # Labels
     data = self.labels_accuracy.high_confidence_counts.data
     values = data['true_suggestions'] / data['num_suggestions']
     plot = PlotDataset(values, 'Labels Suggestions')
     max_value = 1
     plt.plot(iterations,
              plot.values,
              label=plot.label,
              color=plot.color,
              linewidth=plot.linewidth,
              marker=plot.marker)
     # Families
     data = self.families_accuracy.high_confidence_counts.data
     values = data['true_suggestions'] / data['num_suggestions']
     plot = PlotDataset(values, 'Families Suggestions')
     max_value = 1
     plt.plot(iterations,
              plot.values,
              label=plot.label,
              color='purple',
              linewidth=plot.linewidth,
              marker=plot.marker)
     # Plot
     plt.ylim(0, max_value)
     plt.xlabel('Iteration')
     plt.ylabel('Suggestions Accuracy')
     lgd = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                      loc=3,
                      ncol=2,
                      mode='expand',
                      borderaxespad=0.,
                      fontsize='large')
     filename = path.join(
         monitoring_dir, 'labels_families_high_confidence_suggestions.png')
     plt.savefig(filename, bbox_extra_artists=(lgd, ), bbox_inches='tight')
     plt.clf()
Example #12
0
def getClusterStats(experiment_id):
    experiment = updateCurrentExperiment(experiment_id)
    clustering = ClusteringExp.from_json(experiment.output_dir())
    num_clusters = clustering.num_clusters
    num_instances_v = []
    labels = []
    for c in range(num_clusters):
        instances_in_cluster = clustering.clusters[c].instances_ids
        num_instances = len(instances_in_cluster)
        # the empty clusters are not displayed

        # if num_instances > 0:
        num_instances_v.append(num_instances)
        #labels.append('c_' + str(c))
        labels.append(clustering.clusters[c].label)
    barplot = BarPlot(labels)
    dataset = PlotDataset(num_instances_v, 'Num. Instances')
    barplot.add_dataset(dataset)
    return jsonify(barplot.to_json())
 def plotPerfEvolution(self, estimators, output_filename, data,
                       monitoring_dir):
     iterations = list(range(1, self.monitoring.iteration_number + 1))
     plt.clf()
     for estimator in estimators:
         plot = PlotDataset(data[estimator], estimator)
         plt.plot(iterations,
                  plot.values,
                  label=plot.label,
                  color=plot.color,
                  linewidth=plot.linewidth,
                  marker=plot.marker)
     plt.ylim(0, 1)
     plt.xlabel('Iteration')
     plt.ylabel('Performance')
     lgd = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                      loc=3,
                      ncol=3,
                      mode='expand',
                      borderaxespad=0.,
                      fontsize='large')
     filename = self.outputFilename(monitoring_dir, output_filename, 'png')
     plt.savefig(filename, bbox_extra_artists=(lgd, ), bbox_inches='tight')
     plt.clf()
Example #14
0
 def executionTimeDisplay(self):
     clustering = PlotDataset(None, 'Analysis')
     v = [clustering]
     v.extend(Strategy.executionTimeDisplay(self))
     return v
Example #15
0
 def executionTimeDisplay(self):
     uncertain = PlotDataset(None, 'Uncertain Queries')
     malicious = PlotDataset(None, 'Malicious Queries')
     malicious.set_linestyle('dotted')
     malicious.set_color(colors_tools.get_label_color(labels_tools.MALICIOUS))
     benign = PlotDataset(None, 'Benign Queries')
     benign.set_linestyle('dashed')
     benign.set_color(colors_tools.get_label_color(labels_tools.BENIGN))
     return [malicious, uncertain, benign]
Example #16
0
 def executionTimeDisplay(self):
     binary_model = PlotDataset(None, 'Binary model')
     v = [binary_model]
     v.extend(Strategy.executionTimeDisplay(self))
     return v
Example #17
0
 def executionTimeDisplay(self):
     generate_queries = PlotDataset(None, 'Queries generation')
     generate_queries.set_color('purple')
     return [generate_queries]