diff --git a/core/management/commands/importrois.py b/core/management/commands/importrois.py index a898127..9557b5d 100644 --- a/core/management/commands/importrois.py +++ b/core/management/commands/importrois.py @@ -12,12 +12,16 @@ def add_arguments(self, parser): parser.add_argument('directory', type=str, help='directory containing images') parser.add_argument('-c','--collection', type=str, help='image collection to create or add images to') parser.add_argument('-u','--user', type=str, help='username for any created annotations (user must exist)') + parser.add_argument('-f', '--flag', type=str, help='indicate whether annotation is manual or auto. Defaulted to manual, if not specified') + parser.add_argument('-cl', '--classifier', type=str, help='if auto annotated, indicate associated classifier') def handle(self, *args, **options): # handle arguments directory = options['directory'] collection_name = options.get('collection') username = options.get('user') + flag = options.get('flag') + classifier = options.get('classifier') or '' # validate arguments if not os.path.exists(directory): raise CommandError('specified directory does not exist') @@ -31,6 +35,19 @@ def handle(self, *args, **options): if collection_name is not None: collection, created = ImageCollection.objects.get_or_create( name=collection_name) + + auto = False + # If flag not specified, auto defaulted to False + if flag: + if not(flag == "auto" or flag == "manual"): + raise CommandError('values of flag should either be auto or manual') + if flag == "auto": + auto = True + if not classifier: + raise CommandError('classifier must be specified if auto annotated') + # if flag == manual, auto already set to False + + # scan directory and one level of subdirectories def scan(dir): result = [] @@ -61,7 +78,7 @@ def scan(dir): for roi_filename in rois: roi_path = os.path.join(directory, label_name, roi_filename) roi = ROI.objects.create_or_update_roi(roi_path, collection=collection) - Annotation.objects.create_or_verify(roi, label, user) + Annotation.objects.create_or_verify(roi, label, user, auto, classifier) diff --git a/core/migrations/0009_annotation_auto_annotation_classifier.py b/core/migrations/0009_annotation_auto_annotation_classifier.py new file mode 100644 index 0000000..3a2ccac --- /dev/null +++ b/core/migrations/0009_annotation_auto_annotation_classifier.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.5 on 2022-06-22 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0008_roi_winning_annotation'), + ] + + operations = [ + migrations.AddField( + model_name='annotation', + name='auto', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='annotation', + name='classifier', + field=models.TextField(blank=True), + ), + ] diff --git a/core/models.py b/core/models.py index a6d925b..e0d2075 100644 --- a/core/models.py +++ b/core/models.py @@ -168,18 +168,18 @@ class AnnotationManager(models.Manager): def get_queryset(self): return AnnotationQuerySet(self.model, using=self._db) - def create_annotation(self, roi, label, user): - return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power) + def create_annotation(self, roi, label, user, auto, classifier): + return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power, auto=auto, classifier=classifier) - def create_or_verify(self, roi, label, user): + def create_or_verify(self, roi, label, user, auto, classifier): with transaction.atomic(): try: - annotation = self.get(roi=roi, label=label, user=user) + annotation = self.get(roi=roi, label=label, user=user, auto=auto, classifier=classifier) annotation.verify() annotation.save() created = False except Annotation.DoesNotExist: - annotation = self.create_annotation(roi=roi, label=label, user=user) + annotation = self.create_annotation(roi=roi, label=label, user=user, auto=auto, classifier=classifier) created = True roi.cache_winning_annotation() return annotation, created @@ -195,6 +195,8 @@ class Annotation(models.Model): user_power = models.IntegerField(default=1) timestamp = models.DateTimeField(auto_now_add=True) verifications = models.IntegerField(default=0) + auto = models.BooleanField(default = False) + classifier = models.TextField(blank=True) objects = AnnotationManager()