improve dicom json support
This commit is contained in:
+27
-18
@@ -352,20 +352,7 @@ class Case(models.Model):
|
||||
pass
|
||||
|
||||
|
||||
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):
|
||||
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:
|
||||
def extract_image_dicom_json_from_ds(ds, url):
|
||||
to_keep = [
|
||||
"Columns",
|
||||
"Rows",
|
||||
@@ -401,7 +388,24 @@ class SeriesImage(SeriesImageBase):
|
||||
else:
|
||||
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:
|
||||
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(
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user