Skip to content

Commit

Permalink
Merge pull request #318 from JoostJM/filter-out-nans
Browse files Browse the repository at this point in the history
Add --skip-nans command line argument
  • Loading branch information
JoostJM committed Oct 23, 2017
2 parents 60c18e2 + 4a5b250 commit 7535f8d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions radiomics/scripts/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path
import sys

import numpy
import six

import radiomics
Expand All @@ -25,6 +26,8 @@
'Default is "txt": one feature per line in format "name:value". For "csv": one row of feature '
'names, followed by one row of feature values. For "json": Features are written in a JSON '
'format dictionary "{name:value}"')
parser.add_argument('--skip-nans', action='store_true',
help='Add this argument to skip returning features that have an invalid result (NaN)')
parser.add_argument('--param', '-p', metavar='FILE', type=str, default=None,
help='Parameter file containing the settings to be used in extraction')
parser.add_argument('--label', '-l', metavar='N', default=None, type=int,
Expand All @@ -43,6 +46,7 @@
parser.add_argument('--version', action='version', help='Print version and exit',
version='%(prog)s ' + radiomics.__version__)


def main():
args = parser.parse_args()

Expand Down Expand Up @@ -79,6 +83,13 @@ def main():

featureVector.update(extractor.execute(args.image, args.mask, args.label))

# if specified, skip NaN values
if args.skip_nans:
for key in list(featureVector.keys()):
if isinstance(featureVector[key], float) and numpy.isnan(featureVector[key]):
logger.debug('Feature %s computed NaN, removing from results', key)
del featureVector[key]

if args.format == 'csv':
writer = csv.writer(args.out, lineterminator='\n')
writer.writerow(list(featureVector.keys()))
Expand Down
13 changes: 12 additions & 1 deletion radiomics/scripts/commandlinebatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import logging
import os.path

import numpy

import radiomics
from radiomics import featureextractor

Expand All @@ -22,6 +24,8 @@
'Default is "csv": one row of feature names, followed by one row of feature values for each '
'image-mask combination. For "json": Features are written in a JSON format dictionary '
'"{name:value}", one line per image-mask combination')
parser.add_argument('--skip-nans', action='store_true',
help='Add this argument to skip returning features that have an invalid result (NaN)')
parser.add_argument('--param', '-p', metavar='FILE', type=str, default=None,
help='Parameter file containing the settings to be used in extraction')
parser.add_argument('--label', '-l', metavar='N', default=None, type=int,
Expand Down Expand Up @@ -131,6 +135,13 @@ def main():

featureVector.update(extractor.execute(imageFilepath, maskFilepath, label))

# if specified, skip NaN values
if args.skip_nans:
for key in list(featureVector.keys()):
if isinstance(featureVector[key], float) and numpy.isnan(featureVector[key]):
logger.debug('Feature %s computed NaN, removing from results', key)
del featureVector[key]

if args.format == 'csv':
writer = csv.writer(args.outFile, lineterminator='\n')
if headers is None:
Expand All @@ -139,7 +150,7 @@ def main():

row = []
for h in headers:
row.append(featureVector.get(h, "N/A"))
row.append(featureVector.get(h, ""))
writer.writerow(row)
elif args.format == 'json':
json.dump(featureVector, args.out)
Expand Down

0 comments on commit 7535f8d

Please sign in to comment.