Skip to content

Commit 9c1e5d5

Browse files
Merge pull request #2 from DiogoRibeiro7/feat/first_commit
feat: work
2 parents ffdd7e0 + 45de359 commit 9c1e5d5

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.github/workflows/bump-version.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# .github/workflows/bump-version.yml
2+
name: Bump Version on Merge to Main
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
bump-version:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
id-token: write
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-python@v5
20+
with:
21+
python-version: "3.11"
22+
23+
- run: pip install python-semantic-release
24+
25+
- name: Configure Git
26+
run: |
27+
git config user.name "github-actions[bot]"
28+
git config user.email "github-actions[bot]@users.noreply.github.com"
29+
30+
- name: Run Semantic Release
31+
env:
32+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
run: semantic-release version
34+
35+
- name: Push changes
36+
run: |
37+
git push --follow-tags

gen_surv/cmm.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pandas as pd
2+
import numpy as np
3+
from gen_surv.validate import validate_gen_cmm_inputs
4+
from gen_surv.censoring import runifcens, rexpocens
5+
from gen_surv.cmm import generate_event_times
6+
7+
def gen_cmm(n, model_cens, cens_par, beta, covar, rate):
8+
"""
9+
Generate survival data using a continuous-time Markov model (CMM).
10+
11+
Parameters:
12+
- n (int): Number of individuals.
13+
- model_cens (str): "uniform" or "exponential".
14+
- cens_par (float): Parameter for censoring.
15+
- beta (list): Regression coefficients (length 3).
16+
- covar (float): Covariate range (uniformly sampled from [0, covar]).
17+
- rate (list): Transition rates (length 6).
18+
19+
Returns:
20+
- pd.DataFrame with columns: id, start, stop, status, covariate, transition
21+
"""
22+
validate_gen_cmm_inputs(n, model_cens, cens_par, beta, covar, rate)
23+
24+
rfunc = runifcens if model_cens == "uniform" else rexpocens
25+
rows = []
26+
27+
for k in range(n):
28+
z1 = np.random.uniform(0, covar)
29+
c = rfunc(1, cens_par)[0]
30+
events = generate_event_times(z1, beta, rate)
31+
32+
t12, t13, t23 = events["t12"], events["t13"], events["t23"]
33+
min_event_time = min(t12, t13, c)
34+
35+
if min_event_time < c:
36+
if t12 <= t13:
37+
transition = 1 # 1 -> 2
38+
rows.append([k + 1, 0, t12, 1, z1, transition])
39+
else:
40+
transition = 2 # 1 -> 3
41+
rows.append([k + 1, 0, t13, 1, z1, transition])
42+
else:
43+
# Censored before any event
44+
rows.append([k + 1, 0, c, 0, z1, np.nan])
45+
46+
return pd.DataFrame(rows, columns=["id", "start", "stop", "status", "covariate", "transition"])
47+

gen_surv/validate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,30 @@ def validate_gen_cphm_inputs(n: int, model_cens: str, cens_par: float, covar: fl
2020
if covar <= 0:
2121
raise ValueError("Argument 'covar' must be greater than 0")
2222

23+
def validate_gen_cmm_inputs(n: int, model_cens: str, cens_par: float, beta: list, covar: float, rate: list):
24+
"""
25+
Validate inputs for generating CMM (Continuous-Time Markov Model) data.
26+
27+
Parameters:
28+
- n (int): Number of individuals.
29+
- model_cens (str): Censoring model, must be "uniform" or "exponential".
30+
- cens_par (float): Parameter for censoring distribution, must be > 0.
31+
- beta (list): Regression coefficients, must have length 3.
32+
- covar (float): Covariate value, must be > 0.
33+
- rate (list): Transition rates, must have length 6.
34+
35+
Raises:
36+
- ValueError: If any parameter is invalid.
37+
"""
38+
if n <= 0:
39+
raise ValueError("Argument 'n' must be greater than 0")
40+
if model_cens not in {"uniform", "exponential"}:
41+
raise ValueError("Argument 'model_cens' must be one of 'uniform' or 'exponential'")
42+
if cens_par <= 0:
43+
raise ValueError("Argument 'cens_par' must be greater than 0")
44+
if len(beta) != 3:
45+
raise ValueError("Argument 'beta' must be a list of length 3")
46+
if covar <= 0:
47+
raise ValueError("Argument 'covar' must be greater than 0")
48+
if len(rate) != 6:
49+
raise ValueError("Argument 'rate' must be a list of length 6")

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ numpy = "^1.26"
1414
[tool.poetry.dev-dependencies]
1515
pytest = "^8.0"
1616

17+
[tool.semantic_release]
18+
version_source = "tag"
19+
version_variable = ["pyproject.toml:project.version"]
20+
commit_version_number = true
21+
changelog_file = "CHANGELOG.md"
22+
upload_to_repository = false
23+
branch = "main"
24+
build_command = ""
25+
1726
[build-system]
1827
requires = ["poetry-core"]
1928
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)