示例#1
0
文件: pandas.py 项目: oxomoron/AMICI
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
示例#2
0
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