Skip to content

fix(glcm)!: average the _AVE codes over the directions that have pairs - #558

Open
darkclad wants to merge 3 commits into
PolusAI:mainfrom
darkclad:main-3d-glcm-empty-dir
Open

darkclad wants to merge 3 commits into
PolusAI:mainfrom
darkclad:main-3d-glcm-empty-dir

Conversation

@darkclad

@darkclad darkclad commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

A user comparing Nyxus 3D features with PyRadiomics on CT lung lobes found that one lobe, cut to a single slice by the scan's field of view, gave 3D GLCM averages far below PyRadiomics.

A direction along which a ROI is one pixel or voxel thin has no pairs, so its co-occurrence matrix is empty. Such a direction reported the --noval substitute (0 by default), and the _AVE codes averaged that substitute together with the directions that have pairs:

  • 3D: on a ROI one plane deep, the 9 directions that leave the plane are empty, so every 3GLCM_*_AVE value was 4/13 of the IBSI value. On the test fixture, 3GLCM_CONTRAST_AVE was 1.775, where MIRP 3d_average and PyRadiomics give 5.769.
  • 2D: a ROI one pixel tall has pairs at 0 degrees only, so every GLCM_*_AVE value was a quarter of the 0-degree value. GLCM_CONTRAST_AVE was 1.833, where MIRP 2d_average gives 7.333.

A 3D ROI of one grey level was also computed: 3GLCM_ASM 1, 3GLCM_JAVE the level, and the substitute for correlation. A 2D ROI of one grey level reports the substitute in every column.

Change

  • _AVE codes, 2D and 3D: the average is taken over the directions that have pairs, as IBSI, MIRP and PyRadiomics take it. When no direction has pairs, the average is the substitute. The per-direction columns are unchanged: an empty direction still reports the substitute. When every direction has pairs, the average is the same std::reduce as before, so values on such ROIs are bit-identical.
  • Constant 3D ROI: a 3D ROI of one grey level after binning reports the substitute in every 3D GLCM column, as a 2D ROI does. This includes IBSI mode, where MIRP computes limiting values; 2D already reports the substitute there.
  • Out of core: the 3D out-of-core path shares finalize_angle() with the in-memory path, so it follows both rules, and its "nothing to featurize" branch now covers the one-level ROI too.

The rule lives in one place, TextureFeature::calc_ave_populated(). GLCMFeature and D3_GLCM_feature record, per direction, whether the matrix has pairs.

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 66 0 of 751
a synthetic scene of 300 elliptical ROIs 300 0 of 751
ut_inten.nii + ut_mask57.nii, volmin_inten.nii + volmin_mask.nii 2 0 of 217

The change affects only ROIs that are one pixel or voxel thin along some direction, and 3D ROIs of one grey level:

Case Columns Before After
3D ROI one plane deep every 3GLCM_*_AVE 4/13 of the mean of the in-plane directions the mean of the in-plane directions
2D ROI one pixel tall every GLCM_*_AVE 1/4 of the 0-degree value the 0-degree value
3D ROI of one grey level every 3GLCM_* and 3GLCM_*_AVE limiting values (3GLCM_ASM 1, 3GLCM_CONTRAST 0, 3GLCM_JAVE the level) the --noval substitute

The 3D worked example on the docs page changes 3GLCM_CONTRAST_AVE on its one-plane ROI from 0.673 to 2.1875.

Tests

These fail on main:

  • TEST_3D_GLCM_ONE_PLANE_AVE_MIRP: every 3GLCM_*_AVE code on bench_plane9x8_thin, one plane deep, against MIRP 2.6.0 3d_average (IBSI mode, no discretisation). PyRadiomics 3.0.1 gives the same values on this fixture.
  • TEST_2D_GLCM_ONE_ROW_AVE_MIRP: every GLCM_*_AVE feature on bench_row10_thin, one pixel tall, against MIRP 2d_average.
  • TEST_3D_GLCM_CONSTANT_ROI_REPORTS_NOVAL_MECHANICS: a constant 3D ROI reports the substitute in every column, under IBSI, MATLAB-style and PyRadiomics-style binning, in memory and out of core.

These pass on main and guard what must not change:

  • TEST_3D_GLCM_THREE_PLANE_AVE_MIRP: the control, three planes with all 13 directions populated, against MIRP.
  • TEST_2D_GLCM_NO_PAIRS_REPORTS_NOVAL_MECHANICS and TEST_3D_GLCM_NO_PAIRS_REPORTS_NOVAL_MECHANICS: a two-level ROI with no pair in any direction reports the substitute in every column, the _AVE codes included.
  • TEST_3D_GLCM_OOC_GAP_LEVELS_INVARIANT and TEST_3D_GLCM_OOC_ONE_PLANE_INVARIANT: the 3D out-of-core path equals the in-memory path, every value of every direction, on the gap-levels CT crop at PyRadiomics binCount 20 and on the one-plane ROI.

The three-plane control has a level-shifted middle plane. On three identical planes, the z direction pairs only equal levels, and MIRP then leaves that direction out of inverse variance, where the definition, PyRadiomics and Nyxus give it 0. The shift keeps the control off that difference between the tools.

Goldens come from tests/vetting/oracles/gen_glcm3d_mirp.py (new) and gen_glcm_mirp.py (extended). Both read the fixtures from the test headers and re-verify every pin.

Vetting registry

  • 30 vetted/mirp rows for the 3GLCM_*_AVE codes (recipe glcm3d.mirp_ibsi_3d_average).
  • 30 vetted/mirp rows for the 2D GLCM_*_AVE features (recipe glcm.ibsi_identity_one_row).
  • 60 invariant rows for the 3D out-of-core comparison (recipe glcm3d.ooc_parity).
  • The new fixtures are in benchmarks.md and the 3D IBSI cell is in matrix/glcm.md.
  • scan_glcm3d_coverage.py reads the new MIRP and invariant files. scan_glcm_coverage.py credits the one-row table.
  • report_output.* and featurelist.rst are regenerated.

Docs

  • f_glcm_3d.rst: the _AVE definition is now the mean over the directions that have pairs, and the constant-ROI entry follows 2D. The validation section names the MIRP checks.
  • The 3D page no longer says 3GLCM_HOM2 has no _AVE column; it has one.
  • f_glcm.rst: the _AVE definition and validation section follow.
  • conventions.rst: the 2D/3D constant-ROI difference is removed from both tables.
  • docs/examples/glcm_3d_worked_example.py: the one-plane check expects 2.1875.

Benchmark instruction counts

The 2D GLCM kernel records one flag per angle and takes the _AVE codes through calc_ave_populated(). Its output value is unchanged on the benchmark fixture, which populates every angle, but its instruction count may move. If the job reports a change, the updated instructions.json follows in its own test(benchmarks): commit.

Phase-0 files touched

glcm.cpp, glcm.h, 3d_glcm.cpp and 3d_glcm.h. The change is limited to the averaging and the constant-ROI check, so the GLCM settings work planned for phase 0 can be written on top of it.

Closes PN-229, PN-304.

Demian Vladi added 3 commits December 2, 2026 11:35
A direction along which a ROI is one pixel or voxel thin has no pairs, and
its matrix is empty. Such a direction reported the --noval substitute, and
the _AVE codes averaged that substitute with the directions that have
pairs. On a 3D ROI one plane deep, 9 of the 13 directions are empty, so
every 3GLCM_*_AVE value was 4/13 of the IBSI value: 3GLCM_CONTRAST_AVE
1.775 where MIRP and PyRadiomics give 5.769. A 2D ROI one pixel tall has
pairs at 0 degrees only, and every GLCM_*_AVE value was a quarter of it.

The _AVE codes now average the directions that have pairs, as IBSI, MIRP
and PyRadiomics do, and report the substitute when none has. The angled
columns are unchanged: an empty direction still reports the substitute.
The 2D path, the 3D in-memory path and the 3D out-of-core path share the
rule through TextureFeature::calc_ave_populated().

A 3D ROI of one grey level after binning now reports the substitute in
every 3D GLCM column, as a 2D ROI does, instead of limiting values
(3GLCM_ASM 1, 3GLCM_JAVE the level) next to substitute correlations.

Tests: MIRP 3d_average on a one-plane ROI and a three-plane control, MIRP
2d_average on a one-row ROI, the constant-ROI and no-pairs substitutes in
2D and 3D, and the 3D out-of-core path against the in-memory path on the
gap-levels CT crop and the one-plane ROI.

Refs: PN-229, PN-304
…he mixed case

calc_ave_populated() took the plain mean of every value whenever its
flags held no false, which included a flag list shorter than the values
or empty. A value recorded without a flag now counts as a direction with
no pairs and stays out of the average.

The no-pairs mechanics tests have every direction at the substitute, so
the old mean of substitutes passed them too. New mechanics tests cover
the mixed case: a 2D row and a 3D row with pairs along one direction
only, where every _AVE code must equal that direction's value on both
3D paths, and a direct test of the unflagged-direction rule. All three
fail without the change they guard.

The glcm and glcm_3d family notes in feature_descriptions.toml now say
the mean is over the directions that have pairs; featurelist.rst is
regenerated from them. A test comment drops a board reference.
…filtered averaging

The _AVE codes now keep one has-pairs flag per direction and average
only the flagged values, which adds about 2,660 instructions to each 2D
GLCM kernel (1.000x); the feature values are unchanged. The two
kernels/glcm entries take the counts of the instruction-count job on
dac1779; no other kernel's count changed.
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.

1 participant