# Usage¶

## Example¶

• PyRadiomics example code and data is available in the Github repository
• The sample sample data is provided in pyradiomics/data
• Jupyter can also be used to run the example notebook as shown in the instruction video
• The example notebook can be found in pyradiomics/examples/Notebooks
• The parameter file used in the instruction video is available in pyradiomics/examples/exampleSettings
• If jupyter is not installed, run the python script alternatives contained in the folder (pyradiomics/examples):
• python helloVoxel.py (voxel-based extraction)

## Voxel-based extraction¶

As of version 2.0, pyradiomics also implements a voxel-based extraction. It is both available from the command line and in the interactive use. See below for details.

Important to know here is that this extraction takes longer (features have to be calculated for each voxel), and that the output is a SimpleITK image of the parameter map instead of a float value for each feature.

## Command Line Use¶

• PyRadiomics can be used directly from the commandline via the entry point pyradiomics. Depending on the input provided, PyRadiomics is run in either single-extraction or batch-extraction mode.

• To extract features from a single image and segmentation run:

• To extract features from a batch run:

• The input file for batch processing is a CSV file where the first row is contains headers and each subsequent row represents one combination of an image and a segmentation and contains at least 2 elements: 1) path/to/image, 2) path/to/mask. The headers specify the column names and must be “Image” and “Mask” for image and mask location, respectively (capital sensitive). Additional columns may also be specified, all columns are copied to the output in the same order (with calculated features appended after last column). To specify custom values for label in each combination, a column “Label” can optionally be added, which specifies the desired extraction label for each combination. Values specified in this column take precedence over label values specified in the parameter file or on the commandline. If a row contains no value, the default (or globally customized) value is used instead. Similarly, an optional value for the label_channel setting can be provided in a column “Label_channel”.

Note

All headers should be unique and different from headers provided by PyRadiomics (<filter>_<class>_<feature>). In case of conflict, values are overwritten by the PyRadiomics values.

• By default, results are printed out to the console window. To store the results in a CSV-structured text file, add the -o <PATH> and -f csv arguments, where <PATH> specifies the filepath where the results should be stored. e.g.:

pyradiomics <path/to/image> <path/to/segmentation> -o results.csv -f csv
pyradiomics <path/to/input> -o results.csv -f csv

• Extraction can be customized by specifying a parameter file <radiomics-parameter-file-label> in the --param argument and/or by specifying override settings (only type 3 customization <radiomics-settings-label>) in the --setting argument. Multiple overrides can be used by specifying --setting multiple times.

• To extract feature maps (“voxel-based” extraction), simply add the argument --mode voxel. The calculated feature maps are then stored as images (NRRD format) in the current working directory. The name convention used is “Case-<idx>_<FeatureName>.nrrd”. An alternative output directory can be provided in the --out-dir command line switch. The results that are printed to the console window or the out file will still contain the diagnostic information, and the value of the extracted features is set to the location the feature maps are stored.

## Interactive Use¶

• (LINUX) To run from source code, add pyradiomics to the environment variable PYTHONPATH (Not necessary when PyRadiomics is installed):

• Start the python interactive session:

• python
• Import the necessary classes:

import os

import SimpleITK as sitk
import six

• Set up a pyradiomics directory variable:

• You will find sample data files brain1_image.nrrd and brain1_label.nrrd in that directory. Note that NRRD format used here does not mean that your image and label must always be in this format. Any format readable by ITK is suitable (e.g., NIfTI, MHA, MHD, HDR, etc). See more details in this section of FAQ https://pyradiomics.readthedocs.io/en/latest/faq.html#what-file-types-are-supported-by-pyradiomics-for-input-image-and-mask_.

• Store the path of your image and mask in two variables:

• Also store the path to the file containing the extraction settings:

params = os.path.join(dataDir, "examples", "exampleSettings", "Params.yaml")

• Instantiate the feature extractor class with the parameter file:

• Calculate the features (segment-based):

for key, val in six.iteritems(result):
print("\t%s: %s" %(key, val))

• Calculate the features (voxel-based):

for key, val in six.iteritems(result):
if isinstance(val, sitk.Image):  # Feature map
sitk.WriteImage(val, key + '.nrrd', True)
print("Stored feature %s in %s" % (key, key + ".nrrd"))
else:  # Diagnostic information
print("\t%s: %s" %(key, val))

• See the feature extractor class for more information on using this core class.

A convenient front-end interface is provided as the ‘Radiomics’ extension for 3D Slicer. It is available here.

## Using feature classes directly¶

• This represents an example where feature classes are used directly, circumventing checks and preprocessing done by the radiomics feature extractor class, and is not intended as standard use.

• (LINUX) To run from source code, add pyradiomics to the environment variable PYTHONPATH (Not necessary when PyRadiomics is installed):

• Start the python interactive session:

• python
• Import the necessary classes:

from radiomics import firstorder, glcm, imageoperations, shape, glrlm, glszm, getTestCase
import SimpleITK as sitk
import six
import sys, os

• Set up a data directory variable:

• You will find sample data files brain1_image.nrrd and brain1_label.nrrd in that directory.

• Calculate the first order features:

firstOrderFeatures.enableAllFeatures()  # On the feature class level, all features are disabled by default.
firstOrderFeatures.calculateFeatures()
for (key,val) in six.iteritems(firstOrderFeatures.featureValues):
print("\t%s: %s" % (key, val))

• See the Radiomic Features section for more features that you can calculate.

## Setting Up Logging¶

PyRadiomics features extensive logging to help track down any issues with the extraction of features. By default PyRadiomics logging reports messages of level WARNING and up (reporting any warnings or errors that occur), and prints this to the output (stderr). By default, PyRadiomics does not create a log file.

To change the amount of information that is printed to the output, use setVerbosity() in interactive use and the optional --verbosity argument in commandline use.

When using PyRadiomics in interactive mode, enable storing the PyRadiomics logging in a file by adding an appropriate handler to the pyradiomics logger: