Esempio n. 1
0
    def test_metrics_and_loss_caluclation(self, metric):
        
        if metric == 'bad_metric':
            with pytest.raises(NotImplementedError):
                StaticDatatypes.measurement_wo_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate])
        # Only for metric SS (sum-of-squares) the loss can be calculated from Measurement objects without having errors
        elif metric == 'SS':
            assert not np.isnan(StaticDatatypes.measurement_wo_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate]))
            assert not np.isnan(StaticDatatypes.measurement_w_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate]))
        else:
            with pytest.raises(AttributeError):
                StaticDatatypes.measurement_wo_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate])
            assert not np.isnan(StaticDatatypes.measurement_w_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate]))

            # Fail fast for ambiguous, non-unique predictions: list of prediction with the same 'name' and 'replicate_id'
            with pytest.raises(ValueError):
                StaticDatatypes.measurement_w_errs.get_loss(metric=metric, predictions=[StaticDatatypes.modelstate]*2)

            modelstate = ModelState(
                name=StaticHelpers.name, 
                timepoints=StaticHelpers.timepoints[::2], # Use less timepoints
                values=StaticHelpers.values[::2], # Use less values, corresponding to using less timepoints
            )
            # Using predictions that have not matching replicate_ids returns nan loss
            assert np.isnan(StaticDatatypes.measurement_wo_errs.get_loss(metric='SS', predictions=[modelstate]))

            # When adding at least one matching prediction, a loss can be calculated
            assert not np.isnan(StaticDatatypes.measurement_w_errs.get_loss(metric=metric, predictions=[modelstate, StaticDatatypes.modelstate]))
Esempio n. 2
0
class StaticDatatypes():

    timeseries = TimeSeries(
        name=StaticHelpers.name, 
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values,
        replicate_id=StaticHelpers.replicate_id
    )

    modelstate = ModelState(
        name=StaticHelpers.name, 
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values,
        replicate_id=StaticHelpers.replicate_id
    )

    measurement_wo_errs = Measurement(
        name=StaticHelpers.name, 
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values,
        replicate_id=StaticHelpers.replicate_id
    )

    measurement_w_errs = Measurement(
        name=StaticHelpers.name, 
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values,
        errors=StaticHelpers.errors,
        replicate_id=StaticHelpers.replicate_id
    )

    observation = Observation(
        name=StaticHelpers.name, 
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values, 
        observed_state=StaticHelpers.state, 
        replicate_id=StaticHelpers.replicate_id
    )

    sensitivity = Sensitivity(
        timepoints=StaticHelpers.timepoints, 
        values=StaticHelpers.values, 
        response=StaticHelpers.state,
        parameter=StaticHelpers.parameter, 
        replicate_id=StaticHelpers.replicate_id
    )
Esempio n. 3
0
    def test_init_datatypes(self, values, errors, info, replicate_id):
        """
        To test typical instantiations of datatypes. 
        """
        
        # Standard instatiations
        TimeSeries(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, info=info, replicate_id=replicate_id)
        ModelState(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, info=info, replicate_id=replicate_id)
        Measurement(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, errors=errors, info=info, replicate_id=replicate_id)
        Observation(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, observed_state='y1', replicate_id=replicate_id)
        Sensitivity(timepoints=StaticHelpers.timepoints, values=values, response='y1', parameter='p1', replicate_id=replicate_id)
        Sensitivity(timepoints=StaticHelpers.timepoints, values=values, response='y1', parameter='p1', h=1e-8, replicate_id=replicate_id)

        # Must provide timepoints
        with pytest.raises(ValueError):
            TimeSeries(name=StaticHelpers.name, timepoints=None, values=values)

        # Must provide values
        with pytest.raises(ValueError):
            TimeSeries(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=None)

        # Measurements can be created with error_models
        Measurement(
            name=StaticHelpers.name, 
            timepoints=StaticHelpers.timepoints, 
            values=values, 
            error_model=StaticErrorModelHelpers.constant_error_model, 
            error_model_parameters=StaticErrorModelHelpers.constant_error_model_parameters,
        )

        # Must provide a subclass of rvs.continous as p.d.f.
        with pytest.raises(ValueError):
            Measurement(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, error_distribution=scipy.stats.bernoulli)

        # Error values must be >0
        with pytest.raises(ValueError):
            Measurement(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, errors=[0]*len(StaticHelpers.values))
        with pytest.raises(ValueError):
            Measurement(name=StaticHelpers.name, timepoints=StaticHelpers.timepoints, values=values, errors=[-1]*len(StaticHelpers.values))
Esempio n. 4
0
    def test_init_observe(self, simulator, obsfun_name):

        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
        })]
        observer = ModelObserver(
            observation_functions_parameters=obsfuns_params)
        # The observed_state must be indicated in the dictionary with observation function parameters
        with pytest.raises(KeyError):
            ModelObserver(
                observation_functions_parameters=[(obsfun, obsfun_parameters)])
        # Create and observe a Modelstate
        modelstate = ModelState(name=observed_state,
                                timepoints=[1, 2, 3],
                                values=[10, 20, 30])
        observer.get_observations(model_states=[modelstate])
        # There is also a str method
        print(observer)