def constructEdataFromDataFrame(df, model, condition): """ Constructs an ExpData instance according to the provided Model and DataFrame Arguments: df: pd.DataFrame with Observable Names/Ids as columns standard deviations may be specified by appending '_std' as suffix model: Model instance condition: pd.Series with FixedParameter Names/Ids as columns preequilibration conditions may be specified by appending '_preeq' as suffix presimulation conditions may be specified by appending '_presim' as suffix Returns: ExpData instance Raises: """ edata = ExpData(model.get()) # timepoints df = df.sort_values(by='time', ascending=True) edata.setTimepoints(df['time'].values) overwrite_preeq = {} overwrite_presim = {} for par in list(_get_names_or_ids(model, 'FixedParameter')): if par + '_preeq' in condition.keys() \ and not math.isnan(condition[par + '_preeq']): overwrite_preeq[par] = condition[par + '_preeq'] if par + '_presim' in condition.keys() \ and not math.isnan(condition[par + '_presim']): overwrite_presim[par] = condition[par + '_presim'] # fixedParameters edata.fixedParameters = \ condition[_get_names_or_ids(model, 'FixedParameter')].values if any([overwrite_preeq[key] != condition[key] for key in overwrite_preeq.keys()]): edata.fixedParametersPreequilibration = \ _get_specialized_fixed_parameters(model, condition,overwrite_preeq) elif len(overwrite_preeq.keys()): edata.fixedParametersPreequilibration = copy.deepcopy( edata.fixedParameters ) if any([overwrite_presim[key] != condition[key] for key in overwrite_presim.keys()]): edata.fixedParametersPresimulation = _get_specialized_fixed_parameters( model, condition,overwrite_presim ) elif len(overwrite_presim.keys()): edata.fixedParametersPresimulation = copy.deepcopy( edata.fixedParameters ) if 't_presim' in condition.keys(): edata.t_presim = condition['t_presim'] # data for obs_index, obs in enumerate(_get_names_or_ids(model, 'Observable')): if obs in df.keys(): edata.setObservedData(df[obs].values, obs_index) if obs + '_std' in df.keys(): edata.setObservedDataStdDev( df[obs + '_std'].values, obs_index ) return edata
def fill_in_parameters_for_condition( edata: amici.ExpData, problem_parameters: Dict[str, numbers.Number], scaled_parameters: bool, parameter_mapping: ParameterMappingForCondition, amici_model: AmiciModel) -> None: """Fill fixed and dynamic parameters into the edata for condition (in-place). :param edata: Experimental data object to fill parameters into. :param problem_parameters: Problem parameters as parameterId=>value dict. Only parameters included here will be set. Remaining parameters will be used as already set in `amici_model` and `edata`. :param scaled_parameters: If True, problem_parameters are assumed to be on the scale provided in the parameter mapping. If False, they are assumed to be in linear scale. :param parameter_mapping: Parameter mapping for current condition. :param amici_model: AMICI model """ map_sim_var = parameter_mapping.map_sim_var scale_map_sim_var = parameter_mapping.scale_map_sim_var map_preeq_fix = parameter_mapping.map_preeq_fix scale_map_preeq_fix = parameter_mapping.scale_map_preeq_fix map_sim_fix = parameter_mapping.map_sim_fix scale_map_sim_fix = parameter_mapping.scale_map_sim_fix # Parameter mapping may contain parameter_ids as values, these *must* # be replaced def _get_par(model_par, value): """Replace parameter IDs in mapping dicts by values from problem_parameters where necessary""" if isinstance(value, str): # estimated parameter # (condition table overrides must have been handled already, # e.g. by the PEtab parameter mapping) return problem_parameters[value] if model_par in problem_parameters: # user-provided return problem_parameters[model_par] # constant value return value map_preeq_fix = { key: _get_par(key, val) for key, val in map_preeq_fix.items() } map_sim_fix = {key: _get_par(key, val) for key, val in map_sim_fix.items()} map_sim_var = {key: _get_par(key, val) for key, val in map_sim_var.items()} # If necessary, (un)scale parameters if scaled_parameters: unscale_parameters_dict(map_preeq_fix, scale_map_preeq_fix) unscale_parameters_dict(map_sim_fix, scale_map_sim_fix) if not scaled_parameters: # We scale all parameters to the scale they are estimated on, and pass # that information to amici via edata.{parameters,pscale}. # The scaling is necessary to obtain correct derivatives. scale_parameters_dict(map_sim_var, scale_map_sim_var) # We can skip preequilibration parameters, because they are identical # with simulation parameters, and only the latter are used from here # on. ########################################################################## # variable parameters and parameter scale # parameter list from mapping dict parameters = [ map_sim_var[par_id] for par_id in amici_model.getParameterIds() ] # scales list from mapping dict scales = [ petab_to_amici_scale(scale_map_sim_var[par_id]) for par_id in amici_model.getParameterIds() ] if parameters: edata.parameters = parameters if scales: edata.pscale = amici.parameterScalingFromIntVector(scales) ########################################################################## # fixed parameters preequilibration if map_preeq_fix: fixed_pars_preeq = [ map_preeq_fix[par_id] for par_id in amici_model.getFixedParameterIds() ] edata.fixedParametersPreequilibration = fixed_pars_preeq ########################################################################## # fixed parameters simulation if map_sim_fix: fixed_pars_sim = [ map_sim_fix[par_id] for par_id in amici_model.getFixedParameterIds() ] edata.fixedParameters = fixed_pars_sim
def constructEdataFromDataFrame(df, model, condition, by_id=False): """ Constructs an ExpData instance according to the provided Model and DataFrame. Arguments: df: pd.DataFrame with Observable Names/Ids as columns. Standard deviations may be specified by appending '_std' as suffix. model: Model instance. condition: pd.Series with FixedParameter Names/Ids as columns. Preequilibration conditions may be specified by appending '_preeq' as suffix. Presimulation conditions may be specified by appending '_presim' as suffix. by_id: bool, optional (default = False) Indicate whether in the arguments, column headers are based on ids or names. This should correspond to the way `df` and `condition` was created in the first place. Returns: ExpData instance. Raises: """ # initialize edata edata = ExpData(model.get()) # timepoints df = df.sort_values(by='time', ascending=True) edata.setTimepoints(df['time'].values) # get fixed parameters from condition overwrite_preeq = {} overwrite_presim = {} for par in list(_get_names_or_ids(model, 'FixedParameter', by_id=by_id)): if par + '_preeq' in condition.keys() \ and not math.isnan(condition[par + '_preeq']): overwrite_preeq[par] = condition[par + '_preeq'] if par + '_presim' in condition.keys() \ and not math.isnan(condition[par + '_presim']): overwrite_presim[par] = condition[par + '_presim'] # fill in fixed parameters edata.fixedParameters = \ condition[_get_names_or_ids(model, 'FixedParameter', by_id=by_id)].values # fill in preequilibration parameters if any([ overwrite_preeq[key] != condition[key] for key in overwrite_preeq.keys() ]): edata.fixedParametersPreequilibration = \ _get_specialized_fixed_parameters( model, condition, overwrite_preeq, by_id=by_id) elif len(overwrite_preeq.keys()): edata.fixedParametersPreequilibration = copy.deepcopy( edata.fixedParameters) # fill in presimulation parameters if any([ overwrite_presim[key] != condition[key] for key in overwrite_presim.keys() ]): edata.fixedParametersPresimulation = _get_specialized_fixed_parameters( model, condition, overwrite_presim, by_id=by_id) elif len(overwrite_presim.keys()): edata.fixedParametersPresimulation = copy.deepcopy( edata.fixedParameters) # fill in presimulation time if 't_presim' in condition.keys(): edata.t_presim = condition['t_presim'] # fill in data and stds for obs_index, obs in enumerate( _get_names_or_ids(model, 'Observable', by_id=by_id)): if obs in df.keys(): edata.setObservedData(df[obs].values, obs_index) if obs + '_std' in df.keys(): edata.setObservedDataStdDev(df[obs + '_std'].values, obs_index) return edata