Ejemplo n.º 1
0
def load_power_input(input_fmt, power_or_path, fmt='on', normalize=True):
    if isinstance(power_or_path, str):
        power = Power(fmt=fmt)
        power.load_power(power_or_path, fmt=fmt)
        power.extend_lp_info(real_only=False)
    else:
        power = power_or_path
    values = np.empty(input_fmt.shape[0], dtype=np.float32) + np.nan
    for i, (idx, etype, dtype, name) in \
            enumerate(input_fmt[['etype', 'dtype', 'name']].itertuples()):
        if etype == 'ed':
            # TODO: deal with ed
            continue
        else:
            try:
                v = power.data[etype].loc[power.data[etype]['name'] == name,
                                          dtype]
                if len(v) > 0:
                    values[i] = v.values[0]
                # values.append(v.values[0] if len(v) > 0 else np.nan)
            except KeyError:
                # print(etype, dtype, name)
                # values.append(np.nan)
                continue
    if normalize:
        values = mm_normalize(values, input_fmt['min'].values,
                              input_fmt['max'].values)
    values[np.isnan(values)] = -1.
    return values
Ejemplo n.º 2
0
def restore_power_input(input_fmt,
                        power_or_path,
                        fmt='on',
                        normalize=True,
                        clip=True):
    if isinstance(power_or_path, str):
        power = Power(fmt=fmt)
        power.load_power(power_or_path, fmt=fmt)
        power.extend_lp_info(real_only=False)
    else:
        power = power_or_path
    if normalize:
        input_fmt['value'] = mm_denormalize(input_fmt['value'].values,
                                            input_fmt['min'].values,
                                            input_fmt['max'].values)
    if clip:
        input_fmt['value'] = np.clip(input_fmt['value'], input_fmt['min'],
                                     input_fmt['max'])
    groups = input_fmt.groupby(['etype', 'dtype'], sort=False)
    for (e, d), sub in groups:
        values = sub[['name', 'value']].set_index('name')
        values = values.reindex(power.data[e]['name'])
        na_idx = np.isnan(values['value'])
        values[na_idx] = power.data[e][d].values[na_idx]
        set_values(power.data, e, d, values['value'].values)
    return power
Ejemplo n.º 3
0
def restore_power_input(input_fmt, power_or_path, fmt='on', normalize=True):
    if isinstance(power_or_path, str):
        power = Power(fmt=fmt)
        power.load_power(power_or_path, fmt=fmt)
        power.extend_lp_info(real_only=False)
    else:
        power = power_or_path
    if normalize:
        input_fmt['value'] = mm_denormalize(input_fmt['value'].values,
                                            input_fmt['min'].values,
                                            input_fmt['max'].values)
    group = input_fmt.groupby(['etype', 'dtype'], sort=False)
    for (e, d), idx in group.indices.items():
        values = power.data[e][[d, 'name']].copy()
        indices = values.index
        values.set_index('name', inplace=True)
        values = input_fmt.loc[idx, ['name', 'value']].set_index('name')
        values.index = indices
        set_values(power.data, e, d, values['value'])
    return power