Skip to content

fix(glcm): use grey levels, not matrix rows, when PyRadiomics binning leaves bins empty - #550

Open
darkclad wants to merge 4 commits into
PolusAI:mainfrom
darkclad:main-glcm-gap-levels
Open

darkclad wants to merge 4 commits into
PolusAI:mainfrom
darkclad:main-glcm-gap-levels

Conversation

@darkclad

@darkclad darkclad commented Nov 16, 2026 •

Copy link
Copy Markdown

Summary

With PyRadiomics binning (coarse_gray_depth < 0), 2D and 3D GLCM features now match PyRadiomics when the ROI leaves some bins empty. In this mode the co-occurrence matrix has a row only for the binned levels present in the ROI, so row x stands for level I[x]. Several features used the row index where they meant the level. The two agree only when every bin between 1 and the highest level is occupied, so ROIs that use every bin gave the right values and the existing fixtures passed.

  • calculatePxpmy bucketed p_{x+y} and p_{x−y} by row sum and row difference, and kValuesSum / kValuesDiff held the level of whichever pair was written last. DIFAVE, DIFVAR, IV and SUMAVERAGE were far off. A dense block of low bins with a few voxels alone in the top bin makes this worst: one CT ROI had DIFVAR 8.86 against 0.392. DIFENTRO, SUMENTROPY, ID, IDM, IDN and IDMN read the same distributions.
  • IDN and IDMN divided by the number of levels present, not by the highest level.
  • DIS, HOM1 and HOM2 used the row distance |x−y|, and JVAR used x+1 against the level-based JAVE.

p_{x+y} and p_{x−y} are now distributions over level sums 0..2Ng and level differences 0..Ng−1, with Ng the highest level present, as in PyRadiomics. Every feature above works on I[x]. MATLAB and IBSI binning do not change, because there I[x] == x+1. The out-of-core paths share this code.

Reported by @ndonyapour from a 3D CT validation against PyRadiomics (5 volumes × 2 labels, binCount 20). Closes #510.

Change

Two commits:

  1. fix(glcm): use grey levels, not matrix rows, when PyRadiomics binning leaves bins empty: the fix, the tests, the generator and the docs.
  2. refactor(glcm): drop the identity k-value vectors and record the gap-levels recipe: no value changes. It is cleanup plus the registry rows for the new recipe.
  • src/nyx/features/glcm.cpp, src/nyx/features/3d_glcm.cpp: the same changes in both files.
    • calculatePxpmy builds the distributions by level.
    • f_dvar is now a single sum over k.
    • IDM, ID, IDN, IDMN and DIFENTRO iterate over Pxmy.size().
    • DIS, HOM1, HOM2 and JVAR use I[x].
    • SUMAVERAGE, DIFAVE, DIFVAR and IV use k directly.
  • src/nyx/features/glcm.h, src/nyx/features/3d_glcm.h: kValuesSum and kValuesDiff, which held k at index k, are gone. f_dvar and f_dentropy no longer take the matrix they did not read.
  • tests/test_2d_glcm_pyradiomics.h, tests/test_3d_glcm_pyradiomics.h, tests/test_all.cc: TEST_2D_GLCM_GAP_LEVELS_PYRADIOMICS and TEST_3D_GLCM_GAP_LEVELS_PYRADIOMICS pin every GLCM feature on two crops of that CT data. The 2D crop is a 12 × 12 slice with levels {1..8, 11, 13, 20}. The 3D crop is a 9 × 9 × 5 box with levels {1..7, 9, 10, 12, 20}.
  • tests/vetting/oracles/gen_glcm_gap_levels_pyradiomics.py: reads the crops from the test headers, runs PyRadiomics (binCount 20, symmetrical, distance 1, force2D for the slice), and checks the pins in both directions at 1e-12.
  • tests/vetting/oracle_coverage.csv: one vetted pyradiomics row per feature the two new tests pin, 59 in 2D and 59 in 3D, under recipe glcm.pyradiomics_gap_levels_bincount20. Each row carries the band its test applies. report_output.csv and report_output.md are regenerated.
  • Docs:
    • f_glcm.rst and f_glcm_3d.rst say what N_g is in each binning mode: the highest level present under PyRadiomics-style binning, and the bin count under MATLAB and IBSI binning.
    • Both pages describe what happens to levels absent from the ROI, and their Validation sections name the new fixture.
    • The "Absent grey levels" section of the 2D PyRadiomics vetting report and the glcm.pyradiomics_gap_levels_bincount20 recipe in config_recipes.md record the same.

Changed values

These change only under PyRadiomics binning, and only for ROIs that leave a bin below their highest level empty. 3D, the three affected ROIs of the CT set, binCount 20:

ROI (bins used) Feature PyRadiomics Before After
PE9f577d label 2 (13) 3GLCM_DIFAVE 0.473667 3.36868 0.473667
PE9f577d label 2 (13) 3GLCM_DIFVAR 0.391885 8.86459 0.391885
PE9f577d label 2 (13) 3GLCM_IV 0.375733 0.00627683 0.375733
PE9f577d label 2 (13) 3GLCM_SUMAVERAGE 4.44079 4.46096 4.44079
PE9f577d label 2 (13) 3GLCM_IDN 0.977754 0.966839 0.977754
PE9f577d label 2 (13) 3GLCM_IDMN 0.998462 0.996399 0.998462
PE9f577d label 1 (14) 3GLCM_DIFAVE 0.52317 1.03947 0.52317
PE9f577d label 1 (14) 3GLCM_SUMAVERAGE 5.51851 6.21361 5.51851
PE9f577d label 1 (14) 3GLCM_IV 0.402256 0.10069 0.402256
PE9f4bc5 label 2 (19) 3GLCM_DIFAVE 0.663213 1.15443 0.663213
PE9f4bc5 label 2 (19) 3GLCM_IV 0.41054 0.10658 0.41054

After this change, every non-logarithmic GLCM feature of all 10 ROIs, twins included, matches PyRadiomics to ≤ 6e-14. On the 7 ROIs that use all 20 bins, the values did not change. The entropy features and IMC1/IMC2 keep the documented fast_log10 band, and their values did not change.

In 2D, 30 slices of the same data (10 of them leave bins empty) failed 13 features before this change, DIS, HOM1, HOM2 and JVAR among them. All 30 match after it.

Testing

The tests below ran on each commit.

  • Full gtest suite (Windows): 1338 passed. pytest tests/python/ on the whole directory: 346 passed, 4 skipped.
  • Both new tests fail on the code before this change (13 features each) and pass after it. The other 49 GLCM PyRadiomics tests pass both before and after.
  • ASan + UBSan (Linux, gcc): gtest (1277 cases, the new tests included) and pytest tests/python/ through the bindings (327 passed). 0 sanitizer diagnostics.
  • check_coverage.py, check_test_names.py, run_scanners.py, report_features.py --check, gen_feature_reference.py --check, gen_cli_reference.py --check and sphinx-build -W: clean. gen_glcm_gap_levels_pyradiomics.py re-verifies every pin.

Not in this PR, filed on Axle's PN Jira board (https://labshare.atlassian.net/jira/software/projects/PN/boards/851):

  • PN-303: share the GLCM feature code that 2D and 3D duplicate. This defect had to be fixed in both copies.
  • PN-304: run the gap-levels fixture through the 3D out-of-core path, so in-RAM == OOC is checked on empty bins.

@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 built the head and its base and ran 2D and 3D GLCM on random ROIs, some using every bin and some leaving bins empty, against PyRadiomics 3.0.1 (binCount 20, symmetric):

  • With coarse_gray_depth=-20 and empty bins, the base misses PyRadiomics on 13 features (DIFVAR 0.852 against 15.603 on one ROI). The head matches it on every non-logarithmic feature in 2D and 3D; INFOMEAS1 and INFOMEAS2 keep the fast_log10 gap they had before.
  • MATLAB and IBSI binning, and PyRadiomics binning with every bin used, give exactly the base values.

#552 also edits glcm.cpp, 3d_glcm.cpp and the GLCM docs, and #544 f_glcm.rst, so whichever merges later rebases.

Demian Vladi added 3 commits November 20, 2026 12:37
… leaves bins empty

With a negative coarse_gray_depth the 2D and 3D co-occurrence matrices hold a
row only for the binned levels present in the ROI, so row x stands for level
I[x]. When a bin is empty the two differ, and several features read x where
they meant I[x]:

- calculatePxpmy bucketed p_{x+y} and p_{x-y} by row sum and difference, and
  kValuesSum / kValuesDiff kept the level of whichever pair was written last.
  DIFAVE, DIFVAR, IV and SUMAVERAGE were far off; DIFENTRO, SUMENTROPY, ID,
  IDM, IDN and IDMN read the same distributions.
- IDN and IDMN divided by the number of levels present, not the highest level.
- DIS, HOM1 and HOM2 used the row distance |x-y|, and JVAR used x+1 against the
  level-based JAVE.

p_{x+y} and p_{x-y} are now distributions over level sums and differences,
0..2Ng and 0..Ng-1 with Ng the highest level, as in PyRadiomics, and every
feature above works on I[x]. f_dvar is one sum over k. MATLAB and IBSI binning
are unchanged: there I[x] == x+1. Both out-of-core paths share this code.

On a 3D CT validation set (5 volumes x 2 labels, binCount 20) the three ROIs
that leave bins empty were off by up to 22x in DIFVAR; 30 2D slices of the same
data failed 13 features. Both now match PyRadiomics to <= 6e-14 on every
non-logarithmic feature, twins included.

Tests: TEST_2D_GLCM_GAP_LEVELS_PYRADIOMICS and
TEST_3D_GLCM_GAP_LEVELS_PYRADIOMICS pin every GLCM feature on a 12x12 slice
crop and a 9x9x5 volume crop of that CT data, whose levels leave interior bins
empty and put a few voxels alone in the top bin. Goldens come from
tests/vetting/oracles/gen_glcm_gap_levels_pyradiomics.py (PyRadiomics 3.0.1),
which reads the crops from the test headers. Both tests fail on the previous
code (13 features each) and pass with this change. The existing PyRadiomics
fixtures could not see it: the 3D phantom occupies all 20 bins and the 2D ones
run the IBSI identity recipe, where every level keeps a row.

Docs: f_glcm.rst and f_glcm_3d.rst state the level convention and the new
fixture; the 2D PyRadiomics vetting report and config_recipes.md record it.

Closes PolusAI#510
…levels recipe

- glcm.cpp, 3d_glcm.cpp: kValuesSum and kValuesDiff held k at index k, so
  SUMAVERAGE, DIFAVE, DIFVAR and IV now use k directly and both members are
  gone. f_dvar and f_dentropy no longer take the matrix they did not read.
  Values are unchanged.
- The comments on calculatePxpmy, IDN and IDMN, and the N_g row of
  f_glcm.rst, say what N_g is in each binning mode: the highest level present
  under PyRadiomics-style binning, the bin count under MATLAB and IBSI binning.
- oracle_coverage.csv: one vetted pyradiomics row per feature pinned by
  TEST_2D_GLCM_GAP_LEVELS_PYRADIOMICS and TEST_3D_GLCM_GAP_LEVELS_PYRADIOMICS,
  recipe glcm.pyradiomics_gap_levels_bincount20, with the bands those tests
  apply. coverage_report.md and report_output.* regenerated.
- The 2D gap-levels test restores GLCMFeature::angles on every exit path,
  test_3d_glcm_pyradiomics.h no longer repeats includes test_main_nyxus.h
  provides, and the generator keeps a single tolerance.
… tests

The gap-levels tests assert GLCM features against PyRadiomics, so the
generated oracle column now lists PyRadiomics next to MIRP for those rows.
@darkclad
darkclad force-pushed the main-glcm-gap-levels branch from 5340dc0 to 6371abc Compare November 23, 2026 14:48
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(glcm): DIFAVE, SUMAVERAGE, IV, JVAR and DIFVAR mix positions and values under PyRadiomics binning

2 participants