Skip to content

Avoid integer overflow in global volume normalization - #486

Merged
CPBridge merged 3 commits into
ImagingDataCommons:masterfrom
jdotc1:bugfix/jdotc1-integer-normalization-20261022
Oct 27, 2026
Merged

CPBridge merged 3 commits into
ImagingDataCommons:masterfrom
jdotc1:bugfix/jdotc1-integer-normalization-20261022

Conversation

@jdotc1

@jdotc1 jdotc1 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Full Highdicom suite: 2,087 passed, 153 skipped (optional codec and integration dependencies unavailable).
  • Integer normalization regressions cover all signed/unsigned widths, full ranges, adjacent int64/uint64 values (global and per channel), constant inputs, non-mutation, strided arrays, and byte order. Tests are grouped with related normalization tests in tests/test_volume.py.
  • In one exploratory tracemalloc comparison 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.

@CPBridge CPBridge added the bug Something isn't working label Oct 22, 2026

@CPBridge CPBridge left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Comment thread tests/test_volume.py Outdated
from tests.utils import write_and_read_dataset


@pytest.mark.parametrize('dtype', [

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These tests are quite out of place here at the top of the file. Please group them with the related tests around line 1310

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread src/highdicom/volume.py Outdated
else:
imin = self.array.min()
imax = self.array.max()
if np.issubdtype(array.dtype, np.integer):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@CPBridge CPBridge left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@CPBridge
CPBridge merged commit 0e4f31f into ImagingDataCommons:master Oct 27, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants