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

DM-41814: Fix quantile scaling when number of bins is capped. #69

Merged
merged 2 commits into from
Nov 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/lsst/summit/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,10 @@ def getQuantiles(data, nColors):
if np.isnan(minVal): # cdf calculation has failed because all data is nan
return np.asarray([np.nan for _ in range(nColors)])

scale = (maxVal - minVal)/len(cdf)

boundaries = np.asarray(
[np.argmax(cdf >= i) + minVal for i in range(nColors)] + [maxVal]
[np.argmax(cdf >= i)*scale + minVal for i in range(nColors)] + [maxVal]
)
return boundaries

Expand All @@ -1034,5 +1036,6 @@ def digitizeData(data, nColors=256):
Scaled data in the [0, nColors - 1] range.
"""
cdf, minVal, maxVal = getCdf(data, nColors - 1)
bins = np.floor((data - minVal)).astype(np.int64)
scale = (maxVal - minVal)/len(cdf)
bins = np.floor((data*scale - minVal)).astype(np.int64)
return cdf[bins]
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ def test_quantiles(self):
# We understand that our algorithm gives very large rounding error
# compared to the generic numpy method. But still test it.
np.random.seed(1234)
# too big of a width violates the tolerance in the test to cap at 10k
dataRanges = [(50, 1, -1), (100_000, 5_000, -1), (5_000_000, 10_000, -2)]
dataRanges = [(50, 1, -1), (100_000, 5_000, -2), (5_000_000, 10_000, -2), (50_000, 100_000, -3)]
colorRanges = [2, 256, 999] # [very few, nominal, lots and an odd number]
for nColors, (mean, width, decimal) in itertools.product(colorRanges, dataRanges):
data = np.random.normal(mean, width, (100, 100))
Expand Down