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

Apply some caching to histogram thresholds. #1042

Merged
merged 1 commit into from
Jan 30, 2023
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
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