Skip to content

Commit cb4604d

Browse files
committed
GHA: Add simulation workflow
Add workflow to check that all problems can be simulated. This will run for quite some time.
1 parent 864a39b commit cb4604d

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

.github/workflows/simulate.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Simulate
2+
on: [push, pull_request, workflow_dispatch]
3+
4+
jobs:
5+
simulate_amici:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Check out repository
9+
uses: actions/checkout@v5
10+
11+
- name: Prepare Python
12+
uses: actions/setup-python@v6
13+
with:
14+
python-version: "3.13"
15+
16+
- name: Install AMICI
17+
env:
18+
AMICI_PARALLEL_COMPILE: ""
19+
run: |
20+
pip install --upgrade pip
21+
# TODO: once amici 1.0 is released, replace with:
22+
# pip install amici[petab]
23+
pip install petab
24+
pip install "git+https://github.com/dweindl/amici.git@6a85422ad233fc268fb71272bba39f5b82db387a#egg=amici&subdirectory=python/sdist"
25+
26+
- name: Simulate with nominal parameters
27+
env:
28+
AMICI_PARALLEL_COMPILE: ""
29+
run: |
30+
for problem in `ls -1 Benchmark-Models/`; do
31+
python src/python/simulate.py "$problem" -j
32+
echo
33+
echo
34+
done

src/python/simulate.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to simulate a model using AMICI.
4+
5+
Basic usage: simulate.py <problem_id>
6+
See --help for more options.
7+
"""
8+
9+
import os
10+
import argparse
11+
import benchmark_models_petab
12+
import amici.sim.sundials as ass
13+
from amici.importers.petab.v1 import (
14+
import_petab_problem,
15+
simulate_petab,
16+
LLH,
17+
RDATAS,
18+
)
19+
import logging
20+
from petab.v1.lint import measurement_table_has_timepoint_specific_mappings
21+
from petab.v1.core import flatten_timepoint_specific_output_overrides
22+
23+
24+
def parse_cli_args():
25+
"""Parse command line arguments."""
26+
parser = argparse.ArgumentParser(
27+
description="Simulate a model using AMICI."
28+
)
29+
parser.add_argument(
30+
"problem_id",
31+
type=str,
32+
help="Identifier for the problem/model to simulate.",
33+
)
34+
parser.add_argument(
35+
"-j",
36+
dest="num_threads",
37+
nargs="?",
38+
default=None,
39+
const="all",
40+
help=(
41+
"Number of cores to use. "
42+
"`-j` (no value) uses all available cores. "
43+
"If omitted, threading is left unchanged."
44+
),
45+
)
46+
args = parser.parse_args()
47+
48+
args.num_threads = (
49+
os.cpu_count()
50+
if args.num_threads == "all"
51+
else 1
52+
if args.num_threads is None
53+
else max(1, int(args.num_threads))
54+
)
55+
56+
return args
57+
58+
59+
def main():
60+
"""Simulate a PEtab problem with AMICI using nominal parameter values."""
61+
args = parse_cli_args()
62+
63+
# Import and simulate
64+
print(args.problem_id)
65+
print("-" * len(args.problem_id), flush=True)
66+
67+
problem = benchmark_models_petab.get_problem(args.problem_id)
68+
69+
if measurement_table_has_timepoint_specific_mappings(
70+
problem.measurement_df,
71+
):
72+
flatten_timepoint_specific_output_overrides(problem)
73+
74+
model = import_petab_problem(
75+
problem, generate_sensitivity_code=False, verbose=logging.INFO
76+
)
77+
solver = model.create_solver()
78+
solver.set_return_data_reporting_mode(
79+
ass.RDataReporting.observables_likelihood
80+
)
81+
res = simulate_petab(
82+
problem, model, solver=solver, num_threads=args.num_threads
83+
)
84+
85+
for rdata in res[RDATAS]:
86+
print(
87+
f"{rdata.id}: llh = {rdata.llh}, chi2 = {rdata.chi2}, status = {rdata.status}"
88+
)
89+
90+
print()
91+
print("Total log-likelihood:", res[LLH])
92+
print()
93+
94+
95+
if __name__ == "__main__":
96+
main()

0 commit comments

Comments
 (0)