Skip to content

Commit 112723d

Browse files
WillButAgainwill tepe
authored andcommitted
Fix DICOMSeriesToVolumeOperator casting bug (Project-MONAI#529)
* fix casting, add check Signed-off-by: will tepe <[email protected]> * code review changes Signed-off-by: will tepe <[email protected]> * fix fir slope as well --------- Signed-off-by: will tepe <[email protected]> Co-authored-by: will tepe <[email protected]> Signed-off-by: Simone Bendazzoli <[email protected]>
1 parent da9939f commit 112723d

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

monai/deploy/operators/dicom_series_to_volume_operator.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def generate_voxel_data(self, series):
112112
# with the NumPy array returned from the ITK GetArrayViewFromImage on the image
113113
# loaded from the same DICOM series.
114114
vol_data = np.stack([s.get_pixel_array() for s in slices], axis=0)
115-
# The above get_pixel_array() already considers the PixelRepresentation attribute,
116-
# 0 is unsigned int, 1 is signed int
117-
if slices[0][0x0028, 0x0103].value == 0:
115+
if slices[0][0x0028,0x0103].value == 1:
118116
vol_data = vol_data.astype(np.uint16)
119117

120118
# For now we support monochrome image only, for which DICOM Photometric Interpretation
@@ -157,35 +155,24 @@ def generate_voxel_data(self, series):
157155
except KeyError:
158156
slope = 1
159157

158+
160159
# check if vol_data, intercept, and slope can be cast to uint16 without data loss
161-
if (
162-
np.can_cast(vol_data, np.uint16, casting="safe")
163-
and np.can_cast(intercept, np.uint16, casting="safe")
164-
and np.can_cast(slope, np.uint16, casting="safe")
165-
):
166-
logging.info("Casting to uint16")
160+
if np.can_cast(vol_data, np.uint16, casting='safe') and np.can_cast(intercept, np.uint16, casting='safe') and np.can_cast(slope, np.uint16, casting='safe'):
161+
logging.info(f"Casting to uint16")
167162
vol_data = np.array(vol_data, dtype=np.uint16)
168163
intercept = np.uint16(intercept)
169164
slope = np.uint16(slope)
170-
elif (
171-
np.can_cast(vol_data, np.float32, casting="safe")
172-
and np.can_cast(intercept, np.float32, casting="safe")
173-
and np.can_cast(slope, np.float32, casting="safe")
174-
):
175-
logging.info("Casting to float32")
165+
elif np.can_cast(vol_data, np.float32, casting='safe') and np.can_cast(intercept, np.float32, casting='safe') and np.can_cast(slope, np.float32, casting='safe'):
166+
logging.info(f"Casting to float32")
176167
vol_data = np.array(vol_data, dtype=np.float32)
177168
intercept = np.float32(intercept)
178169
slope = np.float32(slope)
179-
elif (
180-
np.can_cast(vol_data, np.float64, casting="safe")
181-
and np.can_cast(intercept, np.float64, casting="safe")
182-
and np.can_cast(slope, np.float64, casting="safe")
183-
):
184-
logging.info("Casting to float64")
170+
elif np.can_cast(vol_data, np.float64, casting='safe') and np.can_cast(intercept, np.float64, casting='safe') and np.can_cast(slope, np.float64, casting='safe'):
171+
logging.info(f"Casting to float64")
185172
vol_data = np.array(vol_data, dtype=np.float64)
186173
intercept = np.float64(intercept)
187174
slope = np.float64(slope)
188-
175+
189176
if slope != 1:
190177
vol_data = slope * vol_data
191178

0 commit comments

Comments
 (0)