From 498be5282880d00feb79c6568bc249e166b0a75a Mon Sep 17 00:00:00 2001 From: M Q Date: Mon, 21 Apr 2025 18:04:05 -0700 Subject: [PATCH 1/2] The tag value for usinged int is 0 Signed-off-by: M Q --- monai/deploy/operators/dicom_series_to_volume_operator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/monai/deploy/operators/dicom_series_to_volume_operator.py b/monai/deploy/operators/dicom_series_to_volume_operator.py index d589354f..a585c726 100644 --- a/monai/deploy/operators/dicom_series_to_volume_operator.py +++ b/monai/deploy/operators/dicom_series_to_volume_operator.py @@ -112,7 +112,9 @@ def generate_voxel_data(self, series): # with the NumPy array returned from the ITK GetArrayViewFromImage on the image # loaded from the same DICOM series. vol_data = np.stack([s.get_pixel_array() for s in slices], axis=0) - if slices[0][0x0028,0x0103].value == 1: + # The above get_pixel_array() already considers the PixelRepresentation attribute, + # 0 is unsigned int, 1 is signed int + if slices[0][0x0028,0x0103].value == 0: vol_data = vol_data.astype(np.uint16) # For now we support monochrome image only, for which DICOM Photometric Interpretation From 6d940f08219507a555b9e8519b1cb8d7a339267a Mon Sep 17 00:00:00 2001 From: M Q Date: Mon, 21 Apr 2025 18:39:49 -0700 Subject: [PATCH 2/2] Fix formmating complaints Signed-off-by: M Q --- .../dicom_series_to_volume_operator.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/monai/deploy/operators/dicom_series_to_volume_operator.py b/monai/deploy/operators/dicom_series_to_volume_operator.py index a585c726..57c60c17 100644 --- a/monai/deploy/operators/dicom_series_to_volume_operator.py +++ b/monai/deploy/operators/dicom_series_to_volume_operator.py @@ -114,7 +114,7 @@ def generate_voxel_data(self, series): vol_data = np.stack([s.get_pixel_array() for s in slices], axis=0) # The above get_pixel_array() already considers the PixelRepresentation attribute, # 0 is unsigned int, 1 is signed int - if slices[0][0x0028,0x0103].value == 0: + if slices[0][0x0028, 0x0103].value == 0: vol_data = vol_data.astype(np.uint16) # For now we support monochrome image only, for which DICOM Photometric Interpretation @@ -157,24 +157,35 @@ def generate_voxel_data(self, series): except KeyError: slope = 1 - # check if vol_data, intercept, and slope can be cast to uint16 without data loss - 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'): - logging.info(f"Casting to uint16") + 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") + ): + logging.info("Casting to uint16") vol_data = np.array(vol_data, dtype=np.uint16) intercept = np.uint16(intercept) slope = np.uint16(slope) - 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'): - logging.info(f"Casting to float32") + 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") + ): + logging.info("Casting to float32") vol_data = np.array(vol_data, dtype=np.float32) intercept = np.float32(intercept) slope = np.float32(slope) - 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'): - logging.info(f"Casting to float64") + 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") + ): + logging.info("Casting to float64") vol_data = np.array(vol_data, dtype=np.float64) intercept = np.float64(intercept) slope = np.float64(slope) - + if slope != 1: vol_data = slope * vol_data