improve dicom json support

This commit is contained in:
Ross
2023-08-07 21:46:34 +01:00
parent 8ec7662015
commit 522fa6bcb0
+50 -41
View File
@@ -352,13 +352,52 @@ class Case(models.Model):
pass pass
def extract_image_dicom_json_from_ds(ds, url):
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": f"dicomweb:{url}"}
class SeriesImage(SeriesImageBase): class SeriesImage(SeriesImageBase):
image = models.FileField(upload_to=image_directory_path) image = models.FileField(upload_to=image_directory_path)
series = models.ForeignKey( series = models.ForeignKey(
"Series", related_name="images", on_delete=models.SET_NULL, null=True "Series", related_name="images", on_delete=models.SET_NULL, null=True
) )
def get_series_dicom_data(self): def get_extra_dicom_data(self):
print(self.image) print(self.image)
with pydicom.dcmread(self.image) as d: with pydicom.dcmread(self.image) as d:
return d return d
@@ -366,42 +405,7 @@ class SeriesImage(SeriesImageBase):
def get_image_dicom_json(self): def get_image_dicom_json(self):
try: try:
with pydicom.dcmread(self.image) as ds: with pydicom.dcmread(self.image) as ds:
to_keep = [ return extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{self.image.url}")
"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": f"dicomweb:{REMOTE_URL}{self.image.url}"}
except FileNotFoundError: except FileNotFoundError:
return [] return []
@@ -489,12 +493,13 @@ class Series(SeriesBase):
series_json = {} series_json = {}
to_keep = ["SeriesInstanceUID", "SeriesNumber", "Modality", "SliceThickness"] to_keep = ["SeriesInstanceUID", "SeriesNumber", "Modality", "SliceThickness", "SeriesDescription"]
# TODO: clean up (this is rather convoluted....)
image: SeriesImage image: SeriesImage
for series_n, image in enumerate(self.images.all()): for series_n, image in enumerate(self.images.all()):
if series_n == 0: if series_n == 0:
ds = image.get_series_dicom_data() ds = image.get_extra_dicom_data()
for tag in to_keep: for tag in to_keep:
if tag in ds: if tag in ds:
@@ -505,9 +510,13 @@ class Series(SeriesBase):
else: else:
series_json[tag] = val series_json[tag] = val
instances.append(image.get_image_dicom_json()) instances.append(
extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{image.image.url}")
)
else:
instances.append(image.get_image_dicom_json())
series_json["instances"]= instances series_json["instances"] = instances
return series_json return series_json