improve dicom json support
This commit is contained in:
+26
-17
@@ -352,20 +352,7 @@ class Case(models.Model):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SeriesImage(SeriesImageBase):
|
def extract_image_dicom_json_from_ds(ds, url):
|
||||||
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):
|
|
||||||
print(self.image)
|
|
||||||
with pydicom.dcmread(self.image) as d:
|
|
||||||
return d
|
|
||||||
|
|
||||||
def get_image_dicom_json(self):
|
|
||||||
try:
|
|
||||||
with pydicom.dcmread(self.image) as ds:
|
|
||||||
to_keep = [
|
to_keep = [
|
||||||
"Columns",
|
"Columns",
|
||||||
"Rows",
|
"Rows",
|
||||||
@@ -401,7 +388,24 @@ class SeriesImage(SeriesImageBase):
|
|||||||
else:
|
else:
|
||||||
d[key] = val
|
d[key] = val
|
||||||
|
|
||||||
return {"metadata": d, "url": f"dicomweb:{REMOTE_URL}{self.image.url}"}
|
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_extra_dicom_data(self):
|
||||||
|
print(self.image)
|
||||||
|
with pydicom.dcmread(self.image) as d:
|
||||||
|
return d
|
||||||
|
|
||||||
|
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:
|
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,6 +510,10 @@ class Series(SeriesBase):
|
|||||||
else:
|
else:
|
||||||
series_json[tag] = val
|
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(image.get_image_dicom_json())
|
||||||
|
|
||||||
series_json["instances"] = instances
|
series_json["instances"] = instances
|
||||||
|
|||||||
Reference in New Issue
Block a user