Skip to content

Add the smothing option for 1d-marginals #235

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions src/corner/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
except ImportError:
gaussian_filter = None

try:
from scipy.interpolate import interp1d
except ImportError:
interp1d = None


def corner_impl(
xs,
Expand Down Expand Up @@ -234,11 +239,25 @@ def corner_impl(
else:
if gaussian_filter is None:
raise ImportError("Please install scipy for smoothing")
n, _ = np.histogram(x, bins=bins_1d, weights=weights)
n, _ = np.histogram(x, bins=bins_1d, weights=weights, density=True)
n = gaussian_filter(n, smooth1d)
x0 = np.array(list(zip(bins_1d[:-1], bins_1d[1:]))).flatten()
y0 = np.array(list(zip(n, n))).flatten()
ax.plot(x0, y0, **hist_kwargs)
if smooth1d is not None and interp1d is not None:
# Now use a continuos line for the plot instead of histogram counts
bins_1d_centers = 0.5 * (bins_1d[1:] + bins_1d[:-1])
pdfinterpolated = interp1d(
bins_1d_centers, n, fill_value="extrapolate"
)
newx = np.linspace(bins_1d.min(), bins_1d.max(), bins_1d.size)
nonzero = pdfinterpolated(newx) > 0
ax.plot(
newx[nonzero],
pdfinterpolated(newx)[nonzero],
**hist_kwargs,
)
else:
ax.plot(x0, y0, **hist_kwargs)

# Plot quantiles if wanted.
if len(quantiles) > 0:
Expand Down