Skip to content

Commit

Permalink
Merge pull request #1042 from girder/cache-histogram-threshold
Browse files Browse the repository at this point in the history
Apply some caching to histogram thresholds.
  • Loading branch information
manthey authored Jan 30, 2023
2 parents 0d19800 + 01da576 commit e961c9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Render item lists faster ([#1036](../../pull/1036))
- Reduce bioformats source memory usage ([#1038](../../pull/1038))
- Better pick the largest image from bioformats ([#1039](../../pull/1039), [#1040](../../pull/1040))
- Cache histogram thresholds ([#1042](../../pull/1042))

## 1.19.3

Expand Down
20 changes: 16 additions & 4 deletions large_image/tilesource/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,9 @@ def getTileFramesQuadInfo(metadata, options=None):
return status


_recentThresholds = {}


def histogramThreshold(histogram, threshold, fromMax=False):
"""
Given a histogram and a threshold on a scale of [0, 1], return the bin
Expand All @@ -922,20 +925,29 @@ def histogramThreshold(histogram, threshold, fromMax=False):
:returns: the value the excludes no more than the threshold from the
specified end.
"""
key = (id(histogram), threshold, fromMax)
if key in _recentThresholds:
return _recentThresholds[key]
hist = histogram['hist']
edges = histogram['bin_edges']
samples = histogram['samples'] if not histogram.get('density') else 1
if fromMax:
hist = hist[::-1]
edges = edges[::-1]
tally = 0
result = edges[-1]
for idx in range(len(hist)):
if tally >= threshold * samples:
if tally + hist[idx] > threshold * samples:
if not idx:
return histogram['min' if not fromMax else 'max']
return edges[idx]
result = histogram['min' if not fromMax else 'max']
else:
result = edges[idx]
break
tally += hist[idx]
return edges[-1]
if len(_recentThresholds) > 100:
_recentThresholds.empty()
_recentThresholds[key] = result
return result


def addPILFormatsToOutputOptions():
Expand Down
2 changes: 1 addition & 1 deletion test/test_source_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def testStyleMinMaxThreshold():
output={'maxWidth': 256, 'maxHeight': 256}, format=constants.TILE_FORMAT_NUMPY)
assert numpy.any(image != imageB)
assert image[0][0][0] == 252
assert imageB[0][0][0] == 254
assert imageB[0][0][0] == 246
sourceC = large_image_source_tiff.open(
imagePath, style=json.dumps({'min': 'full', 'max': 'full'}))
imageC, _ = sourceC.getRegion(
Expand Down

0 comments on commit e961c9f

Please sign in to comment.