Beispiel #1
0
def pysb_example_presimulation_module():
    """PySB example_presimulation model module fixture"""

    constant_parameters = ['DRUG_0', 'KIN_0']

    pysb.SelfExporter.cleanup()  # reset pysb
    pysb.SelfExporter.do_export = True

    model_path = os.path.join(os.path.dirname(__file__), '..',
                              'examples', 'example_presimulation')

    with amici.add_path(model_path):
        if 'createModelPresimulation' in sys.modules:
            importlib.reload(sys.modules['createModelPresimulation'])
            model_module = sys.modules['createModelPresimulation']
        else:
            model_module = importlib.import_module('createModelPresimulation')
    model = copy.deepcopy(model_module.model)

    model.name = 'test_model_presimulation_pysb'
    outdir = model.name
    pysb2amici(model, outdir, verbose=True,
               observables=['pPROT_obs'],
               constant_parameters=constant_parameters)

    with amici.add_path(outdir):
        model_module_pysb = importlib.import_module(outdir)

    yield model_module_pysb

    shutil.rmtree(outdir, ignore_errors=True)
Beispiel #2
0
def _can_import_model(model_name: str, model_output_dir: str) -> bool:
    """
    Check whether a module of that name can already be imported.
    """
    # try to import (in particular checks version)
    try:
        with amici.add_path(model_output_dir):
            model_module = importlib.import_module(model_name)
    except ModuleNotFoundError:
        return False

    # no need to (re-)compile
    return hasattr(model_module, "getModel")
Beispiel #3
0
def test_compare_to_pysb_simulation(example):
    pysb = pytest.importorskip("pysb")

    atol = 1e-8
    rtol = 1e-8

    with amici.add_path(os.path.dirname(pysb.examples.__file__)):
        with amici.add_path(
                os.path.join(os.path.dirname(__file__), '..', 'tests',
                             'pysb_test_models')):

            if example == 'earm_1_3' \
                    and platform.sys.version_info[0] == 3 \
                    and platform.sys.version_info[1] < 7:
                return

            # load example

            pysb.SelfExporter.cleanup()  # reset pysb
            pysb.SelfExporter.do_export = True

            module = importlib.import_module(example)
            pysb_model = module.model
            pysb_model.name = pysb_model.name.replace('pysb.examples.', '')
            # avoid naming clash for custom pysb models
            pysb_model.name += '_amici'

            # pysb part

            tspan = np.linspace(0, 100, 101)
            sim = ScipyOdeSimulator(pysb_model,
                                    tspan=tspan,
                                    integrator_options={
                                        'rtol': rtol,
                                        'atol': atol
                                    },
                                    compiler='python')
            pysb_simres = sim.run()

            # amici part

            outdir = pysb_model.name

            if pysb_model.name in ['move_connected_amici']:
                with pytest.raises(Exception):
                    pysb2amici(pysb_model,
                               outdir,
                               verbose=logging.INFO,
                               compute_conservation_laws=True)
                compute_conservation_laws = False
            else:
                compute_conservation_laws = True

            pysb2amici(pysb_model,
                       outdir,
                       verbose=logging.INFO,
                       compute_conservation_laws=compute_conservation_laws)
            sys.path.insert(0, outdir)

            amici_model_module = importlib.import_module(pysb_model.name)

            model_pysb = amici_model_module.getModel()

            model_pysb.setTimepoints(tspan)

            solver = model_pysb.getSolver()
            solver.setMaxSteps(int(1e5))
            solver.setAbsoluteTolerance(atol)
            solver.setRelativeTolerance(rtol)
            rdata = amici.runAmiciSimulation(model_pysb, solver)

            # check agreement of species simulation

            assert np.isclose(rdata['x'], pysb_simres.species, 1e-4,
                              1e-4).all()

            shutil.rmtree(outdir, ignore_errors=True)
Beispiel #4
0
def test_compare_to_pysb_simulation(example):
    pysb = pytest.importorskip("pysb")

    atol = 1e-8
    rtol = 1e-8

    with amici.add_path(os.path.dirname(pysb.examples.__file__)):
        with amici.add_path(
                os.path.join(os.path.dirname(__file__), '..', 'tests',
                             'pysb_test_models')):

            if example == 'earm_1_3' \
                    and platform.sys.version_info[0] == 3 \
                    and platform.sys.version_info[1] < 7:
                return

            # load example
            pysb.SelfExporter.cleanup()  # reset pysb
            pysb.SelfExporter.do_export = True

            module = importlib.import_module(example)
            pysb_model = module.model
            pysb_model.name = pysb_model.name.replace('pysb.examples.', '')
            # avoid naming clash for custom pysb models
            pysb_model.name += '_amici'

            # pysb part
            tspan = np.linspace(0, 100, 101)
            sim = ScipyOdeSimulator(pysb_model,
                                    tspan=tspan,
                                    integrator_options={
                                        'rtol': rtol,
                                        'atol': atol
                                    },
                                    compiler='python')
            pysb_simres = sim.run()

            # amici part

            outdir = pysb_model.name

            if pysb_model.name in ['move_connected_amici']:
                with pytest.raises(Exception):
                    pysb2amici(pysb_model,
                               outdir,
                               verbose=logging.INFO,
                               compute_conservation_laws=True)
                compute_conservation_laws = False
            else:
                compute_conservation_laws = True

            pysb2amici(pysb_model,
                       outdir,
                       verbose=logging.INFO,
                       compute_conservation_laws=compute_conservation_laws,
                       observables=list(pysb_model.observables.keys()))

            amici_model_module = amici.import_model_module(
                pysb_model.name, outdir)

            model_pysb = amici_model_module.getModel()

            model_pysb.setTimepoints(tspan)

            solver = model_pysb.getSolver()
            solver.setMaxSteps(int(1e6))
            solver.setAbsoluteTolerance(atol)
            solver.setRelativeTolerance(rtol)
            rdata = amici.runAmiciSimulation(model_pysb, solver)

            # check agreement of species simulation

            assert np.isclose(rdata['x'], pysb_simres.species, 1e-4,
                              1e-4).all()

            if example not in [
                    'fricker_2010_apoptosis', 'fixed_initial',
                    'bngwiki_egfr_simple_deletemolecules'
            ]:
                if example in [
                        'tyson_oscillator', 'bax_pore_sequential', 'bax_pore',
                        'kinase_cascade', 'bngwiki_egfr_simple',
                        'bngwiki_enzymatic_cycle_mm', 'bngwiki_simple'
                ]:
                    solver.setAbsoluteTolerance(1e-14)
                    solver.setRelativeTolerance(1e-14)
                    epsilon = 1e-4
                else:
                    solver.setAbsoluteTolerance(1e-10)
                    solver.setRelativeTolerance(1e-10)
                    epsilon = 1e-3
                model_pysb.setParameterScale(
                    parameterScalingFromIntVector([
                        ParameterScaling.log10
                        if p > 0 else ParameterScaling.none
                        for p in model_pysb.getParameters()
                    ]))
                check_derivatives(model_pysb,
                                  solver,
                                  epsilon=epsilon,
                                  rtol=1e-2,
                                  atol=1e-2,
                                  skip_zero_pars=True)

            shutil.rmtree(outdir, ignore_errors=True)
Beispiel #5
0
from pysb.simulator import ScipyOdeSimulator

sys.path.insert(0, os.path.join('..', 'tests'))
from test_pysb import pysb_models

simulation_times = dict()

N_REPEATS = 100

atol = 1e-8
rtol = 1e-8

for example in pysb_models:
    simulation_times[example] = dict()

    with amici.add_path(os.path.dirname(pysb.examples.__file__)):
        with amici.add_path(
                os.path.join(os.path.dirname(__file__), '..', 'tests',
                             'pysb_test_models')):

            pysb.SelfExporter.cleanup()  # reset pysb
            pysb.SelfExporter.do_export = True

            module = importlib.import_module(example)
            pysb_model = module.model
            pysb_model.name = pysb_model.name.replace('pysb.examples.', '')
            # avoid naming clash for custom pysb models
            pysb_model.name += '_amici'

            # pysb part
            tspan = np.linspace(0, 100, 101)