Esempio n. 1
0
    def tests_presimulation(self):
        self.model.getFixedParameterNames()
        combos = itertools.product([(10, 5), (5, 10), ()], repeat=3)
        cases = dict()
        for icombo, combo in enumerate(combos):
            cases[f'{icombo}'] = {
                'fixedParameters': combo[0],
                'fixedParametersPreequilibration': combo[1],
                'fixedParametersPresimulation': combo[2],
            }

        for case in cases:
            with self.subTest(**cases[case]):
                for fp in cases[case]:
                    setattr(self.edata[0], fp, cases[case][fp])

                df_edata = amici.getDataObservablesAsDataFrame(
                    self.model, self.edata)
                edata_reconstructed = amici.getEdataFromDataFrame(
                    self.model, df_edata)

                for fp in [
                        'fixedParameters', 'fixedParametersPreequilibration',
                        'fixedParametersPresimulation'
                ]:

                    if fp != 'fixedParameters' or cases[case][fp] is not ():
                        self.assertTupleEqual(
                            getattr(self.edata[0], fp),
                            getattr(edata_reconstructed[0], fp),
                        )

                        self.assertTupleEqual(
                            cases[case][fp],
                            getattr(edata_reconstructed[0], fp),
                        )

                        self.assertTupleEqual(
                            getattr(self.edata[0], fp),
                            cases[case][fp],
                        )
                    else:
                        self.assertTupleEqual(
                            self.model.getFixedParameters(),
                            getattr(edata_reconstructed[0], fp),
                        )

                        self.assertTupleEqual(
                            self.model.getFixedParameters(),
                            getattr(edata_reconstructed[0], fp),
                        )

                        self.assertTupleEqual(
                            getattr(self.edata[0], fp),
                            cases[case][fp],
                        )
Esempio n. 2
0
def test_steadystate_simulation(model_steadystate_module):
    model = model_steadystate_module.getModel()
    model.setTimepoints(np.linspace(0, 60, 60))
    solver = model.getSolver()
    solver.setSensitivityOrder(amici.SensitivityOrder.first)
    rdata = amici.runAmiciSimulation(model, solver)
    edata = [amici.ExpData(rdata, 1, 0)]
    rdata = amici.runAmiciSimulations(model, solver, edata)

    # check roundtripping of DataFrame conversion
    df_edata = amici.getDataObservablesAsDataFrame(model, edata)
    edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata)

    assert np.isclose(
        amici.ExpDataView(edata[0])['observedData'],
        amici.ExpDataView(edata_reconstructed[0])['observedData']).all()

    assert np.isclose(
        amici.ExpDataView(edata[0])['observedDataStdDev'],
        amici.ExpDataView(edata_reconstructed[0])['observedDataStdDev']).all()

    if len(edata[0].fixedParameters):
        assert list(edata[0].fixedParameters) \
               == list(edata_reconstructed[0].fixedParameters)

    else:
        assert list(model.getFixedParameters()) \
               == list(edata_reconstructed[0].fixedParameters)

    assert list(edata[0].fixedParametersPreequilibration) == \
        list(edata_reconstructed[0].fixedParametersPreequilibration)

    df_state = amici.getSimulationStatesAsDataFrame(model, edata, rdata)
    assert np.isclose(rdata[0]['x'],
                      df_state[list(model.getStateIds())].values).all()

    df_obs = amici.getSimulationObservablesAsDataFrame(model, edata, rdata)
    assert np.isclose(rdata[0]['y'],
                      df_obs[list(model.getObservableIds())].values).all()
    amici.getResidualsAsDataFrame(model, edata, rdata)

    solver.setRelativeTolerance(1e-12)
    solver.setAbsoluteTolerance(1e-12)
    check_derivatives(model,
                      solver,
                      edata[0],
                      assert_fun,
                      atol=1e-3,
                      rtol=1e-3,
                      epsilon=1e-4)

    # Run some additional tests which need a working Model,
    # but don't need precomputed expectations.
    _test_set_parameters_by_dict(model_steadystate_module)
Esempio n. 3
0
def test_pandas_import_export(sbml_example_presimulation_module, case):
    """TestCase class for testing csv import using pandas"""

    # setup
    model = sbml_example_presimulation_module.getModel()
    model.setTimepoints(np.linspace(0, 60, 61))
    solver = model.getSolver()
    rdata = amici.runAmiciSimulation(model, solver)
    edata = [amici.ExpData(rdata, 0.01, 0)]

    # test copy constructor
    _ = amici.ExpData(edata[0])

    for fp in case:
        setattr(edata[0], fp, case[fp])

    df_edata = amici.getDataObservablesAsDataFrame(model, edata)
    edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata)

    for fp in [
            'fixedParameters', 'fixedParametersPreequilibration',
            'fixedParametersPresimulation'
    ]:

        if fp != 'fixedParameters' or case[fp] != ():
            assert getattr(edata[0], fp) == getattr(edata_reconstructed[0], fp)

            assert case[fp] == getattr(edata_reconstructed[0], fp)

        else:
            assert model.getFixedParameters() \
                == getattr(edata_reconstructed[0], fp)

            assert model.getFixedParameters() == \
                getattr(edata_reconstructed[0], fp)

        assert getattr(edata[0], fp) == case[fp]
    def test_steadystate_scaled(self):
        """
        Test SBML import and simulation from AMICI python interface
        """
        def assert_fun(x):
            return self.assertTrue(x)

        sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python',
                                'examples', 'example_steadystate',
                                'model_steadystate_scaled.xml')
        sbmlImporter = amici.SbmlImporter(sbmlFile)

        observables = amici.assignmentRules2observables(
            sbmlImporter.sbml,
            filter_function=lambda variable:
                variable.getId().startswith('observable_') and
                not variable.getId().endswith('_sigma')
        )

        outdir = 'test_model_steadystate_scaled'
        sbmlImporter.sbml2amici('test_model_steadystate_scaled',
                                outdir,
                                observables=observables,
                                constantParameters=['k0'],
                                sigmas={'observable_x1withsigma':
                                        'observable_x1withsigma_sigma'})

        sys.path.insert(0, outdir)
        import test_model_steadystate_scaled as modelModule

        model = modelModule.getModel()
        model.setTimepoints(np.linspace(0, 60, 60))
        solver = model.getSolver()
        solver.setSensitivityOrder(amici.SensitivityOrder_first)
        rdata = amici.runAmiciSimulation(model, solver)
        edata = [amici.ExpData(rdata, 1, 0)]
        rdata = amici.runAmiciSimulations(model, solver, edata)

        # check roundtripping of DataFrame conversion
        df_edata = amici.getDataObservablesAsDataFrame(model, edata)
        edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata)

        self.assertTrue(
            np.isclose(
                amici.ExpDataView(edata[0])
                ['observedData'],
                amici.ExpDataView(edata_reconstructed[0])
                ['observedData'],
            ).all()
        )
        self.assertTrue(
            np.isclose(
                amici.ExpDataView(edata[0])
                ['observedDataStdDev'],
                amici.ExpDataView(edata_reconstructed[0])
                ['observedDataStdDev'],
            ).all()
        )
        if len(edata[0].fixedParameters):
            self.assertListEqual(
                list(edata[0].fixedParameters),
                list(edata_reconstructed[0].fixedParameters),
            )
        else:
            self.assertListEqual(
                list(model.getFixedParameters()),
                list(edata_reconstructed[0].fixedParameters),
            )

        self.assertListEqual(
            list(edata[0].fixedParametersPreequilibration),
            list(edata_reconstructed[0].fixedParametersPreequilibration),
        )

        df_state = amici.getSimulationStatesAsDataFrame(model, edata, rdata)
        self.assertTrue(
            np.isclose(
                rdata[0]['x'],
                df_state[list(model.getStateIds())].values
            ).all()
        )
        df_obs = amici.getSimulationObservablesAsDataFrame(model, edata, rdata)
        self.assertTrue(
            np.isclose(
                rdata[0]['y'],
                df_obs[list(model.getObservableIds())].values
            ).all()
        )
        amici.getResidualsAsDataFrame(model, edata, rdata)

        solver.setRelativeTolerance(1e-12)
        solver.setAbsoluteTolerance(1e-12)
        check_derivatives(model, solver, edata[0], assert_fun, atol=1e-3,
                          rtol=1e-3, epsilon=1e-4)
Esempio n. 5
0
    def test_steadystate_scaled(self):
        '''
        Test SBML import and simulation from AMICI python interface
        '''

        sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python',
                                'examples', 'example_steadystate',
                                'model_steadystate_scaled.xml')
        sbmlImporter = amici.SbmlImporter(sbmlFile)

        observables = amici.assignmentRules2observables(
            sbmlImporter.sbml,
            filter_function=lambda variable: variable.getId().startswith(
                'observable_') and not variable.getId().endswith('_sigma'))

        outdir = 'test_model_steadystate_scaled'
        sbmlImporter.sbml2amici(
            'test_model_steadystate_scaled',
            outdir,
            observables=observables,
            constantParameters=['k0'],
            sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'})

        sys.path.insert(0, outdir)
        import test_model_steadystate_scaled as modelModule

        model = modelModule.getModel()
        model.setTimepoints(amici.DoubleVector(np.linspace(0, 60, 60)))
        solver = model.getSolver()
        rdata = amici.runAmiciSimulation(model, solver)
        edata = [amici.ExpData(rdata, 0.01, 0)]
        rdata = amici.runAmiciSimulations(model, solver, edata)

        # check roundtripping of DataFrame conversion
        df_edata = amici.getDataObservablesAsDataFrame(model, edata)
        edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata)

        self.assertTrue(
            np.isclose(
                amici.edataToNumPyArrays(edata[0])['observedData'],
                amici.edataToNumPyArrays(
                    edata_reconstructed[0])['observedData'],
            ).all())
        self.assertTrue(
            np.isclose(
                amici.edataToNumPyArrays(edata[0])['observedDataStdDev'],
                amici.edataToNumPyArrays(
                    edata_reconstructed[0])['observedDataStdDev'],
            ).all())
        if edata[0].fixedParameters.size():
            self.assertListEqual(
                list(edata[0].fixedParameters),
                list(edata_reconstructed[0].fixedParameters),
            )
        else:
            self.assertListEqual(
                list(model.getFixedParameters()),
                list(edata_reconstructed[0].fixedParameters),
            )

        self.assertListEqual(
            list(edata[0].fixedParametersPreequilibration),
            list(edata_reconstructed[0].fixedParametersPreequilibration),
        )

        df_state = amici.getSimulationStatesAsDataFrame(model, edata, rdata)
        self.assertTrue(
            np.isclose(rdata[0]['x'],
                       df_state[list(model.getStateIds())].values).all())
        df_obs = amici.getSimulationObservablesAsDataFrame(model, edata, rdata)
        self.assertTrue(
            np.isclose(rdata[0]['y'],
                       df_obs[list(model.getObservableIds())].values).all())
        amici.getResidualsAsDataFrame(model, edata, rdata)