PEtab-dev / PEtab

PEtab - an SBML and TSV based data format for parameter estimation problems in systems biology
https://petab.readthedocs.io
MIT License
56 stars 12 forks source link

- update copasi tests #556

Closed fbergmann closed 1 year ago

fbergmann commented 1 year ago

these tests now run with generation of LLH and CHI2 with the PetabSimulator. If you want to run it yourself here is the code I used:


#!python3
from math import fabs
from basico.petab.core import PetabSimulator

from petab import Problem 
import petabtests
import logging
import sys
import importlib
import os
import yaml
import pandas as pd

if __name__ == "__main__":
    format = 'sbml'
    version = 'v1.0.0'
    case_list = petabtests.get_cases(format_=format, version=version)

    expected_failures = ['0006', '0009', '0010', '0017', '0018']

    for case_id in case_list:
        case_dir = petabtests.get_case_dir(format_=format, version=version,
                                id_=case_id)
        print('# ', format, version, case_id)

        working_dir = os.path.dirname(__file__) + '/temp_cps/'

        petab_problem = Problem.from_yaml(os.path.join(case_dir, '_' + case_id + '.yaml'))
        sim = PetabSimulator(petab_problem, working_dir=working_dir)
        sim_df = sim.simulate()
        sim_df.to_csv('basico_' + case_id + '.tsv', sep='\t', index=False)

        chi2 = petabtests.core.calculate_chi2(
            petab_problem.measurement_df, sim_df, petab_problem.observable_df,
            petab_problem.parameter_df
        )
        llh = petabtests.core.calculate_llh(
            petab_problem.measurement_df, sim_df, petab_problem.observable_df,
            petab_problem.parameter_df
        )
        with open(os.path.join(case_dir, '_' + case_id + '_solution.yaml'), 'r') as stream:
            result = yaml.safe_load(stream)

        sim_df1 = pd.read_csv(os.path.join(case_dir, result['simulation_files'][0]), sep='\t')
        simulations_match = petabtests.evaluate.evaluate_simulations(sim_df, sim_df1, result['tol_simulations'])

        chi2_1 = petabtests.core.calculate_chi2(
            petab_problem.measurement_df, sim_df1, petab_problem.observable_df,
            petab_problem.parameter_df
        )
        llh1 = petabtests.core.calculate_llh(
            petab_problem.measurement_df, sim_df1, petab_problem.observable_df,
            petab_problem.parameter_df
        )

        print (case_id, 'llh', llh, 'llh1', llh1, 'chi2', chi2, 'chi2_1', chi2_1)
        print(simulations_match, fabs(llh - llh1) < result['tol_llh'], fabs(chi2 - chi2_1) < result['tol_chi2'])

        if fabs(llh - llh1) > result['tol_llh'] or fabs(chi2 - chi2_1) > result['tol_chi2']:
            if case_id not in expected_failures:
                print('FAILED')
                breakpoint()

yielding the following output

basico_petab_testsuite.py
using COPASI: 4.39.272 (Source)
#  sbml v1.0.0 0001
0001 llh -0.8475016760597402 llh1 -0.8475016971318831 chi2 0.791837941540571 chi2_1 0.7918379836848566
True True True
#  sbml v1.0.0 0002
WARNING:root:No entry for nan in parameter table
WARNING:root:No entry for nan in parameter table
0002 llh -4.099835828362101 llh1 -4.099835825206062 chi2 0.8481633910868218 chi2_1 0.8481633847747442
True True True
#  sbml v1.0.0 0003
0003 llh -15.871992810002535 llh1 -15.871992877799777 chi2 30.840820209426163 chi2_1 30.840820345020646
True True True
#  sbml v1.0.0 0004
0004 llh -5.692979592587616 llh1 -5.692979609536926 chi2 7.710205052356541 chi2_1 7.710205086255161
True True True
#  sbml v1.0.0 0005
0005 llh -1.9179793630247313 llh1 -1.91797937195749 chi2 0.1602045932307719 chi2_1 0.16020461109628903
True True True
#  sbml v1.0.0 0006
0006 llh -53.84299832494459 llh1 -65.10833033589059 chi2 104.0102425170705 chi2_1 126.54090653896247
False False False
#  sbml v1.0.0 0007
0007 llh -1.3789410172528962 llh1 -1.378941036858004 chi2 0.26829572247148104 chi2_1 0.2682957616816963
True True True
#  sbml v1.0.0 0008
0008 llh -1.1777832443957261 llh1 -1.1777832801267603 chi2 1.0008183729230882 chi2_1 1.000818444385156
True True True
#  sbml v1.0.0 0009
0009 llh -0.8148480114119038 llh1 -0.7579966825976534 chi2 0.726530612244898 chi2_1 0.612827954616397
False False False
#  sbml v1.0.0 0010
0010 llh -1.6475010726522727 llh1 -1.2062894192614277 chi2 2.3918367347256355 chi2_1 1.5094134279439457
False False False
#  sbml v1.0.0 0011
0011 llh -3.4434182700796145 llh1 -3.44341831317718 chi2 5.98367112958032 chi2_1 5.983671215775451
True True True
#  sbml v1.0.0 0012
0012 llh -1.7781137013290782 llh1 -1.778113677297827 chi2 2.6530619920792464 chi2_1 2.653061944016744
True True True
#  sbml v1.0.0 0013
0013 llh -22.790331273498335 llh1 -22.790331328275116 chi2 44.67749713641776 chi2_1 44.677497245971324
True True True
#  sbml v1.0.0 0014
0014 llh -3.6862952889884673 llh1 -3.686295289831353 chi2 0.03167351766162284 chi2_1 0.03167351934739426
True True True
#  sbml v1.0.0 0015
0015 llh -5.060712080985249 llh1 -5.0607120811959705 chi2 0.00791837941540571 chi2_1 0.007918379836848566
True True True
#  sbml v1.0.0 0016
0016 llh -0.784926204970308 llh1 -0.7849262388960581 chi2 0.4400296287476999 chi2_1 0.44002969659920016
True True True
#  sbml v1.0.0 0017
WARNING:root:No entry for nan in parameter table
0017 llh -1.6475010726522727 llh1 -1.220639576243506 chi2 2.3918367347256355 chi2_1 1.5381137419081023
False False False
#  sbml v1.0.0 0018
WARNING:root:No entry for nan in parameter table
0018 llh -22.42076356750548 llh1 -6.389820438547696 chi2 44.86777777760146 chi2_1 12.805891519685881
False False False