Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Refactor CLI scripts #347

Merged
merged 2 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ Example
Command Line Use
----------------

* PyRadiomics has 2 commandline scripts, ``pyradiomics`` is for single image feature extraction and ``pyradiomicsbatch``
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.
* PyRadiomics can be used directly from the commandline via the entry point ``pyradiomics``. Depending on the input
provided, PyRadiomics is run in either single-extraction or batch-extraction mode.

* 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>
pyradiomics <path/to/input> <path/to/output>

* The input file for batch processing is a CSV file where the first row is contains headers and each subsequent row
represents one combination of an image and a segmentation and contains at least 2 elements: 1) path/to/image,
Expand All @@ -62,17 +60,21 @@ Command Line Use

All headers should be unique and different from headers provided by PyRadiomics (``<filter>_<class>_<feature>``).

* Extraction can be customized by specifying a `parameter file <radiomics-parameter-file-label>` in the ``--param``
argument and/or by specifying override settings (only `type 3 customization <radiomics-settings-label>`) in the
``--setting`` argument. Multiple overrides can be used by specifying ``--setting`` multiple times.

* For more information on the possible command line arguments, run::

pyradiomics -h
pyradiomicsbatch -h


---------------
Interactive Use
---------------

* (LINUX) Add pyradiomics to the environment variable PYTHONPATH:
* (LINUX) To run from source code, add pyradiomics to the environment variable PYTHONPATH (Not necessary when
PyRadiomics is installed):

* ``setenv PYTHONPATH /path/to/pyradiomics/radiomics``

Expand Down Expand Up @@ -126,7 +128,8 @@ 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:
* (LINUX) To run from source code, add pyradiomics to the environment variable PYTHONPATH (Not necessary when
PyRadiomics is installed):

* ``setenv PYTHONPATH /path/to/pyradiomics/radiomics``

Expand Down Expand Up @@ -170,9 +173,8 @@ Setting Up Logging
------------------

PyRadiomics features extensive logging to help track down any issues with the extraction of features.
By default PyRadiomics logging reports messages of level INFO and up (giving some information on progress during
extraction and any warnings or errors that occur), and prints this to the output (stderr). By default, PyRadiomics does
not create a log file.
By default PyRadiomics logging reports messages of level WARNING and up (reporting any warnings or errors that occur),
and prints this to the output (stderr). By default, PyRadiomics does not create a log file.

To change the amount of information that is printed to the output, use :py:func:`~radiomics.setVerbosity` in interactive
use and the optional ``--verbosity`` argument in commandline use.
Expand All @@ -193,4 +195,5 @@ handler to the pyradiomics logger::
radiomics.logger.setLevel(logging.DEBUG)

To store a log file when running pyradiomics from the commandline, specify a file location in the optional
``--log-file`` argument. The amount of logging that is stored is controlled by the ``--log-level`` argument.
``--log-file`` argument. The amount of logging that is stored is controlled by the ``--log-level`` argument
(default level INFO and up).
2 changes: 1 addition & 1 deletion examples/batchprocessing_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def run(case):

imageFilepath = case['Image'] # Required
maskFilepath = case['Mask'] # Required
label = case.get('Label', 1) # Optional
label = case.get('Label', None) # Optional

# Instantiate Radiomics Feature extractor

Expand Down
44 changes: 24 additions & 20 deletions radiomics/featureextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class RadiomicsFeaturesExtractor:
signature specified by these settings for the passed image and labelmap combination. This function can be called
repeatedly in a batch process to calculate the radiomics signature for all image and labelmap combinations.

At initialization, a parameters file can be provided containing all necessary settings. This is done by passing the
location of the file as the single argument in the initialization call, without specifying it as a keyword argument.
If such a file location is provided, any additional kwargs are ignored.
Alternatively, at initialisation, custom settings (*NOT enabled image types and/or feature classes*) can be provided
as keyword arguments, with the setting name as key and its value as the argument value (e.g. ``binWidth=25``). For
more information on possible settings and customization, see
At initialization, a parameters file (string pointing to yaml or json structured file) or dictionary can be provided
containing all necessary settings (top level containing keys "setting", "imageType" and/or "featureClass). This is
done by passing it as the first positional argument. If no positional argument is supplied, or the argument is not
either a dictionary or a string pointing to a valid file, defaults will be applied.
Moreover, at initialisation, custom settings (*NOT enabled image types and/or feature classes*) can be provided
as keyword arguments, with the setting name as key and its value as the argument value (e.g. ``binWidth=25``).
Settings specified here will override those in the parameter file/dict/default settings.
For more information on possible settings and customization, see
:ref:`Customizing the Extraction <radiomics-customization-label>`.

By default, all features in all feature classes are enabled.
Expand All @@ -47,28 +49,30 @@ def __init__(self, *args, **kwargs):
self._enabledImagetypes = {}
self._enabledFeatures = {}

if len(args) == 1 and isinstance(args[0], six.string_types):
if len(args) == 1 and isinstance(args[0], six.string_types) and os.path.isfile(args[0]):
self.logger.info("Loading parameter file")
self.loadParams(args[0])
self._applyParams(paramsFile=args[0])
elif len(args) == 1 and isinstance(args[0], dict):
self.logger.info("Loading parameter dictionary")
self._applyParams(paramsDict=args[0])
else:
# Set default settings and update with and changed settings contained in kwargs
self.settings = self._getDefaultSettings()
if len(kwargs) > 0:
self.logger.info('Applying custom settings')
self.settings.update(kwargs)
else:
self.logger.info('No customized settings, applying defaults')

self.logger.debug("Settings: %s", self.settings)
self.logger.info('No valid config parameter, applying defaults: %s', self.settings)

self._enabledImagetypes = {'Original': {}}
self.logger.info('Enabled image types: %s', self._enabledImagetypes)

self._enabledFeatures = {}

for featureClassName in self.getFeatureClassNames():
self._enabledFeatures[featureClassName] = []
self.logger.info('Enabled features: %s', self._enabledFeatures)

if len(kwargs) > 0:
self.logger.info('Applying custom setting overrides')
self.settings.update(kwargs)
self.logger.debug("Settings: %s", self.settings)

self._setTolerance()

@classmethod
Expand Down Expand Up @@ -270,8 +274,8 @@ def enableAllFeatures(self):
Enable all classes and all features.

.. note::
Individual features that have been marked "deprecated" are not enabled by this function. They can still be enabled manually by
a call to :py:func:`~radiomics.base.RadiomicsBase.enableFeatureByName()`,
Individual features that have been marked "deprecated" are not enabled by this function. They can still be enabled
manually by a call to :py:func:`~radiomics.base.RadiomicsBase.enableFeatureByName()`,
:py:func:`~radiomics.featureextractor.RadiomicsFeaturesExtractor.enableFeaturesByName()`
or in the parameter file (by specifying the feature by name, not when enabling all features).
However, in most cases this will still result only in a deprecation warning.
Expand All @@ -293,8 +297,8 @@ def enableFeatureClassByName(self, featureClass, enabled=True):
Enable or disable all features in given class.

.. note::
Individual features that have been marked "deprecated" are not enabled by this function. They can still be enabled manually by
a call to :py:func:`~radiomics.base.RadiomicsBase.enableFeatureByName()`,
Individual features that have been marked "deprecated" are not enabled by this function. They can still be enabled
manually by a call to :py:func:`~radiomics.base.RadiomicsBase.enableFeatureByName()`,
:py:func:`~radiomics.featureextractor.RadiomicsFeaturesExtractor.enableFeaturesByName()`
or in the parameter file (by specifying the feature by name, not when enabling all features).
However, in most cases this will still result only in a deprecation warning.
Expand Down
Loading