Beispiel #1
0
def _get_model_phases(model: pd.DataFrame, meas: pd.DataFrame, key: str) -> pd.DataFrame:
    model_column = f"{PHASE_ADV}{key[-1]}"
    with logging_tools.log_pandas_settings_with_copy(LOG.debug):
        meas[MODEL] = (model.loc[meas["NAME2"].to_numpy(), model_column].to_numpy() -
                       model.loc[meas.index.to_numpy(), model_column].to_numpy())
        meas[DIFF] = df_diff(meas, VALUE, MODEL)
    return meas
Beispiel #2
0
def _compensate_by_model(input_files, meas_input, df, plane):
    df = pd.merge(df, pd.DataFrame(meas_input.accelerator.model_driven.loc[:, [f"MU{plane}"]]),
                  how='inner', left_index=True, right_index=True, suffixes=("", "comp"))
    phase_compensation = df_diff(df, f"MU{plane}", f"MU{plane}comp")
    df[input_files.get_columns(df, f"MU{plane}")] = ang_sum(
        input_files.get_data(df, f"MU{plane}"), phase_compensation[:, np.newaxis])
    return df
Beispiel #3
0
def convert_old_closed_orbit(
    inputdir: Union[Path, str],
    outputdir: Union[Path, str],
    plane: str,
    old_file_name: str = "CO",
    new_file_name: str = ORBIT_NAME,
) -> None:
    """
    Looks in the provided directory for expected closed orbit file from ``BetaBeat.src`` for a given
    plane, converts it to the output format used by ``omc3`` and  write them to the new location.

    The file naming should be getCO(x,y).out, with the following expected columns: NAME, S, COUNT,
    X, STDX, XMDL, MUXMDL.

    Args:
        inputdir (Union[Path, str]): Location of the directory with BetaBeat.src output files.
        outputdir (Union[Path, str]): Location of the output directory for converted files.
        plane (str): the transverse plane for which to look for the output file.
        old_file_name (str): the standard naming for the old output file.
        new_file_name (str): the standard naming for the new converted file.
    """
    LOGGER.info("Converting old closed orbit file")
    old_file_path = Path(
        inputdir) / f"get{old_file_name}{plane.lower()}{OLD_EXT}"
    if not old_file_path.is_file():
        LOGGER.debug(
            f"Expected BetaBeat.src output at '{old_file_path.absolute()}' is not a file, skipping"
        )
        return

    dframe = tfs.read(old_file_path)
    dframe = dframe.rename(columns={f"STD{plane}": f"{ERR}{plane}"})
    dframe[f"{DELTA}{plane}"] = df_diff(dframe, f"{plane}", f"{plane}{MDL}")
    dframe[f"{ERR}{DELTA}{plane}"] = dframe.loc[:, f"{ERR}{plane}"].to_numpy()
    tfs.write(Path(outputdir) / f"{new_file_name}{plane.lower()}{EXT}", dframe)
Beispiel #4
0
def _add_tunes_if_in_second_turn(df, input_files, line, phase2):
    mask = df_diff(df, "S", "S2") > 0
    tunes = np.empty((2, len(input_files.dpp_frames("X", 0))))
    for i, plane in enumerate(PLANES):
        tunes[i] = np.array([
            lin.headers[f"Q{i+1}"] for lin in input_files.dpp_frames(plane, 0)
        ])
    phase2[mask, :] = phase2[mask, :] + line[0] * tunes[0] + line[1] * tunes[1]
    return phase2
Beispiel #5
0
def convert_old_beta_from_phase(
    inputdir: Union[Path, str],
    outputdir: Union[Path, str],
    suffix: str,
    plane: str,
    old_file_name: str = "beta",
    new_file_name: str = BETA_NAME,
) -> None:
    """
    Looks in the provided directory for expected beta from phase file from ``BetaBeat.src`` for a given
    plane, converts it to the output format used by ``omc3`` and  write them to the new location.

    The file naming should be getbeta(x,y).out, with the following expected columns: NAME, S, COUNT,
    BETX, SYSBETX, STATBETX, ERRBETX, CORR_ALFABETA, ALFX, SYSALFX, STATALFX, ERRALFX, BETXMDL, ALFXMDL,
    MUXMDL, NCOMBINATIONS.

    Args:
        inputdir (Union[Path, str]): Location of the directory with BetaBeat.src output files.
        outputdir (Union[Path, str]): Location of the output directory for converted files.
        suffix (str): Compensation suffix used in the provided BetaBeat.src output files.
        plane (str): the transverse plane for which to look for the output file.
        old_file_name (str): the standard naming for the old output file.
        new_file_name (str): the standard naming for the new converted file.
    """
    LOGGER.info("Converting old beta from phase file")
    old_file_path = Path(
        inputdir) / f"get{old_file_name}{plane.lower()}{suffix}{OLD_EXT}"
    if not old_file_path.is_file():
        LOGGER.debug(
            f"Expected BetaBeat.src output at '{old_file_path.absolute()}' is not a file, skipping"
        )
        return

    dframe = tfs.read(old_file_path)
    if "CORR_ALFABETA" in dframe.columns.to_numpy():
        dframe = dframe.drop(columns=[
            f"STATBET{plane}", f"SYSBET{plane}", "CORR_ALFABETA",
            f"STATALF{plane}", f"SYSALF{plane}"
        ])
    else:
        dframe[f"{ERR}BET{plane}"] = df_err_sum(dframe, f"{ERR}BET{plane}",
                                                f"STDBET{plane}")
        dframe[f"{ERR}ALF{plane}"] = df_err_sum(dframe, f"{ERR}ALF{plane}",
                                                f"STDALF{plane}")

    dframe[f"{DELTA}BET{plane}"] = df_rel_diff(dframe, f"BET{plane}",
                                               f"BET{plane}{MDL}")
    dframe[f"{ERR}{DELTA}BET{plane}"] = df_ratio(dframe, f"{ERR}BET{plane}",
                                                 f"BET{plane}{MDL}")
    dframe[f"{DELTA}ALF{plane}"] = df_diff(dframe, f"ALF{plane}",
                                           f"ALF{plane}{MDL}")
    dframe[
        f"{ERR}{DELTA}ALF{plane}"] = dframe.loc[:,
                                                f"{ERR}ALF{plane}"].to_numpy()
    tfs.write(Path(outputdir) / f"{new_file_name}{plane.lower()}{EXT}", dframe)
Beispiel #6
0
def _get_delta_columns(beta_df, plane):
    beta_df[f"{DELTA}BET{plane}"] = df_rel_diff(beta_df, f"BET{plane}",
                                                f"BET{plane}{MDL}")
    beta_df[f"{ERR}{DELTA}BET{plane}"] = df_ratio(beta_df, f"{ERR}BET{plane}",
                                                  f"BET{plane}{MDL}")
    beta_df[f"{DELTA}ALF{plane}"] = df_diff(beta_df, f"ALF{plane}",
                                            f"ALF{plane}{MDL}")
    beta_df[
        f"{ERR}{DELTA}ALF{plane}"] = beta_df.loc[:,
                                                 f"{ERR}ALF{plane}"].to_numpy(
                                                 )
    return beta_df
def append_model(df: pd.DataFrame,
                 df_model: pd.DataFrame,
                 parameter: str,
                 planes: str = '',
                 beat: bool = False) -> pd.DataFrame:
    """ Add the values to the measurement. """
    LOG.debug(f"Appending model to fake measurement for {parameter}.")
    df[S] = df_model[S]

    for plane in planes:
        df[f'{PHASE_ADV}{plane}{MDL}'] = df_model[f'{PHASE_ADV}{plane}']

    df[f"{parameter}{MDL}"] = df_model[f'{parameter}']
    if beat:
        df[f"{DELTA}{parameter}"] = df_rel_diff(df, parameter,
                                                f"{parameter}{MDL}")
        df[f"{ERR}{DELTA}{parameter}"] = df_ratio(df, f"{ERR}{parameter}",
                                                  f"{parameter}{MDL}")
    else:
        df[f"{DELTA}{parameter}"] = df_diff(df, f'{parameter}',
                                            f'{parameter}{MDL}')
        df[f"{ERR}{DELTA}{parameter}"] = df[f'{ERR}{parameter}']
    return df
def test_df_sum_diff():
    a, b = _arand(), _arand()
    sum_of_columns = toolbox.df_sum(*_df(a, b))
    diff = toolbox.df_diff(*_df(sum_of_columns, b))
    assert _numerically_equal(a, diff)
def test_df_diff_zero(random, zeros):
    assert all(random == toolbox.df_diff(*_df(random, zeros)))
Beispiel #10
0
def _get_model_generic(model: pd.DataFrame, meas: pd.DataFrame, key: str) -> pd.DataFrame:
    with logging_tools.log_pandas_settings_with_copy(LOG.debug):
        meas[MODEL] = model.loc[meas.index.to_numpy(), key].to_numpy()
        meas[DIFF] = df_diff(meas, VALUE, MODEL)
    return meas