Пример #1
0
    def runTest(self):
        """
        test runner routine that loads data expectedResults.h5 hdf file and
        runs individual models/settings as subTests
        """
        expected_results = h5py.File(self.expectedResultsFile, 'r')

        for subTest in expected_results.keys():
            for case in list(expected_results[subTest].keys()):
                if case.startswith('sensi2'):
                    model_name = subTest + '_o2'
                else:
                    model_name = subTest

                with self.subTest(modelName=model_name, caseName=case):
                    print(f'running {model_name}::{case}')

                    def assert_fun(x):
                        return self.assertTrue(x)

                    model_swig_folder = \
                        os.path.join(os.path.dirname(__file__), '..',
                                     'build', 'tests', 'cpputest',
                                     f'external_{model_name}-prefix',
                                     'src', f'external_{model_name}-build',
                                     'swig')
                    sys.path.insert(0, model_swig_folder)
                    test_model_module = importlib.import_module(model_name)
                    self.model = test_model_module.getModel()
                    self.solver = self.model.getSolver()
                    amici.readModelDataFromHDF5(
                        self.expectedResultsFile, self.model.get(),
                        f'/{subTest}/{case}/options'
                    )
                    amici.readSolverSettingsFromHDF5(
                        self.expectedResultsFile, self.solver.get(),
                        f'/{subTest}/{case}/options'
                    )

                    edata = None
                    if 'data' in expected_results[subTest][case].keys():
                        edata = amici.readSimulationExpData(
                            self.expectedResultsFile,
                            f'/{subTest}/{case}/data', self.model.get()
                        )
                    rdata = amici.runAmiciSimulation(self.model, self.solver,
                                                     edata)

                    check_derivative_opts = dict()

                    if model_name == 'model_nested_events':
                        check_derivative_opts['rtol'] = 1e-2
                    elif model_name == 'model_events':
                        check_derivative_opts['atol'] = 1e-3

                    if edata \
                            and self.solver.getSensitivityMethod() \
                            and self.solver.getSensitivityOrder() \
                            and len(self.model.getParameterList()) \
                            and not model_name.startswith('model_neuron') \
                            and not case.endswith('byhandpreeq'):
                        check_derivatives(self.model, self.solver, edata,
                                          assert_fun, **check_derivative_opts)

                    verify_simulation_opts = dict()

                    if model_name.startswith('model_neuron'):
                        verify_simulation_opts['atol'] = 1e-5
                        verify_simulation_opts['rtol'] = 1e-2
          
                    if model_name.startswith('model_robertson') and \
                            case == 'sensiforwardSPBCG':
                        verify_simulation_opts['atol'] = 1e-3
                        verify_simulation_opts['rtol'] = 1e-3
                  
                    verify_simulation_results(
                        rdata, expected_results[subTest][case]['results'],
                        assert_fun, **verify_simulation_opts
                    )

                    if model_name == 'model_steadystate' and \
                            case == 'sensiforwarderrorint':
                        edata = amici.amici.ExpData(self.model.get())

                    if edata and model_name != 'model_neuron_o2' and not (
                        model_name == 'model_robertson' and
                        case == 'sensiforwardSPBCG'
                    ):
                        # Test runAmiciSimulations: ensure running twice
                        # with same ExpData yields same results
                        if isinstance(edata, amici.amici.ExpData):
                            edatas = [edata, edata]
                        else:
                            edatas = [edata.get(), edata.get()]

                        rdatas = amici.runAmiciSimulations(
                            self.model, self.solver, edatas, num_threads=2
                        )
                        verify_simulation_results(
                            rdatas[0],
                            expected_results[subTest][case]['results'],
                            assert_fun, **verify_simulation_opts
                        )
                        verify_simulation_results(
                            rdatas[1],
                            expected_results[subTest][case]['results'],
                            assert_fun, **verify_simulation_opts
                        )

                    self.assertRaises(
                        RuntimeError,
                        self.model.getParameterByName,
                        'thisParameterDoesNotExist'
                    )
Пример #2
0
    if case.startswith('sensi2'):
        model_name = sub_test + '_o2'
    else:
        model_name = sub_test

    model_swig_folder = \
        os.path.join(os.path.dirname(__file__), '..', '..', 'build', 'tests',
                     'cpputest', f'external_{model_name}-prefix',
                     'src', f'external_{model_name}-build', 'swig')

    test_model_module = amici.import_model_module(
        module_name=model_name, module_path=model_swig_folder)
    model = test_model_module.getModel()
    solver = model.getSolver()
    amici.readModelDataFromHDF5(options_file, model.get(),
                                f'/{sub_test}/{case}/options')
    amici.readSolverSettingsFromHDF5(options_file, solver.get(),
                                     f'/{sub_test}/{case}/options')

    edata = None
    if 'data' in expected_results[sub_test][case].keys():
        edata = amici.readSimulationExpData(expected_results_file,
                                            f'/{sub_test}/{case}/data',
                                            model.get())
    rdata = amici.runAmiciSimulation(model, solver, edata)

    check_derivative_opts = dict()

    if model_name == 'model_nested_events':
        check_derivative_opts['rtol'] = 1e-2
    elif model_name == 'model_events':
Пример #3
0
    def apply_model_settings(self, model: 'amici.Model'):
        """Apply settings to Model"""

        amici.readModelDataFromHDF5(self.filename, model.get(),
                                    '/amiciOptions')
Пример #4
0
    if case.startswith('sensi2'):
        model_name = sub_test + '_o2'
    else:
        model_name = sub_test

    model_swig_folder = \
        os.path.join(os.path.dirname(__file__), '..', '..', 'build', 'tests',
                     'cpputest', f'external_{model_name}-prefix',
                     'src', f'external_{model_name}-build', 'swig')

    test_model_module = amici.import_model_module(
        module_name=model_name, module_path=model_swig_folder)
    model = test_model_module.getModel()
    solver = model.getSolver()
    amici.readModelDataFromHDF5(expected_results_file, model.get(),
                                f'/{sub_test}/{case}/options')
    amici.readSolverSettingsFromHDF5(expected_results_file, solver.get(),
                                     f'/{sub_test}/{case}/options')

    edata = None
    if 'data' in expected_results[sub_test][case].keys():
        edata = amici.readSimulationExpData(expected_results_file,
                                            f'/{sub_test}/{case}/data',
                                            model.get())
    rdata = amici.runAmiciSimulation(model, solver, edata)

    check_derivative_opts = dict()

    if model_name == 'model_nested_events':
        check_derivative_opts['rtol'] = 1e-2
    elif model_name == 'model_events':