|
| 1 | +""" |
| 2 | +Generate documentation images for Opproplot. |
| 3 | +
|
| 4 | +Creates: |
| 5 | +- docs/assets/opproplot_hero.png |
| 6 | +- docs/assets/opproplot_example.png |
| 7 | +- docs/assets/opproplot_breast_cancer.png |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import matplotlib |
| 14 | + |
| 15 | +matplotlib.use("Agg") |
| 16 | +import matplotlib.pyplot as plt # noqa: E402 |
| 17 | +import numpy as np # noqa: E402 |
| 18 | +from sklearn.datasets import load_breast_cancer # noqa: E402 |
| 19 | +from sklearn.linear_model import LogisticRegression # noqa: E402 |
| 20 | +from sklearn.model_selection import train_test_split # noqa: E402 |
| 21 | + |
| 22 | +from opproplot import operating_profile_plot # noqa: E402 |
| 23 | + |
| 24 | + |
| 25 | +ASSETS_DIR = Path("docs/assets") |
| 26 | + |
| 27 | + |
| 28 | +def _ensure_assets_dir() -> None: |
| 29 | + ASSETS_DIR.mkdir(parents=True, exist_ok=True) |
| 30 | + |
| 31 | + |
| 32 | +def generate_hero() -> None: |
| 33 | + rng = np.random.default_rng(2) |
| 34 | + y_true = rng.integers(0, 2, size=4000) |
| 35 | + scores = rng.normal(loc=y_true * 0.7 + 0.08, scale=0.3, size=4000) |
| 36 | + scores = np.clip(scores, 0, 1) |
| 37 | + |
| 38 | + fig, ax_hist, ax_metric = operating_profile_plot( |
| 39 | + y_true, |
| 40 | + scores, |
| 41 | + bins=24, |
| 42 | + show_accuracy=True, |
| 43 | + show_key=True, |
| 44 | + key_location="outside", |
| 45 | + show_grid=False, |
| 46 | + title="Operating Profile Plot", |
| 47 | + ) |
| 48 | + |
| 49 | + # Minimal styling for hero |
| 50 | + for ax in (ax_hist, ax_metric): |
| 51 | + ax.set_xlabel("") |
| 52 | + ax.set_ylabel("") |
| 53 | + ax.tick_params(labelbottom=False, labelleft=False, labelright=False, length=0) |
| 54 | + for spine in ax.spines.values(): |
| 55 | + spine.set_visible(False) |
| 56 | + |
| 57 | + fig.set_size_inches(4.6, 2.4) |
| 58 | + fig.tight_layout(pad=0.4) |
| 59 | + fig.savefig(ASSETS_DIR / "opproplot_hero.png", dpi=220, transparent=True, bbox_inches="tight") |
| 60 | + plt.close(fig) |
| 61 | + |
| 62 | + |
| 63 | +def generate_simulated_example() -> None: |
| 64 | + rng = np.random.default_rng(0) |
| 65 | + y_true = rng.integers(0, 2, size=5000) |
| 66 | + scores = rng.random(size=5000) |
| 67 | + |
| 68 | + fig, _, _ = operating_profile_plot( |
| 69 | + y_true, |
| 70 | + scores, |
| 71 | + bins=30, |
| 72 | + show_accuracy=True, |
| 73 | + show_key=True, |
| 74 | + key_location="inside", |
| 75 | + show_grid=False, |
| 76 | + title="Opproplot: Operating Profile", |
| 77 | + ) |
| 78 | + fig.tight_layout() |
| 79 | + fig.savefig(ASSETS_DIR / "opproplot_example.png", dpi=200) |
| 80 | + plt.close(fig) |
| 81 | + |
| 82 | + |
| 83 | +def generate_breast_cancer() -> None: |
| 84 | + data = load_breast_cancer() |
| 85 | + X_train, X_test, y_train, y_test = train_test_split( |
| 86 | + data.data, data.target, test_size=0.25, random_state=0, stratify=data.target |
| 87 | + ) |
| 88 | + clf = LogisticRegression(max_iter=1000) |
| 89 | + clf.fit(X_train, y_train) |
| 90 | + y_score = clf.predict_proba(X_test)[:, 1] |
| 91 | + |
| 92 | + fig, ax_hist, _ = operating_profile_plot( |
| 93 | + y_test, |
| 94 | + y_score, |
| 95 | + bins=30, |
| 96 | + show_accuracy=True, |
| 97 | + show_key=True, |
| 98 | + key_location="inside", |
| 99 | + show_grid=False, |
| 100 | + title="Breast cancer classifier: operating profile", |
| 101 | + ) |
| 102 | + ax_hist.set_title("Breast cancer classifier: operating profile", fontsize=11) |
| 103 | + fig.tight_layout() |
| 104 | + fig.savefig(ASSETS_DIR / "opproplot_breast_cancer.png", dpi=200) |
| 105 | + plt.close(fig) |
| 106 | + |
| 107 | + |
| 108 | +def main() -> None: |
| 109 | + _ensure_assets_dir() |
| 110 | + generate_hero() |
| 111 | + generate_simulated_example() |
| 112 | + generate_breast_cancer() |
| 113 | + print("Generated docs images in docs/assets/") |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments