# Tracking how the values in GLCM, GLSZM matrices are populated

**URL:** <https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808>\
**Category:** Radiomics\
**Created:** [October 2, 2020, 1:07am UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808 "2020-10-02T01:07:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankita\_Singh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ankita_singh/32/6068_2.png) [@Ankita\_Singh](https://discourse.slicer.org/u/Ankita_Singh)\
**Post date:** [October 2, 2020, 1:07am UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/1 "2020-10-02T01:07:41Z")

</div>

Hello,

I am interested in understanding how the values in GLCM and GLSZM are computed for each gray level. I know that there are C++ functions calculate\_glcm and calculate\_glszm that compute these matrices. Using these functions and adding some more lines of code, can I, for example, track step-by-step how size zones are calculated for each gray level in the GLSZM? Is it possible?

Thank you  
Ankita

---

<div class="post-metadata">

**Author:** ![JoostJM](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joostjm/32/1091_2.png) [@JoostJM](https://discourse.slicer.org/u/JoostJM)\
**Post date:** [October 10, 2020, 1:36pm UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/2 "2020-10-10T13:36:30Z")

</div>

The actual algortihm filling the matrices of the various feature classes is programmed into the C-extension of PyRadiomics. In the earliest versions, we did have python implementations, but due to unsatisfactory performance we have since moved to C-only calculation of the matrices.

Depending on your specific aim, there are several possibilities. If you need to influence the process or extract intermediate results, you can revisit the history of the repository, extracting the python implementations of the matrix calucaltion (though this does not support voxel-based radiomics).  
Moreover, the python algorithms were slightly different to make optimum use of optimized numpy calls.

However, if you are seeking a conceptual view of how the matrices are filled, I suggest to start with the C-source code included in the repository. Here you have 2 options. Option 1 is to read the current version, which is more complex to allow for voxel-based calculation and to support both 2D and 3D input. Alternatively, you can read the C-extensions of an earlier version, which required 3D input and could therefore have less complex algorithms. The last version of PyRadiomics with this earlier version of the C-extensions can be found [here](https://github.com/Radiomics/pyradiomics/tree/12b512fc5fc7e8e317273ddd4b72552695380676).

---

<div class="post-metadata">

**Author:** ![JoostJM](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joostjm/32/1091_2.png) [@JoostJM](https://discourse.slicer.org/u/JoostJM)\
**Post date:** [October 10, 2020, 1:45pm UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/3 "2020-10-10T13:45:51Z")

</div>

If you have a need of some specific intermediate information, we may be able to help give you some pointers on how to implement this if necessary.

---

<div class="post-metadata">

**Author:** ![Ankita\_Singh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ankita_singh/32/6068_2.png) [@Ankita\_Singh](https://discourse.slicer.org/u/Ankita_Singh)\
**Post date:** [October 11, 2020, 9:31pm UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/4 "2020-10-11T21:31:56Z")

</div>

Hi Joost,

Thank you very much for your help. I will look into the two options and see which one works best for my case.

I had one question from what you mentioned above:

> we did have python implementations, but due to unsatisfactory performance we have since moved to C-only calculation of the matrices.

What do you exactly mean by ‘unsatisfactory performance’? Is that in terms of computational efficiency?

---

<div class="post-metadata">

**Author:** ![Ankita\_Singh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ankita_singh/32/6068_2.png) [@Ankita\_Singh](https://discourse.slicer.org/u/Ankita_Singh)\
**Post date:** [October 14, 2020, 7:51am UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/5 "2020-10-14T07:51:01Z")

</div>

Hi Joost,

In the [cmatrices.c](https://github.com/Radiomics/pyradiomics/blob/master/radiomics/src/cmatrices.c) file, there is a function [calculate\_glszm](https://github.com/Radiomics/pyradiomics/blob/master/radiomics/src/cmatrices.c#L94) which, as I understand, calculates the size zones for each gray-level and this is stored in temp\_data. Later, the function [fill\_glszm](https://github.com/Radiomics/pyradiomics/blob/master/radiomics/src/cmatrices.c#L281) is used to create glszm. I couldn’t locate how fill\_glszm is called in the C implementation. Could you please let me know how that is done?

Thank you.

---

<div class="post-metadata">

**Author:** ![JoostJM](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joostjm/32/1091_2.png) [@JoostJM](https://discourse.slicer.org/u/JoostJM)\
**Post date:** [November 18, 2020, 6:54pm UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/6 "2020-11-18T18:54:14Z")

</div>

It’s called here: [https://github.com/Radiomics/pyradiomics/blob/master/radiomics/src/\_cmatrices.c#L437](https://github.com/Radiomics/pyradiomics/blob/master/radiomics/src/_cmatrices.c#L437)

---

<div class="post-metadata">

**Author:** ![JoostJM](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joostjm/32/1091_2.png) [@JoostJM](https://discourse.slicer.org/u/JoostJM)\
**Post date:** [November 18, 2020, 6:56pm UTC](https://discourse.slicer.org/t/tracking-how-the-values-in-glcm-glszm-matrices-are-populated/13808/7 "2020-11-18T18:56:31Z")

</div>

Yes, when comparing time required for matrix calculation in C and in Python only, there was a 300-1000 fold faster calculation in C

note: this only applies to the actual calculation of the matrices. Though overall the time gain was less dramatic, it was still quite significant (in the order of: “instead of a week it just took a couple of minutes”)
