# Source: content/notes/libraries/visualization.md
# Independent CPU example; use the curriculum environment.
# See /notes/ml/#example-environment or /notes/deep-learning/#example-environment.

from io import BytesIO
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from sklearn.calibration import calibration_curve
y = np.array([1]*5 + [0]*95)
p = np.full(100, .1)
observed, predicted = calibration_curve(y, p, n_bins=5)
assert np.isclose(observed[0], .05) and np.isclose(predicted[0], .1)
assert 1-observed[0] > 1-predicted[0]
with plt.rc_context({"font.size": 10}):
    fig, ax = plt.subplots(figsize=(5, 4), constrained_layout=True)
    ax.plot([0, 1], [0, 1], linestyle="--", color="gray")
    ax.scatter(predicted, observed, label="100 cases; 5 positives")
    ax.set(xlabel="Predicted positive probability", ylabel="Observed positive fraction",
           title="Positive overprediction, negative underconfidence", xlim=(0, 1), ylim=(0, 1))
    ax.legend()
    buffer = BytesIO()
    fig.savefig(buffer, format="png", dpi=100)
    assert len(buffer.getvalue()) > 5000
    assert ax.get_xlabel() and ax.get_ylabel()
    plt.close(fig)
assert not plt.get_fignums()
print("calibration interpretation, rendered PNG, labels and figure cleanup passed")
