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

Conversation

oesteban
Copy link
Member

Addresses the issue reported by @psadil with his proposed solution 1 (considering the slope and intercept).

cc/ @effigies for a correctness check.

Resolves: #1315.

@oesteban
Copy link
Member Author

Let's roll it out and come back to this if @effigies disagrees.

@oesteban oesteban merged commit 164be35 into master Aug 19, 2024
15 checks passed
@oesteban oesteban deleted the fix/1315 branch August 19, 2024 17:33
Comment on lines +665 to 678

# 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'])
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.

oesteban added a commit that referenced this pull request Aug 19, 2024
Co-authored-by: Chris Markiewicz <[email protected]>
oesteban added a commit that referenced this pull request Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Numeric overflow when computing drift on int16 DWI (that has slope)
2 participants