Skip to content

Commit

Permalink
Better error handling for single frame images
Browse files Browse the repository at this point in the history
  • Loading branch information
CPBridge committed Jan 22, 2025
1 parent a1758d2 commit cff5769
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
49 changes: 29 additions & 20 deletions src/highdicom/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@

# TODO new type hints
# TODO rebase parametric map
# TODO behavior of simple frame images
# TODO referenced images for non-seg images
# TODO allow tolerance parameter to be passed for volumes
# TODO exports/inits and docs
# TODO add labelmap to seg documentation
# TODO quickstart for image/volume
Expand Down Expand Up @@ -1653,26 +1650,27 @@ def _build_luts_single_frame(self) -> None:
col_defs.append('FrameNumber INTEGER PRIMARY KEY')
col_data = [[1]]

for t in [
0x0020_0032, # ImagePositionPatient
0x0020_0037, # ImageOrientionPatient
0x0028_0030, # PixelSpacing
0x0018_0088, # SpacingBetweenSlices
]:
vr, vm_str, _, _, kw = get_entry(t)
if self._coordinate_system == CoordinateSystemNames.PATIENT:
for t in [
0x0020_0032, # ImagePositionPatient
0x0020_0037, # ImageOrientionPatient
0x0028_0030, # PixelSpacing
0x0018_0088, # SpacingBetweenSlices
]:
vr, vm_str, _, _, kw = get_entry(t)

vm = int(vm_str)
sql_type = _DCM_SQL_TYPE_MAP[vr]
vm = int(vm_str)
sql_type = _DCM_SQL_TYPE_MAP[vr]

if kw in self:
v = self.get(kw)
if vm > 1:
for i, v in enumerate(v):
col_defs.append(f'{kw}_{i} {sql_type} NOT NULL')
if kw in self:
v = self.get(kw)
if vm > 1:
for i, v in enumerate(v):
col_defs.append(f'{kw}_{i} {sql_type} NOT NULL')
col_data.append([v])
else:
col_defs.append(f'{kw} {sql_type} NOT NULL')
col_data.append([v])
else:
col_defs.append(f'{kw} {sql_type} NOT NULL')
col_data.append([v])

if 'SourceImageSequence' in self:
self._single_source_frame_per_frame = (
Expand Down Expand Up @@ -2947,6 +2945,12 @@ def _get_volume_geometry(
Geometry of the volume.
"""
if self._coordinate_system is None:
raise RuntimeError(
"Image does not exist within a frame-of-reference "
"coordinate system."
)

if is_multiframe_image(self):
if (
self.is_tiled and
Expand Down Expand Up @@ -4623,6 +4627,11 @@ def get_volume(
that have to be decoded and transformed.
""" # noqa: E501
if self._coordinate_system is None:
raise RuntimeError(
"Image does not exist within a frame-of-reference "
"coordinate system."
)
if self.is_tiled:
total_rows = self.TotalPixelMatrixRows
total_columns = self.TotalPixelMatrixColumns
Expand Down
4 changes: 2 additions & 2 deletions src/highdicom/sr/value_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def _assert_value_type(
Parameters
----------
dataset: pydicom.dataset.Dataset
dataset: pydicom.Dataset
Dataset representing an SR Content Item
value_type: highdicom.sr.enum.ValueTypeValues
value_type: highdicom.sr.ValueTypeValues
Expected value of Value Type attribute
Raises
Expand Down
7 changes: 7 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,13 @@ def test_imread_all_test_files(f):
vol_lazy = im_lazy.get_volume()

assert np.array_equal(vol.array, vol_lazy.array)
elif im.coordinate_system is None:
msg = (
'Image does not exist within a frame-of-reference coordinate '
'system.'
)
with pytest.raises(RuntimeError, match=msg):
im.get_volume()

assert isinstance(im.pixel_array, np.ndarray)
assert np.array_equal(im.pixel_array, im_lazy.pixel_array)
Expand Down

0 comments on commit cff5769

Please sign in to comment.