@@ -155,17 +155,28 @@ def generate_voxel_data(self, series):
155155 except KeyError :
156156 slope = 1
157157
158- if slope != 1 :
159- vol_data = slope * vol_data .astype (np .float64 )
160- vol_data += intercept
161158
162- # Check if vol_data can be cast to uint16 without data loss
163- if np .can_cast (vol_data , np .uint16 , casting = 'safe' ):
159+ # 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" )
164162 vol_data = np .array (vol_data , dtype = np .uint16 )
165- elif np .can_cast (vol_data , np .float32 , casting = 'safe' ):
166- vol_data = np .array (vol_data , dtype = np .float32 )
167- elif np .can_cast (vol_data , np .float64 , casting = 'safe' ):
163+ intercept = np .uint16 (intercept )
164+ 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" )
168167 vol_data = np .array (vol_data , dtype = np .float32 )
168+ intercept = np .float32 (intercept )
169+ 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" )
172+ vol_data = np .array (vol_data , dtype = np .float64 )
173+ intercept = np .float64 (intercept )
174+ slope = np .float64 (slope )
175+
176+ if slope != 1 :
177+ vol_data = slope * vol_data
178+
179+ vol_data += intercept
169180 return vol_data
170181
171182 def create_volumetric_image (self , vox_data , metadata ):
0 commit comments