Skip to content

Commit

Permalink
Merge pull request #307 from JoostJM/update-batch-script
Browse files Browse the repository at this point in the history
Add additional checks to batch processing script
  • Loading branch information
JoostJM authored Oct 9, 2017
2 parents f268b92 + 9222e23 commit d389152
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions radiomics/featureextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,15 @@ def loadImage(self, ImageFilePath, MaskFilePath):
padding as specified in padDistance) after assignment of image and mask.
"""
self.logger.info('Loading image and mask')
if isinstance(ImageFilePath, six.string_types) and os.path.exists(ImageFilePath):
if isinstance(ImageFilePath, six.string_types) and os.path.isfile(ImageFilePath):
image = sitk.ReadImage(ImageFilePath)
elif isinstance(ImageFilePath, sitk.SimpleITK.Image):
image = ImageFilePath
else:
self.logger.warning('Error reading image Filepath or SimpleITK object')
return None, None # this function is expected to always return a tuple of 2 elements

if isinstance(MaskFilePath, six.string_types) and os.path.exists(MaskFilePath):
if isinstance(MaskFilePath, six.string_types) and os.path.isfile(MaskFilePath):
mask = sitk.ReadImage(MaskFilePath)
elif isinstance(MaskFilePath, sitk.SimpleITK.Image):
mask = MaskFilePath
Expand Down
28 changes: 22 additions & 6 deletions radiomics/scripts/commandlinebatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ def main():
cr = csv.DictReader(args.inFile, lineterminator='\n')
flists = [row for row in cr]
args.inFile.close()

# Check if required Image and Mask columns are present
if 'Image' not in cr.fieldnames:
logger.error('Required column "Image" not present in input, unable to extract features...')
exit(-1)
if 'Mask' not in cr.fieldnames:
logger.error('Required column "Mask" not present in input, unable to extract features...')
exit(-1)
except Exception:
logging.error('CSV READ FAILED', exc_info=True)
args.inFile.close()
args.outFile.close()
if args.log_file is not None:
args.log_file.close()
exit(-1)

# Initialize extractor
Expand All @@ -85,22 +89,34 @@ def main():
extractor = featureextractor.RadiomicsFeaturesExtractor()
except Exception:
logger.error('EXTRACTOR INITIALIZATION FAILED', exc_info=True)
args.outFile.close()
args.log_file.close()
exit(-1)

# Extract features
logger.info('Extracting features with kwarg settings: %s', str(extractor.settings))

headers = None
for idx, entry in enumerate(flists, start=1):
# Check if the required columns have a value, if not, skip them.
if entry['Image'] is '':
logger.error('Missing value in column "Image", cannot process. Skipping patient (%d/%d)', idx, len(flists))
continue
if entry['Mask'] is '':
logger.error('Missing value in column "Mask", cannot process. Skipping patient (%d/%d)', idx, len(flists))
continue

logger.info("(%d/%d) Processing Patient (Image: %s, Mask: %s)", idx, len(flists), entry['Image'], entry['Mask'])

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

if (imageFilepath is not None) and (maskFilepath is not None):
if not os.path.isabs(imageFilepath):
imageFilepath = os.path.abspath(os.path.join(os.path.dirname(args.inFile.name), imageFilepath))
logger.debug('Updated relative image filepath to be relative to input CSV: %s', imageFilepath)
if not os.path.isabs(maskFilepath):
maskFilepath = os.path.abspath(os.path.join(os.path.dirname(args.inFile.name), maskFilepath))
logger.debug('Updated relative mask filepath to be relative to input CSV: %s', maskFilepath)

featureVector = collections.OrderedDict(entry)
if args.shorten:
featureVector['Image'] = os.path.basename(imageFilepath)
Expand Down

0 comments on commit d389152

Please sign in to comment.