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

Fix timepoint specific observables #1469

Merged
merged 49 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
29d0e3d
flatten out timepoint specific noise/obs par overrides
FFroehlich Mar 18, 2021
c4bdb88
add more benchmark models
FFroehlich Mar 18, 2021
91bd10b
fix fieder reference
FFroehlich Mar 18, 2021
35e7340
fix sign
FFroehlich Mar 18, 2021
9855825
remove note, fix value brannmark
FFroehlich Mar 18, 2021
3d1de5c
only preprocess measurements/observables if necessary
FFroehlich Mar 18, 2021
1d322c8
remove sobotta due to missing petab implementation
FFroehlich Mar 18, 2021
b8a29d8
fix better timepoint specific override detection
FFroehlich Mar 18, 2021
b2ace96
remove Isensee from test
FFroehlich Mar 18, 2021
1b2f851
fixup
FFroehlich Mar 18, 2021
f6f0ec9
fixup
FFroehlich Mar 18, 2021
5beb58a
reduce grouping
FFroehlich Mar 18, 2021
c895d56
fixup
FFroehlich Mar 18, 2021
1121af6
fix test
FFroehlich Mar 18, 2021
b1ebda1
fix parameter mapping?
FFroehlich Mar 18, 2021
36004e2
fixup
FFroehlich Mar 18, 2021
48cbfd2
use petab function
FFroehlich Mar 18, 2021
6eee7a5
Apply suggestions from code review
Mar 18, 2021
e0e9b0f
fix override check
FFroehlich Mar 18, 2021
b252fc6
Merge branch 'fix_timepoint_specific_observables' of https://github.c…
FFroehlich Mar 18, 2021
221251d
fixup
FFroehlich Mar 18, 2021
0265793
fixup
FFroehlich Mar 18, 2021
3b06d35
fixup
FFroehlich Mar 18, 2021
281a42c
fixup
FFroehlich Mar 18, 2021
912ba13
Update python/amici/petab_import.py
Mar 18, 2021
16e2864
Update python/amici/petab_import.py
Mar 18, 2021
fb13863
fixupfix types and typehints and adress reviews
FFroehlich Mar 18, 2021
c5096fa
Merge branch 'develop' into fix_timepoint_specific_observables
Mar 18, 2021
16958af
use petab functionality
FFroehlich Mar 19, 2021
c5b7645
move table check out of get_observation_model function
FFroehlich Mar 19, 2021
2a74d36
cleanup
FFroehlich Mar 19, 2021
efac68d
Merge branch 'develop' into fix_timepoint_specific_observables
Mar 19, 2021
2b03640
require newer petab
FFroehlich Mar 19, 2021
8016921
Merge branch 'fix_timepoint_specific_observables' of https://github.c…
FFroehlich Mar 19, 2021
42ddc5a
temp pin petab
FFroehlich Mar 19, 2021
bb42bf7
Update .github/workflows/test_petab_test_suite.yml
dweindl Mar 19, 2021
74771a7
use develop branch
FFroehlich Mar 19, 2021
d2c3129
fixup
FFroehlich Mar 19, 2021
b597da0
Merge branch 'develop' into fix_timepoint_specific_observables
Mar 20, 2021
d629571
fix petab test suite
FFroehlich Mar 20, 2021
b1b70bf
temp fix python tests
FFroehlich Mar 20, 2021
d9534df
Update python/sdist/setup.cfg
dweindl Mar 22, 2021
83ce36a
remove temp pins
FFroehlich Mar 22, 2021
834aafe
Merge branch 'develop' into fix_timepoint_specific_observables
Mar 22, 2021
ab7b17d
add flatten to petab test suite
FFroehlich Mar 22, 2021
dd8581d
repair CI
FFroehlich Mar 22, 2021
e0cdc24
fix petab tests
FFroehlich Mar 23, 2021
e03bd3b
fixup
FFroehlich Mar 23, 2021
cc17e66
allow flattening in pysb problem import
FFroehlich Mar 23, 2021
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
42 changes: 32 additions & 10 deletions python/amici/petab_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,20 @@ def import_model_sbml(
sbml_importer = amici.SbmlImporter(sbml_model)
sbml_model = sbml_importer.sbml

allow_n_noise_pars = \
petab.lint.observable_table_has_nontrivial_noise_formula(
observable_df
)
if petab.lint.measurement_table_has_timepoint_specific_mappings(
measurement_table,
allow_scalar_numeric_noise_parameters=allow_n_noise_pars
):
raise ValueError(
'AMICI does not support importing models with timepoint specific '
'mappings for noise or observable parameters. Please flatten '
'the problem and try again.'
)

if observable_df is not None:
observables, noise_distrs, sigmas = \
get_observation_model(observable_df)
Expand Down Expand Up @@ -574,10 +588,10 @@ def import_model_sbml(
import_model = import_model_sbml


def get_observation_model(observable_df: pd.DataFrame
) -> Tuple[Dict[str, Dict[str, str]],
Dict[str, str],
Dict[str, Union[str, float]]]:
def get_observation_model(
observable_df: pd.DataFrame,
) -> Tuple[Dict[str, Dict[str, str]], Dict[str, str],
Dict[str, Union[str, float]]]:
"""
Get observables, sigmas, and noise distributions from PEtab observation
table in a format suitable for
Expand All @@ -596,13 +610,13 @@ def get_observation_model(observable_df: pd.DataFrame
observables = {}
sigmas = {}

nan_pat = r'^[nN]a[nN]$'
for _, observable in observable_df.iterrows():
oid = observable.name
oid = str(observable.name)
# need to sanitize due to https://github.com/PEtab-dev/PEtab/issues/447
pat = r'^[nN]a[nN]$'
name = re.sub(pat, '', str(observable.get(OBSERVABLE_NAME, '')))
formula_obs = re.sub(pat, '', str(observable[OBSERVABLE_FORMULA]))
formula_noise = re.sub(pat, '', str(observable[NOISE_FORMULA]))
name = re.sub(nan_pat, '', str(observable.get(OBSERVABLE_NAME, '')))
formula_obs = re.sub(nan_pat, '', str(observable[OBSERVABLE_FORMULA]))
formula_noise = re.sub(nan_pat, '', str(observable[NOISE_FORMULA]))
observables[oid] = {'name': name, 'formula': formula_obs}
sigmas[oid] = formula_noise

Expand All @@ -617,7 +631,8 @@ def get_observation_model(observable_df: pd.DataFrame
return observables, noise_distrs, sigmas


def petab_noise_distributions_to_amici(observable_df: pd.DataFrame) -> Dict:
def petab_noise_distributions_to_amici(observable_df: pd.DataFrame
) -> Dict[str, str]:
"""
Map from the petab to the amici format of noise distribution
identifiers.
Expand Down Expand Up @@ -689,6 +704,10 @@ def parse_cli_args():
parser.add_argument('--no-compile', action='store_false',
dest='compile',
help='Only generate model code, do not compile')
parser.add_argument('--flatten', dest='flatten', default=False,
action='store_true',
help='Flatten measurement specific overrides of '
'observable and noise parameters')
Comment on lines +707 to +710
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# Call with set of files
parser.add_argument('-s', '--sbml', dest='sbml_file_name',
Expand Down Expand Up @@ -740,6 +759,9 @@ def main():
# First check for valid PEtab
petab.lint_problem(pp)

if args.flatten:
petab.flatten_timepoint_specific_output_overrides(pp)

import_model(model_name=args.model_name,
sbml_model=pp.sbml_model,
condition_table=pp.condition_df,
Expand Down
56 changes: 42 additions & 14 deletions python/amici/petab_import_pysb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ def __init__(self, pysb_model: 'pysb.Model' = None, *args, **kwargs):
:param args: See :meth:`petab.Problem.__init__`
:param kwargs: See :meth:`petab.Problem.__init__`
"""

flatten = kwargs.pop('flatten', False)
super().__init__(*args, **kwargs)
if flatten:
petab.flatten_timepoint_specific_output_overrides(self)

self.pysb_model: 'pysb.Model' = pysb_model
self._add_observation_model()
Expand Down Expand Up @@ -108,17 +110,33 @@ def from_files(condition_file: str = None,
visualization_files: Union[str, Iterable[str]] = None,
observable_files: Union[str, Iterable[str]] = None,
pysb_model_file: str = None,
) -> 'PysbPetabProblem':
flatten: bool = False) -> 'PysbPetabProblem':
"""
Factory method to load model and tables from files.

Arguments:
condition_file: PEtab condition table
measurement_file: PEtab measurement table
parameter_file: PEtab parameter table
visualization_files: PEtab visualization tables
observable_files: PEtab observables tables
pysb_model_file: PySB model file
:param condition_file:
PEtab condition table

:param measurement_file:
PEtab measurement table

:param parameter_file:
PEtab parameter table

:param visualization_files:
PEtab visualization tables

:param observable_files:
PEtab observables tables

:param pysb_model_file:
PySB model file

:param flatten:
Flatten the petab problem

:return:
Petab Problem
"""

condition_df = measurement_df = parameter_df = visualization_df = None
Expand Down Expand Up @@ -152,18 +170,27 @@ def from_files(condition_file: str = None,
measurement_df=measurement_df,
parameter_df=parameter_df,
observable_df=observable_df,
visualization_df=visualization_df)
visualization_df=visualization_df,
flatten=flatten
)

@staticmethod
def from_yaml(yaml_config: Union[Dict, str]) -> 'PysbPetabProblem':
def from_yaml(yaml_config: Union[Dict, str],
flatten: bool = False) -> 'PysbPetabProblem':
"""
Factory method to load model and tables as specified by YAML file.

NOTE: The PySB model is currently expected in the YAML file under
``sbml_files``.

Arguments:
yaml_config: PEtab configuration as dictionary or YAML file name
:param yaml_config:
PEtab configuration as dictionary or YAML file name

:param flatten:
Flatten the petab problem

:return:
Petab Problem
"""
from petab.yaml import (load_yaml, is_composite_problem,
assert_single_condition_and_sbml_file)
Expand Down Expand Up @@ -210,7 +237,8 @@ def from_yaml(yaml_config: Union[Dict, str]) -> 'PysbPetabProblem':
for f in problem0.get(VISUALIZATION_FILES, [])],
observable_files=[
os.path.join(path_prefix, f)
for f in problem0.get(OBSERVABLE_FILES, [])]
for f in problem0.get(OBSERVABLE_FILES, [])],
flatten=flatten
)


Expand Down
2 changes: 1 addition & 1 deletion python/sdist/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ include_package_data = True
zip_safe = False

[options.extras_require]
petab = petab>=0.1.11
petab = petab>=0.1.17
pysb = pysb>=1.11.0

[options.package_data]
Expand Down
6 changes: 2 additions & 4 deletions tests/benchmark-models/benchmark_models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Borghans_BiophysChem1997:
note: benchmark collection reference value matches up to sign when applying log10-correction +sum(log(meas*log(10)) / 2

Brannmark_JBC2010:
llh: 283.778227541074
note: unchecked
llh: -141.889113770537

#Bruno_JExpBio2016: None

Expand All @@ -36,8 +35,7 @@ Elowitz_Nature2000:
note: benchmark collection reference value matches up to sign when applying log10-correction +sum(log(meas*log(10))) / 2

Fiedler_BMC2016:
llh: -117.16780323362
note: unchecked
llh: 58.58390161681

Fujita_SciSignal2010:
llh: 53.08749124997969
Expand Down
13 changes: 6 additions & 7 deletions tests/benchmark-models/test_benchmark_collection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ models="
Beer_MolBioSystems2014
Boehm_JProteomeRes2014
Borghans_BiophysChem1997
Brannmark_JBC2010
Crauste_CellSystems2017
Elowitz_Nature2000
Fiedler_BMC2016
Fujita_SciSignal2010
Schwen_PONE2014
Sneyd_PNAS2002
Expand All @@ -36,12 +38,6 @@ Zheng_PNAS2012"
# Perelson_Science1996
# Bruno_JExpBio2016
#
# Timepoint-specific parameter overrides
# Fiedler_BMC2016
# Brannmark_JBC2010
# Isensee_JCB2018
# Sobotta_Frontiers2017
#
# yaml missing:
# Casaletto_PNAS2019
#
Expand All @@ -59,6 +55,9 @@ Zheng_PNAS2012"
#
# Evaluation is known to be inconsistent:
# Chen_MSB2009
#
# Integration Failure:
# Isensee_JCB2018

set -e

Expand Down Expand Up @@ -91,7 +90,7 @@ for model in $models; do
yaml="${model_dir}"/"${model}"/"${model}".yaml
amici_model_dir=test_bmc/"${model}"
mkdir -p "$amici_model_dir"
cmd_import="amici_import_petab --verbose -y ${yaml} -o ${amici_model_dir} -n ${model}"
cmd_import="amici_import_petab --verbose -y ${yaml} -o ${amici_model_dir} -n ${model} --flatten"
cmd_run="$script_path/test_petab_model.py --verbose -y ${yaml} -d ${amici_model_dir} -m ${model} -c"

printf '=%.0s' {1..40}
Expand Down
1 change: 1 addition & 0 deletions tests/benchmark-models/test_petab_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def main():

# load PEtab files
problem = petab.Problem.from_yaml(args.yaml_file_name)
petab.flatten_timepoint_specific_output_overrides(problem)

# load model
if args.model_directory:
Expand Down
9 changes: 8 additions & 1 deletion tests/petab_test_suite/test_petab_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ def _test_case(case, model_type):
case_dir = os.path.join(petabtests.PYSB_DIR, case)
# import petab problem
yaml_file = os.path.join(case_dir, petabtests.problem_yaml_name(case))
problem = PysbPetabProblem.from_yaml(yaml_file)
problem = PysbPetabProblem.from_yaml(yaml_file,
flatten=case.startswith('0006'))
else:
raise ValueError(f"Unsupported model_type: {model_type}")

# compile amici model
if case.startswith('0006') and model_type != "pysb":
petab.flatten_timepoint_specific_output_overrides(problem)
model_output_dir = f'amici_models/model_{case}'
model = import_petab_problem(
problem, model_output_dir=model_output_dir,
Expand All @@ -83,6 +86,10 @@ def _test_case(case, model_type):
gt_chi2 = solution[petabtests.CHI2]
gt_llh = solution[petabtests.LLH]
gt_simulation_dfs = solution[petabtests.SIMULATION_DFS]
if case.startswith('0006'):
# account for flattening
gt_simulation_dfs[0].loc[:, petab.OBSERVABLE_ID] = ('obs_a__10__c0',
'obs_a__15__c0')
tol_chi2 = solution[petabtests.TOL_CHI2]
tol_llh = solution[petabtests.TOL_LLH]
tol_simulations = solution[petabtests.TOL_SIMULATIONS]
Expand Down