Precision Saving uint16 array #1345
Replies: 1 comment
-
Generally, if you pass in a float64 data array and save it as Anyway, img = nb.load(filepath)
data = np.asanyarray(img.dataobj) # Returns the raw data array if no scale factors, float64 if scale factors
data = np.asanyarray(img.dataobj, dtype='uint16') # Returns uint16 array, regardless of on-disk type or scale factors
data = np.uint16(img.dataobj) # Same as above In all cases, the data is scaled if scale factors are set, but with the latter two methods, you have control over the output type. For creating new arrays where the input type might be different, I would be explicit: new_img = nb.Nifti1Image(data_array.astype(target_type), img.affine, img.header) If you really want to pass your data in as floats and not scale, you can also manually set the slope and intercept: new_img = nb.Nifti1Image(data_array, img.affine, img.header)
new_img.header.set_slope_inter(slope=1, inter=0) |
Beta Was this translation helpful? Give feedback.
-
Reading in a file with
uint16
as data type, saving it again and then reading it again results inconsistent data values.In the example below an integer value of
3
is converted to2.999999999301508
. Converting the data array fromget_fdata()
fromfloat64
touint16
solves the problem. Is this working as intended? In my opinion the conversion should not be necessary. This can be an issue when segmentation masks are edited. File attached below.Output
myseg.nii.gz
Beta Was this translation helpful? Give feedback.
All reactions