Skip to content

Commit f92a584

Browse files
refactor: unify error handling (#60)
1 parent aa6ee6d commit f92a584

39 files changed

+1346
-633
lines changed

benchmarks/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Benchmarks
2+
3+
This directory contains performance benchmarks run with `pytest-benchmark`.
4+
Run them with:
5+
6+
```bash
7+
pytest benchmarks -q --benchmark-only
8+
```
9+
10+
## Available benchmarks
11+
12+
- validation helpers
13+
- TDCM generation

benchmarks/test_tdcm_benchmark.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
pytest.importorskip("pytest_benchmark")
4+
5+
from gen_surv.tdcm import gen_tdcm
6+
7+
8+
def test_tdcm_generation_benchmark(benchmark):
9+
benchmark(
10+
gen_tdcm,
11+
n=1000,
12+
dist="weibull",
13+
corr=0.5,
14+
dist_par=[1, 2, 1, 2],
15+
model_cens="uniform",
16+
cens_par=1.0,
17+
beta=[0.1, 0.2, 0.3],
18+
lam=1.0,
19+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
import pytest
3+
4+
pytest.importorskip("pytest_benchmark")
5+
6+
from gen_surv._validation import ensure_positive_sequence
7+
8+
9+
def test_positive_sequence_benchmark(benchmark):
10+
seq = np.random.rand(10000) + 1.0
11+
benchmark(ensure_positive_sequence, seq, "seq")

binder/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-e .
2+
jupyterlab

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ sphinx-autodoc-typehints
55
sphinx-copybutton
66
sphinx-design
77
linkify-it-py>=2.0
8+
matplotlib

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"sphinx.ext.intersphinx",
2222
"sphinx.ext.autosummary",
2323
"sphinx.ext.githubpages",
24+
"sphinx.ext.plot_directive",
2425
"myst_parser",
2526
"sphinx_copybutton",
2627
"sphinx_design",

docs/source/examples/cmm.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Continuous-Time Multi-State Markov Model (CMM)
2+
3+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DiogoRibeiro7/genSurvPy/HEAD?urlpath=lab/tree/examples/notebooks/cmm.ipynb)
4+
5+
Visualize transition times from the CMM generator:
6+
7+
```{plot}
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from gen_surv import generate
11+
12+
np.random.seed(0)
13+
14+
df = generate(
15+
model="cmm",
16+
n=200,
17+
model_cens="exponential",
18+
cens_par=2.0,
19+
beta=[0.1, 0.2, 0.3],
20+
covariate_range=1.0,
21+
rate=[0.1, 1.0, 0.2, 1.0, 0.1, 1.0],
22+
)
23+
24+
plt.hist(df["stop"], bins=20, color="#4C72B0")
25+
plt.xlabel("Time")
26+
plt.ylabel("Frequency")
27+
plt.title("CMM Transition Times")
28+
```

docs/source/examples/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ Some examples may require optional packages such as
1212
`pip install scikit-survival` before running these examples.
1313
```
1414

15+
Each example page includes a [Binder](https://mybinder.org/) badge so you can
16+
launch the corresponding notebook and experiment interactively in your
17+
browser.
18+
1519
```{toctree}
1620
:maxdepth: 2
1721
22+
tdcm
23+
cmm
24+
thmm
1825
```

docs/source/examples/tdcm.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Time-Dependent Covariate Model (TDCM)
2+
3+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DiogoRibeiro7/genSurvPy/HEAD?urlpath=lab/tree/examples/notebooks/tdcm.ipynb)
4+
5+
A basic visualization of event times produced by the TDCM generator:
6+
7+
```{plot}
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from gen_surv import generate
11+
12+
np.random.seed(0)
13+
14+
df = generate(
15+
model="tdcm",
16+
n=200,
17+
dist="weibull",
18+
corr=0.5,
19+
dist_par=[1, 2, 1, 2],
20+
model_cens="uniform",
21+
cens_par=1.0,
22+
beta=[0.1, 0.2, 0.3],
23+
lam=1.0,
24+
)
25+
26+
plt.hist(df["stop"], bins=20, color="#4C72B0")
27+
plt.xlabel("Time")
28+
plt.ylabel("Frequency")
29+
plt.title("TDCM Event Times")
30+
```

docs/source/examples/thmm.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time-Homogeneous Hidden Markov Model (THMM)
2+
3+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DiogoRibeiro7/genSurvPy/HEAD?urlpath=lab/tree/examples/notebooks/thmm.ipynb)
4+
5+
An example of event times generated by the THMM:
6+
7+
```{plot}
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from gen_surv import generate
11+
12+
np.random.seed(0)
13+
14+
df = generate(
15+
model="thmm",
16+
n=200,
17+
model_cens="exponential",
18+
cens_par=3.0,
19+
beta=[0.1, 0.2, 0.3],
20+
covariate_range=1.0,
21+
rate=[0.2, 0.1, 0.3],
22+
)
23+
24+
plt.hist(df["time"], bins=20, color="#4C72B0")
25+
plt.xlabel("Time")
26+
plt.ylabel("Frequency")
27+
plt.title("THMM Event Times")
28+
```

0 commit comments

Comments
 (0)