示例#1
0
def extended_simulator(request):
    modelclass = ModelLibrary.modelclasses[request.param]
    model_parameters = ModelLibrary.model_parameters[request.param]
    initial_values = ModelLibrary.initial_values[request.param]
    return ExtendedSimulator(bioprocess_model_class=modelclass,
                             model_parameters=model_parameters,
                             initial_values=initial_values)
示例#2
0
 def test_init(self, name):
     modelclass = ModelLibrary.modelclasses[name]
     initial_values = ModelLibrary.initial_values[name]
     model_parameters = ModelLibrary.model_parameters[name]
     ExtendedSimulator(bioprocess_model_class=modelclass,
                       model_parameters=model_parameters,
                       initial_values=initial_values)
示例#3
0
 def test_bad_observation_function_checking(self, model_checker, model,
                                            bad_obsfun):
     # Get all building blocks for the bioprocess model
     name = 'model03'
     model_parameters = ModelLibrary.model_parameters[name]
     initial_values = ModelLibrary.initial_values[name]
     # Get all buidling blocks for the observation function
     name = 'obsfun01'
     observed_state = ObservationFunctionLibrary.observed_states[name]
     observation_parameters = ObservationFunctionLibrary.observation_function_parameters[
         name]
     # Create an extended simulator for checking
     extended_simulator = ExtendedSimulator(
         bioprocess_model_class=model,
         model_parameters=model_parameters,
         initial_values=initial_values,
         observation_functions_parameters=[
             (bad_obsfun, {
                 **observation_parameters,
                 'observed_state': observed_state,
             }),
         ])
     # These checks sould raise any warnings
     with pytest.warns(UserWarning):
         model_checker.check_model_consistency(extended_simulator)
示例#4
0
 def test_bioprocess_model_checking(self, model_checker, model):
     name = 'model03'
     model_parameters = ModelLibrary.model_parameters[name]
     initial_values = ModelLibrary.initial_values[name]
     extended_simulator = ExtendedSimulator(
         bioprocess_model_class=model,
         model_parameters=model_parameters,
         initial_values=initial_values)
     # These models should not raise any warnings
     model_checker.check_model_consistency(extended_simulator)
示例#5
0
 def test_bioprocess_bad_model_checking(self, model_checker, bad_model):
     name = 'model03'
     model_parameters = ModelLibrary.model_parameters[name]
     initial_values = ModelLibrary.initial_values[name]
     extended_simulator = ExtendedSimulator(
         bioprocess_model_class=bad_model,
         model_parameters=model_parameters,
         initial_values=initial_values)
     # These models should raise warnings for different reasons (cf. the specific model in the modelling library for details)
     with pytest.warns(UserWarning):
         model_checker.check_model_consistency(extended_simulator)
示例#6
0
 def test_model06_variants(self, model_checker, model_variant,
                           initial_switches, expected_behavior):
     name = 'model06'
     model_parameters = ModelLibrary.model_parameters[name]
     initial_values = ModelLibrary.initial_values[name]
     extended_simulator = ExtendedSimulator(
         bioprocess_model_class=model_variant,
         model_parameters=model_parameters,
         initial_values=initial_values,
         initial_switches=initial_switches,
     )
     if expected_behavior == 'UserWarning':
         with pytest.warns(UserWarning):
             model_checker.check_model_consistency(extended_simulator)
     elif expected_behavior == 'NameError':
         with pytest.raises(NameError):
             model_checker.check_model_consistency(extended_simulator)
     else:
         model_checker.check_model_consistency(extended_simulator)
示例#7
0
    def test_extended_simulator_with_observations(self):
        # Get building blocks for BioprocessModel
        name = 'model01'
        modelclass = ModelLibrary.modelclasses[name]
        initial_values = ModelLibrary.initial_values[name]
        model_parameters = ModelLibrary.model_parameters[name]
        # Get building blocks for ObservationFunctions
        obsfun_name = 'obsfun01'
        obsfun = ObservationFunctionLibrary.observation_functions[obsfun_name]
        obsfun_parameters = ObservationFunctionLibrary.observation_function_parameters[
            obsfun_name]
        observed_state = ObservationFunctionLibrary.observed_states[
            obsfun_name]
        obsfuns_params = [(obsfun, {
            **obsfun_parameters, 'observed_state':
            observed_state
        })]

        # Set new values for parameters, using an extended simulator
        extended_simulator = ExtendedSimulator(
            bioprocess_model_class=modelclass,
            model_parameters=model_parameters,
            initial_values=initial_values,
            observation_functions_parameters=obsfuns_params,
        )
        params = extended_simulator.get_all_parameters()
        extended_simulator.set_parameters(
            {_p: params[_p] * 1.05
             for _p in params})

        # Get some prediction to be used as artifical data
        predicitions = extended_simulator.simulate(t=24)
        measurements = [
            Measurement(name=_prediction.name,
                        timepoints=_prediction.timepoints,
                        values=_prediction.values,
                        errors=np.ones_like(_prediction.values))
            for _prediction in predicitions
        ]
        extended_simulator._get_loss(metric='negLL', measurements=measurements)
示例#8
0
 def test_with_model_enforcing_CVodeError(self):
     name = 'model06'
     modelclass = ModelLibrary.modelclasses[name]
     model_parameters = ModelLibrary.model_parameters[name]
     initial_values = ModelLibrary.initial_values[name]
     extended_simulator = ExtendedSimulator(
         bioprocess_model_class=modelclass,
         model_parameters=model_parameters,
         initial_values=initial_values)
     # The chosen model will create an integration error for rate = 0. A RuntimeWarning is tehrefore raised before the CVodeError is raised
     with pytest.warns(RuntimeWarning):
         with pytest.raises(CVodeError):
             extended_simulator.simulate(t=24, parameters={'rate0': 0})
     with pytest.warns(RuntimeWarning):
         with pytest.raises(CVodeError):
             extended_simulator._get_loss_for_minimzer(
                 metric='negLL',
                 guess_dict={'rate0': 0},
                 measurements=[
                     Measurement(name='y0',
                                 timepoints=[1, 2, 3],
                                 values=[10, 20, 30]),
                 ],
                 handle_CVodeError=False,
                 verbosity_CVodeError=False,
             )
     # For toxic parameters causing integration errors, CVodeError handling results in inf loss
     with pytest.warns(RuntimeWarning):
         assert np.isinf(
             extended_simulator._get_loss_for_minimzer(
                 metric='negLL',
                 guess_dict={'rate0': 0},
                 measurements=[
                     Measurement(name='y0',
                                 timepoints=[1, 2, 3],
                                 values=[10, 20, 30]),
                 ],
                 handle_CVodeError=True,
                 verbosity_CVodeError=True,
             ))