Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Add to the `hierarchical_problem` the indices of data points as in the
amici.ExpData list.
"""
if hierarchical_problem.is_empty():
# no need to iterate over data again
return
# the iteration over the data must be the same as for the creation
# of the edatas
condition_df = self.petab_problem.condition_df.reset_index()
measurement_df = self.petab_problem.measurement_df
observable_ids = model.getObservableIds()
for condition_ix, condition in simulation_conditions.iterrows():
df_for_condition = petab.core.get_rows_for_condition(
measurement_df, condition)
timepoints = sorted(df_for_condition.time.unique().astype(float))
timepoints_w_reps = _get_timepoints_w_reps(
df_for_condition, timepoints)
for time in timepoints:
df_for_time = df_for_condition[df_for_condition.time == time]
time_ix_0 = timepoints_w_reps.index(time)
time_ix_for_obs_ix = {}
for _, row in df_for_time.iterrows():
observable_ix = observable_ids.index(
f'observable_{row.observableId}')
# (preequilibrationConditionId, simulationConditionId) pairs.
# Can be improved by checking for identical condition vectors.
if simulation_conditions is None:
simulation_conditions = petab.core.get_simulation_conditions(
measurement_df)
observable_ids = model.getObservableIds()
fixed_parameter_ids = model.getFixedParameterIds()
edatas = []
for _, condition in simulation_conditions.iterrows():
# amici.ExpData for each simulation
# extract rows for condition
df_for_condition = petab.core.get_rows_for_condition(
measurement_df, condition)
# make list of all timepoints for which measurements exist
timepoints = sorted(
df_for_condition.time.unique().astype(float))
# init edata object
edata = amici.ExpData(model.get())
# list of timepoints with number-of-datapoints replicates
timepoints_w_reps = _get_timepoints_w_reps(
df_for_condition, timepoints)
# set time points in edata
edata.setTimepoints(timepoints_w_reps)
measurement_df)
# get observable ids
observable_ids = model.getObservableIds()
# iterate over conditions
for data_idx, condition in simulation_conditions.iterrows():
# current rdata
rdata = rdatas[data_idx]
# current simulation matrix
y = rdata['y']
# time array used in rdata
t = list(rdata['t'])
# extract rows for condition
cur_measurement_df = petab.core.get_rows_for_condition(
measurement_df, condition)
# iterate over entries for the given condition
# note: this way we only generate a dataframe entry for every
# row that existed in the original dataframe. if we want to
# e.g. have also timepoints non-existent in the original file,
# we need to instead iterate over the rdata['y'] entries
for _, row in cur_measurement_df.iterrows():
# copy row
row_sim = copy.deepcopy(row)
# extract simulated measurement value
timepoint_idx = t.index(row.time)
observable_idx = observable_ids.index(
"observable_" + row.observableId)
measurement_sim = y[timepoint_idx, observable_idx]