fix a number of things

This commit is contained in:
Ross
2023-08-07 13:42:22 +01:00
parent b36fa370f1
commit c3e36bfb12
16 changed files with 302 additions and 34 deletions
+120 -9
View File
@@ -13,6 +13,7 @@ from django.utils import timezone
import pydicom
import pydicom.multival
from django.core.files.storage import FileSystemStorage
from django.conf import settings
@@ -272,8 +273,9 @@ class Case(models.Model):
related_name="atlas_edited_cases",
)
scrapped = models.BooleanField(
default=False, help_text="Question has been scrapped and will not be shown"
archive = models.BooleanField(
default=False,
help_text="Case has been archived, this will remain on the system but will not be shown",
)
open_access = models.BooleanField(
@@ -325,6 +327,30 @@ class Case(models.Model):
examinations = [series.get_examination() for series in self.series.all()]
return f"{self.pk}:{self.title} {'/'.join([str(c) for c in self.condition.all()])} [{', '.join(examinations)}]"
def get_case_dicom_json(self):
series_json = []
for series in self.series.all():
series_json.append(series.get_series_dicom_json())
print(series_json)
return {
"studies": [
{
"StudyInstanceUID": "1.3.6.1.4.1.14519.5.2.1.6279.6001.298806137288633453246975630178",
"StudyDate": "20000101",
"StudyTime": "",
"PatientName": "",
"PatientID": "LIDC-IDRI-0001",
"AccessionNumber": "",
"PatientAge": "",
"PatientSex": "",
"series": series_json,
}
]
}
pass
class SeriesImage(SeriesImageBase):
image = models.FileField(upload_to=image_directory_path)
@@ -332,6 +358,50 @@ class SeriesImage(SeriesImageBase):
"Series", related_name="images", on_delete=models.SET_NULL, null=True
)
def get_image_dicom_json(self):
try:
with pydicom.dcmread(self.image) as ds:
to_keep = [
"Columns",
"Rows",
"InstanceNumber",
"SOPClassUID",
"PhotometricInterpretation",
"BitsAllocated",
"BitsStored",
"PixelRepresentation",
"SamplesPerPixel",
"PixelSpacing",
"HighBit",
"ImageOrientationPatient",
"ImagePositionPatient",
"FrameOfReferenceUID",
"ImageType",
"Modality",
"SOPInstanceUID",
"SeriesInstanceUID",
"StudyInstanceUID",
"WindowCenter",
"WindowWidth",
"SeriesDate",
]
d = {}
for key in to_keep:
val = ds[key].value
if type(val) == pydicom.multival.MultiValue:
d[key] = list(val)
else:
d[key] = val
return {"metadata": d, "url": self.image.url}
except FileNotFoundError:
return []
class SeriesFinding(models.Model):
series = models.ForeignKey(
@@ -411,6 +481,23 @@ class Series(SeriesBase):
"<a href='{}'>{}</a>", self.get_absolute_url(), self.get_full_str()
)
def get_series_dicom_json(self):
instances = []
image: SeriesImage
for image in self.images.all():
instances.append(image.get_image_dicom_json())
series_json = {
"SeriesInstanceUID": "1.3.6.1.4.1.14519.5.2.1.6279.6001.179049373636438705059720603192",
"SeriesNumber": 3000566,
"Modality": "CT",
"SliceThickness": 2.5,
"instances": instances,
}
return series_json
class CaseCollection(ExamCollectionGenericBase):
app_name = "atlas"
@@ -522,10 +609,14 @@ class CaseCollection(ExamCollectionGenericBase):
return False
def get_case_by_index(self, case_index:int, case_count=False) -> Case | Tuple[Case, int]:
def get_case_by_index(
self, case_index: int, case_count=False
) -> Case | Tuple[Case, int]:
# Seems to be a bug when case_number is 0 or 1 the same (first) object gets returned
# forces a list fixes (but is likely inefficient)
cases = list(self.cases.all().order_by("casedetail__sort_order").prefetch_related())
cases = list(
self.cases.all().order_by("casedetail__sort_order").prefetch_related()
)
try:
case = cases[case_index]
@@ -534,10 +625,20 @@ class CaseCollection(ExamCollectionGenericBase):
raise Http404(s)
if case_count:
return case, len(cases)
return case, len(cases)
else:
return case
def get_case_take_url(self, case):
cases = list(
self.cases.all().order_by("casedetail__sort_order").prefetch_related()
)
return reverse(
"atlas:collection_case_view_take_user",
kwargs={"pk": self.pk, "case_number": cases.index(case)},
)
class CaseDetail(models.Model):
case = models.ForeignKey(Case, on_delete=models.CASCADE)
@@ -617,9 +718,7 @@ class SelfReview(models.Model):
review_update_date = models.DateTimeField(auto_now=True)
def get_absolute_url(self):
return reverse(
"atlas:collection_take_overview_user", kwargs={"pk": self.user_exam.exam.pk}
)
return self.user_exam.exam.get_case_take_url(self.case)
def get_display_block(self):
html = """<div class='self-feedback-block'>
@@ -628,15 +727,27 @@ class SelfReview(models.Model):
Findings: <span class="findings">{findings}</span><br/>
Interpretation: <span class="interpretation">{interpretation}</span><br/>
<span class="date">Date: {date}</span><br/>
<span class="date">Date: {date}</span><br/><a href="{edit_url}">Edit</a>
</div>
"""
edit_url = reverse(
"atlas:self_review_update",
kwargs={
"user_exam_id": self.user_exam.pk,
"case_id": self.case.pk,
"pk": self.pk,
},
)
# edit_url = "TEST"
return format_html(
html,
comments=self.comments,
findings=self.findings,
interpretation=self.interpretation,
date=self.review_date,
id=self.pk,
edit_url=edit_url,
)