Skip to content

FIX: Resolve numeric overflow in drift estimation node#1324

Merged
oesteban merged 1 commit into
masterfrom
fix/1315
Aug 19, 2024
Merged

FIX: Resolve numeric overflow in drift estimation node#1324
oesteban merged 1 commit into
masterfrom
fix/1315

Conversation

@oesteban

Copy link
Copy Markdown
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
Copy Markdown
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
@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'])

@effigies effigies Aug 19, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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 <effigies@gmail.com>
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