@@ -112,7 +112,9 @@ 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- if slices [0 ][0x0028 ,0x0103 ].value == 1 :
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 :
116118 vol_data = vol_data .astype (np .uint16 )
117119
118120 # For now we support monochrome image only, for which DICOM Photometric Interpretation
@@ -155,24 +157,35 @@ def generate_voxel_data(self, series):
155157 except KeyError :
156158 slope = 1
157159
158-
159160 # check if vol_data, intercept, and slope can be cast to uint16 without data loss
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" )
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" )
162167 vol_data = np .array (vol_data , dtype = np .uint16 )
163168 intercept = np .uint16 (intercept )
164169 slope = np .uint16 (slope )
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" )
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" )
167176 vol_data = np .array (vol_data , dtype = np .float32 )
168177 intercept = np .float32 (intercept )
169178 slope = np .float32 (slope )
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" )
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" )
172185 vol_data = np .array (vol_data , dtype = np .float64 )
173186 intercept = np .float64 (intercept )
174187 slope = np .float64 (slope )
175-
188+
176189 if slope != 1 :
177190 vol_data = slope * vol_data
178191
0 commit comments