Skip to content

Commit

Permalink
ENH: Add optional progress reporting for voxel-based extraction
Browse files Browse the repository at this point in the history
Update base.py to add progress reporting when computing voxel batches. Uses `radiomics.getProgressReporter`, which should be set to something like `tqdm.tqdm` to work. Only shows progress bar when logging level is at least INFO (so not when WARNING or higher).
  • Loading branch information
Joost van Griethuysen committed Sep 5, 2020
1 parent e17e3da commit a568ee0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 4 additions & 1 deletion radiomics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class _DummyProgressReporter(object):
In this class, the __iter__ function redirects to the __iter__ function of the iterable passed at initialization.
The __enter__ and __exit__ functions enable usage in a 'with' statement
"""
def __init__(self, iterable, desc=''):
def __init__(self, iterable=None, desc='', total=None):
self.desc = desc # A description is not required, but is provided by PyRadiomics
self.iterable = iterable # Iterable is required

Expand All @@ -232,6 +232,9 @@ def __enter__(self):
def __exit__(self, exc_type, exc_value, tb):
pass # Nothing needs to be closed or handled, so just specify 'pass'

def update(self, n=1):
pass # Nothing needs to be updated, so just specify 'pass'


def getProgressReporter(*args, **kwargs):
"""
Expand Down
20 changes: 11 additions & 9 deletions radiomics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,17 @@ def _calculateVoxels(self):
if voxelBatch < 0:
voxelBatch = voxel_count
n_batches = numpy.ceil(float(voxel_count) / voxelBatch)
while voxel_batch_idx < voxel_count:
self.logger.debug('Calculating voxel batch no. %i/%i', int(voxel_batch_idx / voxelBatch) + 1, n_batches)
voxelCoords = self.labelledVoxelCoordinates[:, voxel_batch_idx:voxel_batch_idx + voxelBatch]
# Calculate the feature values for the current kernel
for success, featureName, featureValue in self._calculateFeatures(voxelCoords):
if success:
self.featureValues[featureName][tuple(voxelCoords)] = featureValue

voxel_batch_idx += voxelBatch
with self.progressReporter(total=n_batches, desc='batch') as pbar:
while voxel_batch_idx < voxel_count:
self.logger.debug('Calculating voxel batch no. %i/%i', int(voxel_batch_idx / voxelBatch) + 1, n_batches)
voxelCoords = self.labelledVoxelCoordinates[:, voxel_batch_idx:voxel_batch_idx + voxelBatch]
# Calculate the feature values for the current kernel
for success, featureName, featureValue in self._calculateFeatures(voxelCoords):
if success:
self.featureValues[featureName][tuple(voxelCoords)] = featureValue

voxel_batch_idx += voxelBatch
pbar.update(1) # Update progress bar

# Convert the output to simple ITK image objects
for feature, enabled in six.iteritems(self.enabledFeatures):
Expand Down

0 comments on commit a568ee0

Please sign in to comment.