Skip to content

fix(intensity)!: divide the covered range by the whole image and sum squares in double - #559

Open
darkclad wants to merge 1 commit into
PolusAI:mainfrom
darkclad:main-intensity-range
Open

darkclad wants to merge 1 commit into
PolusAI:mainfrom
darkclad:main-intensity-range

Conversation

@darkclad

@darkclad darkclad commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

COVERED_IMAGE_INTENSITY_RANGE (and 3COVERED_IMAGE_INTENSITY_RANGE) is the ROI's intensity range over the image's, so it lies in [0, 1]. It did not:

  • Divisor from the mask (PN-78). The 2D and 3D scans skipped off-mask pixels before updating the slide minimum and maximum, so on a segmented image the "image" range was the ROIs' own range, and an image with one ROI divided that ROI's range by itself. On the 3D phantom ut_inten.nii + ut_mask57.nii the value was 1 where ROI 57 spans [0, 2000] of the volume's [-1024, 2000].
  • Two domains (PN-26). The ROI's range was taken in the grey levels the loader stores, which truncate, round or quantize fractions, and the image's range in the image's own units. A whole-slide run of a float image of [-0.75, 99.75] under --preserve-hu gave 1.004975, 2D and 3D: rounding stores the ends 101 levels apart for a span of 100.5.
  • Fallback (PN-61). With no slide properties, three of the four sites wrote 1 and the 2D in-memory site left 0. For arrays passed to Nyxus.featurize no image range was recorded at all, and the value was 0.

Separately (PN-226), ENERGY, ROOT_MEAN_SQUARED, 3ENERGY and 3ROOT_MEAN_SQUARED squared each grey level as a 32-bit unsigned product, which wraps above 65535: on {100000, 70000, 65536, 3, 3000000} ENERGY was 4058613001 where the sum of squares is 9019194967305.

Change

  • The image's range is every pixel's. The scans record the extrema of every finite pixel, masked or not: max_allpix_inten joins the existing min_allpix_inten, both carried by inherit_intensity_domain and written by record_scanned_intensity_range. The masked range (min/max_preroi_inten) is unchanged and still sets the quantized map of a real-valued image, so no other feature moves.
  • One domain. SlideProps::covered_intensity_range(lo, hi) divides the ROI's stored grey-level range by the image's ends put through the same forward map, without its range clamps (grey_coordinate()), since the quantized map of a segmented image spans the masked pixels only. Both sides are counts of grey levels on one map, so the ratio has no unit and report_in_source_domain no longer multiplies it by the map's scale. A stored level is that map clamped and the map is monotone, so a ROI of finite samples never spans more levels than the image. A non-finite sample is stored at level 0 but left out of the extrema, so the ratio is capped at 1; grey_coordinate() saturates at the grey type's maximum, as every narrowing does.
  • Fallback. Dataset::covered_intensity_range(slide_idx, lo, hi) answers 1 when the ROI names no scanned image, and SlideProps::covered_intensity_range answers 1 when the image's range is zero. All four sites call it. 1 rather than 0: with no image to compare with, the ROI is taken as its own image, which it covers whole; a constant image is covered whole by any ROI of it; and 0 would say the ROI covers none of the image, which is false whenever the ROI has a range. 1 is also what three of the four sites already wrote.
  • In-memory arrays. processMontage measures each array's masked and all-pixel ranges, as the scan measures a file's, so Nyxus.featurize reports the real value. test_featurize_montage now compares this column between the montage and directory runs too.
  • Energy. The four sums square in double.

Every copy, all changed in this PR: PixelIntensityFeatures::calculate_grey_levels and osized_calculate_grey_levels (2D in memory and out of core), D3_VoxelIntensityFeatures::calculate_grey_levels and osized_calculate_grey_levels (3D), gatherRoisMetrics_2_slideprops_2D, _3D and _2D_montage, and processMontage. Other first-order sums of squares or powers were checked: Moments4 and the dispersion, skewness and hyper-moment sums already work in double.

Values that change

A value diff of *ALL* (2D) and *3D_ALL* (3D) between main and this branch, at the default settings:

Corpus ROIs Columns changed
tests/python/data p0_y1_r1_c0.ome.tif, segmented 66 0 of 752
the same, whole slide 1 0 of 752
a synthetic scene of 300 elliptical ROIs 300 0 of 752
hounsfield/ct_float.tif + mask.tif 1 0 of 752
ut_inten.nii + ut_mask57.nii 1 1 of 218: 3COVERED_IMAGE_INTENSITY_RANGE 1 → 0.66138 (2000/3024)
volmin_inten.nii + volmin_mask.nii 1 1 of 218: 3COVERED_IMAGE_INTENSITY_RANGE 1 → 0.84567
hounsfield/ct3d_float_slope.nii + mask3d.nii 1 0 of 218

The 2D corpus images' ROIs reach the images' own extremes, so their values hold. On the test fixtures:

Fixture Code main This branch
float slide [-0.5, 99.5], ROI [24.5, 74.5], segmented, with and without --preserve-hu (2D, 3D) COVERED_IMAGE_INTENSITY_RANGE, 3COVERED_… 1 0.5
float slide [-0.75, 99.75], whole slide, --preserve-hu (2D, 3D) the same 1.004975 1
uint32 array, off-mask 10 and 210, ROI 60 .. 160, Nyxus.featurize COVERED_IMAGE_INTENSITY_RANGE 0 0.5
2D worked example (mask covers the image), Nyxus.featurize COVERED_IMAGE_INTENSITY_RANGE 0 1
3D worked example, ROI [2, 6] of the volume's [0, 6] 3COVERED_IMAGE_INTENSITY_RANGE 1 0.6667
{100000, 70000, 65536, 3, 3000000} (2D, 3D) ENERGY, 3ENERGY 4058613001 9019194967305
2D worked example × 20000 ENERGY / ROOT_MEAN_SQUARED 5.4145e10 / 46538 9.28e10 / 60926
3D worked example × 20000 3ENERGY / 3ROOT_MEAN_SQUARED 4.894e10 / 47166 1.692e11 / 87698

Tests

These fail on main:

  • tests/python/test_2d_firstorder_analytic.py, test_3d_firstorder_analytic.py: the covered range at most 1 and equal to its closed form on images with off-mask pixels darker and brighter than the ROI, segmented (TIFF in RAM and out of core; NIfTI in RAM with and without the Hounsfield offset; OME-TIFF in RAM and out of core) and whole slide or volume, plus Nyxus.featurize; ENERGY / 3ENERGY exact on uint32 data above 65535, in memory, in RAM and out of core.
  • TEST_2D_FIRSTORDER_ENERGY_WIDE_ANALYTIC, TEST_3D_FIRSTORDER_ENERGY_WIDE_ANALYTIC: exact sums of squares in RAM and out of core.
  • TEST_2D_FIRSTORDER_COVERED_RANGE_ANALYTIC, TEST_3D_FIRSTORDER_COVERED_RANGE_ANALYTIC: an offset-mapped slide off the map's grid, and a segmented quantized slide whose ends lie past the map's clamps, in RAM and out of core.
  • TEST_2D_FIRSTORDER_COVERED_RANGE_NO_SLIDE_MECHANICS (0 in RAM on main), TEST_2D/3D_FIRSTORDER_COVERED_RANGE_FLAT_SLIDE_MECHANICS (NaN on main).
  • TEST_2D_FIRSTORDER_COVERED_RANGE_NONFINITE_SAMPLE_MECHANICS (2 without the cap) and _SATURATED_SLIDE_MECHANICS (0.2147 without the saturation).
  • TEST_3D_FIRSTORDER_COVERED_IMAGE_INTENSITY_RANGE_REGRESSION: the ut phantom pin, now 2000/3024.

These pass on main and guard what must not change: the 16-bit ENERGY cases (TEST_2D/3D_FIRSTORDER_ENERGY_16BIT_ANALYTIC, top=65535 in the Python files), the quantized whole-slide cases, and TEST_3D_FIRSTORDER_COVERED_RANGE_NO_SLIDE_MECHANICS (3D already wrote 1).

Vetting registry

  • New vetted/analytic rows: COVERED_IMAGE_INTENSITY_RANGE, ENERGY, ROOT_MEAN_SQUARED (2D) and their 3D codes, recipes firstorder2d/3d.covered_range_analytic and .wide_levels_analytic, fixtures bench_covered_range_slides and bench_wide_grey_levels.
  • The 3COVERED_IMAGE_INTENSITY_RANGE regression row pins 2000/3024; not_covered.md §F and both first-order matrices follow.
  • Both first-order scanners read the new test files. report_output.*, coverage_report.md and featurelist.rst are regenerated.

Docs

  • f_pixel_intensity.rst, f_intensity_3d.rst: the covered range is defined over every pixel or voxel of the image (every channel and timepoint in 3D) and does not change under the intensity map; the in-memory and 32-bit known-defect entries are replaced by the current behaviour; the Validation sections name the closed-form checks.
  • conventions.rst: the constant-ROI entry. feature_descriptions.toml, feature_extras.toml, and the two worked-example scripts, which pin the new values.
  • benchmarks/perf: the numpy exemption for this column is removed from the digest comparison, with its mechanics test.

Benchmark instruction counts

No counted kernel runs PixelIntensityFeatures (the imoments kernel is the intensity moments), so no count should move. If the job reports a change, the updated instructions.json follows in its own test(benchmarks): commit.

Phase-0 files touched

None.

Found and filed separately

  • PN-312: UNIFORMITY_PIU adds the two extrema in 32 bits, which wraps once their sum reaches 2^32.
  • PN-313: a segmented real-valued image is quantized over its masked pixels' range, while the docs say over the image's own range.

Closes PN-26, PN-61, PN-78, PN-226. Closes #490.

…squares in double

COVERED_IMAGE_INTENSITY_RANGE is the ROI's intensity range over the
image's, so it lies in [0, 1]. Three things broke that:

- The slide range it divided by came from the masked pixels only, so on a
  segmented image it was the ROIs' own range, and a single ROI divided its
  range by itself. The 2D and 3D scans now also record the extrema of
  every pixel (max_allpix_inten joins min_allpix_inten), and the feature
  divides by those. The masked range still sets the quantized map of a
  real-valued image, so no other feature moves.
- The ROI's range was taken in the loader's grey levels, which truncate,
  round or quantize fractions, and the image's range in its own units. On
  the preserve_hu offset map a ROI spanning a fractional image read
  1.004975. Both ranges are now grey-level counts on the image's own map:
  SlideProps::covered_intensity_range puts the image's ends through the
  forward map without its range clamps, so the ratio is unitless and the
  inverse map no longer rescales it. A non-finite sample, stored at grey
  level 0 but left out of the extrema, cannot take it above 1.
- With no slide properties, the 2D in-memory site left 0 while the other
  three wrote 1. Dataset::covered_intensity_range answers 1 on every path,
  as does an image whose range is zero: a ROI is then taken as its own
  image, which it covers whole. Nyxus.featurize now measures each array's
  ranges itself, so in-memory input reports the real value instead of 0.

ENERGY, ROOT_MEAN_SQUARED, 3ENERGY and 3ROOT_MEAN_SQUARED squared each
grey level as a 32-bit unsigned product, which wraps above 65535. The
four sums (2D and 3D, in memory and out of core) square in double.

Values that change: COVERED_IMAGE_INTENSITY_RANGE and
3COVERED_IMAGE_INTENSITY_RANGE on segmented images whose background
reaches beyond the ROIs' range (ut_inten.nii ROI 57: 1 to 2000/3024,
volmin: 1 to 0.8457), on fractional images under preserve_hu, and in
memory (0 to the real value); ENERGY and ROOT_MEAN_SQUARED (2D and 3D)
wherever a grey level exceeds 65535.

Tests that fail without the fix: test_2d/3d_firstorder_analytic.py
(covered range 0.5 against 1 on segmented fractional slides with and
without preserve_hu, NIfTI and OME-TIFF, in RAM and out of core; 1 against
1.004975 on whole slides and volumes; 0.5 against 0 in memory; ENERGY
exact above 65535), TEST_2D/3D_FIRSTORDER_ENERGY_WIDE_ANALYTIC,
_COVERED_RANGE_ANALYTIC, _COVERED_RANGE_FLAT_SLIDE_MECHANICS,
TEST_2D_FIRSTORDER_COVERED_RANGE_NO_SLIDE_MECHANICS,
_NONFINITE_SAMPLE_MECHANICS, _SATURATED_SLIDE_MECHANICS and the
3COVERED regression pin. The 16-bit ENERGY cases are the negative tests.

Closes PN-26, PN-61, PN-78, PN-226 and PolusAI#490.
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.

fix(intensity): ENERGY and ROOT_MEAN_SQUARED square intensities in 32 bits

1 participant