# Voxel-based radiomic feature extraction

**URL:** <https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901>\
**Category:** Support\
**Tags:** python, radiomics, pyradiomics\
**Created:** [July 23, 2021, 9:02pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901 "2021-07-23T21:02:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 23, 2021, 9:02pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/1 "2021-07-23T21:02:22Z")

</div>

Hi everyone, I am doing radiomic feature extraction using voxel-based mode. Although I can get the feature maps (.nrrd files), I can’t find a way to get each voxel’s values (radiomic feature). I need those values in a .csv or .xls format to train a classification model. Could someone please help me?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 24, 2021, 12:58pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/2 "2021-07-24T12:58:21Z")

</div>

The feature maps are the per-voxel radiomics features. There are a lot of them so the binary nrrd format is more appropriate than a csv file would be. You can access the per-voxel values by working with the feature maps as a numpy array [like the examples here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-value-of-a-volume-at-specific-voxel-coordinates).

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 24, 2021, 7:31pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/3 "2021-07-24T19:31:29Z")

</div>

Hi Steve,  
Thank you for your quick reply. But, I need to extract all the voxel values (not just for a coordinate) of the feature map. With the code slicer.util.arrayFromVolume(volumeNode), I can get just some values. Any suggestion ?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 25, 2021, 2:39pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/4 "2021-07-25T14:39:25Z")

</div>

> [@smcch](#):
>
> With the code slicer.util.arrayFromVolume(volumeNode), I can get just some values.

I’m not sure what you mean here. The array will have one element for each voxel in the volume. If the volume is a feature map the element is the feature value for that voxel.

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 25, 2021, 3:02pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/5 "2021-07-25T15:02:49Z")

</div>

This is the script to obtain one feature (Entropy)

```auto
import numpy
import radiomics
from radiomics import featureextractor
import six
import sys, os
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
dataDir = '----my directory-----'
imageName = os.path.join(dataDir, 'brain.nii.gz')
maskName = os.path.join(dataDir, 'roi.nii.gz')
params = os.path.join(dataDir, 'Params.yaml')
extractor = featureextractor.RadiomicsFeatureExtractor(params)
extractor.disableAllFeatures() # disable all features
extractor.enableFeaturesByName(firstorder=['Entropy']) # Only enable firstorder Entropy

extractor.settings['initValue'] = 0 # Set the non-calculated voxels to 0

result = extractor.execute(imageName, maskName, voxelBased=True)

for key, val in six.iteritems(result):
  if isinstance(val, sitk.Image):
    parametermap = sitk.GetArrayFromImage(val)
    parametermap[np.isnan(parametermap)] = 0

plt.imshow(parametermap[15, :, :])
plt.show()
print(parametermap)

```

 ![entropy](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/5/35969559762731d7e5084460a1cde9de1494d2a8.png)

```auto
[[[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]

 [[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]

 [[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]

 ...

 [[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]

 [[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]

 [[0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  ...
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]
  [0. 0. 0. ... 0. 0. 0.]]]

Process finished with exit code 0

```

As you see, I got only 0 values…  
I tried to save the array to csv, but It seem not to be compatible with 3D arrays. So, I reshaped the array to 3D, but It doesn’t work…

The map looks fine, but I need the values of every voxel from all the radiomic features

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 25, 2021, 3:56pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/6 "2021-07-25T15:56:18Z")

</div>

Ah, that’s because numpy shortens the output by only showing the first and last values, so for volumes with some padding it often looks empty but isn’t. Try `print(parametermap.mean())` or `print(parametermap.max())` instead.

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 25, 2021, 5:51pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/7 "2021-07-25T17:51:16Z")

</div>

Yes, with .mean = 0.5405715539212325, and with .max = 4.095795255000928. But, I need the entire array of the volume (voxel by voxel) in a table format.  
Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 25, 2021, 6:44pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/8 "2021-07-25T18:44:54Z")

</div>

Hmm, that’s going to be a long table, but if that’s what you need then you can just make a nested loop through all the slice, row, column indices and save the corresponding values into a table. You can get the dimensions from the array’s `shape` property. You might use a [csv writer](https://docs.python.org/3/library/csv.html).

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 26, 2021, 8:47am UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/9 "2021-07-26T08:47:35Z")

</div>

Thanks Steve, but I can’t get a proper table, there is a problem with 3D array type. I wonder if there is a “clean” way to do what I’m looking for…

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [July 26, 2021, 9:18am UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/10 "2021-07-26T09:18:20Z")

</div>

By the way, with np.set\_printoptions(threshold=np.inf) I get all values from 1 slice of my volume

---

<div class="post-metadata">

**Author:** ![Rongjun\_Dong](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rongjun_dong/32/15766_2.png) [@Rongjun\_Dong](https://discourse.slicer.org/u/Rongjun_Dong)\
**Post date:** [June 23, 2022, 7:39pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/11 "2022-06-23T19:39:53Z")

</div>

Hi, did you solve this problem please. I recently had this one problem too and would like some advice.

---

<div class="post-metadata">

**Author:** ![Ywatana29](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ywatana29/32/9178_2.png) [@Ywatana29](https://discourse.slicer.org/u/Ywatana29)\
**Post date:** [June 2, 2024, 8:23pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/12 "2024-06-02T20:23:18Z")

</div>

Have we solved this problem? I need the answer. My question is related. How do I write voxel-based feature map data in a nrrd file, which I can import to 3D slicer?

---

<div class="post-metadata">

**Author:** ![smcch](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smcch/32/11763_2.png) [@smcch](https://discourse.slicer.org/u/smcch)\
**Post date:** [June 3, 2024, 12:00pm UTC](https://discourse.slicer.org/t/voxel-based-radiomic-feature-extraction/18901/13 "2024-06-03T12:00:33Z")

</div>

Altought Pyradiomics offers the option to save the feature maps as nrrd. The best solution I found is to extract the features and convert them directly into an array while keeping the spatial information from the source segmentation (mask). Please check out this repo [GitHub - smcch/Predicting\_Glioblastoma\_Recurrence\_MRI](https://github.com/smcch/Predicting_Glioblastoma_Recurrence_MRI/tree/main)
