Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Resolve numeric overflow in drift estimation node #1324

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mriqc/interfaces/diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,17 @@ def _run_interface(self, runtime):
self.inputs.full_epi, suffix='_nodriftfull', newpath=runtime.cwd
)
full_img = nb.load(self.inputs.full_epi)

# Read slope and intercept (see #1315)
slope, intercept = full_img.header.get_slope_inter()
slope = slope if slope is not None else 1.0
intercept = intercept if intercept is not None else 0.0
corrected = (
full_img.get_fdata() * fitted[np.newaxis, np.newaxis, np.newaxis, :] / slope
- intercept
)
full_img.__class__(
(full_img.get_fdata() * fitted[np.newaxis, np.newaxis, np.newaxis, :]).astype(
full_img.header.get_data_dtype()
),
corrected.astype(full_img.header.get_data_dtype()),
full_img.affine,
full_img.header,
).to_filename(self._results['out_full_file'])
Comment on lines +665 to 678
Copy link
Member

@effigies effigies Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. No, you just want:

full_img.__class__(
    full_img.get_fdata() * fitted[np.newaxis, np.newaxis, np.newaxis, :],
    full_img.affine
    full_img.header
).to_filename(self._results['out_full_file'])

You pretty much never want to dynamically coerce the type. All of nibabel's machinery assumes that the data is valid and that the header encodes the intent, and will perform scaling on your behalf if continuous data needs to be discretized into an integer dtype.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that applies the slope and intercept when generating the spatialimage object, correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will calculate a new slope and intercept based on the min/max values of the data array.

Expand Down
Loading