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
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):
image = models.FileField(upload_to=image_directory_path)
series = models.ForeignKey(
"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)
with pydicom.dcmread(self.image) as d:
return d
@@ -366,42 +405,7 @@ class SeriesImage(SeriesImageBase):
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": f"dicomweb:{REMOTE_URL}{self.image.url}"}
return extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{self.image.url}")
except FileNotFoundError:
return []
@@ -489,12 +493,13 @@ class Series(SeriesBase):
series_json = {}
to_keep = ["SeriesInstanceUID", "SeriesNumber", "Modality", "SliceThickness"]
to_keep = ["SeriesInstanceUID", "SeriesNumber", "Modality", "SliceThickness", "SeriesDescription"]
# TODO: clean up (this is rather convoluted....)
image: SeriesImage
for series_n, image in enumerate(self.images.all()):
if series_n == 0:
ds = image.get_series_dicom_data()
ds = image.get_extra_dicom_data()
for tag in to_keep:
if tag in ds:
@@ -505,9 +510,13 @@ class Series(SeriesBase):
else:
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