Skip to content

Commit

Permalink
ENH: Add pandas example
Browse files Browse the repository at this point in the history
Add an example to show how pandas can be used to handle data returned by PyRadiomics.
Pandas is not needed to handle data inside PyRadiomics, and therefore does not have to be a dependency.
The result from pyradiomics can be converted to pandas by using it to instantiate a pandas Series.

The example shows how to use pandas for batchprocessing and storing the result, which users can adapt to suit their own needs.
  • Loading branch information
JoostJM committed Mar 29, 2017
1 parent e7e2fe6 commit c5fab34
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions bin/batchExamples/batchProcessingPandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

from __future__ import print_function

import logging
import os

import pandas
import SimpleITK as sitk

import radiomics
from radiomics import featureextractor


def main():
outPath = r'E:\Git-Repos\PyRadiomicsNKI\pyradiomics-API'

inputCSV = outPath + os.path.sep + "TestCases.csv"
outputFilepath = outPath + os.path.sep + "radiomics_features.csv"
progress_filename = outPath + os.path.sep + "pyrad_log.txt"

# Configure logging
rLogger = logging.getLogger('radiomics')

# Set logging level
# rLogger.setLevel(logging.INFO) # Not needed, default log level of logger is INFO

# Create handler for writing to log file
handler = logging.FileHandler(filename=progress_filename, mode='w')
handler.setFormatter(logging.Formatter("%(levelname)s:%(name)s: %(message)s"))
rLogger.addHandler(handler)

# Initialize logging for batch log messages
logger = rLogger.getChild('batch')

# Set verbosity level for output to stderr (default level = WARNING)
radiomics.setVerbosity(logging.INFO)

logger.info('Loading CSV')

# ####### Up to this point, this script is equal to the 'regular' batchprocessing script ########

try:
# Use pandas to read and transpose ('.T') the input data
# The transposition is needed so that each column represents one test case. This is easier for iteration over
# the input cases
flists = pandas.read_csv(inputCSV).T
except Exception:
logging.error('CSV READ FAILED', exc_info=True)
exit(-1)

logging.info('Loading Done')
logging.info('Patients: %d', len(flists))

kwargs = {}
kwargs['binWidth'] = 25
kwargs['resampledPixelSpacing'] = None # [3,3,3]
kwargs['interpolator'] = sitk.sitkBSpline
kwargs['enableCExtensions'] = True

logger.info('pyradiomics version: %s', radiomics.__version__)
logger.info('Extracting features with kwarg settings: %s', str(kwargs))

extractor = featureextractor.RadiomicsFeaturesExtractor(**kwargs)
# extractor.enableInputImages(Original={}) # Original enabled by default
# extractor.enableInputImages(wavelet= {'level': 2})

# Instantiate a pandas data frame to hold the results of all patients
results = pandas.DataFrame()

for entry in flists: # Loop over all columns (i.e. the test cases)
logger.info("(%d/%d) Processing Patient (Image: %s, Mask: %s)",
entry + 1,
len(flists),
flists[entry]['Image'],
flists[entry]['Mask'])

imageFilepath = flists[entry]['Image']
maskFilepath = flists[entry]['Mask']

if (imageFilepath is not None) and (maskFilepath is not None):
featureVector = flists[entry] # This is a pandas Series
featureVector['Image'] = os.path.basename(imageFilepath)
featureVector['Mask'] = os.path.basename(maskFilepath)

try:
# PyRadiomics returns the result as an ordered dictionary, which can be easily converted to a pandas Series
# The keys in the dictionary will be used as the index (labels for the rows), with the values of the features
# as the values in the rows.
result = pandas.Series(extractor.execute(imageFilepath, maskFilepath))
featureVector = featureVector.append(result)
except Exception:
logger.error('FEATURE EXTRACTION FAILED:', exc_info=True)

# To add the calculated features for this case to our data frame, the series must have a name (which will be the
# name of the column.
featureVector.name = entry
# By specifying an 'outer' join, all calculated features are added to the data frame, including those not
# calculated for previous cases. This also ensures we don't end up with an empty frame, as for the first patient
# it is 'joined' with the empty data frame.
results = results.join(featureVector, how='outer') # If feature extraction failed, results will be all NaN

logger.info('Extraction complete, writing CSV')
# .T transposes the data frame, so that each line will represent one patient, with the extracted features as columns
results.T.to_csv(outputFilepath, index=False, na_rep='NaN')
logger.info('CSV writing complete')

if __name__ == '__main__':
main()

0 comments on commit c5fab34

Please sign in to comment.