backend/image_markuper/dicom/models/shapes.py

94 lines
2.1 KiB
Python
Raw Normal View History

2022-11-05 22:14:19 +03:00
from dicom.models import Layer
2022-10-28 21:52:02 +03:00
from django.db import models
from polymorphic.models import PolymorphicModel
class BaseShape(PolymorphicModel):
2022-11-01 12:20:43 +03:00
TYPE = "no_type"
min_coordinates = None
max_coordinates = None
2022-11-05 22:14:19 +03:00
layer = models.ForeignKey(Layer, related_name="shapes", on_delete=models.CASCADE)
2022-10-28 21:52:02 +03:00
def serialize_self(self):
2022-11-01 12:20:43 +03:00
return {
"id": self.id,
"type": self.TYPE,
"image_number": self.image_number,
"coordinates": self.coordinates,
}
def serialize_self_without_layer(self):
return {
"id": self.id,
"type": self.TYPE,
"coordinates": self.coordinates,
}
2022-10-28 21:52:02 +03:00
@property
def coordinates(self) -> [(int, int)]:
return self.shape_coordinates.all().values("x", "y")
def __str__(self):
return self.dicom.file.name
class Coordinate(models.Model):
2022-10-29 22:46:19 +03:00
x = models.FloatField()
y = models.FloatField()
2022-10-28 21:52:02 +03:00
shape = models.ForeignKey(
to=BaseShape,
null=False,
blank=False,
on_delete=models.CASCADE,
related_name="shape_coordinates",
)
class Circle(BaseShape):
2022-10-30 16:40:46 +03:00
radius = models.FloatField()
2022-11-01 12:20:43 +03:00
max_coordinates = 1
2022-10-28 21:52:02 +03:00
def serialize_self(self):
return {
"id": self.id,
"type": "circle",
"image_number": self.image_number,
"radius": self.radius,
"coordinates": self.coordinates,
}
2022-10-30 16:40:46 +03:00
def serialize_self_without_layer(self):
return {
"id": self.id,
"type": "circle",
"radius": self.radius,
"coordinates": self.coordinates,
}
2022-10-28 21:52:02 +03:00
def __str__(self):
return f"circle on {self.dicom.file.name}"
2022-10-30 00:12:13 +03:00
class Roi(BaseShape):
2022-11-01 12:20:43 +03:00
TYPE = "roi"
2022-10-30 16:40:46 +03:00
2022-10-28 21:52:02 +03:00
def __str__(self):
2022-10-30 00:12:13 +03:00
return f"Roi on {self.dicom.file.name}"
2022-11-01 12:20:43 +03:00
class FreeHand(BaseShape):
TYPE = "free_hand"
def __str__(self):
return f"FreeHand on {self.dicom.file.name}"
class Ruler(BaseShape):
TYPE = "ruler"
max_coordinates = 2
min_coordinates = 2
def __str__(self):
return f"Ruler on {self.dicom.file.name}"