Exemplo n.º 1
0
def create_data_tables(model, condition_df):
    """Create synthetic data for parameter estimation

    - Simulate time-course for four different conditions
    - Add gaussian noise according to selected sigma parameter
    - Mimic 2 experimental batches: odd-numbered condition indices
    and even-numbered conditions have different offset parameter

    Arguments:

    """
    timepoints = np.logspace(-5, 1, 20)
    sigma_default = 0.1  # parameters are lin
    sigma_parameter = 0.2
    offset_batch_1 = 3.0
    offset_batch_2 = 4.0

    parameter_ids = list(model.getParameterIds())
    observable_ids = list(model.getObservableIds())

    sigma_parameter_observable_idx = \
        observable_ids.index('obs_x1withsigma')
    model_offset_parameter_idx = \
        parameter_ids.index('observableParameter1_obs_x2_offsetted')
    sigma_parameter_idx = \
        parameter_ids.index('noiseParameter1_obs_x1withsigma')

    # set true parameters
    default_parameters = np.array(model.getParameters())
    default_parameters[sigma_parameter_idx] = sigma_parameter
    print('Default model parameters:')
    for p, val in zip(model.getParameterIds(), model.getParameters()):
        print(f'\t{p}: {val}')
    print()

    true_parameters = {
        pid: val
        for pid, val in zip(model.getParameterIds(), default_parameters)
    }
    # output parameters don't have default values from SBML mdoel
    true_parameters['observableParameter1_obs_x1_scaled'] = 2.0
    true_parameters['noiseParameter1_obs_x1withsigma'] = 0.2
    true_parameters['noiseParameter1_obs_x1_scaled'] = 0.2
    true_parameters['noiseParameter1_obs_x2'] = 0.2
    true_parameters['noiseParameter1_obs_x1'] = 0.2
    true_parameters['noiseParameter1_obs_x2_offsetted'] = 0.2
    true_parameters['noiseParameter1_obs_x3'] = 0.2
    true_parameters['observableParameter1_obs_x2_offsetted'] = 3.0

    true_parameters['scaling_x1_common'] = \
        true_parameters['observableParameter1_obs_x1_scaled']
    # extend to optimization parameter vector: add second offset parameter
    true_parameters['offset_x2_batch_0'] = offset_batch_1
    true_parameters['offset_x2_batch_1'] = offset_batch_2
    true_parameters['x1withsigma_sigma'] = sigma_parameter

    print('True parameters:\t%s' % true_parameters)

    # setup model
    model.setTimepoints(timepoints)
    model.setParameters(default_parameters)

    # setup solver
    solver = model.getSolver()
    solver.setMaxSteps(10000)

    print(condition_df)
    measurement_df = petab.create_measurement_df()

    print()

    # set sigmas
    sigmay = np.ones(shape=(model.nt(), model.nytrue)) * sigma_default
    # observable with sigma parameter
    sigmay[:, sigma_parameter_observable_idx] = np.nan

    # llh for noisy simulated data with true parameters
    expected_llh = 0.0

    for condition_idx, condition_id in enumerate(condition_df.index.values):
        condition_parameters = condition_df.loc[condition_id, :]
        print(
            f'Condition {condition_idx} "{condition_id}":  {condition_parameters}'
        )

        # different offset for two "batches"
        batch_id = condition_idx % 2
        model_parameters = default_parameters[:]
        if batch_id == 0:
            model_parameters[model_offset_parameter_idx] = offset_batch_1
        else:
            model_parameters[model_offset_parameter_idx] = offset_batch_2

        print('Model parameters:', model_parameters)

        # simulate condition
        rdata = getReturnDataForCondition(model, solver, condition_parameters,
                                          model_parameters, sigmay,
                                          sigma_parameter_observable_idx,
                                          sigma_parameter_idx)

        print('\tllh: ', rdata['llh'])
        print('\tsllh', rdata['sllh'])

        expected_llh += rdata['llh']

        measurement_df = append_measurements_for_condition(
            model, measurement_df, sigmay, condition_id, batch_id, rdata)
        print()

    print('Expected llh: ', expected_llh)

    return measurement_df, true_parameters, expected_llh
Exemplo n.º 2
0
def test_create_measurement_df():
    """Test measurements.create_measurement_df."""
    df = petab.create_measurement_df()
    assert set(df.columns.values) == set(MEASUREMENT_DF_COLS)