2022-10-30 00:12:13 +03:00
|
|
|
from dicom.models import Circle, Coordinate, Dicom, Roi
|
2022-10-28 21:52:02 +03:00
|
|
|
from drf_spectacular.utils import extend_schema_field
|
2022-10-26 23:22:04 +03:00
|
|
|
from rest_framework import serializers
|
2022-10-28 21:52:02 +03:00
|
|
|
from rest_framework.generics import get_object_or_404
|
|
|
|
|
|
|
|
|
|
|
|
def create_coordinate(coordinates, obj):
|
|
|
|
for coordinate in coordinates:
|
|
|
|
Coordinate.objects.create(
|
|
|
|
x=coordinate["x"],
|
|
|
|
y=coordinate["y"],
|
|
|
|
shape=obj,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CoordinateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Coordinate
|
|
|
|
fields = ["x", "y"]
|
2022-10-26 23:22:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ListDicomSerializer(serializers.ModelSerializer):
|
|
|
|
url = serializers.HyperlinkedIdentityField(
|
|
|
|
view_name="get_update_delete_dicom", lookup_field="slug"
|
|
|
|
)
|
|
|
|
file = serializers.FileField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Dicom
|
2022-10-28 22:17:29 +03:00
|
|
|
fields = ["file", "uploaded", "pathology_type", "url"]
|
2022-10-26 23:22:04 +03:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
return Dicom.objects.create(**validated_data, user=self.context["request"].user)
|
|
|
|
|
|
|
|
|
2022-10-28 21:52:02 +03:00
|
|
|
class BaseShapeSerializer(serializers.Serializer):
|
2022-10-30 16:40:46 +03:00
|
|
|
type = serializers.ChoiceField(choices=["circle", "roi"])
|
2022-10-28 21:52:02 +03:00
|
|
|
image_number = serializers.IntegerField()
|
|
|
|
coordinates = CoordinateSerializer(many=True)
|
|
|
|
|
|
|
|
|
2022-10-30 16:40:46 +03:00
|
|
|
class BaseShapeLayerSerializer(serializers.Serializer):
|
|
|
|
type = serializers.ChoiceField(choices=["circle", "roi"])
|
|
|
|
radius = serializers.FloatField(required=False)
|
|
|
|
coordinates = CoordinateSerializer(many=True)
|
|
|
|
|
|
|
|
|
2022-10-26 23:22:04 +03:00
|
|
|
class DicomSerializer(serializers.ModelSerializer):
|
|
|
|
file = serializers.FileField()
|
2022-10-28 21:52:02 +03:00
|
|
|
shapes = serializers.SerializerMethodField("get_dicom_shapes")
|
|
|
|
|
|
|
|
@extend_schema_field(field=BaseShapeSerializer)
|
|
|
|
def get_dicom_shapes(self, obj):
|
|
|
|
return [x.serialize_self() for x in obj.shapes.all()]
|
2022-10-26 23:22:04 +03:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Dicom
|
2022-10-28 22:17:29 +03:00
|
|
|
fields = ["file", "uploaded", "pathology_type", "shapes"]
|
2022-10-28 21:52:02 +03:00
|
|
|
|
|
|
|
|
2022-10-30 00:12:13 +03:00
|
|
|
class RoiSerializer(serializers.ModelSerializer):
|
2022-10-28 21:52:02 +03:00
|
|
|
coordinates = CoordinateSerializer(many=True)
|
|
|
|
|
|
|
|
class Meta:
|
2022-10-30 00:12:13 +03:00
|
|
|
model = Roi
|
2022-10-28 21:52:02 +03:00
|
|
|
fields = ["id", "image_number", "coordinates"]
|
|
|
|
extra_kwargs = {"id": {"read_only": True}}
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
if "coordinates" not in validated_data:
|
|
|
|
raise serializers.ValidationError
|
|
|
|
dicom = get_object_or_404(
|
|
|
|
Dicom, slug=self.context["request"].parser_context["kwargs"]["slug"]
|
|
|
|
)
|
2022-10-30 00:12:13 +03:00
|
|
|
roi = Roi.objects.create(
|
2022-10-28 21:52:02 +03:00
|
|
|
dicom=dicom, image_number=validated_data["image_number"]
|
|
|
|
)
|
|
|
|
|
2022-10-30 00:12:13 +03:00
|
|
|
create_coordinate(validated_data["coordinates"], roi)
|
|
|
|
return roi
|
2022-10-28 21:52:02 +03:00
|
|
|
|
|
|
|
def update(self, obj: Circle, validated_data):
|
|
|
|
Coordinate.objects.filter(shape=obj).delete()
|
|
|
|
create_coordinate(validated_data["coordinates"], obj)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
class CircleSerializer(serializers.ModelSerializer):
|
|
|
|
coordinates = CoordinateSerializer(many=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Circle
|
|
|
|
fields = ["id", "image_number", "radius", "coordinates"]
|
|
|
|
extra_kwargs = {"id": {"read_only": True}}
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
if (
|
|
|
|
"coordinates" not in validated_data
|
|
|
|
and len(validated_data["coordinates"]) > 1
|
|
|
|
):
|
|
|
|
raise serializers.ValidationError
|
|
|
|
dicom = get_object_or_404(
|
|
|
|
Dicom, slug=self.context["request"].parser_context["kwargs"]["slug"]
|
|
|
|
)
|
|
|
|
circle = Circle.objects.create(
|
|
|
|
dicom=dicom,
|
|
|
|
image_number=validated_data["image_number"],
|
|
|
|
radius=validated_data["radius"],
|
|
|
|
)
|
|
|
|
|
|
|
|
create_coordinate(validated_data["coordinates"], circle)
|
|
|
|
return circle
|
|
|
|
|
|
|
|
def update(self, obj: Circle, validated_data):
|
|
|
|
Coordinate.objects.filter(shape=obj).delete()
|
|
|
|
create_coordinate(validated_data["coordinates"], obj)
|
|
|
|
if "radius" in validated_data:
|
|
|
|
obj.radius = validated_data["radius"]
|
|
|
|
obj.save()
|
|
|
|
return obj
|
2022-10-29 22:46:19 +03:00
|
|
|
|
|
|
|
|
2022-10-30 00:12:13 +03:00
|
|
|
class SmartFileUploadSerializer(serializers.Serializer):
|
2022-10-29 22:46:19 +03:00
|
|
|
file = serializers.FileField()
|