Repository navigation
Conversation
There was a problem hiding this comment.
Thank you @jdotc1 this is an unfortunate problem, thanks for spotting it. I made a couple of comments below. Also, generally speaking I notice that the per_channel branch already has a slightly different solution for a similar problem that someone else already contributed. But the two solutions are different (the per_channel solution casts imin and imax to float, you cast to an unsigned integer data type). I think probably the float solution is superior since it doesn't involve creating a copy of the array (which could be large). But I could be convinced otherwise. Either way, I would certainly want to use the same solution for the per_channel and global cases
| from tests.utils import write_and_read_dataset | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('dtype', [ |
There was a problem hiding this comment.
These tests are quite out of place here at the top of the file. Please group them with the related tests around line 1310
There was a problem hiding this comment.
Done—I moved all four integer min–max regression functions from the top of the file beside the existing normalization tests, and kept the new per-channel int64/uint64 test there as well. The affected volume test module passes (155 tests), and the earlier full suite passed (2,087 passed, 153 skipped). Thanks for pointing this out.
| else: | ||
| imin = self.array.min() | ||
| imax = self.array.max() | ||
| if np.issubdtype(array.dtype, np.integer): |
There was a problem hiding this comment.
Is this the right test to use? It seems to me (and I quickly checked this), that there is only a problem when the array has a signed integer array type. Unsigned integers are fine without any special treatment, but this path would result in an unnecessary copy due to array.astype(...), which could be quite a large expense for large arrays. Note that this may not be relevant if we change the approach (as in my overall comment)
There was a problem hiding this comment.
Agreed the full-array astype was too expensive. The new shared global/per-channel integer path uses NumPy unsigned subtraction with out pointing directly to the float64 output, avoiding that extra full-volume copy. Casting extrema to float64 before subtracting loses adjacent int64/uint64 differences—the new per-channel regression reproduced both failures before this change and passes afterward. On one 2-million-element int64 example, peak traced allocation decreased from about 48.1 MB to 16.1 MB; full suite: 2,087 passed, 153 skipped. Arbitrary integer magnitudes are still not exactly representable as float64.
Summary
Normalize integer-valued volumes without signed overflow or loss of adjacent large 64-bit differences. The global and per-channel paths now share the same integer calculation: NumPy's matching-width unsigned subtraction before floating-point scaling, writing directly into the float64 output rather than allocating an additional full-volume unsigned copy. Floating-point normalization retains its existing behavior.
Tests
tests/test_volume.py.tracemalloccomparison on a 2-million-voxel int64 array, peak traced allocation fell from approximately 48.1 MB to 16.1 MB. This is a local illustrative measurement, not a cross-platform performance benchmark.Precision boundary: The subtraction preserves small integer offsets before conversion, but float64 normalization does not make arbitrary 64-bit integer magnitudes exactly representable.