You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import uuid
  2. from django.contrib.auth.models import User
  3. from django.core.exceptions import ValidationError
  4. from django.db import models
  5. from .managers import (
  6. BoundingBoxManager,
  7. CategoryManager,
  8. LabelManager,
  9. RelationManager,
  10. SegmentationManager,
  11. SpanManager,
  12. TextLabelManager,
  13. )
  14. from examples.models import Example
  15. from label_types.models import CategoryType, RelationType, SpanType
  16. class Label(models.Model):
  17. objects = LabelManager()
  18. uuid = models.UUIDField(default=uuid.uuid4, unique=True)
  19. prob = models.FloatField(default=0.0)
  20. manual = models.BooleanField(default=False)
  21. user = models.ForeignKey(User, on_delete=models.CASCADE)
  22. created_at = models.DateTimeField(auto_now_add=True)
  23. updated_at = models.DateTimeField(auto_now=True)
  24. class Meta:
  25. abstract = True
  26. class Category(Label):
  27. objects = CategoryManager()
  28. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="categories")
  29. label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE)
  30. class Meta:
  31. unique_together = ("example", "user", "label")
  32. class Span(Label):
  33. objects = SpanManager()
  34. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="spans")
  35. label = models.ForeignKey(to=SpanType, on_delete=models.CASCADE)
  36. start_offset = models.IntegerField()
  37. end_offset = models.IntegerField()
  38. def __str__(self):
  39. text = self.example.text[self.start_offset : self.end_offset]
  40. return f"({text}, {self.start_offset}, {self.end_offset}, {self.label.text})"
  41. def validate_unique(self, exclude=None):
  42. allow_overlapping = getattr(self.example.project, "allow_overlapping", False)
  43. is_collaborative = self.example.project.collaborative_annotation
  44. if allow_overlapping:
  45. super().validate_unique(exclude=exclude)
  46. return
  47. overlapping_span = (
  48. Span.objects.exclude(id=self.id)
  49. .filter(example=self.example)
  50. .filter(
  51. models.Q(start_offset__gte=self.start_offset, start_offset__lt=self.end_offset)
  52. | models.Q(end_offset__gt=self.start_offset, end_offset__lte=self.end_offset)
  53. | models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset)
  54. )
  55. )
  56. if is_collaborative:
  57. if overlapping_span.exists():
  58. raise ValidationError("This overlapping is not allowed in this project.")
  59. else:
  60. if overlapping_span.filter(user=self.user).exists():
  61. raise ValidationError("This overlapping is not allowed in this project.")
  62. def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
  63. self.full_clean()
  64. super().save(force_insert, force_update, using, update_fields)
  65. def is_overlapping(self, other: "Span"):
  66. return (
  67. (other.start_offset <= self.start_offset < other.end_offset)
  68. or (other.start_offset < self.end_offset <= other.end_offset)
  69. or (self.start_offset < other.start_offset and other.end_offset < self.end_offset)
  70. )
  71. class Meta:
  72. constraints = [
  73. models.CheckConstraint(check=models.Q(start_offset__gte=0), name="startOffset >= 0"),
  74. models.CheckConstraint(check=models.Q(end_offset__gte=0), name="endOffset >= 0"),
  75. models.CheckConstraint(check=models.Q(start_offset__lt=models.F("end_offset")), name="start < end"),
  76. ]
  77. class TextLabel(Label):
  78. objects = TextLabelManager()
  79. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="texts")
  80. text = models.TextField()
  81. def is_same_text(self, other: "TextLabel"):
  82. return self.text == other.text
  83. class Meta:
  84. unique_together = ("example", "user", "text")
  85. class Relation(Label):
  86. objects = RelationManager()
  87. from_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="from_relations")
  88. to_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="to_relations")
  89. type = models.ForeignKey(RelationType, on_delete=models.CASCADE)
  90. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="relations")
  91. def __str__(self):
  92. text = self.example.text
  93. from_span = text[self.from_id.start_offset : self.from_id.end_offset]
  94. to_span = text[self.to_id.start_offset : self.to_id.end_offset]
  95. type_text = self.type.text
  96. return f"{from_span} - ({type_text}) -> {to_span}"
  97. def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
  98. self.full_clean()
  99. super().save(force_insert, force_update, using, update_fields)
  100. def clean(self):
  101. same_example = self.from_id.example == self.to_id.example == self.example
  102. if not same_example:
  103. raise ValidationError("You need to label the same example.")
  104. return super().clean()
  105. class BoundingBox(Label):
  106. objects = BoundingBoxManager()
  107. x = models.FloatField()
  108. y = models.FloatField()
  109. width = models.FloatField()
  110. height = models.FloatField()
  111. label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE)
  112. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="bboxes")
  113. class Meta:
  114. constraints = [
  115. models.CheckConstraint(check=models.Q(x__gte=0), name="x >= 0"),
  116. models.CheckConstraint(check=models.Q(y__gte=0), name="y >= 0"),
  117. models.CheckConstraint(check=models.Q(width__gte=0), name="width >= 0"),
  118. models.CheckConstraint(check=models.Q(height__gte=0), name="height >= 0"),
  119. ]
  120. class Segmentation(Label):
  121. objects = SegmentationManager()
  122. points = models.JSONField(default=list)
  123. label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE)
  124. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="segmentations")