Skip to content

Commit

Permalink
Merge pull request #58 from benjamin-russoniello-pa/57-enh-handle-dic…
Browse files Browse the repository at this point in the history
…om-files-that-dont-have-pixel-data

Add handling for dicom files without pixel data
  • Loading branch information
erinaldidb authored Oct 4, 2024
2 parents 489952b + 3f8143c commit 759b13c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions dbx/pixels/dicom/dicom_udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

from pyspark.sql.functions import udf

from pydicom import Dataset

def cloud_open(path: str, anon: bool = False):
try:
Expand All @@ -22,6 +22,18 @@ def cloud_open(path: str, anon: bool = False):
except Exception as e:
raise Exception(f"path: {path} is_anon: {anon} exception: {e} exception.args: {e.args}")

def check_pixel_data(ds: Dataset) -> Dataset | None:
"""Check if pixel data exists before attempting to access it.
pydicom.Dataset.pixel_array will throw an exception if the
image contains no pixel data.
params:
df -- An object of type pydicom.Dataset
"""
try:
a = ds.pixel_array
except:
return None
return a

@udf
def dicom_meta_udf(path: str, deep: bool = True, anon: bool = False) -> dict:
Expand All @@ -38,22 +50,27 @@ def dicom_meta_udf(path: str, deep: bool = True, anon: bool = False) -> dict:

fp, fsize = cloud_open(path, anon)
with dcmread(fp, defer_size=1000, stop_before_pixels=(not deep)) as ds:
a = None
js = ds.to_json_dict()
# remove binary images
if "60003000" in js:
del js["60003000"]
if "7FE00010" in js:
del js["7FE00010"]

if deep:
a = check_pixel_data(ds)
if deep and a is not None:
a = ds.pixel_array
a.flags.writeable = False
js["has_pixel"] = True
js["hash"] = hashlib.sha1(a).hexdigest()
js["img_min"] = np.min(a).item()
js["img_max"] = np.max(a).item()
js["img_avg"] = np.average(a).item()
js["img_shape_x"] = a.shape[0]
js["img_shape_y"] = a.shape[1]
elif deep:
js["has_pixel"] = False

js["file_size"] = fsize

Expand All @@ -62,4 +79,4 @@ def dicom_meta_udf(path: str, deep: bool = True, anon: bool = False) -> dict:
except_str = str(
{"udf": "dicom_meta_udf", "error": str(err), "args": str(err.args), "path": path}
)
return except_str
return except_str

0 comments on commit 759b13c

Please sign in to comment.