Skip to content
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

Reorganise files to separate notebooks and python files #8

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ jupyterlite_contents
*.egg-info

# generate predictions
python_files/predictions
content/predictions

# jupyter-book build
book/_build
python_files/*.ipynb
content/notebooks
2 changes: 1 addition & 1 deletion book/content
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# %%
from pathlib import Path

Path.mkdir(Path.cwd() / "predictions", exist_ok=True)
Path.mkdir(Path.cwd() / ".." / "predictions", exist_ok=True)

# %%
import numpy as np
Expand All @@ -12,7 +12,7 @@
n_samples=100_000, class_sep=0.5, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
np.save("./predictions/y_true.npy", y_test)
np.save("../predictions/y_true.npy", y_test)

# %%
from sklearn.linear_model import LogisticRegression
Expand All @@ -24,7 +24,7 @@
y_pred = model.predict_proba(X_test)[:, 1]

# %%
np.save("./predictions/y_prob_1.npy", y_pred)
np.save("../predictions/y_prob_1.npy", y_pred)

# %%
from scipy.special import expit
Expand All @@ -33,7 +33,7 @@
y_pred = expit(10 * (y_pred - 0.5))

# %%
np.save("./predictions/y_prob_2.npy", y_pred)
np.save("../predictions/y_prob_2.npy", y_pred)


# %%
Expand All @@ -42,12 +42,12 @@
y_pred = (y_pred - y_pred.min()) / (y_pred.max() - y_pred.min())

# %%
np.save("./predictions/y_prob_3.npy", y_pred)
np.save("../predictions/y_prob_3.npy", y_pred)

# %%
y_pred = model.predict_proba(X_test)[:, 1]
y_pred = np.exp(y_pred * 5)
y_pred = (y_pred - y_pred.min()) / (y_pred.max() - y_pred.min())

# %%
np.save("./predictions/y_prob_4.npy", y_pred)
np.save("../predictions/y_prob_4.npy", y_pred)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from IPython import get_ipython

ipython = get_ipython()
ipython.run_line_magic("run", "_generate_predictions.py")
ipython.run_line_magic("run", "../python_files/_generate_predictions.py")

# %% [markdown]
#
Expand All @@ -29,8 +29,8 @@
# %%
import numpy as np

y_true = np.load("./predictions/y_true.npy")
y_prob = np.load("./predictions/y_prob_2.npy")
y_true = np.load("../predictions/y_true.npy")
y_prob = np.load("../predictions/y_prob_2.npy")

# %%
y_true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from IPython import get_ipython

ipython = get_ipython()
ipython.run_line_magic("run", "_generate_predictions.py")
ipython.run_line_magic("run", "../python_files/_generate_predictions.py")

# %% [markdown]
#
Expand All @@ -25,7 +25,7 @@
# %%
import numpy as np

y_true = np.load("./predictions/y_true.npy")
y_true = np.load("../predictions/y_true.npy")
y_true

# %% [markdown]
Expand All @@ -34,7 +34,7 @@
# sets of predictions of probabilities estimated by different models.

# %%
y_proba = np.load("./predictions/y_prob_1.npy")
y_proba = np.load("../predictions/y_prob_1.npy")
y_proba

# %% [markdown]
Expand Down Expand Up @@ -76,7 +76,7 @@
# We now repeat the same analysis for the other sets of predictions.

# %%
y_proba = np.load("./predictions/y_prob_2.npy")
y_proba = np.load("../predictions/y_prob_2.npy")
disp = CalibrationDisplay.from_predictions(y_true, y_proba, **params)
_ = disp.ax_.set(
title="Uncalibrated model",
Expand Down Expand Up @@ -119,7 +119,7 @@
# Let's use the same approach to analyze some other typical calibration curves.

# %%
y_proba = np.load("./predictions/y_prob_3.npy")
y_proba = np.load("../predictions/y_prob_3.npy")
disp = CalibrationDisplay.from_predictions(y_true, y_proba, **params)
disp.ax_.axvline(0.5, color="tab:orange", label="Estimated probability = 0.5")
disp.ax_.legend()
Expand Down Expand Up @@ -147,7 +147,7 @@
# Let's check the last set of predictions.

# %%
y_proba = np.load("./predictions/y_prob_4.npy")
y_proba = np.load("../predictions/y_prob_4.npy")
disp = CalibrationDisplay.from_predictions(y_true, y_proba, **params)
disp.ax_.axvline(0.5, color="tab:orange", label="Estimated probability = 0.5")
disp.ax_.legend()
Expand Down
8 changes: 4 additions & 4 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ channels = ["conda-forge"]
platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"]

[tasks]
generate-predictions = { cmd = "python _generate_predictions.py", cwd = "python_files" }
build-calibration-curve = { cmd = "ipython build_calibration_curve.py", cwd = "python_files" }
generate-predictions = { cmd = "python _generate_predictions.py", cwd = "content/python_files" }
build-calibration-curve = { cmd = "ipython build_calibration_curve.py", cwd = "content/python_files" }
check-generated-predictions = { cmd = "python check_generated_predictions.py", cwd = "tests" }
different-calibration-curves = { cmd = "ipython different_calibration_curves.py", cwd = "python_files" }
different-calibration-curves = { cmd = "ipython different_calibration_curves.py", cwd = "content/python_files" }

[dependencies]
jupyterlab = ">=4.2.4,<5"
Expand All @@ -32,7 +32,7 @@ black = "*"
jupyter-book = "*"

[feature.doc.tasks]
convert-to-notebooks = { cmd = "jupytext --to notebook --execute python_files/*.py" }
convert-to-notebooks = { cmd = "jupytext --to notebook --execute ./content/python_files/*.py && mkdir -p ./content/notebooks && mv ./content/python_files/*.ipynb ./content/notebooks" }
build-book = { cmd = "jupyter-book build book", depends-on = ["convert-to-notebooks"] }

[environments]
Expand Down
2 changes: 1 addition & 1 deletion tests/check_generated_predictions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# %%
from pathlib import Path

path_to_predictions = Path(Path.cwd() / ".." / "python_files" / "predictions")
path_to_predictions = Path(Path.cwd() / ".." / "content" / "predictions")
assert path_to_predictions.exists()

# %%
Expand Down