From 32b861f213f047c28806aa988ba3588e63914314 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 7 Aug 2023 22:31:52 +0100 Subject: [PATCH] fake SOPInsanceUID --- atlas/models.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/atlas/models.py b/atlas/models.py index 7d7f1ac8..7d971225 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -351,7 +351,7 @@ class Case(models.Model): pass -def extract_image_dicom_json_from_ds(ds, url): +def extract_image_dicom_json_from_ds(ds, url, image_index): to_keep = [ "Columns", "Rows", @@ -388,6 +388,9 @@ def extract_image_dicom_json_from_ds(ds, url): else: d[key] = val + if d == {}: + d["SOPInstanceUID"] = f"1.2.840.1111.{image_index}" + return {"metadata": d, "url": f"dicomweb:{url}"} @@ -397,7 +400,7 @@ class SeriesImage(SeriesImageBase): "Series", related_name="images", on_delete=models.SET_NULL, null=True ) - def get_extra_dicom_data(self): + def get_dicom_data(self): print(self.image) try: with pydicom.dcmread(self.image) as d: @@ -405,14 +408,16 @@ class SeriesImage(SeriesImageBase): except InvalidDicomError: return {} - def get_image_dicom_json(self): - try: - with pydicom.dcmread(self.image) as ds: - return extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{self.image.url}") - except FileNotFoundError: - return [] - except InvalidDicomError: - return {"url": f"dicomweb:{REMOTE_URL}{self.image.url}"} + #def get_image_dicom_json(self, image_index): + # try: + # with pydicom.dcmread(self.image) as ds: + # return extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{self.image.url}") + # except FileNotFoundError: + # return [] + # except InvalidDicomError: + # return {"url": f"dicomweb:{REMOTE_URL}{self.image.url}", "metadata": { + # "SOPInstanceUID" : f"1.2.840.1111.{image_index}" + # }} class SeriesFinding(models.Model): @@ -503,8 +508,8 @@ class Series(SeriesBase): # TODO: clean up (this is rather convoluted....) image: SeriesImage for series_n, image in enumerate(self.images.all()): + ds = image.get_dicom_data() if series_n == 0: - ds = image.get_extra_dicom_data() for tag in to_keep: if tag in ds: @@ -515,11 +520,11 @@ class Series(SeriesBase): else: series_json[tag] = val - instances.append( - extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{image.image.url}") - ) - else: - instances.append(image.get_image_dicom_json()) + instances.append( + extract_image_dicom_json_from_ds(ds, url=f"{REMOTE_URL}{image.image.url}", image_index=series_n) + ) + #else: + # instances.append(image.get_image_dicom_json(image_index)) description = f"{self.examination} ({self.plane})" if self.contrast: