Usage¶
Example¶
- PyRadiomics example code and data is available in the Github repository
- The sample sample data is provided in
pyradiomics/data
. - Use jupyter to run the helloRadiomics example, located in
pyradiomics/bin/Notebooks
. - If jupyter is not installed, run the python script alternative (
pyradiomics/bin/helloRadiomics.py
):python helloRadiomics.py
Command Line Use¶
PyRadiomics has 2 commandline scripts,
pyradiomics
is for single image feature extraction andpyradiomicsbatch
is for feature extraction from a batch of images and segmentations.Both scripts can be run directly from a command line window, anywhere in your system.
To extract features from a single image and segmentation run:
pyradiomics <path/to/image> <path/to/segmentation>
To extract features from a batch run:
pyradiomicsbatch <path/to/input> <path/to/output>
The input file for batch processing is a CSV file where each row represents one combination of an image and a segmentation and contains 5 elements: 1) patient ID, 2) sequence name (image identifier), 3) reader (segmentation identifier), 4) path/to/image, 5) path/to/mask.
For more information on passing parameter files, setting up logging and controlling output format, run:
pyradiomics -h pyradiomicsbatch -h
Interactive Use¶
(LINUX) Add pyradiomics to the environment variable PYTHONPATH:
setenv PYTHONPATH /path/to/pyradiomics/radiomics
Start the python interactive session:
python
Import the necessary classes:
from radiomics import featureextractor import sys, os
Set up a pyradiomics directory variable:
dataDir = '/path/to/pyradiomics'
You will find sample data files brain1_image.nrrd and brain1_label.nrrd in that directory.
Store the path of your image and mask in two variables:
imageName = os.path.join(dataDir, "data", 'brain1_image.nrrd') maskName = os.path.join(dataDir, "data", 'brain1_label.nrrd')
Also store the path to the file containing the extraction settings:
params = os.path.join(dataDir, "bin", "Params.yaml")
Instantiate the feature extractor class with the parameter file:
extractor = featureextractor.RadiomicsFeaturesExtractor(params)
Calculate the features:
result = extractor.execute(imageName, maskName) for key, val in result.iteritems(): print "\t%s: %s" %(key, val)
See the feature extractor class for more information on using this core class.
PyRadiomics in 3D Slicer¶
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 example.
(LINUX) Add pyradiomics to the environment variable PYTHONPATH:
setenv PYTHONPATH /path/to/pyradiomics/radiomics
Start the python interactive session:
python
Import the necessary classes:
from radiomics import firstorder, glcm, imageoperations, shape, glrlm, glszm import SimpleITK as sitk import sys, os
Set up a data directory variable:
dataDir = '/path/to/pyradiomics/data'
You will find sample data files brain1_image.nrrd and brain1_label.nrrd in that directory.
Use SimpleITK to read a the brain image and mask:
imageName = str(dataDir + os.path.sep + 'brain1_image.nrrd') maskName = str(dataDir + os.path.sep + 'brain1_label.nrrd') image = sitk.ReadImage(imageName) mask = sitk.ReadImage(maskName)
Calculate the first order features:
firstOrderFeatures = firstorder.RadiomicsFirstOrder(image,mask) firstOrderFeatures.calculateFeatures() for (key,val) in firstOrderFeatures.featureValues.iteritems(): print "\t%s: %s" % (key, val)
See the radomics package for more features that you can calculate.