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.
import os # needed navigate the system to get the input data
from radiomics import featureextractor # This module is used for interaction with pyradiomics
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
# 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)
Now that we have our input, we need to define the parameters and instantiate the extractor. For this there are three possibilities:
Use defaults, don't define custom settings
Define parameters in a dictionary, control filters and features after initialisation
Use a parameter file
# 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
# 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
# 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
# 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
# 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
Now that we have our extractor set up with the correct parameters, we can start extracting features:
result = extractor.execute(imagePath, labelPath)
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