From 522fa6bcb0b362401923daf1d32626fea9e8f59e Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 7 Aug 2023 21:46:34 +0100 Subject: [PATCH] improve dicom json support --- atlas/models.py | 91 +++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/atlas/models.py b/atlas/models.py index 52ba88dd..74fa1d47 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -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