From d52bbda75ef48789510fcf47d06ec4fe1679074f Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 08:36:05 +0900 Subject: [PATCH 1/6] Add boilerplate for labels app --- backend/labels/__init__.py | 0 backend/labels/admin.py | 3 +++ backend/labels/apps.py | 6 ++++++ backend/labels/migrations/__init__.py | 0 backend/labels/models.py | 3 +++ backend/labels/permissions.py | 0 backend/labels/serializers.py | 0 backend/labels/tests.py | 3 +++ backend/labels/urls.py | 0 backend/labels/views.py | 3 +++ 10 files changed, 18 insertions(+) create mode 100644 backend/labels/__init__.py create mode 100644 backend/labels/admin.py create mode 100644 backend/labels/apps.py create mode 100644 backend/labels/migrations/__init__.py create mode 100644 backend/labels/models.py create mode 100644 backend/labels/permissions.py create mode 100644 backend/labels/serializers.py create mode 100644 backend/labels/tests.py create mode 100644 backend/labels/urls.py create mode 100644 backend/labels/views.py diff --git a/backend/labels/__init__.py b/backend/labels/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/labels/admin.py b/backend/labels/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/backend/labels/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/labels/apps.py b/backend/labels/apps.py new file mode 100644 index 00000000..434a5c6d --- /dev/null +++ b/backend/labels/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class LabelsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'labels' diff --git a/backend/labels/migrations/__init__.py b/backend/labels/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/labels/models.py b/backend/labels/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/backend/labels/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/backend/labels/permissions.py b/backend/labels/permissions.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/labels/tests.py b/backend/labels/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/backend/labels/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/labels/urls.py b/backend/labels/urls.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/labels/views.py b/backend/labels/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/backend/labels/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From b26b0ec68c0186d16f0f6de0f68ce090ed0bcdb0 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 11:29:04 +0900 Subject: [PATCH 2/6] Move code related labels to labels app --- Pipfile | 2 +- backend/api/admin.py | 24 +- backend/api/managers.py | 75 ------ .../api/migrations/0031_auto_20220127_0032.py | 88 +++++++ backend/api/models.py | 116 +-------- backend/api/permissions.py | 14 - backend/api/serializers.py | 73 +----- backend/api/tests/test_models.py | 125 +-------- backend/api/tests/test_span.py | 142 ---------- backend/api/urls.py | 46 ---- backend/api/views/tasks/base.py | 63 ----- backend/api/views/tasks/category.py | 18 -- backend/api/views/tasks/relation.py | 34 +-- backend/api/views/tasks/span.py | 13 - backend/api/views/tasks/text.py | 13 - backend/app/settings.py | 1 + backend/app/urls.py | 1 + backend/auto_labeling/pipeline/labels.py | 3 +- backend/auto_labeling/tests/test_views.py | 2 +- backend/data_import/pipeline/labels.py | 6 +- backend/data_import/tests/test_tasks.py | 4 +- backend/labels/admin.py | 22 +- backend/labels/managers.py | 76 ++++++ backend/labels/migrations/0001_initial.py | 118 +++++++++ backend/labels/models.py | 118 ++++++++- backend/labels/permissions.py | 15 ++ backend/labels/serializers.py | 69 +++++ backend/labels/tests.py | 3 - backend/labels/tests/__init__.py | 0 .../{api => labels}/tests/test_category.py | 15 +- backend/labels/tests/test_span.py | 246 ++++++++++++++++++ .../{api => labels}/tests/test_text_label.py | 17 +- .../tests/test_views.py} | 9 +- backend/labels/urls.py | 50 ++++ backend/labels/views.py | 126 ++++++++- backend/metrics/views.py | 3 +- 36 files changed, 981 insertions(+), 769 deletions(-) create mode 100644 backend/api/migrations/0031_auto_20220127_0032.py delete mode 100644 backend/api/tests/test_span.py create mode 100644 backend/labels/managers.py create mode 100644 backend/labels/migrations/0001_initial.py delete mode 100644 backend/labels/tests.py create mode 100644 backend/labels/tests/__init__.py rename backend/{api => labels}/tests/test_category.py (90%) create mode 100644 backend/labels/tests/test_span.py rename backend/{api => labels}/tests/test_text_label.py (86%) rename backend/{api/tests/api/test_annotation.py => labels/tests/test_views.py} (97%) diff --git a/Pipfile b/Pipfile index 1ffe7096..19394288 100644 --- a/Pipfile +++ b/Pipfile @@ -58,7 +58,7 @@ python_version = "3.8" [scripts] isort = "isort api -c --skip migrations" -flake8 = "flake8 --filename \"*.py\" --extend-exclude \"api/migrations\"" +flake8 = "flake8 --filename \"*.py\" --extend-exclude \"*/migrations\"" wait_for_db = "python manage.py wait_for_db" test = "python manage.py test --pattern=\"test*.py\"" migrate = "python manage.py migrate" diff --git a/backend/api/admin.py b/backend/api/admin.py index 3c10304d..b72ffdb7 100644 --- a/backend/api/admin.py +++ b/backend/api/admin.py @@ -1,8 +1,8 @@ from django.contrib import admin -from .models import (Category, CategoryType, Comment, Example, Project, - Seq2seqProject, SequenceLabelingProject, Span, SpanType, - Tag, TextClassificationProject, TextLabel) +from .models import (CategoryType, Comment, Example, Project, Seq2seqProject, + SequenceLabelingProject, SpanType, Tag, + TextClassificationProject) class LabelAdmin(admin.ModelAdmin): @@ -31,21 +31,6 @@ class ProjectAdmin(admin.ModelAdmin): search_fields = ('name',) -class SpanAdmin(admin.ModelAdmin): - list_display = ('example', 'label', 'start_offset', 'user') - ordering = ('example',) - - -class CategoryAdmin(admin.ModelAdmin): - list_display = ('example', 'label', 'user') - ordering = ('example',) - - -class TextLabelAdmin(admin.ModelAdmin): - list_display = ('example', 'text', 'user') - ordering = ('example',) - - class TagAdmin(admin.ModelAdmin): list_display = ('project', 'text', ) ordering = ('project', 'text', ) @@ -58,9 +43,6 @@ class CommentAdmin(admin.ModelAdmin): search_fields = ('user',) -admin.site.register(Category, CategoryAdmin) -admin.site.register(Span, SpanAdmin) -admin.site.register(TextLabel, TextLabelAdmin) admin.site.register(CategoryType, CategoryTypeAdmin) admin.site.register(SpanType, SpanTypeAdmin) admin.site.register(Example, ExampleAdmin) diff --git a/backend/api/managers.py b/backend/api/managers.py index eac6fd74..f7192a92 100644 --- a/backend/api/managers.py +++ b/backend/api/managers.py @@ -1,81 +1,6 @@ from django.db.models import Count, Manager -class AnnotationManager(Manager): - - def calc_label_distribution(self, examples, members, labels): - """Calculate label distribution. - - Args: - examples: example queryset. - members: user queryset. - labels: label queryset. - - Returns: - label distribution per user. - - Examples: - >>> self.calc_label_distribution(examples, members, labels) - {'admin': {'positive': 10, 'negative': 5}} - """ - distribution = {member.username: {label.text: 0 for label in labels} for member in members} - items = self.filter(example_id__in=examples)\ - .values('user__username', 'label__text')\ - .annotate(count=Count('label__text')) - for item in items: - username = item['user__username'] - label = item['label__text'] - count = item['count'] - distribution[username][label] = count - return distribution - - def get_labels(self, label, project): - if project.collaborative_annotation: - return self.filter(example=label.example) - else: - return self.filter(example=label.example, user=label.user) - - def can_annotate(self, label, project) -> bool: - raise NotImplementedError('Please implement this method in the subclass') - - def filter_annotatable_labels(self, labels, project): - return [label for label in labels if self.can_annotate(label, project)] - - -class CategoryManager(AnnotationManager): - - def can_annotate(self, label, project) -> bool: - is_exclusive = project.single_class_classification - categories = self.get_labels(label, project) - if is_exclusive: - return not categories.exists() - else: - return not categories.filter(label=label.label).exists() - - -class SpanManager(AnnotationManager): - - def can_annotate(self, label, project) -> bool: - overlapping = getattr(project, 'allow_overlapping', False) - spans = self.get_labels(label, project) - if overlapping: - return True - for span in spans: - if span.is_overlapping(label): - return False - return True - - -class TextLabelManager(AnnotationManager): - - def can_annotate(self, label, project) -> bool: - texts = self.get_labels(label, project) - for text in texts: - if text.is_same_text(label): - return False - return True - - class ExampleManager(Manager): def bulk_create(self, objs, batch_size=None, ignore_conflicts=False): diff --git a/backend/api/migrations/0031_auto_20220127_0032.py b/backend/api/migrations/0031_auto_20220127_0032.py new file mode 100644 index 00000000..1a65c832 --- /dev/null +++ b/backend/api/migrations/0031_auto_20220127_0032.py @@ -0,0 +1,88 @@ +# Generated by Django 3.2.11 on 2022-01-27 00:32 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0030_delete_autolabelingconfig'), + ] + + operations = [ + migrations.SeparateDatabaseAndState( + state_operations=[ + migrations.AlterUniqueTogether( + name='category', + unique_together=None, + ), + migrations.RemoveField( + model_name='category', + name='example', + ), + migrations.RemoveField( + model_name='category', + name='label', + ), + migrations.RemoveField( + model_name='category', + name='user', + ), + migrations.RemoveField( + model_name='span', + name='example', + ), + migrations.RemoveField( + model_name='span', + name='label', + ), + migrations.RemoveField( + model_name='span', + name='user', + ), + migrations.AlterUniqueTogether( + name='textlabel', + unique_together=None, + ), + migrations.RemoveField( + model_name='textlabel', + name='example', + ), + migrations.RemoveField( + model_name='textlabel', + name='user', + ), + migrations.DeleteModel( + name='AnnotationRelations', + ), + migrations.DeleteModel( + name='Category', + ), + migrations.DeleteModel( + name='Span', + ), + migrations.DeleteModel( + name='TextLabel', + ), + ], + database_operations=[ + migrations.AlterModelTable( + name='Span', + table='labels_span' + ), + migrations.AlterModelTable( + name='Category', + table='labels_category' + ), + migrations.AlterModelTable( + name='TextLabel', + table='labels_textlabel' + ), + migrations.AlterModelTable( + name='AnnotationRelations', + table='labels_annotationrelations' + ) + ] + ) + + ] diff --git a/backend/api/models.py b/backend/api/models.py index 2d075020..17958267 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -8,8 +8,7 @@ from django.core.exceptions import ValidationError from django.db import models from polymorphic.models import PolymorphicModel -from .managers import (AnnotationManager, CategoryManager, ExampleManager, - ExampleStateManager, SpanManager, TextLabelManager) +from .managers import ExampleManager, ExampleStateManager DOCUMENT_CLASSIFICATION = 'DocumentClassification' SEQUENCE_LABELING = 'SequenceLabeling' @@ -321,104 +320,6 @@ class Tag(models.Model): return self.text -class Annotation(models.Model): - objects = AnnotationManager() - - prob = models.FloatField(default=0.0) - manual = models.BooleanField(default=False) - user = models.ForeignKey(User, on_delete=models.CASCADE) - created_at = models.DateTimeField(auto_now_add=True) - updated_at = models.DateTimeField(auto_now=True) - - class Meta: - abstract = True - - -class Category(Annotation): - objects = CategoryManager() - example = models.ForeignKey( - to=Example, - on_delete=models.CASCADE, - related_name='categories' - ) - label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE) - - class Meta: - unique_together = ( - 'example', - 'user', - 'label' - ) - - -class Span(Annotation): - objects = SpanManager() - example = models.ForeignKey( - to=Example, - on_delete=models.CASCADE, - related_name='spans' - ) - label = models.ForeignKey(to=SpanType, on_delete=models.CASCADE) - start_offset = models.IntegerField() - end_offset = models.IntegerField() - - def validate_unique(self, exclude=None): - allow_overlapping = getattr(self.example.project, 'allow_overlapping', False) - is_collaborative = self.example.project.collaborative_annotation - if allow_overlapping: - super().validate_unique(exclude=exclude) - return - - overlapping_span = Span.objects.exclude(id=self.id).filter(example=self.example).filter( - models.Q(start_offset__gte=self.start_offset, start_offset__lt=self.end_offset) | - models.Q(end_offset__gt=self.start_offset, end_offset__lte=self.end_offset) | - models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset) - ) - if is_collaborative: - if overlapping_span.exists(): - raise ValidationError('This overlapping is not allowed in this project.') - else: - if overlapping_span.filter(user=self.user).exists(): - raise ValidationError('This overlapping is not allowed in this project.') - - def save(self, force_insert=False, force_update=False, using=None, - update_fields=None): - self.full_clean() - super().save(force_insert, force_update, using, update_fields) - - def is_overlapping(self, other: 'Span'): - return (other.start_offset <= self.start_offset < other.end_offset) or\ - (other.start_offset < self.end_offset <= other.end_offset) or\ - (self.start_offset < other.start_offset and other.end_offset < self.end_offset) - - class Meta: - constraints = [ - models.CheckConstraint(check=models.Q(start_offset__gte=0), name='startOffset >= 0'), - models.CheckConstraint(check=models.Q(end_offset__gte=0), name='endOffset >= 0'), - models.CheckConstraint(check=models.Q(start_offset__lt=models.F('end_offset')), name='start < end') - ] - - -class TextLabel(Annotation): - objects = TextLabelManager() - example = models.ForeignKey( - to=Example, - on_delete=models.CASCADE, - related_name='texts' - ) - text = models.TextField() - - def is_same_text(self, other: 'TextLabel'): - return self.text == other.text - - class Meta: - unique_together = ( - 'example', - 'user', - 'text' - ) - - class RelationTypes(models.Model): color = models.TextField() name = models.TextField() @@ -429,18 +330,3 @@ class RelationTypes(models.Model): class Meta: unique_together = ('color', 'name') - - -class AnnotationRelations(models.Model): - annotation_id_1 = models.IntegerField() - annotation_id_2 = models.IntegerField() - type = models.ForeignKey(RelationTypes, related_name='annotation_relations', on_delete=models.CASCADE) - timestamp = models.DateTimeField() - user = models.ForeignKey(User, related_name='annotation_relations', on_delete=models.CASCADE) - project = models.ForeignKey(Project, related_name='annotation_relations', on_delete=models.CASCADE) - - def __str__(self): - return self.__dict__.__str__() - - class Meta: - unique_together = ('annotation_id_1', 'annotation_id_2', 'type', 'project') diff --git a/backend/api/permissions.py b/backend/api/permissions.py index ad70faf6..e2d6596b 100644 --- a/backend/api/permissions.py +++ b/backend/api/permissions.py @@ -1,20 +1,6 @@ from rest_framework.permissions import BasePermission -class CanEditAnnotation(BasePermission): - - def __init__(self, queryset): - super().__init__() - self.queryset = queryset - - def has_permission(self, request, view): - if request.user.is_superuser: - return True - - annotation_id = view.kwargs.get('annotation_id') - return self.queryset.filter(id=annotation_id, user=request.user).exists() - - class IsOwnComment(BasePermission): @classmethod def has_object_permission(cls, request, view, obj): diff --git a/backend/api/serializers.py b/backend/api/serializers.py index f99893c4..0c632ad2 100644 --- a/backend/api/serializers.py +++ b/backend/api/serializers.py @@ -2,12 +2,12 @@ from rest_framework import serializers from rest_framework.exceptions import ValidationError from rest_polymorphic.serializers import PolymorphicSerializer -from .models import (AnnotationRelations, Category, CategoryType, Comment, - Example, ExampleState, ImageClassificationProject, +from .models import (CategoryType, Comment, Example, ExampleState, + ImageClassificationProject, IntentDetectionAndSlotFillingProject, Label, Project, RelationTypes, Seq2seqProject, SequenceLabelingProject, - Span, SpanType, Speech2textProject, Tag, - TextClassificationProject, TextLabel) + SpanType, Speech2textProject, Tag, + TextClassificationProject) class LabelSerializer(serializers.ModelSerializer): @@ -226,61 +226,6 @@ class ProjectPolymorphicSerializer(PolymorphicSerializer): } -class CategorySerializer(serializers.ModelSerializer): - label = serializers.PrimaryKeyRelatedField(queryset=CategoryType.objects.all()) - example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) - - class Meta: - model = Category - fields = ( - 'id', - 'prob', - 'user', - 'example', - 'created_at', - 'updated_at', - 'label', - ) - read_only_fields = ('user',) - - -class SpanSerializer(serializers.ModelSerializer): - label = serializers.PrimaryKeyRelatedField(queryset=SpanType.objects.all()) - example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) - - class Meta: - model = Span - fields = ( - 'id', - 'prob', - 'user', - 'example', - 'created_at', - 'updated_at', - 'label', - 'start_offset', - 'end_offset', - ) - read_only_fields = ('user',) - - -class TextLabelSerializer(serializers.ModelSerializer): - example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) - - class Meta: - model = TextLabel - fields = ( - 'id', - 'prob', - 'user', - 'example', - 'created_at', - 'updated_at', - 'text', - ) - read_only_fields = ('user',) - - class RelationTypesSerializer(serializers.ModelSerializer): def validate(self, attrs): @@ -289,13 +234,3 @@ class RelationTypesSerializer(serializers.ModelSerializer): class Meta: model = RelationTypes fields = ('id', 'color', 'name') - - -class AnnotationRelationsSerializer(serializers.ModelSerializer): - - def validate(self, attrs): - return super().validate(attrs) - - class Meta: - model = AnnotationRelations - fields = ('id', 'annotation_id_1', 'annotation_id_2', 'type', 'user', 'timestamp') diff --git a/backend/api/tests/test_models.py b/backend/api/tests/test_models.py index 8b73f27c..e9a05e17 100644 --- a/backend/api/tests/test_models.py +++ b/backend/api/tests/test_models.py @@ -3,9 +3,8 @@ from django.db.utils import IntegrityError from django.test import TestCase from model_mommy import mommy -from api.models import (IMAGE_CLASSIFICATION, SEQUENCE_LABELING, Category, - CategoryType, ExampleState, Span, SpanType, TextLabel, - generate_random_hex_color) +from api.models import (IMAGE_CLASSIFICATION, SEQUENCE_LABELING, CategoryType, + ExampleState, generate_random_hex_color) from .api.utils import prepare_project @@ -61,103 +60,6 @@ class TestLabel(TestCase): self.fail(msg=ValidationError) -class TestCategory(TestCase): - - def test_uniqueness(self): - a = mommy.make('Category') - with self.assertRaises(IntegrityError): - Category(example=a.example, user=a.user, label=a.label).save() - - -class TestSpan(TestCase): - - def setUp(self): - self.project = prepare_project(SEQUENCE_LABELING, allow_overlapping=False) - self.example = mommy.make('Example', project=self.project.item) - self.user = self.project.users[0] - - def test_start_offset_is_not_negative(self): - with self.assertRaises(IntegrityError): - mommy.make('Span', start_offset=-1, end_offset=0) - - def test_end_offset_is_not_negative(self): - with self.assertRaises(IntegrityError): - mommy.make('Span', start_offset=-2, end_offset=-1) - - def test_start_offset_is_less_than_end_offset(self): - with self.assertRaises(IntegrityError): - mommy.make('Span', start_offset=0, end_offset=0) - - def test_unique_constraint(self): - mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user) - mommy.make('Span', example=self.example, start_offset=0, end_offset=5, user=self.user) - mommy.make('Span', example=self.example, start_offset=10, end_offset=15, user=self.user) - - def test_unique_constraint_violated(self): - mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user) - spans = [(5, 10), (5, 11), (4, 10), (6, 9), (9, 15), (0, 6)] - for start_offset, end_offset in spans: - with self.assertRaises(ValidationError): - mommy.make( - 'Span', - example=self.example, - start_offset=start_offset, - end_offset=end_offset, - user=self.user - ) - - def test_unique_constraint_if_overlapping_is_allowed(self): - project = prepare_project(SEQUENCE_LABELING, allow_overlapping=True) - example = mommy.make('Example', project=project.item) - user = project.users[0] - mommy.make('Span', example=example, start_offset=5, end_offset=10, user=user) - spans = [(5, 10), (5, 11), (4, 10), (6, 9), (9, 15), (0, 6)] - for start_offset, end_offset in spans: - mommy.make('Span', example=example, start_offset=start_offset, end_offset=end_offset, user=user) - - def test_update(self): - span = mommy.make('Span', example=self.example, start_offset=0, end_offset=5) - span.end_offset = 6 - span.save() - - -class TestSpanWithoutCollaborativeMode(TestCase): - - def setUp(self): - self.project = prepare_project(SEQUENCE_LABELING, False, allow_overlapping=False) - self.example = mommy.make('Example', project=self.project.item) - - def test_allow_users_to_create_same_spans(self): - mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.project.users[0]) - mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.project.users[1]) - - -class TestSpanWithCollaborativeMode(TestCase): - - def test_deny_users_to_create_same_spans(self): - project = prepare_project(SEQUENCE_LABELING, True, allow_overlapping=False) - example = mommy.make('Example', project=project.item) - mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[0]) - with self.assertRaises(ValidationError): - mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[1]) - - def test_allow_users_to_create_same_spans_if_overlapping_is_allowed(self): - project = prepare_project(SEQUENCE_LABELING, True, allow_overlapping=True) - example = mommy.make('Example', project=project.item) - mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[0]) - mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[1]) - - -class TestSeq2seqAnnotation(TestCase): - - def test_uniqueness(self): - a = mommy.make('TextLabel') - with self.assertRaises(IntegrityError): - TextLabel(example=a.example, - user=a.user, - text=a.text).save() - - class TestGeneratedColor(TestCase): def test_length(self): @@ -219,29 +121,6 @@ class TestExampleState(TestCase): self.assertEqual(progress, {'total': 2, 'progress': expected_progress}) -class TestLabelDistribution(TestCase): - - def setUp(self): - self.project = prepare_project(SEQUENCE_LABELING, allow_overlapping=False) - self.example = mommy.make('Example', project=self.project.item) - self.user = self.project.users[0] - - def test_calc_label_distribution(self): - label_a = mommy.make('SpanType', text='labelA', project=self.project.item) - label_b = mommy.make('SpanType', text='labelB', project=self.project.item) - mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user, label=label_a) - mommy.make('Span', example=self.example, start_offset=10, end_offset=15, user=self.user, label=label_b) - distribution = Span.objects.calc_label_distribution( - examples=self.project.item.examples.all(), - members=self.project.users, - labels=SpanType.objects.all() - ) - expected = {user.username: {label.text: 0 for label in SpanType.objects.all()} for user in self.project.users} - expected[self.user.username][label_a.text] = 1 - expected[self.user.username][label_b.text] = 1 - self.assertEqual(distribution, expected) - - class TestExample(TestCase): def test_text_project_returns_text_as_data_property(self): diff --git a/backend/api/tests/test_span.py b/backend/api/tests/test_span.py deleted file mode 100644 index eac8fe00..00000000 --- a/backend/api/tests/test_span.py +++ /dev/null @@ -1,142 +0,0 @@ -import abc - -from django.test import TestCase -from model_mommy import mommy - -from api.models import SEQUENCE_LABELING, Span - -from .api.utils import prepare_project - - -class TestSpanAnnotation(abc.ABC, TestCase): - overlapping = False - collaborative = False - - @classmethod - def setUpTestData(cls): - cls.project = prepare_project( - SEQUENCE_LABELING, - allow_overlapping=cls.overlapping, - collaborative_annotation=cls.collaborative - ) - cls.example = mommy.make('Example', project=cls.project.item) - cls.label_type = mommy.make('SpanType', project=cls.project.item) - users = cls.project.users - cls.user = users[0] - cls.another_user = users[1] - cls.span = Span( - example=cls.example, - label=cls.label_type, - user=cls.user, - start_offset=0, - end_offset=5 - ) - - def test_can_annotate_span_to_unannotated_data(self): - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertTrue(can_annotate) - - -class NonCollaborativeMixin: - - def test_allow_another_user_to_annotate_same_span(self): - mommy.make( - 'Span', - example=self.example, - label=self.label_type, - user=self.another_user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertTrue(can_annotate) - - -class TestNonOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): - overlapping = False - collaborative = False - - def test_cannot_annotate_same_span_to_annotated_data(self): - mommy.make( - 'Span', - example=self.example, - label=self.label_type, - user=self.user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset, - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertFalse(can_annotate) - - def test_cannot_annotate_different_span_type_to_annotated_data(self): - mommy.make( - 'Span', - example=self.example, - user=self.user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertFalse(can_annotate) - - -class TestOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): - overlapping = True - collaborative = False - - def test_can_annotate_same_span_to_annotated_data(self): - mommy.make( - 'Span', - example=self.example, - label=self.label_type, - user=self.user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset, - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertTrue(can_annotate) - - -class TestCollaborativeNonOverlappingSpanAnnotation(TestSpanAnnotation): - overlapping = False - collaborative = True - - def test_deny_another_user_to_annotate_same_span_type(self): - mommy.make( - 'Span', - example=self.example, - label=self.label_type, - user=self.another_user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertFalse(can_annotate) - - def test_deny_another_user_to_annotate_different_span_type(self): - mommy.make( - 'Span', - example=self.example, - user=self.another_user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertFalse(can_annotate) - - -class TestCollaborativeOverlappingSpanAnnotation(TestSpanAnnotation): - overlapping = True - collaborative = True - - def test_allow_another_user_to_annotate_same_span(self): - mommy.make( - 'Span', - example=self.example, - label=self.label_type, - user=self.another_user, - start_offset=self.span.start_offset, - end_offset=self.span.end_offset - ) - can_annotate = Span.objects.can_annotate(self.span, self.project.item) - self.assertTrue(can_annotate) diff --git a/backend/api/urls.py b/backend/api/urls.py index b1fac0e1..ae2dfb98 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -2,7 +2,6 @@ from django.urls import include, path from .views import (comment, example, example_state, health, label, project, tag, task) -from .views.tasks import category, relation, span, text urlpatterns_project = [ path( @@ -60,51 +59,6 @@ urlpatterns_project = [ view=label.RelationTypeDetail.as_view(), name='relation_type_detail' ), - path( - route='annotation_relations', - view=relation.RelationList.as_view(), - name='relation_types_list' - ), - path( - route='annotation_relation-upload', - view=relation.RelationUploadAPI.as_view(), - name='annotation_relation-upload' - ), - path( - route='annotation_relations/', - view=relation.RelationDetail.as_view(), - name='annotation_relation_detail' - ), - path( - route='examples//categories', - view=category.CategoryListAPI.as_view(), - name='category_list' - ), - path( - route='examples//categories/', - view=category.CategoryDetailAPI.as_view(), - name='category_detail' - ), - path( - route='examples//spans', - view=span.SpanListAPI.as_view(), - name='span_list' - ), - path( - route='examples//spans/', - view=span.SpanDetailAPI.as_view(), - name='span_detail' - ), - path( - route='examples//texts', - view=text.TextLabelListAPI.as_view(), - name='text_list' - ), - path( - route='examples//texts/', - view=text.TextLabelDetailAPI.as_view(), - name='text_detail' - ), path( route='tags', view=tag.TagList.as_view(), diff --git a/backend/api/views/tasks/base.py b/backend/api/views/tasks/base.py index dce93c7b..e69de29b 100644 --- a/backend/api/views/tasks/base.py +++ b/backend/api/views/tasks/base.py @@ -1,63 +0,0 @@ -from functools import partial - -from django.core.exceptions import ValidationError -from django.shortcuts import get_object_or_404 -from rest_framework import generics, status -from rest_framework.permissions import IsAuthenticated -from rest_framework.response import Response - -from members.permissions import IsInProjectOrAdmin - -from ...models import Project -from ...permissions import CanEditAnnotation - - -class BaseListAPI(generics.ListCreateAPIView): - annotation_class = None - pagination_class = None - permission_classes = [IsAuthenticated & IsInProjectOrAdmin] - swagger_schema = None - - @property - def project(self): - return get_object_or_404(Project, pk=self.kwargs['project_id']) - - def get_queryset(self): - queryset = self.annotation_class.objects.filter(example=self.kwargs['example_id']) - if not self.project.collaborative_annotation: - queryset = queryset.filter(user=self.request.user) - return queryset - - def create(self, request, *args, **kwargs): - request.data['example'] = self.kwargs['example_id'] - try: - response = super().create(request, args, kwargs) - except ValidationError as err: - response = Response({'detail': err.messages}, status=status.HTTP_400_BAD_REQUEST) - return response - - def perform_create(self, serializer): - serializer.save(example_id=self.kwargs['example_id'], user=self.request.user) - - def delete(self, request, *args, **kwargs): - queryset = self.get_queryset() - queryset.all().delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - -class BaseDetailAPI(generics.RetrieveUpdateDestroyAPIView): - lookup_url_kwarg = 'annotation_id' - swagger_schema = None - - @property - def project(self): - return get_object_or_404(Project, pk=self.kwargs['project_id']) - - def get_permissions(self): - if self.project.collaborative_annotation: - self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin] - else: - self.permission_classes = [ - IsAuthenticated & IsInProjectOrAdmin & partial(CanEditAnnotation, self.queryset) - ] - return super().get_permissions() diff --git a/backend/api/views/tasks/category.py b/backend/api/views/tasks/category.py index 7f3eee5d..e69de29b 100644 --- a/backend/api/views/tasks/category.py +++ b/backend/api/views/tasks/category.py @@ -1,18 +0,0 @@ -from ...models import Category -from ...serializers import CategorySerializer -from .base import BaseDetailAPI, BaseListAPI - - -class CategoryListAPI(BaseListAPI): - annotation_class = Category - serializer_class = CategorySerializer - - def create(self, request, *args, **kwargs): - if self.project.single_class_classification: - self.get_queryset().delete() - return super().create(request, args, kwargs) - - -class CategoryDetailAPI(BaseDetailAPI): - queryset = Category.objects.all() - serializer_class = CategorySerializer diff --git a/backend/api/views/tasks/relation.py b/backend/api/views/tasks/relation.py index 4126142e..cdb49874 100644 --- a/backend/api/views/tasks/relation.py +++ b/backend/api/views/tasks/relation.py @@ -2,44 +2,18 @@ import json from django.db import IntegrityError, transaction from django.shortcuts import get_object_or_404 -from rest_framework import generics, status +from rest_framework import status from rest_framework.exceptions import ParseError from rest_framework.parsers import MultiPartParser from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from members.permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin +from labels.serializers import AnnotationRelationsSerializer +from members.permissions import IsProjectAdmin from ...exceptions import AnnotationRelationValidationError -from ...models import AnnotationRelations, Project -from ...serializers import AnnotationRelationsSerializer - - -class RelationList(generics.ListCreateAPIView): - serializer_class = AnnotationRelationsSerializer - pagination_class = None - permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - - def get_queryset(self): - project = get_object_or_404(Project, pk=self.kwargs['project_id']) - return project.annotation_relations - - def perform_create(self, serializer): - project = get_object_or_404(Project, pk=self.kwargs['project_id']) - serializer.save(project=project) - - def delete(self, request, *args, **kwargs): - delete_ids = request.data['ids'] - AnnotationRelations.objects.filter(pk__in=delete_ids).delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - -class RelationDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = AnnotationRelations.objects.all() - serializer_class = AnnotationRelationsSerializer - lookup_url_kwarg = 'annotation_relation_id' - permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] +from ...models import Project class RelationUploadAPI(APIView): diff --git a/backend/api/views/tasks/span.py b/backend/api/views/tasks/span.py index 9100b21d..e69de29b 100644 --- a/backend/api/views/tasks/span.py +++ b/backend/api/views/tasks/span.py @@ -1,13 +0,0 @@ -from ...models import Span -from ...serializers import SpanSerializer -from .base import BaseDetailAPI, BaseListAPI - - -class SpanListAPI(BaseListAPI): - annotation_class = Span - serializer_class = SpanSerializer - - -class SpanDetailAPI(BaseDetailAPI): - queryset = Span.objects.all() - serializer_class = SpanSerializer diff --git a/backend/api/views/tasks/text.py b/backend/api/views/tasks/text.py index c6c0d23c..e69de29b 100644 --- a/backend/api/views/tasks/text.py +++ b/backend/api/views/tasks/text.py @@ -1,13 +0,0 @@ -from ...models import TextLabel -from ...serializers import TextLabelSerializer -from .base import BaseDetailAPI, BaseListAPI - - -class TextLabelListAPI(BaseListAPI): - annotation_class = TextLabel - serializer_class = TextLabelSerializer - - -class TextLabelDetailAPI(BaseDetailAPI): - queryset = TextLabel.objects.all() - serializer_class = TextLabelSerializer diff --git a/backend/app/settings.py b/backend/app/settings.py index 8ce096ea..ff2e93dd 100644 --- a/backend/app/settings.py +++ b/backend/app/settings.py @@ -59,6 +59,7 @@ INSTALLED_APPS = [ 'data_import.apps.DataImportConfig', 'data_export.apps.DataExportConfig', 'auto_labeling.apps.AutoLabelingConfig', + 'labels.apps.LabelsConfig', 'rest_framework', 'rest_framework.authtoken', 'django_filters', diff --git a/backend/app/urls.py b/backend/app/urls.py index 6148d59c..e2748636 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -48,6 +48,7 @@ urlpatterns += [ path('v1/projects//', include('members.urls')), path('v1/projects//metrics/', include('metrics.urls')), path('v1/projects//', include('auto_labeling.urls')), + path('v1/projects//', include('labels.urls')), path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), re_path('', TemplateView.as_view(template_name='index.html')), ] diff --git a/backend/auto_labeling/pipeline/labels.py b/backend/auto_labeling/pipeline/labels.py index 17f46b0b..ff172d37 100644 --- a/backend/auto_labeling/pipeline/labels.py +++ b/backend/auto_labeling/pipeline/labels.py @@ -4,7 +4,8 @@ from typing import List from auto_labeling_pipeline.labels import Labels from django.contrib.auth.models import User -from api.models import Project, Example, Annotation, CategoryType, Category, SpanType, Span, TextLabel +from api.models import Project, Example, CategoryType, SpanType +from labels.models import Annotation, Category, Span, TextLabel class LabelCollection(abc.ABC): diff --git a/backend/auto_labeling/tests/test_views.py b/backend/auto_labeling/tests/test_views.py index cd5d176a..71daabe8 100644 --- a/backend/auto_labeling/tests/test_views.py +++ b/backend/auto_labeling/tests/test_views.py @@ -8,7 +8,7 @@ from rest_framework import status from rest_framework.reverse import reverse from api.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ -from api.models import Category, Span, TextLabel +from labels.models import Category, Span, TextLabel from api.tests.api.utils import CRUDMixin, make_doc, prepare_project from auto_labeling.pipeline.labels import Categories, Spans, Texts diff --git a/backend/data_import/pipeline/labels.py b/backend/data_import/pipeline/labels.py index 667f537e..31f57f50 100644 --- a/backend/data_import/pipeline/labels.py +++ b/backend/data_import/pipeline/labels.py @@ -3,10 +3,10 @@ from typing import Any, Dict, Optional, Union from pydantic import BaseModel, validator -from api.models import Category, CategoryType +from api.models import CategoryType from api.models import Label as LabelModel -from api.models import Project, Span, SpanType -from api.models import TextLabel as TL +from api.models import Project, SpanType +from labels.models import Category, Span, TextLabel as TL class Label(BaseModel, abc.ABC): diff --git a/backend/data_import/tests/test_tasks.py b/backend/data_import/tests/test_tasks.py index 566b0315..3f7f7a62 100644 --- a/backend/data_import/tests/test_tasks.py +++ b/backend/data_import/tests/test_tasks.py @@ -5,8 +5,8 @@ from django.test import TestCase from data_import.celery_tasks import import_dataset from api.models import (DOCUMENT_CLASSIFICATION, INTENT_DETECTION_AND_SLOT_FILLING, SEQ2SEQ, - SEQUENCE_LABELING, Category, CategoryType, Example, Span, - SpanType) + SEQUENCE_LABELING, CategoryType, Example, SpanType) +from labels.models import Category, Span from api.tests.api.utils import prepare_project diff --git a/backend/labels/admin.py b/backend/labels/admin.py index 8c38f3f3..9304660f 100644 --- a/backend/labels/admin.py +++ b/backend/labels/admin.py @@ -1,3 +1,23 @@ from django.contrib import admin -# Register your models here. +from .models import Category, Span, TextLabel + + +class SpanAdmin(admin.ModelAdmin): + list_display = ('example', 'label', 'start_offset', 'user') + ordering = ('example',) + + +class CategoryAdmin(admin.ModelAdmin): + list_display = ('example', 'label', 'user') + ordering = ('example',) + + +class TextLabelAdmin(admin.ModelAdmin): + list_display = ('example', 'text', 'user') + ordering = ('example',) + + +admin.site.register(Category, CategoryAdmin) +admin.site.register(Span, SpanAdmin) +admin.site.register(TextLabel, TextLabelAdmin) diff --git a/backend/labels/managers.py b/backend/labels/managers.py new file mode 100644 index 00000000..6452f246 --- /dev/null +++ b/backend/labels/managers.py @@ -0,0 +1,76 @@ +from django.db.models import Manager, Count + + +class AnnotationManager(Manager): + + def calc_label_distribution(self, examples, members, labels): + """Calculate label distribution. + + Args: + examples: example queryset. + members: user queryset. + labels: label queryset. + + Returns: + label distribution per user. + + Examples: + >>> self.calc_label_distribution(examples, members, labels) + {'admin': {'positive': 10, 'negative': 5}} + """ + distribution = {member.username: {label.text: 0 for label in labels} for member in members} + items = self.filter(example_id__in=examples)\ + .values('user__username', 'label__text')\ + .annotate(count=Count('label__text')) + for item in items: + username = item['user__username'] + label = item['label__text'] + count = item['count'] + distribution[username][label] = count + return distribution + + def get_labels(self, label, project): + if project.collaborative_annotation: + return self.filter(example=label.example) + else: + return self.filter(example=label.example, user=label.user) + + def can_annotate(self, label, project) -> bool: + raise NotImplementedError('Please implement this method in the subclass') + + def filter_annotatable_labels(self, labels, project): + return [label for label in labels if self.can_annotate(label, project)] + + +class CategoryManager(AnnotationManager): + + def can_annotate(self, label, project) -> bool: + is_exclusive = project.single_class_classification + categories = self.get_labels(label, project) + if is_exclusive: + return not categories.exists() + else: + return not categories.filter(label=label.label).exists() + + +class SpanManager(AnnotationManager): + + def can_annotate(self, label, project) -> bool: + overlapping = getattr(project, 'allow_overlapping', False) + spans = self.get_labels(label, project) + if overlapping: + return True + for span in spans: + if span.is_overlapping(label): + return False + return True + + +class TextLabelManager(AnnotationManager): + + def can_annotate(self, label, project) -> bool: + texts = self.get_labels(label, project) + for text in texts: + if text.is_same_text(label): + return False + return True diff --git a/backend/labels/migrations/0001_initial.py b/backend/labels/migrations/0001_initial.py new file mode 100644 index 00000000..401784ae --- /dev/null +++ b/backend/labels/migrations/0001_initial.py @@ -0,0 +1,118 @@ +# Generated by Django 3.2.11 on 2022-01-27 00:32 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.db.models.expressions + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('api', '0031_auto_20220127_0032'), + ] + + operations = [ + migrations.SeparateDatabaseAndState( + state_operations=[ + migrations.CreateModel( + name='Span', + fields=[ + ('id', + models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('prob', models.FloatField(default=0.0)), + ('manual', models.BooleanField(default=False)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('start_offset', models.IntegerField()), + ('end_offset', models.IntegerField()), + ('example', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='spans', + to='api.example')), + ('label', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.spantype')), + ('user', + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Category', + fields=[ + ('id', + models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('prob', models.FloatField(default=0.0)), + ('manual', models.BooleanField(default=False)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('example', + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='categories', + to='api.example')), + ( + 'label', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.categorytype')), + ('user', + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='AnnotationRelations', + fields=[ + ('id', + models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('annotation_id_1', models.IntegerField()), + ('annotation_id_2', models.IntegerField()), + ('timestamp', models.DateTimeField()), + ('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, + related_name='annotation_relations', to='api.project')), + ('type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, + related_name='annotation_relations', to='api.relationtypes')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, + related_name='annotation_relations', to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='TextLabel', + fields=[ + ('id', + models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('prob', models.FloatField(default=0.0)), + ('manual', models.BooleanField(default=False)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('text', models.TextField()), + ('example', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='texts', + to='api.example')), + ('user', + models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'unique_together': {('example', 'user', 'text')}, + }, + ), + migrations.AddConstraint( + model_name='span', + constraint=models.CheckConstraint(check=models.Q(('start_offset__gte', 0)), + name='startOffset >= 0'), + ), + migrations.AddConstraint( + model_name='span', + constraint=models.CheckConstraint(check=models.Q(('end_offset__gte', 0)), name='endOffset >= 0'), + ), + migrations.AddConstraint( + model_name='span', + constraint=models.CheckConstraint( + check=models.Q(('start_offset__lt', django.db.models.expressions.F('end_offset'))), + name='start < end'), + ), + migrations.AlterUniqueTogether( + name='category', + unique_together={('example', 'user', 'label')}, + ), + migrations.AlterUniqueTogether( + name='annotationrelations', + unique_together={('annotation_id_1', 'annotation_id_2', 'type', 'project')}, + ), + ], + database_operations=[] + ) + ] diff --git a/backend/labels/models.py b/backend/labels/models.py index 71a83623..8c0c8716 100644 --- a/backend/labels/models.py +++ b/backend/labels/models.py @@ -1,3 +1,119 @@ +from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.db import models -# Create your models here. +from .managers import AnnotationManager, CategoryManager, SpanManager, TextLabelManager +from api.models import Example, CategoryType, SpanType, RelationTypes, Project + + +class Annotation(models.Model): + objects = AnnotationManager() + + prob = models.FloatField(default=0.0) + manual = models.BooleanField(default=False) + user = models.ForeignKey(User, on_delete=models.CASCADE) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + abstract = True + + +class Category(Annotation): + objects = CategoryManager() + example = models.ForeignKey( + to=Example, + on_delete=models.CASCADE, + related_name='categories' + ) + label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE) + + class Meta: + unique_together = ( + 'example', + 'user', + 'label' + ) + + +class Span(Annotation): + objects = SpanManager() + example = models.ForeignKey( + to=Example, + on_delete=models.CASCADE, + related_name='spans' + ) + label = models.ForeignKey(to=SpanType, on_delete=models.CASCADE) + start_offset = models.IntegerField() + end_offset = models.IntegerField() + + def validate_unique(self, exclude=None): + allow_overlapping = getattr(self.example.project, 'allow_overlapping', False) + is_collaborative = self.example.project.collaborative_annotation + if allow_overlapping: + super().validate_unique(exclude=exclude) + return + + overlapping_span = Span.objects.exclude(id=self.id).filter(example=self.example).filter( + models.Q(start_offset__gte=self.start_offset, start_offset__lt=self.end_offset) | + models.Q(end_offset__gt=self.start_offset, end_offset__lte=self.end_offset) | + models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset) + ) + if is_collaborative: + if overlapping_span.exists(): + raise ValidationError('This overlapping is not allowed in this project.') + else: + if overlapping_span.filter(user=self.user).exists(): + raise ValidationError('This overlapping is not allowed in this project.') + + def save(self, force_insert=False, force_update=False, using=None, + update_fields=None): + self.full_clean() + super().save(force_insert, force_update, using, update_fields) + + def is_overlapping(self, other: 'Span'): + return (other.start_offset <= self.start_offset < other.end_offset) or\ + (other.start_offset < self.end_offset <= other.end_offset) or\ + (self.start_offset < other.start_offset and other.end_offset < self.end_offset) + + class Meta: + constraints = [ + models.CheckConstraint(check=models.Q(start_offset__gte=0), name='startOffset >= 0'), + models.CheckConstraint(check=models.Q(end_offset__gte=0), name='endOffset >= 0'), + models.CheckConstraint(check=models.Q(start_offset__lt=models.F('end_offset')), name='start < end') + ] + + +class TextLabel(Annotation): + objects = TextLabelManager() + example = models.ForeignKey( + to=Example, + on_delete=models.CASCADE, + related_name='texts' + ) + text = models.TextField() + + def is_same_text(self, other: 'TextLabel'): + return self.text == other.text + + class Meta: + unique_together = ( + 'example', + 'user', + 'text' + ) + + +class AnnotationRelations(models.Model): + annotation_id_1 = models.IntegerField() + annotation_id_2 = models.IntegerField() + type = models.ForeignKey(RelationTypes, related_name='annotation_relations', on_delete=models.CASCADE) + timestamp = models.DateTimeField() + user = models.ForeignKey(User, related_name='annotation_relations', on_delete=models.CASCADE) + project = models.ForeignKey(Project, related_name='annotation_relations', on_delete=models.CASCADE) + + def __str__(self): + return self.__dict__.__str__() + + class Meta: + unique_together = ('annotation_id_1', 'annotation_id_2', 'type', 'project') diff --git a/backend/labels/permissions.py b/backend/labels/permissions.py index e69de29b..e2ec568e 100644 --- a/backend/labels/permissions.py +++ b/backend/labels/permissions.py @@ -0,0 +1,15 @@ +from rest_framework.permissions import BasePermission + + +class CanEditAnnotation(BasePermission): + + def __init__(self, queryset): + super().__init__() + self.queryset = queryset + + def has_permission(self, request, view): + if request.user.is_superuser: + return True + + annotation_id = view.kwargs.get('annotation_id') + return self.queryset.filter(id=annotation_id, user=request.user).exists() diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py index e69de29b..059003cf 100644 --- a/backend/labels/serializers.py +++ b/backend/labels/serializers.py @@ -0,0 +1,69 @@ +from rest_framework import serializers + +from api.models import CategoryType, Example, SpanType +from .models import Category, Span, TextLabel, AnnotationRelations + + +class CategorySerializer(serializers.ModelSerializer): + label = serializers.PrimaryKeyRelatedField(queryset=CategoryType.objects.all()) + example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) + + class Meta: + model = Category + fields = ( + 'id', + 'prob', + 'user', + 'example', + 'created_at', + 'updated_at', + 'label', + ) + read_only_fields = ('user',) + + +class SpanSerializer(serializers.ModelSerializer): + label = serializers.PrimaryKeyRelatedField(queryset=SpanType.objects.all()) + example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) + + class Meta: + model = Span + fields = ( + 'id', + 'prob', + 'user', + 'example', + 'created_at', + 'updated_at', + 'label', + 'start_offset', + 'end_offset', + ) + read_only_fields = ('user',) + + +class TextLabelSerializer(serializers.ModelSerializer): + example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all()) + + class Meta: + model = TextLabel + fields = ( + 'id', + 'prob', + 'user', + 'example', + 'created_at', + 'updated_at', + 'text', + ) + read_only_fields = ('user',) + + +class AnnotationRelationsSerializer(serializers.ModelSerializer): + + def validate(self, attrs): + return super().validate(attrs) + + class Meta: + model = AnnotationRelations + fields = ('id', 'annotation_id_1', 'annotation_id_2', 'type', 'user', 'timestamp') diff --git a/backend/labels/tests.py b/backend/labels/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/backend/labels/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/backend/labels/tests/__init__.py b/backend/labels/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/api/tests/test_category.py b/backend/labels/tests/test_category.py similarity index 90% rename from backend/api/tests/test_category.py rename to backend/labels/tests/test_category.py index cd059727..e07a60d4 100644 --- a/backend/api/tests/test_category.py +++ b/backend/labels/tests/test_category.py @@ -1,11 +1,12 @@ import abc +from django.db import IntegrityError from django.test import TestCase from model_mommy import mommy -from api.models import DOCUMENT_CLASSIFICATION, Category - -from .api.utils import prepare_project +from api.models import DOCUMENT_CLASSIFICATION +from labels.models import Category +from api.tests.api.utils import prepare_project class TestCategoryAnnotation(abc.ABC, TestCase): @@ -112,3 +113,11 @@ class TestCollaborativeNonExclusiveCategoryAnnotation(TestCategoryAnnotation, Co ) can_annotate = Category.objects.can_annotate(self.category, self.project.item) self.assertTrue(can_annotate) + + +class TestCategory(TestCase): + + def test_uniqueness(self): + a = mommy.make('Category') + with self.assertRaises(IntegrityError): + Category(example=a.example, user=a.user, label=a.label).save() diff --git a/backend/labels/tests/test_span.py b/backend/labels/tests/test_span.py new file mode 100644 index 00000000..61388db5 --- /dev/null +++ b/backend/labels/tests/test_span.py @@ -0,0 +1,246 @@ +import abc + +from django.core.exceptions import ValidationError +from django.db import IntegrityError +from django.test import TestCase +from model_mommy import mommy + +from api.models import SEQUENCE_LABELING, SpanType +from labels.models import Span +from api.tests.api.utils import prepare_project + + +class TestSpanAnnotation(abc.ABC, TestCase): + overlapping = False + collaborative = False + + @classmethod + def setUpTestData(cls): + cls.project = prepare_project( + SEQUENCE_LABELING, + allow_overlapping=cls.overlapping, + collaborative_annotation=cls.collaborative + ) + cls.example = mommy.make('Example', project=cls.project.item) + cls.label_type = mommy.make('SpanType', project=cls.project.item) + users = cls.project.users + cls.user = users[0] + cls.another_user = users[1] + cls.span = Span( + example=cls.example, + label=cls.label_type, + user=cls.user, + start_offset=0, + end_offset=5 + ) + + def test_can_annotate_span_to_unannotated_data(self): + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertTrue(can_annotate) + + +class NonCollaborativeMixin: + + def test_allow_another_user_to_annotate_same_span(self): + mommy.make( + 'Span', + example=self.example, + label=self.label_type, + user=self.another_user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertTrue(can_annotate) + + +class TestNonOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): + overlapping = False + collaborative = False + + def test_cannot_annotate_same_span_to_annotated_data(self): + mommy.make( + 'Span', + example=self.example, + label=self.label_type, + user=self.user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset, + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertFalse(can_annotate) + + def test_cannot_annotate_different_span_type_to_annotated_data(self): + mommy.make( + 'Span', + example=self.example, + user=self.user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertFalse(can_annotate) + + +class TestOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): + overlapping = True + collaborative = False + + def test_can_annotate_same_span_to_annotated_data(self): + mommy.make( + 'Span', + example=self.example, + label=self.label_type, + user=self.user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset, + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertTrue(can_annotate) + + +class TestCollaborativeNonOverlappingSpanAnnotation(TestSpanAnnotation): + overlapping = False + collaborative = True + + def test_deny_another_user_to_annotate_same_span_type(self): + mommy.make( + 'Span', + example=self.example, + label=self.label_type, + user=self.another_user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertFalse(can_annotate) + + def test_deny_another_user_to_annotate_different_span_type(self): + mommy.make( + 'Span', + example=self.example, + user=self.another_user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertFalse(can_annotate) + + +class TestCollaborativeOverlappingSpanAnnotation(TestSpanAnnotation): + overlapping = True + collaborative = True + + def test_allow_another_user_to_annotate_same_span(self): + mommy.make( + 'Span', + example=self.example, + label=self.label_type, + user=self.another_user, + start_offset=self.span.start_offset, + end_offset=self.span.end_offset + ) + can_annotate = Span.objects.can_annotate(self.span, self.project.item) + self.assertTrue(can_annotate) + + +class TestSpan(TestCase): + + def setUp(self): + self.project = prepare_project(SEQUENCE_LABELING, allow_overlapping=False) + self.example = mommy.make('Example', project=self.project.item) + self.user = self.project.users[0] + + def test_start_offset_is_not_negative(self): + with self.assertRaises(IntegrityError): + mommy.make('Span', start_offset=-1, end_offset=0) + + def test_end_offset_is_not_negative(self): + with self.assertRaises(IntegrityError): + mommy.make('Span', start_offset=-2, end_offset=-1) + + def test_start_offset_is_less_than_end_offset(self): + with self.assertRaises(IntegrityError): + mommy.make('Span', start_offset=0, end_offset=0) + + def test_unique_constraint(self): + mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user) + mommy.make('Span', example=self.example, start_offset=0, end_offset=5, user=self.user) + mommy.make('Span', example=self.example, start_offset=10, end_offset=15, user=self.user) + + def test_unique_constraint_violated(self): + mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user) + spans = [(5, 10), (5, 11), (4, 10), (6, 9), (9, 15), (0, 6)] + for start_offset, end_offset in spans: + with self.assertRaises(ValidationError): + mommy.make( + 'Span', + example=self.example, + start_offset=start_offset, + end_offset=end_offset, + user=self.user + ) + + def test_unique_constraint_if_overlapping_is_allowed(self): + project = prepare_project(SEQUENCE_LABELING, allow_overlapping=True) + example = mommy.make('Example', project=project.item) + user = project.users[0] + mommy.make('Span', example=example, start_offset=5, end_offset=10, user=user) + spans = [(5, 10), (5, 11), (4, 10), (6, 9), (9, 15), (0, 6)] + for start_offset, end_offset in spans: + mommy.make('Span', example=example, start_offset=start_offset, end_offset=end_offset, user=user) + + def test_update(self): + span = mommy.make('Span', example=self.example, start_offset=0, end_offset=5) + span.end_offset = 6 + span.save() + + +class TestSpanWithoutCollaborativeMode(TestCase): + + def setUp(self): + self.project = prepare_project(SEQUENCE_LABELING, False, allow_overlapping=False) + self.example = mommy.make('Example', project=self.project.item) + + def test_allow_users_to_create_same_spans(self): + mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.project.users[0]) + mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.project.users[1]) + + +class TestSpanWithCollaborativeMode(TestCase): + + def test_deny_users_to_create_same_spans(self): + project = prepare_project(SEQUENCE_LABELING, True, allow_overlapping=False) + example = mommy.make('Example', project=project.item) + mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[0]) + with self.assertRaises(ValidationError): + mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[1]) + + def test_allow_users_to_create_same_spans_if_overlapping_is_allowed(self): + project = prepare_project(SEQUENCE_LABELING, True, allow_overlapping=True) + example = mommy.make('Example', project=project.item) + mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[0]) + mommy.make('Span', example=example, start_offset=5, end_offset=10, user=project.users[1]) + + +class TestLabelDistribution(TestCase): + + def setUp(self): + self.project = prepare_project(SEQUENCE_LABELING, allow_overlapping=False) + self.example = mommy.make('Example', project=self.project.item) + self.user = self.project.users[0] + + def test_calc_label_distribution(self): + label_a = mommy.make('SpanType', text='labelA', project=self.project.item) + label_b = mommy.make('SpanType', text='labelB', project=self.project.item) + mommy.make('Span', example=self.example, start_offset=5, end_offset=10, user=self.user, label=label_a) + mommy.make('Span', example=self.example, start_offset=10, end_offset=15, user=self.user, label=label_b) + distribution = Span.objects.calc_label_distribution( + examples=self.project.item.examples.all(), + members=self.project.users, + labels=SpanType.objects.all() + ) + expected = {user.username: {label.text: 0 for label in SpanType.objects.all()} for user in self.project.users} + expected[self.user.username][label_a.text] = 1 + expected[self.user.username][label_b.text] = 1 + self.assertEqual(distribution, expected) diff --git a/backend/api/tests/test_text_label.py b/backend/labels/tests/test_text_label.py similarity index 86% rename from backend/api/tests/test_text_label.py rename to backend/labels/tests/test_text_label.py index f8dcb74a..9cda5af1 100644 --- a/backend/api/tests/test_text_label.py +++ b/backend/labels/tests/test_text_label.py @@ -1,11 +1,12 @@ import abc +from django.db import IntegrityError from django.test import TestCase from model_mommy import mommy -from api.models import SEQ2SEQ, TextLabel - -from .api.utils import prepare_project +from api.models import SEQ2SEQ +from labels.models import TextLabel +from api.tests.api.utils import prepare_project class TestTextLabelAnnotation(abc.ABC, TestCase): @@ -82,3 +83,13 @@ class TestCollaborativeTextLabelAnnotation(TestTextLabelAnnotation): ) can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item) self.assertTrue(can_annotate) + + +class TestSeq2seqAnnotation(TestCase): + + def test_uniqueness(self): + a = mommy.make('TextLabel') + with self.assertRaises(IntegrityError): + TextLabel(example=a.example, + user=a.user, + text=a.text).save() diff --git a/backend/api/tests/api/test_annotation.py b/backend/labels/tests/test_views.py similarity index 97% rename from backend/api/tests/api/test_annotation.py rename to backend/labels/tests/test_views.py index fc20d5ab..2c765541 100644 --- a/backend/api/tests/api/test_annotation.py +++ b/backend/labels/tests/test_views.py @@ -1,11 +1,10 @@ from rest_framework import status from rest_framework.reverse import reverse -from api.models import (DOCUMENT_CLASSIFICATION, SEQ2SEQ, SEQUENCE_LABELING, - Category, Span, TextLabel) - -from .utils import (CRUDMixin, make_annotation, make_doc, make_label, - make_user, prepare_project) +from api.models import (DOCUMENT_CLASSIFICATION, SEQ2SEQ, SEQUENCE_LABELING) +from labels.models import Category, Span, TextLabel +from api.tests.api.utils import (CRUDMixin, make_annotation, make_doc, make_label, + make_user, prepare_project) class TestAnnotationList: diff --git a/backend/labels/urls.py b/backend/labels/urls.py index e69de29b..e689a66b 100644 --- a/backend/labels/urls.py +++ b/backend/labels/urls.py @@ -0,0 +1,50 @@ +from django.urls import path + +from .views import CategoryListAPI, CategoryDetailAPI +from .views import SpanListAPI, SpanDetailAPI +from .views import TextLabelListAPI, TextLabelDetailAPI +from .views import RelationList, RelationDetail + + +urlpatterns = [ + path( + route='annotation_relations', + view=RelationList.as_view(), + name='relation_types_list' + ), + path( + route='annotation_relations/', + view=RelationDetail.as_view(), + name='annotation_relation_detail' + ), + path( + route='examples//categories', + view=CategoryListAPI.as_view(), + name='category_list' + ), + path( + route='examples//categories/', + view=CategoryDetailAPI.as_view(), + name='category_detail' + ), + path( + route='examples//spans', + view=SpanListAPI.as_view(), + name='span_list' + ), + path( + route='examples//spans/', + view=SpanDetailAPI.as_view(), + name='span_detail' + ), + path( + route='examples//texts', + view=TextLabelListAPI.as_view(), + name='text_list' + ), + path( + route='examples//texts/', + view=TextLabelDetailAPI.as_view(), + name='text_detail' + ), +] diff --git a/backend/labels/views.py b/backend/labels/views.py index 91ea44a2..b689111c 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -1,3 +1,125 @@ -from django.shortcuts import render +from functools import partial -# Create your views here. +from django.core.exceptions import ValidationError +from django.shortcuts import get_object_or_404 +from rest_framework import generics, status +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response + +from api.models import Project +from labels.models import Category, Span, TextLabel, AnnotationRelations +from members.permissions import IsInProjectOrAdmin, IsInProjectReadOnlyOrAdmin +from .permissions import CanEditAnnotation +from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, AnnotationRelationsSerializer + + +class BaseListAPI(generics.ListCreateAPIView): + annotation_class = None + pagination_class = None + permission_classes = [IsAuthenticated & IsInProjectOrAdmin] + swagger_schema = None + + @property + def project(self): + return get_object_or_404(Project, pk=self.kwargs['project_id']) + + def get_queryset(self): + queryset = self.annotation_class.objects.filter(example=self.kwargs['example_id']) + if not self.project.collaborative_annotation: + queryset = queryset.filter(user=self.request.user) + return queryset + + def create(self, request, *args, **kwargs): + request.data['example'] = self.kwargs['example_id'] + try: + response = super().create(request, args, kwargs) + except ValidationError as err: + response = Response({'detail': err.messages}, status=status.HTTP_400_BAD_REQUEST) + return response + + def perform_create(self, serializer): + serializer.save(example_id=self.kwargs['example_id'], user=self.request.user) + + def delete(self, request, *args, **kwargs): + queryset = self.get_queryset() + queryset.all().delete() + return Response(status=status.HTTP_204_NO_CONTENT) + + +class BaseDetailAPI(generics.RetrieveUpdateDestroyAPIView): + lookup_url_kwarg = 'annotation_id' + swagger_schema = None + + @property + def project(self): + return get_object_or_404(Project, pk=self.kwargs['project_id']) + + def get_permissions(self): + if self.project.collaborative_annotation: + self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin] + else: + self.permission_classes = [ + IsAuthenticated & IsInProjectOrAdmin & partial(CanEditAnnotation, self.queryset) + ] + return super().get_permissions() + + +class CategoryListAPI(BaseListAPI): + annotation_class = Category + serializer_class = CategorySerializer + + def create(self, request, *args, **kwargs): + if self.project.single_class_classification: + self.get_queryset().delete() + return super().create(request, args, kwargs) + + +class CategoryDetailAPI(BaseDetailAPI): + queryset = Category.objects.all() + serializer_class = CategorySerializer + + +class SpanListAPI(BaseListAPI): + annotation_class = Span + serializer_class = SpanSerializer + + +class SpanDetailAPI(BaseDetailAPI): + queryset = Span.objects.all() + serializer_class = SpanSerializer + + +class TextLabelListAPI(BaseListAPI): + annotation_class = TextLabel + serializer_class = TextLabelSerializer + + +class TextLabelDetailAPI(BaseDetailAPI): + queryset = TextLabel.objects.all() + serializer_class = TextLabelSerializer + + +class RelationList(generics.ListCreateAPIView): + serializer_class = AnnotationRelationsSerializer + pagination_class = None + permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] + + def get_queryset(self): + project = get_object_or_404(Project, pk=self.kwargs['project_id']) + return project.annotation_relations + + def perform_create(self, serializer): + project = get_object_or_404(Project, pk=self.kwargs['project_id']) + serializer.save(project=project) + + def delete(self, request, *args, **kwargs): + delete_ids = request.data['ids'] + AnnotationRelations.objects.filter(pk__in=delete_ids).delete() + return Response(status=status.HTTP_204_NO_CONTENT) + + +class RelationDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = AnnotationRelations.objects.all() + serializer_class = AnnotationRelationsSerializer + lookup_url_kwarg = 'annotation_relation_id' + permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] diff --git a/backend/metrics/views.py b/backend/metrics/views.py index b184a26f..4f2730f6 100644 --- a/backend/metrics/views.py +++ b/backend/metrics/views.py @@ -5,7 +5,8 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from api.models import Example, ExampleState, Annotation, Label, Category, CategoryType, Span, SpanType +from api.models import Example, ExampleState, Label, CategoryType, SpanType +from labels.models import Annotation, Category, Span from members.models import Member from members.permissions import IsInProjectReadOnlyOrAdmin From 3165650e8d631fbd3526e45971fa053176af4d76 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 11:33:01 +0900 Subject: [PATCH 3/6] Remove unused files and code --- backend/api/exceptions.py | 5 ---- backend/api/views/tasks/__init__.py | 0 backend/api/views/tasks/base.py | 0 backend/api/views/tasks/category.py | 0 backend/api/views/tasks/relation.py | 37 ----------------------------- backend/api/views/tasks/span.py | 0 backend/api/views/tasks/text.py | 0 7 files changed, 42 deletions(-) delete mode 100644 backend/api/views/tasks/__init__.py delete mode 100644 backend/api/views/tasks/base.py delete mode 100644 backend/api/views/tasks/category.py delete mode 100644 backend/api/views/tasks/relation.py delete mode 100644 backend/api/views/tasks/span.py delete mode 100644 backend/api/views/tasks/text.py diff --git a/backend/api/exceptions.py b/backend/api/exceptions.py index 353d00c7..fd5cd1b1 100644 --- a/backend/api/exceptions.py +++ b/backend/api/exceptions.py @@ -5,8 +5,3 @@ from rest_framework.exceptions import APIException class LabelValidationError(APIException): status_code = status.HTTP_400_BAD_REQUEST default_detail = 'You cannot create a label with same name or shortcut key.' - - -class AnnotationRelationValidationError(APIException): - status_code = status.HTTP_400_BAD_REQUEST - default_detail = 'You cannot create an annotation relation between the same annotation.' diff --git a/backend/api/views/tasks/__init__.py b/backend/api/views/tasks/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/api/views/tasks/base.py b/backend/api/views/tasks/base.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/api/views/tasks/category.py b/backend/api/views/tasks/category.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/api/views/tasks/relation.py b/backend/api/views/tasks/relation.py deleted file mode 100644 index cdb49874..00000000 --- a/backend/api/views/tasks/relation.py +++ /dev/null @@ -1,37 +0,0 @@ -import json - -from django.db import IntegrityError, transaction -from django.shortcuts import get_object_or_404 -from rest_framework import status -from rest_framework.exceptions import ParseError -from rest_framework.parsers import MultiPartParser -from rest_framework.permissions import IsAuthenticated -from rest_framework.response import Response -from rest_framework.views import APIView - -from labels.serializers import AnnotationRelationsSerializer -from members.permissions import IsProjectAdmin - -from ...exceptions import AnnotationRelationValidationError -from ...models import Project - - -class RelationUploadAPI(APIView): - parser_classes = (MultiPartParser,) - permission_classes = [IsAuthenticated & IsProjectAdmin] - - @transaction.atomic - def post(self, request, *args, **kwargs): - if 'file' not in request.data: - raise ParseError('Empty content') - project = get_object_or_404(Project, pk=kwargs['project_id']) - try: - annotation_relations = json.load(request.data) - serializer = AnnotationRelationsSerializer(data=annotation_relations, many=True) - serializer.is_valid(raise_exception=True) - serializer.save(project=project) - return Response(status=status.HTTP_201_CREATED) - except json.decoder.JSONDecodeError: - raise ParseError('The file format is invalid.') - except IntegrityError: - raise AnnotationRelationValidationError diff --git a/backend/api/views/tasks/span.py b/backend/api/views/tasks/span.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/api/views/tasks/text.py b/backend/api/views/tasks/text.py deleted file mode 100644 index e69de29b..00000000 From a7ce45142f72cbfb10387b55a64870d434af0d96 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 11:36:54 +0900 Subject: [PATCH 4/6] Rename RelationSerializer --- backend/labels/serializers.py | 2 +- backend/labels/views.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py index 059003cf..d3d04a57 100644 --- a/backend/labels/serializers.py +++ b/backend/labels/serializers.py @@ -59,7 +59,7 @@ class TextLabelSerializer(serializers.ModelSerializer): read_only_fields = ('user',) -class AnnotationRelationsSerializer(serializers.ModelSerializer): +class RelationSerializer(serializers.ModelSerializer): def validate(self, attrs): return super().validate(attrs) diff --git a/backend/labels/views.py b/backend/labels/views.py index b689111c..3a0715b5 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -10,7 +10,7 @@ from api.models import Project from labels.models import Category, Span, TextLabel, AnnotationRelations from members.permissions import IsInProjectOrAdmin, IsInProjectReadOnlyOrAdmin from .permissions import CanEditAnnotation -from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, AnnotationRelationsSerializer +from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, RelationSerializer class BaseListAPI(generics.ListCreateAPIView): @@ -100,7 +100,7 @@ class TextLabelDetailAPI(BaseDetailAPI): class RelationList(generics.ListCreateAPIView): - serializer_class = AnnotationRelationsSerializer + serializer_class = RelationSerializer pagination_class = None permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] @@ -120,6 +120,6 @@ class RelationList(generics.ListCreateAPIView): class RelationDetail(generics.RetrieveUpdateDestroyAPIView): queryset = AnnotationRelations.objects.all() - serializer_class = AnnotationRelationsSerializer + serializer_class = RelationSerializer lookup_url_kwarg = 'annotation_relation_id' permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] From 375604ba2aeced42de553ffb8e0db1cf67c4889c Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 11:41:51 +0900 Subject: [PATCH 5/6] Rename Relation model --- ...002_rename_annotationrelations_relation.py | 20 +++++++++++++++++++ backend/labels/models.py | 2 +- backend/labels/serializers.py | 4 ++-- backend/labels/views.py | 6 +++--- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 backend/labels/migrations/0002_rename_annotationrelations_relation.py diff --git a/backend/labels/migrations/0002_rename_annotationrelations_relation.py b/backend/labels/migrations/0002_rename_annotationrelations_relation.py new file mode 100644 index 00000000..f23d31bb --- /dev/null +++ b/backend/labels/migrations/0002_rename_annotationrelations_relation.py @@ -0,0 +1,20 @@ +# Generated by Django 3.2.11 on 2022-01-27 02:39 + +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0031_auto_20220127_0032'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('labels', '0001_initial'), + ] + + operations = [ + migrations.RenameModel( + old_name='AnnotationRelations', + new_name='Relation', + ), + ] diff --git a/backend/labels/models.py b/backend/labels/models.py index 8c0c8716..6f46ca5d 100644 --- a/backend/labels/models.py +++ b/backend/labels/models.py @@ -104,7 +104,7 @@ class TextLabel(Annotation): ) -class AnnotationRelations(models.Model): +class Relation(models.Model): annotation_id_1 = models.IntegerField() annotation_id_2 = models.IntegerField() type = models.ForeignKey(RelationTypes, related_name='annotation_relations', on_delete=models.CASCADE) diff --git a/backend/labels/serializers.py b/backend/labels/serializers.py index d3d04a57..e87c66b8 100644 --- a/backend/labels/serializers.py +++ b/backend/labels/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers from api.models import CategoryType, Example, SpanType -from .models import Category, Span, TextLabel, AnnotationRelations +from .models import Category, Span, TextLabel, Relation class CategorySerializer(serializers.ModelSerializer): @@ -65,5 +65,5 @@ class RelationSerializer(serializers.ModelSerializer): return super().validate(attrs) class Meta: - model = AnnotationRelations + model = Relation fields = ('id', 'annotation_id_1', 'annotation_id_2', 'type', 'user', 'timestamp') diff --git a/backend/labels/views.py b/backend/labels/views.py index 3a0715b5..8f031b43 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -7,7 +7,7 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from api.models import Project -from labels.models import Category, Span, TextLabel, AnnotationRelations +from labels.models import Category, Span, TextLabel, Relation from members.permissions import IsInProjectOrAdmin, IsInProjectReadOnlyOrAdmin from .permissions import CanEditAnnotation from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, RelationSerializer @@ -114,12 +114,12 @@ class RelationList(generics.ListCreateAPIView): def delete(self, request, *args, **kwargs): delete_ids = request.data['ids'] - AnnotationRelations.objects.filter(pk__in=delete_ids).delete() + Relation.objects.filter(pk__in=delete_ids).delete() return Response(status=status.HTTP_204_NO_CONTENT) class RelationDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = AnnotationRelations.objects.all() + queryset = Relation.objects.all() serializer_class = RelationSerializer lookup_url_kwarg = 'annotation_relation_id' permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] From b446015eaea9a7979363a849ea6876b25b186705 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 27 Jan 2022 13:31:29 +0900 Subject: [PATCH 6/6] Rename annotation --- backend/auto_labeling/pipeline/labels.py | 6 ++-- backend/labels/managers.py | 8 ++--- backend/labels/models.py | 12 +++---- backend/labels/permissions.py | 2 +- backend/labels/tests/test_category.py | 10 +++--- backend/labels/tests/test_span.py | 10 +++--- backend/labels/tests/test_text_label.py | 23 ++++++-------- backend/labels/tests/test_views.py | 40 ++++++++++++------------ backend/labels/urls.py | 6 ++-- backend/labels/views.py | 16 +++++----- backend/metrics/views.py | 8 ++--- 11 files changed, 69 insertions(+), 72 deletions(-) diff --git a/backend/auto_labeling/pipeline/labels.py b/backend/auto_labeling/pipeline/labels.py index ff172d37..fabcc91c 100644 --- a/backend/auto_labeling/pipeline/labels.py +++ b/backend/auto_labeling/pipeline/labels.py @@ -5,7 +5,7 @@ from auto_labeling_pipeline.labels import Labels from django.contrib.auth.models import User from api.models import Project, Example, CategoryType, SpanType -from labels.models import Annotation, Category, Span, TextLabel +from labels.models import Label, Category, Span, TextLabel class LabelCollection(abc.ABC): @@ -15,7 +15,7 @@ class LabelCollection(abc.ABC): def __init__(self, labels): self.labels = labels - def transform(self, project: Project, example: Example, user: User) -> List[Annotation]: + def transform(self, project: Project, example: Example, user: User) -> List[Label]: mapping = { c.text: c for c in self.label_type.objects.filter(project=project) } @@ -48,7 +48,7 @@ class Spans(LabelCollection): class Texts(LabelCollection): model = TextLabel - def transform(self, project: Project, example: Example, user: User) -> List[Annotation]: + def transform(self, project: Project, example: Example, user: User) -> List[Label]: annotations = [] for label in self.labels: label['example'] = example diff --git a/backend/labels/managers.py b/backend/labels/managers.py index 6452f246..ad91f7fb 100644 --- a/backend/labels/managers.py +++ b/backend/labels/managers.py @@ -1,7 +1,7 @@ from django.db.models import Manager, Count -class AnnotationManager(Manager): +class LabelManager(Manager): def calc_label_distribution(self, examples, members, labels): """Calculate label distribution. @@ -42,7 +42,7 @@ class AnnotationManager(Manager): return [label for label in labels if self.can_annotate(label, project)] -class CategoryManager(AnnotationManager): +class CategoryManager(LabelManager): def can_annotate(self, label, project) -> bool: is_exclusive = project.single_class_classification @@ -53,7 +53,7 @@ class CategoryManager(AnnotationManager): return not categories.filter(label=label.label).exists() -class SpanManager(AnnotationManager): +class SpanManager(LabelManager): def can_annotate(self, label, project) -> bool: overlapping = getattr(project, 'allow_overlapping', False) @@ -66,7 +66,7 @@ class SpanManager(AnnotationManager): return True -class TextLabelManager(AnnotationManager): +class TextLabelManager(LabelManager): def can_annotate(self, label, project) -> bool: texts = self.get_labels(label, project) diff --git a/backend/labels/models.py b/backend/labels/models.py index 6f46ca5d..48b40b5b 100644 --- a/backend/labels/models.py +++ b/backend/labels/models.py @@ -2,12 +2,12 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models -from .managers import AnnotationManager, CategoryManager, SpanManager, TextLabelManager +from .managers import LabelManager, CategoryManager, SpanManager, TextLabelManager from api.models import Example, CategoryType, SpanType, RelationTypes, Project -class Annotation(models.Model): - objects = AnnotationManager() +class Label(models.Model): + objects = LabelManager() prob = models.FloatField(default=0.0) manual = models.BooleanField(default=False) @@ -19,7 +19,7 @@ class Annotation(models.Model): abstract = True -class Category(Annotation): +class Category(Label): objects = CategoryManager() example = models.ForeignKey( to=Example, @@ -36,7 +36,7 @@ class Category(Annotation): ) -class Span(Annotation): +class Span(Label): objects = SpanManager() example = models.ForeignKey( to=Example, @@ -84,7 +84,7 @@ class Span(Annotation): ] -class TextLabel(Annotation): +class TextLabel(Label): objects = TextLabelManager() example = models.ForeignKey( to=Example, diff --git a/backend/labels/permissions.py b/backend/labels/permissions.py index e2ec568e..39fddb69 100644 --- a/backend/labels/permissions.py +++ b/backend/labels/permissions.py @@ -1,7 +1,7 @@ from rest_framework.permissions import BasePermission -class CanEditAnnotation(BasePermission): +class CanEditLabel(BasePermission): def __init__(self, queryset): super().__init__() diff --git a/backend/labels/tests/test_category.py b/backend/labels/tests/test_category.py index e07a60d4..345ce68e 100644 --- a/backend/labels/tests/test_category.py +++ b/backend/labels/tests/test_category.py @@ -9,7 +9,7 @@ from labels.models import Category from api.tests.api.utils import prepare_project -class TestCategoryAnnotation(abc.ABC, TestCase): +class TestCategoryLabeling(abc.ABC, TestCase): exclusive = True collaborative = False @@ -54,7 +54,7 @@ class NonCollaborativeMixin: self.assertTrue(can_annotate) -class TestExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin): +class TestExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin): exclusive = True collaborative = False @@ -64,7 +64,7 @@ class TestExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMi self.assertFalse(can_annotate) -class TestNonExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin): +class TestNonExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin): exclusive = False collaborative = False @@ -87,7 +87,7 @@ class CollaborativeMixin: self.assertFalse(can_annotate) -class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin): +class TestCollaborativeExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin): exclusive = True collaborative = True @@ -101,7 +101,7 @@ class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation, Colla self.assertFalse(can_annotate) -class TestCollaborativeNonExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin): +class TestCollaborativeNonExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin): exclusive = False collaborative = True diff --git a/backend/labels/tests/test_span.py b/backend/labels/tests/test_span.py index 61388db5..79e32f20 100644 --- a/backend/labels/tests/test_span.py +++ b/backend/labels/tests/test_span.py @@ -10,7 +10,7 @@ from labels.models import Span from api.tests.api.utils import prepare_project -class TestSpanAnnotation(abc.ABC, TestCase): +class TestSpanLabeling(abc.ABC, TestCase): overlapping = False collaborative = False @@ -54,7 +54,7 @@ class NonCollaborativeMixin: self.assertTrue(can_annotate) -class TestNonOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): +class TestNonOverlappingSpanLabeling(TestSpanLabeling, NonCollaborativeMixin): overlapping = False collaborative = False @@ -82,7 +82,7 @@ class TestNonOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin self.assertFalse(can_annotate) -class TestOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): +class TestOverlappingSpanLabeling(TestSpanLabeling, NonCollaborativeMixin): overlapping = True collaborative = False @@ -99,7 +99,7 @@ class TestOverlappingSpanAnnotation(TestSpanAnnotation, NonCollaborativeMixin): self.assertTrue(can_annotate) -class TestCollaborativeNonOverlappingSpanAnnotation(TestSpanAnnotation): +class TestCollaborativeNonOverlappingSpanLabeling(TestSpanLabeling): overlapping = False collaborative = True @@ -127,7 +127,7 @@ class TestCollaborativeNonOverlappingSpanAnnotation(TestSpanAnnotation): self.assertFalse(can_annotate) -class TestCollaborativeOverlappingSpanAnnotation(TestSpanAnnotation): +class TestCollaborativeOverlappingSpanLabeling(TestSpanLabeling): overlapping = True collaborative = True diff --git a/backend/labels/tests/test_text_label.py b/backend/labels/tests/test_text_label.py index 9cda5af1..2fac946e 100644 --- a/backend/labels/tests/test_text_label.py +++ b/backend/labels/tests/test_text_label.py @@ -9,7 +9,7 @@ from labels.models import TextLabel from api.tests.api.utils import prepare_project -class TestTextLabelAnnotation(abc.ABC, TestCase): +class TestTextLabeling(abc.ABC, TestCase): collaborative = False @classmethod @@ -32,8 +32,15 @@ class TestTextLabelAnnotation(abc.ABC, TestCase): can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item) self.assertTrue(can_annotate) + def test_uniqueness(self): + a = mommy.make('TextLabel') + with self.assertRaises(IntegrityError): + TextLabel(example=a.example, + user=a.user, + text=a.text).save() -class TestNonCollaborativeTextLabelAnnotation(TestTextLabelAnnotation): + +class TestNonCollaborativeTextLabeling(TestTextLabeling): collaborative = False def test_cannot_annotate_same_text_to_annotated_data(self): @@ -62,7 +69,7 @@ class TestNonCollaborativeTextLabelAnnotation(TestTextLabelAnnotation): self.assertTrue(can_annotate) -class TestCollaborativeTextLabelAnnotation(TestTextLabelAnnotation): +class TestCollaborativeTextLabeling(TestTextLabeling): collaborative = True def test_deny_another_user_to_annotate_same_text(self): @@ -83,13 +90,3 @@ class TestCollaborativeTextLabelAnnotation(TestTextLabelAnnotation): ) can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item) self.assertTrue(can_annotate) - - -class TestSeq2seqAnnotation(TestCase): - - def test_uniqueness(self): - a = mommy.make('TextLabel') - with self.assertRaises(IntegrityError): - TextLabel(example=a.example, - user=a.user, - text=a.text).save() diff --git a/backend/labels/tests/test_views.py b/backend/labels/tests/test_views.py index 2c765541..76d698c2 100644 --- a/backend/labels/tests/test_views.py +++ b/backend/labels/tests/test_views.py @@ -7,7 +7,7 @@ from api.tests.api.utils import (CRUDMixin, make_annotation, make_doc, make_labe make_user, prepare_project) -class TestAnnotationList: +class TestLabelList: model = Category task = DOCUMENT_CLASSIFICATION view_name = 'annotation_list' @@ -42,13 +42,13 @@ class TestAnnotationList: self.assertEqual(count, 2) # delete only own annotation -class TestCategoryList(TestAnnotationList, CRUDMixin): +class TestCategoryList(TestLabelList, CRUDMixin): model = Category task = DOCUMENT_CLASSIFICATION view_name = 'category_list' -class TestSpanList(TestAnnotationList, CRUDMixin): +class TestSpanList(TestLabelList, CRUDMixin): model = Span task = SEQUENCE_LABELING view_name = 'span_list' @@ -58,13 +58,13 @@ class TestSpanList(TestAnnotationList, CRUDMixin): make_annotation(cls.task, doc=doc, user=member, start_offset=0, end_offset=1) -class TestTextList(TestAnnotationList, CRUDMixin): +class TestTextList(TestLabelList, CRUDMixin): model = TextLabel task = SEQ2SEQ view_name = 'text_list' -class TestSharedAnnotationList: +class TestSharedLabelList: model = Category task = DOCUMENT_CLASSIFICATION view_name = 'annotation_list' @@ -92,13 +92,13 @@ class TestSharedAnnotationList: self.assertEqual(count, 0) # delete all annotation in the doc -class TestSharedCategoryList(TestSharedAnnotationList, CRUDMixin): +class TestSharedCategoryList(TestSharedLabelList, CRUDMixin): model = Category task = DOCUMENT_CLASSIFICATION view_name = 'category_list' -class TestSharedSpanList(TestSharedAnnotationList, CRUDMixin): +class TestSharedSpanList(TestSharedLabelList, CRUDMixin): model = Span task = SEQUENCE_LABELING view_name = 'span_list' @@ -116,13 +116,13 @@ class TestSharedSpanList(TestSharedAnnotationList, CRUDMixin): cls.start_offset += 1 -class TestSharedTextList(TestSharedAnnotationList, CRUDMixin): +class TestSharedTextList(TestSharedLabelList, CRUDMixin): model = TextLabel task = SEQ2SEQ view_name = 'text_list' -class TestAnnotationCreation: +class TestDataLabeling: task = DOCUMENT_CLASSIFICATION view_name = 'annotation_list' @@ -148,11 +148,11 @@ class TestAnnotationCreation: self.assert_create(expected=status.HTTP_403_FORBIDDEN) -class TestCategoryCreation(TestAnnotationCreation, CRUDMixin): +class TestCategoryCreation(TestDataLabeling, CRUDMixin): view_name = 'category_list' -class TestSpanCreation(TestAnnotationCreation, CRUDMixin): +class TestSpanCreation(TestDataLabeling, CRUDMixin): task = SEQUENCE_LABELING view_name = 'span_list' @@ -161,7 +161,7 @@ class TestSpanCreation(TestAnnotationCreation, CRUDMixin): return {'label': label.id, 'start_offset': 0, 'end_offset': 1} -class TestTextLabelCreation(TestAnnotationCreation, CRUDMixin): +class TestTextLabelCreation(TestDataLabeling, CRUDMixin): task = SEQ2SEQ view_name = 'text_list' @@ -169,7 +169,7 @@ class TestTextLabelCreation(TestAnnotationCreation, CRUDMixin): return {'text': 'example'} -class TestAnnotationDetail: +class TestLabelDetail: task = SEQUENCE_LABELING view_name = 'annotation_detail' @@ -225,7 +225,7 @@ class TestAnnotationDetail: self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN) -class TestCategoryDetail(TestAnnotationDetail, CRUDMixin): +class TestCategoryDetail(TestLabelDetail, CRUDMixin): task = DOCUMENT_CLASSIFICATION view_name = 'category_detail' @@ -233,12 +233,12 @@ class TestCategoryDetail(TestAnnotationDetail, CRUDMixin): return make_annotation(task=self.task, doc=doc, user=self.project.users[0]) -class TestSpanDetail(TestAnnotationDetail, CRUDMixin): +class TestSpanDetail(TestLabelDetail, CRUDMixin): task = SEQUENCE_LABELING view_name = 'span_detail' -class TestTextDetail(TestAnnotationDetail, CRUDMixin): +class TestTextDetail(TestLabelDetail, CRUDMixin): task = SEQ2SEQ view_name = 'text_detail' @@ -250,7 +250,7 @@ class TestTextDetail(TestAnnotationDetail, CRUDMixin): return make_annotation(task=self.task, doc=doc, user=self.project.users[0]) -class TestSharedAnnotationDetail: +class TestSharedLabelDetail: task = DOCUMENT_CLASSIFICATION view_name = 'annotation_detail' @@ -277,11 +277,11 @@ class TestSharedAnnotationDetail: self.assert_delete(self.project.users[1], status.HTTP_204_NO_CONTENT) -class TestSharedCategoryDetail(TestSharedAnnotationDetail, CRUDMixin): +class TestSharedCategoryDetail(TestSharedLabelDetail, CRUDMixin): view_name = 'category_detail' -class TestSharedSpanDetail(TestSharedAnnotationDetail, CRUDMixin): +class TestSharedSpanDetail(TestSharedLabelDetail, CRUDMixin): task = SEQUENCE_LABELING view_name = 'span_detail' @@ -289,7 +289,7 @@ class TestSharedSpanDetail(TestSharedAnnotationDetail, CRUDMixin): return make_annotation(self.task, doc=doc, user=member, start_offset=0, end_offset=1) -class TestSharedTextDetail(TestSharedAnnotationDetail, CRUDMixin): +class TestSharedTextDetail(TestSharedLabelDetail, CRUDMixin): task = SEQ2SEQ view_name = 'text_detail' diff --git a/backend/labels/urls.py b/backend/labels/urls.py index e689a66b..c36cf5bb 100644 --- a/backend/labels/urls.py +++ b/backend/labels/urls.py @@ -10,12 +10,12 @@ urlpatterns = [ path( route='annotation_relations', view=RelationList.as_view(), - name='relation_types_list' + name='relation_list' ), path( - route='annotation_relations/', + route='annotation_relations/', view=RelationDetail.as_view(), - name='annotation_relation_detail' + name='relation_detail' ), path( route='examples//categories', diff --git a/backend/labels/views.py b/backend/labels/views.py index 8f031b43..e2cc8fb2 100644 --- a/backend/labels/views.py +++ b/backend/labels/views.py @@ -9,12 +9,12 @@ from rest_framework.response import Response from api.models import Project from labels.models import Category, Span, TextLabel, Relation from members.permissions import IsInProjectOrAdmin, IsInProjectReadOnlyOrAdmin -from .permissions import CanEditAnnotation +from .permissions import CanEditLabel from .serializers import CategorySerializer, SpanSerializer, TextLabelSerializer, RelationSerializer class BaseListAPI(generics.ListCreateAPIView): - annotation_class = None + label_class = None pagination_class = None permission_classes = [IsAuthenticated & IsInProjectOrAdmin] swagger_schema = None @@ -24,7 +24,7 @@ class BaseListAPI(generics.ListCreateAPIView): return get_object_or_404(Project, pk=self.kwargs['project_id']) def get_queryset(self): - queryset = self.annotation_class.objects.filter(example=self.kwargs['example_id']) + queryset = self.label_class.objects.filter(example=self.kwargs['example_id']) if not self.project.collaborative_annotation: queryset = queryset.filter(user=self.request.user) return queryset @@ -59,13 +59,13 @@ class BaseDetailAPI(generics.RetrieveUpdateDestroyAPIView): self.permission_classes = [IsAuthenticated & IsInProjectOrAdmin] else: self.permission_classes = [ - IsAuthenticated & IsInProjectOrAdmin & partial(CanEditAnnotation, self.queryset) + IsAuthenticated & IsInProjectOrAdmin & partial(CanEditLabel, self.queryset) ] return super().get_permissions() class CategoryListAPI(BaseListAPI): - annotation_class = Category + label_class = Category serializer_class = CategorySerializer def create(self, request, *args, **kwargs): @@ -80,7 +80,7 @@ class CategoryDetailAPI(BaseDetailAPI): class SpanListAPI(BaseListAPI): - annotation_class = Span + label_class = Span serializer_class = SpanSerializer @@ -90,7 +90,7 @@ class SpanDetailAPI(BaseDetailAPI): class TextLabelListAPI(BaseListAPI): - annotation_class = TextLabel + label_class = TextLabel serializer_class = TextLabelSerializer @@ -121,5 +121,5 @@ class RelationList(generics.ListCreateAPIView): class RelationDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Relation.objects.all() serializer_class = RelationSerializer - lookup_url_kwarg = 'annotation_relation_id' + lookup_url_kwarg = 'annotation_id' permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] diff --git a/backend/metrics/views.py b/backend/metrics/views.py index 4f2730f6..43b795fc 100644 --- a/backend/metrics/views.py +++ b/backend/metrics/views.py @@ -5,8 +5,8 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from api.models import Example, ExampleState, Label, CategoryType, SpanType -from labels.models import Annotation, Category, Span +from api.models import Example, ExampleState, CategoryType, SpanType, Label as LabelType +from labels.models import Label, Category, Span from members.models import Member from members.permissions import IsInProjectReadOnlyOrAdmin @@ -33,8 +33,8 @@ class MemberProgressAPI(APIView): class LabelDistribution(abc.ABC, APIView): permission_classes = [IsAuthenticated & IsInProjectReadOnlyOrAdmin] - model = Annotation - label_type = Label + model = Label + label_type = LabelType def get(self, request, *args, **kwargs): labels = self.label_type.objects.filter(project=self.kwargs['project_id'])