Skip to content

fix(glcm): refuse a ROI whose 2D co-occurrence matrix cannot be held on its own - #544

Merged
sameeul merged 2 commits into
PolusAI:mainfrom
darkclad:main-glcm-ibsi-alloc
Nov 20, 2026
Merged

sameeul merged 2 commits into
PolusAI:mainfrom
darkclad:main-glcm-ibsi-alloc

Conversation

@darkclad

Copy link
Copy Markdown

Summary

Fixes one ROI's GLCM allocation refusal in IBSI mode zeroing the GLCM values of other ROIs in the image (PN-235).

In IBSI mode the 2D co-occurrence matrix has one row and column per grey level up to the ROI's maximum. The size check multiplied width by height in int, which overflows from 46341 levels, so it threw on every machine. runParallel drops the futures of its workers, so the exception was lost and the rest of that worker's batch kept unset values. Which ROIs went to 0 depended on their order.

Change

  • GLCMFeature::calculate handles a ROI whose matrix cannot be held: every GLCM value of that ROI gets the --noval substitute (0 from Python) and a message on stderr names the ROI. Because this is in calculate, the in-RAM and out-of-core paths behave the same.
  • The matrix is refused before it is allocated when it has more than 46340 levels, the largest square SimpleMatrix can index. This is the boundary at which the old check already fired, so no ROI that computed before is refused now.
  • Out of core, a ROI above the limit used to stop the whole Python run; it now gets the same per-ROI result.

No formula changes. A ROI below the limit gets the same values as before.

Changed values

All 149 2D GLCM_* columns (per-angle and _AVE), only for images with a ROI above 46340 levels in IBSI mode, or with --coarseGrayDepth above 46340. Fixture: two 4x4 ROIs, one with maximum 65535, the other with levels up to 200, the refused ROI first.

ROI Column Before After
levels up to 200 GLCM_ASM_0 0 0.1111 (equal to its run on its own)
levels up to 200 all 149 GLCM_* 147 of 149 were wrong equal to its run on its own
maximum 65535 all 149 GLCM_* 0, no ROI named --noval substitute, ROI named on stderr

Tests

  • tests/python/test_2d_glcm_mechanics.py: isolation in both ROI orders at maxima 46341 and 65535, the same in RAM against out of core, and 8-bit ROIs not refused.
  • tests/test_2d_glcm_mechanics.h: a custom noval reaches every feature value, and a small ROI is not refused.

The new tests fail on the previous code.

Known limits

  • The bad_alloc branch has no test, because forcing it would need an allocator seam.
  • On Linux with overcommit, a matrix below the limit that does not fit in RAM may still end in an OOM kill rather than a refusal.

Related

PN-135 tracks the general runParallel behaviour. PN-260 tracks the same int size check in 3D GLCM and the IDM grey-level-difference overflow; this PR covers its 2D size-check part.

@vjaganat90 vjaganat90 left a comment

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.

LGTM.

I measured it against its base 697eaeb.

  • Isolation. Two 4 × 4 ROIs in IBSI mode: one with a maximum of 46341 or 65535, the other with levels up to 200. I ran both orders, in memory and out of core. In all eight runs the small ROI's 149 GLCM_* columns equal its run alone, the refused ROI reads 0 in every column, and stderr names it.
  • The base. On the base the out-of-core run raises RuntimeError and peaks at 4.9 GB; the head peaks at 120 MB.
  • Unchanged values. 6705 GLCM values on three random images, in IBSI and default mode, match the base exactly.

Nit

  • not_covered.md:20: the heading should say 117, not 111, since that is what section A's tables sum to at this head. main's tables already summed to 111 before this PR, and it adds 6 (3 in test_2d_glcm_mechanics.h, 3 in the new .py). #535 rewrites the same line to 113, so whichever merges second needs the sum taken again (119 with both).

Demian Vladi added 2 commits November 17, 2026 08:20
…on its own

In IBSI mode the matrix has one row and column per grey level up to the ROI's
maximum. Above 46340 levels the size check overflowed int and threw, and the
reduce workers drop that exception, so the rest of the batch kept unset values
and every GLCM column came out 0.

A ROI above that limit, or whose matrix cannot be allocated, now gets the noval
substitute for itself and a message naming it on stderr. The other ROIs are
calculated as usual, in RAM and out of core.

Refs PN-235
The mechanics header is a scanned source of the GLCM registry, so asserting on
GLCM_ASM_AVE there counted as coverage the registry row does not list, and the
registry check failed. The test now walks GLCMFeature::featureset and requires
every value of every feature to differ from the substitute.

Refs PN-235
@darkclad
darkclad force-pushed the main-glcm-ibsi-alloc branch from 7c7eb56 to d085aab Compare November 17, 2026 16:52

@vjaganat90 vjaganat90 left a comment

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.

LGTM. Against 7c7eb56 the rebase changes only tests/vetting/not_covered.md: section A's totals (34 files, 124 test functions; A.1 27 files) now sum from its tables, which settles my nit. main changed no GLCM source in between, so the measurements from my earlier review stand.

@sameeul
sameeul merged commit c029ba3 into PolusAI:main Nov 20, 2026
49 checks passed
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.

3 participants