Created
July 3, 2024 12:58
-
-
Save dkirkby/d780351d03c983b825d24ee53dc27bfd to your computer and use it in GitHub Desktop.
Compute contour levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_contour_levels(data, nsigmas=(2,1)): | |
assert data.ndim == 2 | |
assert np.all(data >= 0) | |
assert np.sum(data) <= 1 + 1e-6 | |
assert np.all(np.asarray(nsigmas) > 0) | |
assert np.all(np.diff(nsigmas) < 0) | |
# Convert sigmas to confidence levels | |
CL = scipy.stats.chi2.cdf(x=np.asarray(nsigmas) ** 2, df=1) | |
# Sort 2D bin values. | |
values = np.sort(data, axis=None)[::-1] | |
sums = np.cumsum(values) | |
# Interpolate to get CL contour levels. | |
return np.interp(CL[::-1], sums, values)[::-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment