Example of using the PyRadiomics toolbox in Python

First, import some built-in Python modules needed to get our testing data. Second, import the toolbox, only the featureextractor is needed, this module handles the interaction with other parts of the toolbox.

In [1]:
import os  # needed navigate the system to get the input data

from radiomics import featureextractor  # This module is used for interaction with pyradiomics

Setting up data

Before we can extract features, we need to get the input data, define the parameters for the extraction and instantiate the class contained within featureextractor.

In the next cell we get our testing data, this consists of an image and corresponding segmentation. This is also available from the PyRadiomics repository and is stored in \pyradiomics\data, whereas this file (and therefore, the current directory) is \pyradiomics\bin\Notebooks

In [2]:
# Define the testcase name
testCase = 'brain1'

# Get the relative path to pyradiomics\data
# os.cwd() returns the current working directory
# ".." points to the parent directory: \pyradiomics\bin\Notebooks\..\ is equal to \pyradiomics\bin\
# Move up 2 directories (i.e. go to \pyradiomics\) and then move into \pyradiomics\data
dataDir = os.path.join(os.getcwd(), "..", "..", "data")
print "dataDir, relative path:", dataDir
print "dataDir, absolute path:", os.path.abspath(dataDir)

# Store the file paths of our testing image and label map into two variables
imagePath = os.path.join(dataDir, testCase + "_image.nrrd")
labelPath = os.path.join(dataDir, testCase + "_label.nrrd")

# Additonally, store the location of the example parameter file, stored in \pyradiomics\bin
paramPath = os.path.join(os.getcwd(), "..", "Params.yaml")
print "Parameter file, absolute path:", os.path.abspath(paramPath)
dataDir, relative path: E:\Git-Repos\pyradiomics\bin\Notebooks\..\..\data
dataDir, absolute path: E:\Git-Repos\pyradiomics\data
Parameter file, absolute path: E:\Git-Repos\pyradiomics\bin\Params.yaml

Instantiating the extractor

Now that we have our input, we need to define the parameters and instantiate the extractor. For this there are three possibilities:

  1. Use defaults, don't define custom settings

  2. Define parameters in a dictionary, control filters and features after initialisation

  3. Use a parameter file

Method 1, use defaults
In [3]:
# Instantiate the extractor
extractor = featureextractor.RadiomicsFeaturesExtractor()

print "Extraction parameters:\n\t", extractor.kwargs
print "Enabled filters:\n\t", extractor.inputImages
print "Enabled features:\n\t", extractor.enabledFeatures
Extraction parameters:
	{'resampledPixelSpacing': None, 'interpolator': 3, 'verbose': False, 'padDistance': 5, 'label': 1}
Enabled filters:
	{'Original': {}}
Enabled features:
	{'firstorder': [], 'glcm': [], 'shape': [], 'glrlm': [], 'glszm': []}
Method 2, hard-coded settings:
In [4]:
# First define the parameters
params = {}
params['binWidth'] = 20
params['sigma'] = [1, 2, 3]
params['verbose'] = True

# Instantiate the extractor
extractor = featureextractor.RadiomicsFeaturesExtractor(**params)  # ** 'unpacks' the dictionary in the function call

print "Extraction parameters:\n\t", extractor.kwargs
print "Enabled filters:\n\t", extractor.inputImages  # Still the default settings
print "Enabled features:\n\t", extractor.enabledFeatures  # Still the default settings
Extraction parameters:
	{'verbose': True, 'binWidth': 20, 'label': 1, 'interpolator': 3, 'resampledPixelSpacing': None, 'sigma': [1, 2, 3], 'padDistance': 5}
Enabled filters:
	{'Original': {}}
Enabled features:
	{'firstorder': [], 'glcm': [], 'shape': [], 'glrlm': [], 'glszm': []}
In [5]:
# This cell is equivalent to the previous cell
extractor = featureextractor.RadiomicsFeaturesExtractor(binWidth=20, sigma=[1, 2, 3], verbose=True)  # Equivalent of code above

print "Extraction parameters:\n\t", extractor.kwargs
print "Enabled filters:\n\t", extractor.inputImages  # Still the default settings
print "Enabled features:\n\t", extractor.enabledFeatures  # Still the default settings
Extraction parameters:
	{'verbose': True, 'binWidth': 20, 'label': 1, 'interpolator': 3, 'resampledPixelSpacing': None, 'sigma': [1, 2, 3], 'padDistance': 5}
Enabled filters:
	{'Original': {}}
Enabled features:
	{'firstorder': [], 'glcm': [], 'shape': [], 'glrlm': [], 'glszm': []}
In [6]:
# Enable a filter (in addition to the 'Original' filter already enabled)
extractor.enableInputImageByName('LoG')
print ""
print "Enabled filters:\n\t", extractor.inputImages

# Disable all feature classes, save firstorder
extractor.disableAllFeatures()
extractor.enableFeatureClassByName('firstorder')
print ""
print "Enabled features:\n\t", extractor.enabledFeatures

# Specify some additional features in the GLCM feature class
extractor.enableFeaturesByName(glcm=['Autocorrelation', 'Homogeneity1', 'SumSquares'])
print ""
print "Enabled features:\n\t", extractor.enabledFeatures
Enabled filters:
	{'Original': {}, 'LoG': {}}

Enabled features:
	{'firstorder': []}

Enabled features:
	{'firstorder': [], 'glcm': ['Autocorrelation', 'Homogeneity1', 'SumSquares']}
Method 3, using a parameter file
In [7]:
# Instantiate the extractor
extractor = featureextractor.RadiomicsFeaturesExtractor(paramPath)

print "Extraction parameters:\n\t", extractor.kwargs
print "Enabled filters:\n\t", extractor.inputImages
print "Enabled features:\n\t", extractor.enabledFeatures
Extraction parameters:
	{'verbose': True, 'binWidth': 25, 'label': 1, 'interpolator': 'sitkBSpline', 'resampledPixelSpacing': None, 'weightingNorm': None, 'padDistance': 5}
Enabled filters:
	{'Original': {}}
Enabled features:
	{'firstorder': [], 'glcm': None, 'shape': None, 'glrlm': None, 'glszm': None}

Extract features

Now that we have our extractor set up with the correct parameters, we can start extracting features:

In [8]:
result = extractor.execute(imagePath, labelPath)
		Computing shape
		Computing firstorder
calculate GLCM: 100%|██████████████████████████████████████████████████████████████████| 33/33 [00:00<00:00, 51.08it/s]
		Computing glcm
		Computing glrlm
calculate GLSZM: 100%|█████████████████████████████████████████████████████████████████| 33/33 [00:00<00:00, 71.43it/s]
		Computing glszm
In [9]:
print "Result type:", type(result)  # result is returned in a Python ordered dictionary
print ""
print "Calculated features"
for key, value in result.iteritems():
    print "\t", key, ":", value
Result type: <class 'collections.OrderedDict'>

Calculated features
	general_info_BoundingBox : (162; 84; 11; 47; 70; 7)
	general_info_GeneralSettings : {'verbose': True; 'binWidth': 25; 'label': 1; 'interpolator': 'sitkBSpline'; 'resampledPixelSpacing': None; 'weightingNorm': None; 'padDistance': 5}
	general_info_ImageHash : 5c9ce3ca174f0f8324aa4d277e0fef82dc5ac566
	general_info_ImageSpacing : (0.7812499999999999; 0.7812499999999999; 6.499999999999998)
	general_info_InputImages : {'Original': {}}
	general_info_MaskHash : 9dc2c3137b31fd872997d92c9a92d5178126d9d3
	general_info_Version : v1.0.post17.dev0+g1b4c5aa
	general_info_VolumeNum : 2
	general_info_VoxelNum : 4137
	original_shape_Maximum3DDiameter : 65.5366145873
	original_shape_Compactness2 : 0.114127701901
	original_shape_Maximum2DDiameterSlice : 47.2187913633
	original_shape_Sphericity : 0.485061744222
	original_shape_Compactness1 : 26.7546787215
	original_shape_Elongation : 1.7789885567
	original_shape_SurfaceVolumeRatio : 0.392308261863
	original_shape_Volume : 16412.6586914
	original_shape_Flatness : 1.21918505897
	original_shape_SphericalDisproportion : 2.06159321347
	original_shape_Roundness : 0.61469066615
	original_shape_SurfaceArea : 6438.82160378
	original_shape_Maximum2DDiameterColumn : 44.5487904052
	original_shape_Maximum2DDiameterRow : 61.5801767135
	original_firstorder_InterquartileRange : 253.0
	original_firstorder_Skewness : 0.275650859086
	original_firstorder_Uniformity : 0.0451569635559
	original_firstorder_MeanAbsoluteDeviation : 133.447261953
	original_firstorder_Energy : 33122817481.0
	original_firstorder_RobustMeanAbsoluteDeviation : 103.00138343
	original_firstorder_Median : 812.0
	original_firstorder_TotalEnergy : 131407662126.0
	original_firstorder_Maximum : 1266.0
	original_firstorder_RootMeanSquared : 2829.57282108
	original_firstorder_90Percentile : 1044.4
	original_firstorder_Minimum : 468.0
	original_firstorder_Entropy : 4.6019355539
	original_firstorder_StandardDeviation : 156.611235894
	original_firstorder_Range : 798.0
	original_firstorder_Variance : 24527.0792084
	original_firstorder_10Percentile : 632.0
	original_firstorder_Kurtosis : 2.18077293939
	original_firstorder_Mean : 825.235436307
	original_glcm_SumVariance : 895.891808819
	original_glcm_Homogeneity1 : 0.276140402104
	original_glcm_Homogeneity2 : 0.189156155892
	original_glcm_ClusterShade : -52.9707943386
	original_glcm_MaximumProbability : 0.00792784235012
	original_glcm_Idmn : 0.957796447609
	original_glcm_SumVariance2 : 103.142793792
	original_glcm_Contrast : 52.2310659277
	original_glcm_DifferenceEntropy : 3.79686113536
	original_glcm_InverseVariance : 0.188666637795
	original_glcm_Entropy : 8.79428086119
	original_glcm_Dissimilarity : 5.58932678922
	original_glcm_DifferenceVariance : 17.6107741076
	original_glcm_Idn : 0.866370546902
	original_glcm_Idm : 0.189156155892
	original_glcm_Correlation : 0.335214788202
	original_glcm_Autocorrelation : 292.684050471
	original_glcm_SumEntropy : 5.31547876648
	original_glcm_AverageIntensity : 17.1242601309
	original_glcm_Energy : 0.00290880217681
	original_glcm_SumSquares : 39.9781084143
	original_glcm_ClusterProminence : 26251.1709801
	original_glcm_SumAverage : 33.4497492152
	original_glcm_Imc2 : 0.692033706271
	original_glcm_Imc1 : -0.091940840043
	original_glcm_DifferenceAverage : 5.58932678922
	original_glcm_Id : 0.276140402104
	original_glcm_ClusterTendency : 103.142793792
	original_glrlm_ShortRunLowGrayLevelEmphasis : 0.00822976624416
	original_glrlm_GrayLevelVariance : 39.118151022
	original_glrlm_LowGrayLevelRunEmphasis : 0.00860039789166
	original_glrlm_GrayLevelNonUniformityNormalized : 0.0451412381498
	original_glrlm_RunVariance : 0.0847945778959
	original_glrlm_GrayLevelNonUniformity : 175.635192315
	original_glrlm_LongRunEmphasis : 1.22684403826
	original_glrlm_ShortRunHighGrayLevelEmphasis : 268.974179841
	original_glrlm_RunLengthNonUniformity : 3500.04323157
	original_glrlm_ShortRunEmphasis : 0.955939173141
	original_glrlm_LongRunHighGrayLevelEmphasis : 341.286579098
	original_glrlm_RunPercentage : 0.940406463249
	original_glrlm_LongRunLowGrayLevelEmphasis : 0.0106011704787
	original_glrlm_RunEntropy : 4.91503800316
	original_glrlm_HighGrayLevelRunEmphasis : 281.066493909
	original_glrlm_RunLengthNonUniformityNormalized : 0.895049465948
	original_glszm_GrayLevelVariance : 40.6031399239
	original_glszm_LowIntensityLargeAreaEmphasis : 0.127238415533
	original_glszm_HighIntensitySmallAreaEmphasis : 193.438051926
	original_glszm_SmallAreaEmphasis : 0.656447899959
	original_glszm_LargeAreaEmphasis : 13.6155080214
	original_glszm_ZoneVariance : 8.72123909749
	original_glszm_SizeZoneVariabilityNormalized : 0.399784380451
	original_glszm_LowIntensitySmallAreaEmphasis : 0.0064169820551
	original_glszm_HighIntensityEmphasis : 288.623529412
	original_glszm_IntensityVariabilityNormalized : 0.0440573079013
	original_glszm_ZonePercentage : 0.4520183708
	original_glszm_LowIntensityEmphasis : 0.00910094202771
	original_glszm_SizeZoneVariability : 747.596791444
	original_glszm_IntensityVariability : 82.3871657754
	original_glszm_ZoneEntropy : 6.5082149862
	original_glszm_HighIntensityLargeAreaEmphasis : 3514.76149733