Skip to content

Instantly share code, notes, and snippets.

@dkirkby
Created July 3, 2024 12:58
Show Gist options
  • Save dkirkby/d780351d03c983b825d24ee53dc27bfd to your computer and use it in GitHub Desktop.
Save dkirkby/d780351d03c983b825d24ee53dc27bfd to your computer and use it in GitHub Desktop.
Compute contour levels
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