Skip to content

Commit

Permalink
TEST: Use different Notebooks while testing in CircleCI
Browse files Browse the repository at this point in the history
Update the config.yml to test the hello* notebooks and the PyRadiomicsExample.ipynb, which more extensively tests different parts of the toolbox and different ways the toolbox can be used.
  • Loading branch information
JoostJM committed May 11, 2017
1 parent b668158 commit 9ef7ea0
Show file tree
Hide file tree
Showing 19 changed files with 863 additions and 447 deletions.
12 changes: 2 additions & 10 deletions .circleci/config.yml
Expand Up @@ -38,20 +38,12 @@ jobs:
command: |
source activate root
nosetests
- run:
name: Install test data
command: |
cp -r data /tmp/example_data
cp examples/exampleSettings/Params.yaml /tmp
cp -r examples/Notebooks/*.ipynb /tmp
# helloFeatureClass is not ready for inclusion yet, so isn't being tested
rm -f /tmp/hello*.ipynb
- run:
name: test notebooks in python 2 and 3
command: |
jupyter nbconvert --ExecutePreprocessor.kernel_name=python2 --ExecutePreprocessor.timeout=-1 --to notebook --execute /tmp/RadiomicsExample.ipynb /tmp/FeatureVisualization.ipynb /tmp/FeatureVisualizationWithClustering.ipynb /tmp/FilteringEffects.ipynb
jupyter nbconvert --ExecutePreprocessor.kernel_name=python2 --ExecutePreprocessor.timeout=-1 --to notebook --output-dir /tmp --execute notebooks/HelloRadiomics.ipynb notebooks/HelloFeatureClass.ipynb notebooks/PyRadiomicsExample.ipynb
jupyter nbconvert --ExecutePreprocessor.kernel_name=python3 --ExecutePreprocessor.timeout=-1 --to notebook --execute /tmp/RadiomicsExample.ipynb /tmp/FeatureVisualization.ipynb /tmp/FeatureVisualizationWithClustering.ipynb /tmp/FilteringEffects.ipynb
jupyter nbconvert --ExecutePreprocessor.kernel_name=python3 --ExecutePreprocessor.timeout=-1 --to notebook --output-dir /tmp --execute notebooks/HelloRadiomics.ipynb notebooks/HelloFeatureClass.ipynb notebooks/PyRadiomicsExample.ipynb
10 changes: 5 additions & 5 deletions Dockerfile
Expand Up @@ -28,11 +28,11 @@ RUN /bin/bash -c "source activate python2 \

# Install sample data and notebooks
ADD data/ /home/jovyan/work/example_data/
ADD bin/Notebooks/RadiomicsExample.ipynb /home/jovyan/work/
ADD bin/Notebooks/FeatureVisualization.ipynb /home/jovyan/work/
ADD bin/Notebooks/FeatureVisualizationWithClustering.ipynb /home/jovyan/work/
ADD bin/Notebooks/FilteringEffects.ipynb /home/jovyan/work/
ADD bin/Params.yaml /home/jovyan/work/
ADD notebooks/RadiomicsExample.ipynb /home/jovyan/work/
ADD notebooks/FeatureVisualization.ipynb /home/jovyan/work/
ADD notebooks/FeatureVisualizationWithClustering.ipynb /home/jovyan/work/
ADD notebooks/FilteringEffects.ipynb /home/jovyan/work/
ADD examples/exampleSettings/Params.yaml /home/jovyan/work/

# Make a global directory and link it to the work directory
RUN mkdir /data
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,3 +1,4 @@
#!/usr/bin/env python

from __future__ import print_function

Expand All @@ -12,11 +13,12 @@


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

inputCSV = outPath + os.path.sep + "TestCases.csv"
outputFilepath = outPath + os.path.sep + "radiomics_features.csv"
progress_filename = outPath + os.path.sep + "pyrad_log.txt"
inputCSV = os.path.join(outPath, 'testCases.csv')
outputFilepath = os.path.join(outPath, 'radiomics_features.csv')
progress_filename = os.path.join(outPath, 'pyrad_log.txt')
params = os.path.join(outPath, 'exampleSettings', 'Params.yaml')

# Configure logging
rLogger = logging.getLogger('radiomics')
Expand All @@ -26,7 +28,7 @@ def main():

# 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"))
handler.setFormatter(logging.Formatter('%(levelname)s:%(name)s: %(message)s'))
rLogger.addHandler(handler)

# Initialize logging for batch log messages
Expand All @@ -35,6 +37,7 @@ def main():
# Set verbosity level for output to stderr (default level = WARNING)
radiomics.setVerbosity(logging.INFO)

logger.info('pyradiomics version: %s', radiomics.__version__)
logger.info('Loading CSV')

# ####### Up to this point, this script is equal to the 'regular' batchprocessing script ########
Expand All @@ -45,24 +48,27 @@ def main():
# the input cases
flists = pandas.read_csv(inputCSV).T
except Exception:
logging.error('CSV READ FAILED', exc_info=True)
logger.error('CSV READ FAILED', exc_info=True)
exit(-1)

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

kwargs = {}
kwargs['binWidth'] = 25
kwargs['resampledPixelSpacing'] = None # [3,3,3]
kwargs['interpolator'] = sitk.sitkBSpline
kwargs['enableCExtensions'] = True
if os.path.isfile(params):
extractor = featureextractor.RadiomicsFeaturesExtractor(params)
else: # Parameter file not found, use hardcoded settings instead
settings = {}
settings['binWidth'] = 25
settings['resampledPixelSpacing'] = None # [3,3,3]
settings['interpolator'] = sitk.sitkBSpline
settings['enableCExtensions'] = True

logger.info('pyradiomics version: %s', radiomics.__version__)
logger.info('Extracting features with kwarg settings: %s', str(kwargs))
extractor = featureextractor.RadiomicsFeaturesExtractor(**settings)
# extractor.enableInputImages(wavelet= {'level': 2})

extractor = featureextractor.RadiomicsFeaturesExtractor(**kwargs)
# extractor.enableInputImages(Original={}) # Original enabled by default
# extractor.enableInputImages(wavelet= {'level': 2})
logger.info('Enabled input images types: %s', extractor.inputImages)
logger.info('Enabled features: %s', extractor.enabledFeatures)
logger.info('Current settings: %s', extractor.kwargs)

# Instantiate a pandas data frame to hold the results of all patients
results = pandas.DataFrame()
Expand Down
@@ -1,3 +1,4 @@
#!/usr/bin/env python

from __future__ import print_function

Expand All @@ -13,11 +14,12 @@


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

inputCSV = outPath + os.path.sep + "TestCases.csv"
outputFilepath = outPath + os.path.sep + "radiomics_features.csv"
progress_filename = outPath + os.path.sep + "pyrad_log.txt"
inputCSV = os.path.join(outPath, 'testCases.csv')
outputFilepath = os.path.join(outPath, 'radiomics_features.csv')
progress_filename = os.path.join(outPath, 'pyrad_log.txt')
params = os.path.join(outPath, 'exampleSettings', 'Params.yaml')

# Configure logging
rLogger = logging.getLogger('radiomics')
Expand All @@ -27,7 +29,7 @@ def main():

# 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"))
handler.setFormatter(logging.Formatter('%(levelname)s:%(name)s: %(message)s'))
rLogger.addHandler(handler)

# Initialize logging for batch log messages
Expand All @@ -36,6 +38,7 @@ def main():
# Set verbosity level for output to stderr (default level = WARNING)
radiomics.setVerbosity(logging.INFO)

logger.info('pyradiomics version: %s', radiomics.__version__)
logger.info('Loading CSV')

flists = []
Expand All @@ -44,23 +47,26 @@ def main():
cr = csv.DictReader(inFile, lineterminator='\n')
flists = [row for row in cr]
except Exception:
logging.error('CSV READ FAILED', exc_info=True)
logger.error('CSV READ FAILED', exc_info=True)

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

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

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

extractor = featureextractor.RadiomicsFeaturesExtractor(**kwargs)
extractor.enableInputImages(Original={})
# extractor.enableInputImages(wavelet= {'level': 2})
if os.path.isfile(params):
extractor = featureextractor.RadiomicsFeaturesExtractor(params)
else: # Parameter file not found, use hardcoded settings instead
settings = {}
settings['binWidth'] = 25
settings['resampledPixelSpacing'] = None # [3,3,3]
settings['interpolator'] = sitk.sitkBSpline
settings['enableCExtensions'] = True

extractor = featureextractor.RadiomicsFeaturesExtractor(**settings)
# extractor.enableInputImages(wavelet= {'level': 2})

logger.info('Enabled input images types: %s', extractor.inputImages)
logger.info('Enabled features: %s', extractor.enabledFeatures)
logger.info('Current settings: %s', extractor.kwargs)

headers = None

Expand Down
1 change: 1 addition & 0 deletions examples/helloFeatureClass.py
@@ -1,3 +1,4 @@
#!/usr/bin/env python

from __future__ import print_function

Expand Down
23 changes: 8 additions & 15 deletions examples/helloRadiomics.py
@@ -1,3 +1,4 @@
#!/usr/bin/env python

from __future__ import print_function

Expand Down Expand Up @@ -97,13 +98,13 @@ def __exit__(self, exc_type, exc_value, tb):

# Define settings for signature calculation
# These are currently set equal to the respective default values
kwargs = {}
kwargs['binWidth'] = 25
kwargs['resampledPixelSpacing'] = None # [3,3,3] is an example for defining resampling (voxels with size 3x3x3mm)
kwargs['interpolator'] = sitk.sitkBSpline
settings = {}
settings['binWidth'] = 25
settings['resampledPixelSpacing'] = None # [3,3,3] is an example for defining resampling (voxels with size 3x3x3mm)
settings['interpolator'] = sitk.sitkBSpline

# Initialize wrapperClass to generate signature
extractor = featureextractor.RadiomicsFeaturesExtractor(**kwargs)
# Initialize feature extractor
extractor = featureextractor.RadiomicsFeaturesExtractor(**settings)

# By default, only original is enabled. Optionally enable some filters:
# extractor.enableInputImages(Original={}, LoG={}, Wavelet={})
Expand All @@ -117,20 +118,12 @@ def __exit__(self, exc_type, exc_value, tb):
# Only enable mean and skewness in firstorder
extractor.enableFeaturesByName(firstorder=['Mean', 'Skewness'])

# Uncomment on of these functions to show how PyRadiomics can use the 'tqdm' or 'click' package to report progress when
# Uncomment one of these functions to show how PyRadiomics can use the 'tqdm' or 'click' package to report progress when
# running in full python mode. Assumes the respective package is installed (not included in the requirements)

# tqdmProgressbar()
# clickProgressbar()

print("Active features:")
for cls, features in six.iteritems(extractor.enabledFeatures):
if len(features) == 0:
features = extractor.getFeatureNames(cls)
for f in features:
print(f)
print(getattr(extractor.featureClasses[cls], 'get%sFeatureValue' % f).__doc__)

print("Calculating features")
featureVector = extractor.execute(imageName, maskName)

Expand Down
120 changes: 120 additions & 0 deletions examples/helloRadiomicsWithSettings.py
@@ -0,0 +1,120 @@
#!/usr/bin/env python

from __future__ import print_function

import logging
import os

import SimpleITK as sitk
import six

import radiomics
from radiomics import featureextractor

def tqdmProgressbar():
"""
This function will setup the progress bar exposed by the 'tqdm' package.
Progress reporting is only used in PyRadiomics for the calculation of GLCM and GLSZM in full python mode, therefore
enable GLCM and full-python mode to show the progress bar functionality
N.B. This function will only work if the 'click' package is installed (not included in the PyRadiomics requirements)
"""
global extractor
extractor.kwargs['enableCExtensions'] = False
# Enable the GLCM class to show the progress bar
extractor.enableFeatureClassByName('glcm')

radiomics.setVerbosity(logging.INFO) # Verbosity must be at least INFO to enable progress bar

import tqdm
radiomics.progressReporter = tqdm.tqdm

def clickProgressbar():
"""
This function will setup the progress bar exposed by the 'click' package.
Progress reporting is only used in PyRadiomics for the calculation of GLCM and GLSZM in full python mode, therefore
enable GLCM and full-python mode to show the progress bar functionality.
Because the signature used to instantiate a click progress bar is different from what PyRadiomics expects, we need to
write a simple wrapper class to enable use of a click progress bar. In this case we only need to change the 'desc'
keyword argument to a 'label' keyword argument.
N.B. This function will only work if the 'click' package is installed (not included in the PyRadiomics requirements)
"""
global extractor

extractor.kwargs['enableCExtensions'] = False
# Enable the GLCM class to show the progress bar
extractor.enableFeatureClassByName('glcm')

radiomics.setVerbosity(logging.INFO) # Verbosity must be at least INFO to enable progress bar

import click

class progressWrapper():
def __init__(self, iterable, desc=''):
# For a click progressbar, the description must be provided in the 'label' keyword argument.
self.bar = click.progressbar(iterable, label=desc)

def __iter__(self):
return self.bar.__iter__() # Redirect to the __iter__ function of the click progressbar

def __enter__(self):
return self.bar.__enter__() # Redirect to the __enter__ function of the click progressbar

def __exit__(self, exc_type, exc_value, tb):
return self.bar.__exit__(exc_type, exc_value, tb) # Redirect to the __exit__ function of the click progressbar

radiomics.progressReporter = progressWrapper

# Get some test data

# repositoryRoot points to the root of the repository. The following line gets that location if this script is run
# from it's default location in \pyradiomics\bin. Otherwise, it will point to some (invalid) folder, causing the
# getTestCase function to fail to find the test case in the repository. In that case, a test case will be downloaded to
# temporary files and it's location is returned.
repositoryRoot = os.path.abspath(os.path.join(os.getcwd(), ".."))
imageName, maskName = radiomics.getTestCase('brain1', repositoryRoot)

# Get the location of the example settings file
paramsFile = os.path.abspath(r'exampleSettings\Params.yaml')

if imageName is None or maskName is None: # Something went wrong, in this case PyRadiomics will also log an error
print('Error getting testcase!')
exit()

# Regulate verbosity with radiomics.verbosity
# radiomics.setVerbosity(logging.INFO)

# Get the PyRadiomics logger (default log-level = INFO
logger = radiomics.logger
logger.setLevel(logging.DEBUG) # set level to DEBUG to include debug log messages in log file

# Write out all log entries to a file
handler = logging.FileHandler(filename='testLog.txt', mode='w')
formatter = logging.Formatter("%(levelname)s:%(name)s: %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

# Initialize feature extractor using the settings file
extractor = featureextractor.RadiomicsFeaturesExtractor(paramsFile)

# Uncomment one of these functions to show how PyRadiomics can use the 'tqdm' or 'click' package to report progress when
# running in full python mode. Assumes the respective package is installed (not included in the requirements)

# tqdmProgressbar()
# clickProgressbar()

print("Active features:")
for cls, features in six.iteritems(extractor.enabledFeatures):
if len(features) == 0:
features = extractor.getFeatureNames(cls)
for f in features:
print(f)
print(getattr(extractor.featureClasses[cls], 'get%sFeatureValue' % f).__doc__)

print("Calculating features")
featureVector = extractor.execute(imageName, maskName)

for featureName in featureVector.keys():
print("Computed %s: %s" % (featureName, featureVector[featureName]))
4 changes: 3 additions & 1 deletion examples/helloResampling.py
@@ -1,3 +1,4 @@
#!/usr/bin/env python

import sys

Expand All @@ -8,7 +9,8 @@
image = sitk.ReadImage(sys.argv[1])
mask = sitk.ReadImage(sys.argv[2])

(ii, im) = imageoperations.resampleImage(image, mask, [2, 2, 2])
# Resamples and crops onto bounding box defined by the label
(ii, im) = imageoperations.resampleImage(image, mask, [2, 2, 2], label=1, padDistance=5)

sitk.WriteImage(ii, sys.argv[3])
sitk.WriteImage(im, sys.argv[4])
6 changes: 6 additions & 0 deletions examples/testCases.csv
@@ -0,0 +1,6 @@
ID,Image,Mask
brain1,..\data\brain1_image.nrrd,..\data\brain1_label.nrrd
brain2,..\data\brain2_image.nrrd,..\data\brain2_label.nrrd
breast1,..\data\breast1_image.nrrd,..\data\breast1_label.nrrd
lung1,..\data\lung1_image.nrrd,..\data\lung1_label.nrrd
lung2,..\data\lung2_image.nrrd,..\data\lung2_label.nrrd
File renamed without changes.
File renamed without changes.

0 comments on commit 9ef7ea0

Please sign in to comment.