FIX: Remove unsafe cast during TransformBase.apply() #189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
np.asanyarray(img.dataobj, img.get_data_dtype())
is almost never a great choice.np.asanyarray(img.dataobj)
is intended to be conservative with its types, where either the original dtype is maintained or scaling is applied and the dtype becomesfloat64
. The case where this fails in particular is when the on-disk dtype isint16
with scale factors. If the scale factors send the data beyond the int16 range (slope > 1), then casting to int16 is going to truncate the data outside the range.The approach here is to be pragmatic. We always use
np.asanyarray(img.dataobj)
to give the best estimate of the values in the data array that NIfTI can provide. If a dtype is provided, then we set both the output array and the image type to that, to avoid unnecessary copies and casting. If it is not provided, then the output array keeps the dtype of the input data array (the "effective dtype") and the output image keeps the on-disk dtype of the input image.get_data_dtype()
scl_slope
/scl_inter
output_dtype
out_img.get_data_dtype()
int16
None
int16
int16
int16
None
float64
int16
int16
float32
float32
float32
int16
float32
float32
float32
float32
None
float32
float32
float32
None
float64
float32
float32
float32
float32
float32
float32
float32
float32
float32