def get_labels_families(session, annotations_type, annotations_id, dataset_id, instance_ids=None, iter_max=None): if annotations_type == AnnotationsTypes.ground_truth: query = get_labels_families_gt(session, dataset_id, instance_ids) elif annotations_type == AnnotationsTypes.ground_truth_if_exists: if has_ground_truth(session, dataset_id): query = get_labels_families_gt(session, dataset_id, instance_ids) else: return {} elif annotations_type == AnnotationsTypes.partial: query = get_labels_families_partial(session, annotations_id, instance_ids, iter_max) else: assert (False) labels = {} for r in query.all(): label = label_bool_to_str(r.label) if label not in list(labels.keys()): labels[label] = {} labels[label][r.family] = 0 return labels
def change_family_label(session, annotations_id, label, family): query = session.query(AnnotationsAlchemy) query = query.filter(AnnotationsAlchemy.label == label) query = query.filter(AnnotationsAlchemy.family == family) query = query.filter(AnnotationsAlchemy.annotations_id == annotations_id) instances = query.all() bool_label = labels_tools.label_str_to_bool(label) new_label = labels_tools.label_bool_to_str(not bool_label) for instance in instances: instance.label = new_label session.flush()
def add_instance(self, instance_id, distance, label, family, annotated): self.instances_ids.append(instance_id) self.distances.append(distance) label = labels_tools.label_bool_to_str(label) if family is not None: key = label + '__' + family if key not in self.clusters_families_stats: self.clusters_families_stats[key] = 0 self.clusters_families_stats[key] += 1 if annotated: self.num_annotated_instances += 1
def getAnnotation(annotations_type, annotations_id, dataset_id, instance_id): annotations_type = AnnotationsTypes[annotations_type] annotation = annotations_db_tools.get_annotation(session, annotations_type, annotations_id, dataset_id, instance_id) if annotation is None: return jsonify({}) else: return jsonify({'label': label_bool_to_str(annotation[0]), 'family': annotation[1]})
def display(self, directory): labels = list(self.predictions.keys()) if self.multiclass: xlabels = labels else: xlabels = [label_bool_to_str(l) for l in labels] barplot = BarPlot(xlabels) if not self.has_ground_truth: self._display(barplot, labels) else: self._display(barplot, labels, error=False) self._display(barplot, labels, error=True) barplot.export_to_json(path.join(directory, 'pred_barplot.json'))
def export_annotations(self, output_filename, ground_truth=False): header = ['instance_id', 'label', 'family'] annotations = self.instances.get_annotations(ground_truth) with open(output_filename, 'w') as f: csv_writer = csv.writer(f) csv_writer.writerow(header) for instance_id in self.ids: print_id = self.get_print_id(instance_id) bool_label = annotations.get_label(instance_id) label = labels_tools.label_bool_to_str(bool_label) family = annotations.get_family(instance_id) row = [print_id, label, family] csv_writer.writerow(row)
def annotate_auto(self, iteration, kind): instances = iteration.datasets.instances label = instances.ground_truth.get_label(self.instance_id) label = labels_tools.label_bool_to_str(label) family = instances.ground_truth.get_family(self.instance_id) # Update the datasets self.update_datasets(iteration, label, family) # Update in the database if kind is not None: method = '%s__annotation' % kind else: method = 'annotation' annotations_conf = iteration.exp.exp_conf.annotations_conf annotations_db_tools.add_annotation(iteration.exp.session, annotations_conf.annotations_id, self.instance_id, label, family, iteration.iter_num, method)
def value_to_str(self): if self.multiclass: return str(self.value) else: return label_bool_to_str(self.value)
def update(self, instance_id, label, family): self.new_annotations = True self.instances.annotations.set_label_family(instance_id, label, family) # Update the annotation count self.num_annotations[label_bool_to_str(label)] += 1