From cdae9d8522554d1d5ad8c9ec727f456336d9c249 Mon Sep 17 00:00:00 2001 From: shravani-whoi Date: Wed, 22 Jun 2022 10:26:04 -0400 Subject: [PATCH 1/3] Add auto Flag to annotation Model indicating auto vs manual annotation --- core/migrations/0009_annotation_auto.py | 18 ++++++++++++++++++ core/models.py | 1 + 2 files changed, 19 insertions(+) create mode 100644 core/migrations/0009_annotation_auto.py diff --git a/core/migrations/0009_annotation_auto.py b/core/migrations/0009_annotation_auto.py new file mode 100644 index 0000000..b364368 --- /dev/null +++ b/core/migrations/0009_annotation_auto.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.5 on 2022-06-21 17:34 + +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), + ), + ] diff --git a/core/models.py b/core/models.py index a6d925b..8fc2757 100644 --- a/core/models.py +++ b/core/models.py @@ -195,6 +195,7 @@ 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) objects = AnnotationManager() From 0588bf756a18162c21bc6f9243773554f9a48570 Mon Sep 17 00:00:00 2001 From: shravani-whoi Date: Wed, 22 Jun 2022 11:56:04 -0400 Subject: [PATCH 2/3] take auto flag during importrois --- core/management/commands/importrois.py | 15 ++++++++++++++- core/models.py | 10 +++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/management/commands/importrois.py b/core/management/commands/importrois.py index a898127..9531966 100644 --- a/core/management/commands/importrois.py +++ b/core/management/commands/importrois.py @@ -12,12 +12,14 @@ 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') def handle(self, *args, **options): # handle arguments directory = options['directory'] collection_name = options.get('collection') username = options.get('user') + flag = options.get('flag') # validate arguments if not os.path.exists(directory): raise CommandError('specified directory does not exist') @@ -31,6 +33,17 @@ 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 flag == manual, auto already set to False + + # scan directory and one level of subdirectories def scan(dir): result = [] @@ -61,7 +74,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) diff --git a/core/models.py b/core/models.py index 8fc2757..1cdde55 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): + return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power, auto=auto) - def create_or_verify(self, roi, label, user): + def create_or_verify(self, roi, label, user, auto): with transaction.atomic(): try: - annotation = self.get(roi=roi, label=label, user=user) + annotation = self.get(roi=roi, label=label, user=user, auto=auto) 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) created = True roi.cache_winning_annotation() return annotation, created From f53e20516926781ba291abf031bef541c468609a Mon Sep 17 00:00:00 2001 From: shravani-whoi Date: Wed, 22 Jun 2022 14:06:26 -0400 Subject: [PATCH 3/3] add classifier testfield for auto annotated rois --- core/management/commands/importrois.py | 6 +++++- ... => 0009_annotation_auto_annotation_classifier.py} | 7 ++++++- core/models.py | 11 ++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) rename core/migrations/{0009_annotation_auto.py => 0009_annotation_auto_annotation_classifier.py} (62%) diff --git a/core/management/commands/importrois.py b/core/management/commands/importrois.py index 9531966..9557b5d 100644 --- a/core/management/commands/importrois.py +++ b/core/management/commands/importrois.py @@ -13,6 +13,7 @@ def add_arguments(self, parser): 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 @@ -20,6 +21,7 @@ def handle(self, *args, **options): 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') @@ -41,6 +43,8 @@ def handle(self, *args, **options): 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 @@ -74,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, auto) + Annotation.objects.create_or_verify(roi, label, user, auto, classifier) diff --git a/core/migrations/0009_annotation_auto.py b/core/migrations/0009_annotation_auto_annotation_classifier.py similarity index 62% rename from core/migrations/0009_annotation_auto.py rename to core/migrations/0009_annotation_auto_annotation_classifier.py index b364368..3a2ccac 100644 --- a/core/migrations/0009_annotation_auto.py +++ b/core/migrations/0009_annotation_auto_annotation_classifier.py @@ -1,4 +1,4 @@ -# Generated by Django 4.0.5 on 2022-06-21 17:34 +# Generated by Django 4.0.5 on 2022-06-22 17:03 from django.db import migrations, models @@ -15,4 +15,9 @@ class Migration(migrations.Migration): 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 1cdde55..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, auto): - return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power, auto=auto) + 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, auto): + def create_or_verify(self, roi, label, user, auto, classifier): with transaction.atomic(): try: - annotation = self.get(roi=roi, label=label, user=user, auto=auto) + 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, auto=auto) + annotation = self.create_annotation(roi=roi, label=label, user=user, auto=auto, classifier=classifier) created = True roi.cache_winning_annotation() return annotation, created @@ -196,6 +196,7 @@ class Annotation(models.Model): timestamp = models.DateTimeField(auto_now_add=True) verifications = models.IntegerField(default=0) auto = models.BooleanField(default = False) + classifier = models.TextField(blank=True) objects = AnnotationManager()