Preserve data dtype in concat_images to fix save/load round-trip (gh-986) - #1526
Open
CedricConday wants to merge 2 commits into
Open
Preserve data dtype in concat_images to fix save/load round-trip (gh-986)#1526CedricConday wants to merge 2 commits into
CedricConday wants to merge 2 commits into
Conversation
…ygh-986) When axis is None, concat_images pre-allocated the output with np.empty, which defaults to float64 and upcast integer input data. The output image kept the first image's header (e.g. uint16), so on save the float64 data was quantized back to the integer storage dtype, making the reloaded data differ from the in-memory data. Collect the images in a list and combine with np.stack, which preserves the input dtype (matching the axis-specified np.concatenate path), so the header dtype and data agree and the image round-trips losslessly.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1526 +/- ##
=======================================
Coverage 95.48% 95.48%
=======================================
Files 209 209
Lines 30050 30060 +10
Branches 4494 4494
=======================================
+ Hits 28692 28702 +10
Misses 926 926
Partials 432 432 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The Windows CI jobs failed test_concat_integer_roundtrip with PermissionError [WinError 32]: the reloaded concat.nii stayed memory-mapped when InTemporaryDirectory tore down, and Windows cannot delete a file that is still open. Load with mmap=False so the handle is released before cleanup. Linux/macOS allow deleting open files, which is why only the Windows jobs were red.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes gh-986.
concat_images(..., axis=None)pre-allocated the output array withnp.empty(out_shape), which defaults tofloat64. Integer input data (e.g.uint16) was therefore upcast tofloat64in memory, but the returned image reuses the first image's header, whose data dtype is stilluint16. On save, thefloat64values are cast back to theuint16storage dtype (with scaling), so the reloaded data no longer equals the in-memory data.Minimal reproducer:
The
axis-specified branch already avoids this because it usesnp.concatenate, which preserves the input dtype. This change makes theaxis=Nonebranch consistent: collect the loaded arrays in a list and combine them withnp.stack(..., axis=-1)instead of filling afloat64np.emptyand callingnp.rollaxis.np.stackproduces the same shape and ordering as the previousempty+rollaxisapproach but keeps the natural (result) dtype, so the header dtype and the data agree and the image round-trips losslessly.Verified that
uint16,int16,int32,float32andfloat64inputs all round-trip exactly (bothaxis=Noneand an explicit axis), that the stacked data matchesnp.stack([a, b], axis=-1), and that the existingtest_concat(which exercises many shapes/axes and mem/file/mixed inputs) still passes. Addedtest_concat_integer_roundtrip, which fails onmainand passes here.Disclosure: this change was written with AI assistance. I reviewed it, confirmed the root cause, and verified the behaviour and tests described above before submitting.