Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions docs/examples/glszm_worked_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,20 @@ def glszm(image, roi, **kw):
assert math.isclose(got, 341.05, rel_tol=5e-4), f"default GLSZM_GLV = {got}, page says 341.05"


# Zone growth: images whose zones are split by the east/south-east/south/south-west walk
split = np.array([[2, 2, 1],
[1, 1, 1]], dtype=np.uint32)
f = glszm(split, np.ones_like(split), ibsi=True)
quoted = {"ZP": 0.5, "SAE": 0.4537}
for name, expected in quoted.items():
got = f[f"GLSZM_{name}"][0]
assert math.isclose(got, expected, rel_tol=5e-4), f"2x3 GLSZM_{name} = {got}, page says {expected}"
# Connectivity: the four 1s of the 2 x 3 image are one zone, reached from the top right by stepping west
west = np.array([[2, 2, 1],
[1, 1, 1]], dtype=np.uint32)
f = glszm(west, np.ones_like(west), ibsi=True)
assert math.isclose(f["GLSZM_ZP"][0], 2 / 6), f"2x3 GLSZM_ZP = {f['GLSZM_ZP'][0]}, expected 2 zones over 6 pixels"

# The 5 x 5 example of the PyRadiomics GLSZM documentation: 11 zones over 25 pixels
pyrad = np.array([[5, 2, 5, 4, 4],
[3, 3, 3, 1, 3],
[2, 1, 1, 1, 3],
[4, 2, 2, 2, 3],
[3, 5, 3, 3, 2]], dtype=np.uint32)
f = glszm(pyrad, np.ones_like(pyrad), ibsi=True)
quoted = {"ZP": 0.52, "SAE": 0.6596}
for name, expected in quoted.items():
got = f[f"GLSZM_{name}"][0]
assert math.isclose(got, expected, rel_tol=5e-4), f"PyRadiomics image GLSZM_{name} = {got}, page says {expected}"
assert math.isclose(f["GLSZM_ZP"][0], 0.44), f"PyRadiomics image GLSZM_ZP = {f['GLSZM_ZP'][0]}, page says 0.44"

# Background as grey level 1: a 6x5 ROI whose four corners are outside the mask
roi_img = np.array([[3, 3, 7, 7, 7, 2],
Expand All @@ -83,26 +78,22 @@ def glszm(image, roi, **kw):
[1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 0]], dtype=np.uint32)
default = glszm(roi_img, roi_mask)
quoted = {"ZP": 0.4, "SAE": 0.4144, "LGLZE": 0.3342}
quoted = {"ZP": 0.3667, "SAE": 0.4261, "LGLZE": 0.3645}
for name, expected in quoted.items():
got = default[f"GLSZM_{name}"][0]
assert math.isclose(got, expected, rel_tol=5e-4), f"default GLSZM_{name} = {got}, page says {expected}"
ibsi = glszm(roi_img, roi_mask, ibsi=True)
assert math.isclose(ibsi["GLSZM_ZP"][0], 8 / 26, rel_tol=5e-4), f"IBSI GLSZM_ZP = {ibsi['GLSZM_ZP'][0]}"
assert math.isclose(ibsi["GLSZM_ZP"][0], 7 / 26, rel_tol=5e-4), f"IBSI GLSZM_ZP = {ibsi['GLSZM_ZP'][0]}"

# Constant ROI: every feature is the missing-value substitute (0 by default)
const = np.full((4, 4), 7, dtype=np.uint32)
f = glszm(const, np.ones_like(const), ibsi=True)
assert all(f[f"GLSZM_{c}"][0] == 0 for c in ("SAE", "ZE", "GLV", "ZP")), "constant ROI is not the substitute"

# The 8-connected values for the 2 x 3 image: one zone of 4 pixels and one of 2
assert math.isclose(2 / 6, 0.3333, rel_tol=5e-4), "8-connected zone percentage"
assert math.isclose((1 / 16 + 1 / 4) / 2, 0.1563, rel_tol=5e-4), "8-connected small area emphasis"

# The 6 x 5 ROI without its corners: IBSI mode on the image binned the default way, corners set to 0
binned = np.minimum(np.floor(64 * roi_img.astype(int) / 9) + 1, 64).astype(np.uint32) * roi_mask
f = glszm(binned, roi_mask, ibsi=True)
quoted = {"ZP": 0.3077, "SAE": 0.1215, "LGLZE": 0.001356}
quoted = {"ZP": 0.2692, "SAE": 0.09821, "LGLZE": 0.001379}
for name, expected in quoted.items():
got = f[f"GLSZM_{name}"][0]
assert math.isclose(got, expected, rel_tol=5e-4), f"corners excluded GLSZM_{name} = {got}, page says {expected}"
Expand Down
2 changes: 1 addition & 1 deletion docs/feature_descriptions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ intensity_histogram = "Available only in IBSI mode (--ibsi=true). All features c
neighbors = "Two ROIs are neighbours when their contours come within --pixelDistance pixels of each other (5 by default), measured between contour pixels, not centroids."
glcm = "The grey-level co-occurrence matrix counts pairs of ROI pixels one pixel apart, built separately for each angle in --glcmAngles (default 0, 45, 90 and 135 degrees); pairs with a pixel of stored intensity 0 are skipped, which covers every pixel outside the ROI and any ROI pixel whose value is 0. Each plain code is output as one column per angle, named with the angle as a suffix (for example GLCM_ASM_45), and its _AVE code is the mean of those columns. Grey levels come from --coarseGrayDepth binning or are the raw intensities under --ibsi; each ordered pair is counted once with a positive grey depth, and the matrix is made symmetric with a negative or zero grey depth and under --ibsi."
glrlm = "A run is a straight line of consecutive pixels with the same grey level; runs are collected separately along 0, 45, 90 and 135 degrees into a grey level by run length matrix. Each plain code is output as one column per angle (suffixes _0, _45, _90 and _135), and its _AVE code is the mean of the four. Grey levels come from --coarseGrayDepth binning, or are the raw intensities under --ibsi. With a positive --coarseGrayDepth (the default), pixels of stored intensity 0, including every bounding-box pixel outside the ROI, are binned to grey level 1 and form runs; under --ibsi or a negative --coarseGrayDepth they are skipped."
glszm = "A zone is a group of connected pixels sharing one grey level, grown from each unvisited pixel in scan order through same-level neighbours to its east, south-east, south and south-west; the matrix counts zones by grey level and size. The family has no directions, so each code is one value per ROI. Grey levels come from --coarseGrayDepth binning, or are the raw intensities under --ibsi. With a positive --coarseGrayDepth (the default), pixels of stored intensity 0, including every bounding-box pixel outside the ROI, are binned to grey level 1 and form zones; under --ibsi or a negative --coarseGrayDepth they are skipped."
glszm = "A zone is an 8-connected group of pixels sharing one grey level, pixels touching by an edge or a corner, as IBSI defines it; the matrix counts zones by grey level and size. The family has no directions, so each code is one value per ROI. Grey levels come from --coarseGrayDepth binning, or are the raw intensities under --ibsi. With a positive --coarseGrayDepth (the default), pixels of stored intensity 0, including every bounding-box pixel outside the ROI, are binned to grey level 1 and form zones; under --ibsi or a negative --coarseGrayDepth they are skipped."
gldm = "A pixel's dependence is one plus the number of its 8 neighbours of nonzero stored intensity that have exactly the same grey level, and the matrix counts the ROI pixels of nonzero stored intensity by grey level and dependence. The family has no directions, so each code is one value per ROI. Grey levels come from --coarseGrayDepth binning, or are the raw intensities under --ibsi."
gldzm = "A zone is an 8-connected group of pixels sharing one grey level. Its distance is the smallest, over its pixels, of the steps along a row or column to the nearest pixel of stored intensity 0 plus one, or to the bounding-box edge, where a pixel on the bounding-box edge is at distance 1. With a positive --coarseGrayDepth (the default) the pixels outside the ROI are binned to grey level 1 and form zones like ROI pixels, so only the bounding-box edge counts; under --ibsi pixels of stored intensity 0 are skipped. The matrix counts zones by grey level and distance; each code is one value per ROI. Grey levels come from --coarseGrayDepth binning, or are the raw intensities under --ibsi."
ngldm = "Every ROI pixel is a neighbourhood centre whose dependence count is one plus the number of its 8 neighbours inside the ROI with the same grey level; the matrix counts centres by grey level and dependence count. Each code is one value per ROI. With a positive --coarseGrayDepth, grey levels come from scaling the range 0 to the ROI maximum by it and truncating (levels 0 to --coarseGrayDepth); 0 and --ibsi keep the raw intensities, and a negative value gives PyRadiomics-style bins over the ROI's range."
Expand Down
69 changes: 30 additions & 39 deletions docs/source/Math/f_glszm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ How the matrix is built
-----------------------

Nyxus scans the ROI's bounding box row by row, from the top left. Each pixel that is not yet in a
zone and has a non-zero grey level starts a new zone. The zone grows through pixels of the same grey
level, one step at a time, to the east, south-east, south or south-west of a pixel already in the
zone. :math:`\textbf{P}(i,j)` counts the zones of grey level :math:`i` and size :math:`j` pixels.

- **Connectivity:** the steps cover half of the 8-neighbourhood. A zone whose pixels can all be
reached this way from its first pixel is the 8-connected zone that IBSI defines. A zone that needs
a step to the west, north-west, north or north-east is split into several zones (see
:ref:`glszm-possible-issues`).
zone and has a non-zero grey level starts a new zone. The zone grows through every pixel of the same
grey level that touches a pixel already in it, by an edge or a corner.
:math:`\textbf{P}(i,j)` counts the zones of grey level :math:`i` and size :math:`j` pixels.

- **Connectivity:** 8-connected, as IBSI, PyRadiomics and MIRP define it. A zone is the full
8-connected component, whatever the scan order, including one that turns back up or to the left.
In the image below the four pixels of level 1 are one zone, though the zone starts at the top
right and reaches the bottom left only by stepping west:

.. math::

\begin{bmatrix} 2 & 2 & 1\\ 1 & 1 & 1 \end{bmatrix}

On the 5 × 5 example of the PyRadiomics GLSZM documentation Nyxus finds the same 11 zones as
PyRadiomics, and a zone percentage of 0.44.
- **Directions:** none. One matrix is built per ROI, and each feature has one value.
- **Grey levels:** intensities are first binned into :math:`N` grey levels, as set by
``--coarseGrayDepth`` (64 by default). A pixel of intensity :math:`x` gets level
Expand Down Expand Up @@ -75,8 +82,7 @@ Take a 5 × 5 ROI that already has five grey levels:

In IBSI mode (no binning) the matrix is below; rows are grey levels 1 to 5 and columns are zone
sizes 1 to 6. The six pixels of level 1 in the lower left form one zone; the 3 at the start of row
2 joins the 3 below it to the south-east, a zone of size 2. Every zone of this image is the same
under Nyxus' scan and under full 8-connectivity.
2 joins the 3 below it to the south-east, a zone of size 2.

.. math::

Expand Down Expand Up @@ -332,8 +338,8 @@ The mean over all zones of one over the squared grey level. High when dark zones
\mathrm{LGLZE} = \frac{1}{N_z}\sum_{i=1}^{N_g} \frac{s_i}{g_i^{\,2}}

**Possible issues.** With the default binning the background zones have level 1 and weight 1, far
above any binned ROI level. On the ROI in :ref:`glszm-possible-issues` the value is 0.3342 with
them and 0.001356 without them.
above any binned ROI level. On the ROI in :ref:`glszm-possible-issues` the value is 0.3645 with
them and 0.001379 without them.


.. _feature-glszm-sae:
Expand All @@ -351,9 +357,6 @@ fine texture. IBSI calls it *small zone emphasis*.

\mathrm{SAE} = \frac{1}{N_z}\sum_{j=1}^{N_s} \frac{s_j}{j^2}

**Possible issues.** A zone split by the scan adds smaller zones and changes the value: 0.4537
instead of 0.1563 on the 2 × 3 image in :ref:`glszm-possible-issues`.


.. _feature-glszm-sahgle:

Expand Down Expand Up @@ -478,23 +481,6 @@ The variance of the zone size over all zones. IBSI calls it *zone size variance*
Possible issues
---------------

Zones are not fully 8-connected
A zone grows only by steps to the east, south-east, south and south-west. A same-level pixel that
can be reached only by a step to the west, north-west, north or north-east starts a zone of its
own. In the image below, the 1 at the lower left touches the 1 to its right, but the zone starts
at the top-right 1 and never steps west, so Nyxus finds zones of size 3 and 1 where IBSI has one
zone of size 4.

.. math::

\begin{bmatrix} 2 & 2 & 1\\ 1 & 1 & 1 \end{bmatrix}

Every feature can change. On this image zone percentage is 0.5 instead of 0.3333, and small area
emphasis 0.4537 instead of 0.1563. On the 5 × 5 example of the PyRadiomics GLSZM documentation
Nyxus finds 13 zones instead of 11, zone percentage is 0.52 instead of 0.44 and small area
emphasis 0.6596 instead of 0.5912. Of the 512 ways to fill a 3 × 3 image with two grey levels,
152 give a different set of zones. IBSI mode does not change the scan.

Background as grey level 1 with the default binning
With a positive ``--coarseGrayDepth``, intensity 0 bins to level 1. The bounding-box pixels
outside the mask then form zones of level 1, and :math:`N_p` is the size of the bounding box. ROI
Expand All @@ -510,10 +496,10 @@ Background as grey level 1 with the default binning
6 & 6 & 4 & 9 & 4 & 4\\
\cdot & 6 & 4 & 4 & 4 & \cdot \end{bmatrix}

With the default settings the four corners add four zones of size 1. Nyxus counts 12 zones over
30 pixels, so zone percentage is 0.4; without the corners the same binning gives 8 zones over 26
pixels, 0.3077. Small area emphasis is 0.4144 instead of 0.1215, and low grey level zone
emphasis 0.3342 instead of 0.001356. IBSI mode excludes the corners and gives 8 zones over 26
With the default settings the four corners add four zones of size 1. Nyxus counts 11 zones over
30 pixels, so zone percentage is 0.3667; without the corners the same binning gives 7 zones over
26 pixels, 0.2692. Small area emphasis is 0.4261 instead of 0.09821, and low grey level zone
emphasis 0.3645 instead of 0.001379. IBSI mode excludes the corners and gives 7 zones over 26
pixels.

Pixels of intensity 0 inside the mask
Expand All @@ -539,9 +525,14 @@ phantom in IBSI mode. Fifteen features agree to an absolute 10\ :sup:`-9`; zone
relative 4 × 10\ :sup:`-3`, which allows for the approximate logarithm. The four-slice means are
also compared with IBSI's published consensus values at a relative 10\ :sup:`-2`, the precision
those values are published to; that comparison is a consistency check, not one of the validations
in the index. The phantom's zones are the same under Nyxus' scan and under
8-connectivity, so these comparisons do not cover the split zones described above. The default
mode is covered by regression tests only.
in the index.

The phantom has no zone that turns back up or to the left, so the zone connectivity is checked
against PyRadiomics on four more images in IBSI mode: the 2 × 3 image above, the 5 × 5 example of
the PyRadiomics documentation, a zone that turns north-east and climbs north, and an image whose
same-level pixels touch only across a gap or through a pixel outside the mask, which must stay
separate zones. Fifteen features agree to an absolute 10\ :sup:`-9` and zone entropy to a relative
4 × 10\ :sup:`-3`. The default mode is covered by regression tests only.


References
Expand Down
4 changes: 2 additions & 2 deletions docs/source/Math/f_glszm_3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ grey level that touches a voxel already in it, by a face, an edge or a corner.
:math:`\textbf{P}(i,j)` counts the zones of grey level :math:`i` and size :math:`j` voxels.

- **Connectivity:** 26-connected. All 26 neighbours of a voxel are searched: 8 in its own slice and
9 in each adjacent slice. A zone is the full 26-connected component, whatever the scan order. This
differs from the 2D family, whose zones grow only to the east, south-east, south and south-west.
9 in each adjacent slice. A zone is the full 26-connected component, whatever the scan order, the
3D counterpart of the 2D family's 8-connected zones.
- **Directions:** none. One matrix is built per ROI, and each feature has one value.
- **Grey levels:** set by ``--coarseGrayDepth`` (``coarse_gray_depth``, 64 by default), or by
the ``3glszm/greydepth`` metaparameter, which overrides it for this family.
Expand Down
3 changes: 1 addition & 2 deletions docs/source/conventions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ they differ in these ways:
- ``3GLCM_*`` and ``3GLRLM_*`` are one column: the direction :math:`(1,1,1)` only. ``_AVE``
averages the 13 directions.
* - Zones
- GLSZM zones grow only east, south-east, south and south-west, so some 8-connected zones
split (see :ref:`glszm-possible-issues`). GLDZM zones are 8-connected.
- GLSZM and GLDZM zones are 8-connected.
- GLSZM and GLDZM zones are 26-connected.
* - GLDZM distance
- A walk along the pixel's row and column to the first pixel of grey level 0 or the box
Expand Down
2 changes: 1 addition & 1 deletion docs/source/featurelist.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ Definitions, formulas and settings: :doc:`/Math/f_glrlm`.
Grey-level size-zone matrix (GLSZM)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A zone is a group of connected pixels sharing one grey level, grown from each unvisited pixel in scan order through same-level neighbours to its east, south-east, south and south-west; the matrix counts zones by grey level and size. The family has no directions, so each code is one value per ROI. Grey levels come from ``--coarseGrayDepth`` binning, or are the raw intensities under ``--ibsi``. With a positive ``--coarseGrayDepth`` (the default), pixels of stored intensity 0, including every bounding-box pixel outside the ROI, are binned to grey level 1 and form zones; under ``--ibsi`` or a negative ``--coarseGrayDepth`` they are skipped.
A zone is an 8-connected group of pixels sharing one grey level, pixels touching by an edge or a corner, as IBSI defines it; the matrix counts zones by grey level and size. The family has no directions, so each code is one value per ROI. Grey levels come from ``--coarseGrayDepth`` binning, or are the raw intensities under ``--ibsi``. With a positive ``--coarseGrayDepth`` (the default), pixels of stored intensity 0, including every bounding-box pixel outside the ROI, are binned to grey level 1 and form zones; under ``--ibsi`` or a negative ``--coarseGrayDepth`` they are skipped.

Definitions, formulas and settings: :doc:`/Math/f_glszm`.

Expand Down
1 change: 0 additions & 1 deletion docs/source/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ ones are:
* Background pixels: with the default binning some families count background as grey level 1.
See :ref:`glrlm-possible-issues`, :ref:`glszm-possible-issues`, :ref:`gldzm-possible-issues` and
:ref:`ngtdm-possible-issues`.
* GLSZM: 2D zones are not fully 8-connected. See :ref:`glszm-possible-issues`.
* GLDZM: the distance next to background is one too large. See :ref:`gldzm-possible-issues`.
* NGLDM: grey levels are converted differently from the other texture families. See
:ref:`ngldm-possible-issues`.
Expand Down
60 changes: 22 additions & 38 deletions src/nyx/features/glszm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,49 +121,33 @@ void GLSZMFeature::calculate (LR& r, const Fsettings& s)
int zoneArea = 1;
D.yx(y, x) = VISITED;

// A zone is 8-connected, the connectivity IBSI defines for GLSZM and the one PyRadiomics and
// MIRP implement: the walk steps to all eight neighbours, so a zone that turns back up or
// to the left stays one zone.
static const int dxs[] = { 1, 1, 0, -1, -1, -1, 0, 1 },
dys[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
for (;;)
{
if (D.safe(y, x + 1) && D.yx(y, x + 1) != VISITED && D.yx(y, x + 1) == pi)
bool advanced = false;
for (int k = 0; k < 8; k++)
{
D.yx(y, x + 1) = VISITED;
zoneArea++;

// Remember this pixel
history.push_back({ x,y });
// Advance
x = x + 1;
// Proceed
continue;
}
if (D.safe(y + 1, x + 1) && D.yx(y + 1, x + 1) != VISITED && D.yx(y + 1, x + 1) == pi)
{
D.yx(y + 1, x + 1) = VISITED;
zoneArea++;

history.push_back({ x,y });
x = x + 1;
y = y + 1;
continue;
}
if (D.safe(y + 1, x) && D.yx(y + 1, x) != VISITED && D.yx(y + 1, x) == pi)
{
D.yx(y + 1, x) = VISITED;
zoneArea++;

history.push_back({ x,y });
y = y + 1;
continue;
int nx = x + dxs[k],
ny = y + dys[k];
if (D.safe(ny, nx) && D.yx(ny, nx) != VISITED && D.yx(ny, nx) == pi)
{
D.yx(ny, nx) = VISITED;
zoneArea++;

// Remember this pixel and advance to the neighbour
history.push_back({ x,y });
x = nx;
y = ny;
advanced = true;
break;
}
}
if (D.safe(y + 1, x - 1) && D.yx(y + 1, x - 1) != VISITED && D.yx(y + 1, x - 1) == pi)
{
D.yx(y + 1, x - 1) = VISITED;
zoneArea++;

history.push_back({ x,y });
x = x - 1;
y = y + 1;
if (advanced)
continue;
}

// Return from the branch
if (history.size() > 0)
Expand Down
Loading
Loading