Exemplo n.º 1
0
    def init_from_model_dir(cls, model_dir):  # prints only for debugging
        
        print("Creating accelerator instance from model dir")
        instance = cls()
        
        instance.modelpath = model_dir

        if os.path.isfile(model_dir):
            model_dir = os.path.dirname(model_dir)
        instance.model_tfs = tfs_pandas.read_tfs(os.path.join(model_dir, "twiss.dat")).set_index("NAME")
        print("model path =", os.path.join(model_dir, "twiss.dat"))
            
        try:
            model_best_knowledge_path = os.path.join(model_dir, "twiss_best_knowledge.dat")
            if os.path.isfile(model_best_knowledge_path):
                instance.model_best_knowledge = tfs_pandas.read_tfs(model_best_knowledge_path).set_index("NAME")
        except IOError:
            instance.model_best_knowledge = None
            
        elements_path = os.path.join(model_dir, "twiss_elements.dat")
        if os.path.isfile(elements_path):
            instance.elements = tfs_pandas.read_tfs(elements_path).set_index("NAME")
        else:
            raise AcceleratorDefinitionError("Elements twiss not found")
        elements_path = os.path.join(model_dir, "twiss_elements_centre.dat")
        if os.path.isfile(elements_path):
            instance.elements_centre = tfs_pandas.read_tfs(elements_path).set_index("NAME")
        else:
            instance.elements_centre = instance.elements
        
        instance.nat_tune_x = float(instance.model_tfs.headers["Q1"])
        instance.nat_tune_y = float(instance.model_tfs.headers["Q2"])
        
        return instance
Exemplo n.º 2
0
def getdiff(meas_path=None):
    """ Calculates the differences between measurement, corrected and uncorrected model.

    After running madx and creating the model with (twiss_cor) and without (twiss_no)
    corrections, run this functions to get tfs-files with the differences between measurements
    and models.

    Args:
        meas_path (str): Path to the measurement folder.
        Needs to contain twiss_cor.dat and twiss_no.dat.
    """
    if meas_path is None:
        meas_path = sys.argv[1]

    LOG.debug("Started 'getdiff' for measurment dir '{:s}'".format(meas_path))

    if not isdir(meas_path):
        raise IOError("No valid measurement directory:" + meas_path)
    corrected_model_path = join(meas_path, TWISS_CORRECTED)
    uncorrected_model_path = join(meas_path, TWISS_NOT_CORRECTED)

    meas = GetLlmMeasurement(meas_path)
    twiss_cor = read_tfs(corrected_model_path).set_index('NAME', drop=False)
    twiss_no = read_tfs(uncorrected_model_path).set_index('NAME', drop=False)
    coup_cor = TwissOptics(twiss_cor,
                           quick_init=True).get_coupling(method='cmatrix')
    coup_no = TwissOptics(twiss_no,
                          quick_init=True).get_coupling(method='cmatrix')
    model = pd.merge(twiss_cor,
                     twiss_no,
                     how='inner',
                     on='NAME',
                     suffixes=('_c', '_n'))
    coupling_model = pd.merge(coup_cor,
                              coup_no,
                              how='inner',
                              left_index=True,
                              right_index=True,
                              suffixes=('_c', '_n'))
    coupling_model['NAME'] = coupling_model.index.values

    for plane in ['x', 'y']:
        _write_beta_diff_file(meas_path, meas, model, plane)
        _write_phase_diff_file(meas_path, meas, model, plane)
        _write_disp_diff_file(meas_path, meas, model, plane)
    _write_coupling_diff_file(meas_path, meas, coupling_model)
    _write_norm_disp_diff_file(meas_path, meas, model)
    _write_chromatic_coupling_files(meas_path, corrected_model_path)
    LOG.debug("Finished 'getdiff'.")
Exemplo n.º 3
0
def _get_bpm_names(orbit_path_left, orbit_path_right):
    return {
        (BEAM1, LEFT):
        tfs_pandas.read_tfs(os.path.join(orbit_path_left,
                                         "LHCB1.orbit1.tfs"))["NAME"],
        (BEAM1, RIGHT):
        tfs_pandas.read_tfs(os.path.join(orbit_path_right,
                                         "LHCB1.orbit1.tfs"))["NAME"],
        (BEAM2, LEFT):
        tfs_pandas.read_tfs(os.path.join(orbit_path_left,
                                         "LHCB2.orbit1.tfs"))["NAME"],
        (BEAM2, RIGHT):
        tfs_pandas.read_tfs(os.path.join(orbit_path_right,
                                         "LHCB2.orbit1.tfs"))["NAME"],
    }
Exemplo n.º 4
0
 def get_variables(cls, frm=None, to=None, classes=None):
     correctors_dir = os.path.join(LHC_DIR, "2012", "correctors")
     all_corrs = _merge_jsons(
         os.path.join(correctors_dir, "correctors_b" + str(cls.get_beam()),
                      "beta_correctors.json"),
         os.path.join(correctors_dir, "correctors_b" + str(cls.get_beam()),
                      "coupling_correctors.json"),
         cls._get_triplet_correctors_file(),
     )
     my_classes = classes
     if my_classes is None:
         my_classes = all_corrs.keys()
     vars_by_class = set(
         _flatten_list([
             all_corrs[corr_cls] for corr_cls in my_classes
             if corr_cls in all_corrs
         ]))
     if frm is None and to is None:
         return list(vars_by_class)
     elems_matrix = tfs_pandas.read_tfs(
         cls._get_corrector_elems()).sort_values("S").set_index("S").loc[
             frm:to, :]
     vars_by_position = _remove_dups_keep_order(
         _flatten_list([
             raw_vars.split(",") for raw_vars in elems_matrix.loc[:, "VARS"]
         ]))
     return _list_intersect_keep_order(vars_by_position, vars_by_class)
 def _get_amp_beta_beat_file(self, label, plane):
     outpath = self._matcher_model.get_output_path()
     file_data = tfs_pandas.read_tfs(
         os.path.join(
             outpath, "sbs",
             "sbsampbetabeat" + plane.lower() + "_" + label + ".out"))
     return file_data
Exemplo n.º 6
0
def clean_tunes(files, limit=DEF_LIMIT):
    for file in files:
        file_df = tfs_pandas.read_tfs(file)
        mask = _get_mask(file_df, limit)
        file_df = file_df.loc[mask, :]
        _recompute_tune_stats(file_df)
        tfs_pandas.write_tfs(file, file_df)
Exemplo n.º 7
0
def _log_rms(files, legends, column_name, mask):
    """ Calculate and print rms value into log """
    file_name = os.path.splitext(os.path.basename(files[0]))[0]
    collected = {}
    LOG.info("Results for '{:s}':".format(file_name))
    if mask is not None:
        LOG.info("  {:<20s} {:^15s} {:^15s} {:^15s} {:^15s}".format(
            "", "RMS", "Mean", "RMS w/ cut", "Mean w/ cut"))
    else:
        LOG.info("  {:<20s} {:^15s} {:^15s}".format("", "RMS", "Mean"))
    for f, l in zip(files, legends):
        collected[l] = {}
        data = tfs_pandas.read_tfs(f, index="NAME")[column_name]
        collected[l]["rms"] = _rms(data)
        collected[l]["mean"] = np.mean(data)
        if mask is not None:
            data = data.loc[mask]
            rms_val = _rms(data)
            mean_val = np.mean(data)
            collected[l]["rms_f"] = rms_val
            collected[l]["mean_f"] = mean_val
            LOG.info(
                "  {:<20s} {:+15.4e} {:+15.4e} {:+15.4e} {:+15.4e}".format(
                    l, collected[l]["rms"], collected[l]["mean"],
                    collected[l]["rms_f"], collected[l]["mean_f"]))
        else:
            LOG.info("  {:<20s} {:+15.4e} {:+15.4e}".format(
                l, collected[l]["rms"], collected[l]["mean"]))
    return collected
Exemplo n.º 8
0
def _log_rms(files, legends, column_name):
    """ Calculate and print rms value into log """
    file_name = os.path.splitext(os.path.basename(files[0]))[0]
    LOG.info("Results for '{:s}':".format(file_name))
    LOG.info("  {:<20s} {:11s} {:11s}".format("", " RMS", " Mean"))
    for f, l in zip(files, legends):
        data = tfs_pandas.read_tfs(f)[column_name]
        LOG.info("  {:<20s} {:+11.4e} {:+11.4e}".format(l, _rms(data), np.mean(data)))
def _create_tfs_data(filepaths, plane):
    bpm_data_rows = []
    for filepath in filepaths:
        bpm_tfs_file = tfs_pandas.read_tfs(filepath)
        bpms_tfs_data = bpm_tfs_file[FEATURES_WITH_NAME.format(
            plane.upper()).split(",")]
        bpm_data_rows.append(bpms_tfs_data)
    return pandas.concat(bpm_data_rows)
Exemplo n.º 10
0
def compute_offset(model1_path, model2_path, orbit_path_left, orbit_path_right,
                   kleft_path, kright_path, ip):

    ks, orbits = _collect_orbit_data(orbit_path_left, orbit_path_right,
                                     kleft_path, kright_path)

    model1 = tfs_pandas.read_tfs(model1_path)
    model1.set_index("NAME", inplace=True)
    model2 = tfs_pandas.read_tfs(model2_path)
    model2.set_index("NAME", inplace=True)

    bpm_names = _get_bpm_names(orbit_path_left, orbit_path_right)
    models = {BEAM1: model1, BEAM2: model2}

    results = _apply_to_beam_side_plane(
        lambda beam, side, plane: _compute_and_clean(
            ip, beam, side, plane, models, bpm_names, ks, orbits))
    return results
Exemplo n.º 11
0
def _load_and_remove_twiss(var_and_path):
    """ Function for pool to retrieve results """
    (var, path) = var_and_path
    twissfile = os.path.join(path, "twiss." + var)
    tfs_data = tfs.read_tfs(twissfile, index="NAME")
    tfs_data['Q1'] = tfs_data.Q1
    tfs_data['Q2'] = tfs_data.Q2
    os.remove(twissfile)
    return var, tfs_data
Exemplo n.º 12
0
def check_tfs(path1, path2, show=False):
    try:
        tfs1 = tfs_pandas.read_tfs(path1)
        tfs2 = tfs_pandas.read_tfs(path2)
    except ValueError:
        LOGGER.debug("Value error occured in loading, will return 0% match")
        return .0
    matching = .0

    if len(tfs1.index) != len(tfs2.index):
        if show:
            LOGGER.info("!!!! indices not equal")
            for key in tfs1.index:
                if key not in tfs2.index:
                    LOGGER.warning("'{}' not in new output".format(tfs1.loc[key, "NAME"]))
        return 0.0

    for column in tfs1.columns:
        if column not in tfs2.columns:
            if show:
                LOGGER.info("!!!! column {} in file1 but not in file2".format(column))
        else:
            if tfs1.dtypes[column] is np.dtype('float64'):
                equalindex = abs(tfs1[column]/tfs2[column] - 1.0) > 1.0e-2
            else:
                equalindex = tfs1[column] != tfs2[column]
            diff = pandas.concat(
                [tfs1.loc[equalindex, column], tfs2.loc[equalindex, column]], axis=1)
            if diff.empty:
                if show:
                    LOGGER.info("column \33[38;2;128;128;128m{:16s}\33[0m\33[1m \33[38;2;200;255;200mmatch\33[0m".format(column))
                matching += 1.0
            else:
                if show:
                    LOGGER.info(
                        "column \33[38;2;128;128;128m{:16s}\33[0m\33[1m\33[38;2;255;200;200m"
                        .format(column) +
                        " diff\33[0m\n{}"
                        .format(diff))
                matching += 1.0 - float(len(diff)) / float(len(tfs1.index))

    if show:
        LOGGER.info("{} columns  out of {} matched".format(matching, len(tfs1.columns)))
    return matching / len(tfs1.columns)
Exemplo n.º 13
0
def _collect_orbit_data(orbit_path_left, orbit_path_right, kleft_path,
                        kright_path):
    k_file_left = tfs_pandas.read_tfs(kleft_path)
    k_file_left["TIME"] = k_file_left["TIME"].astype(long)
    k_file_left.set_index("TIME", inplace=True)

    k_file_right = tfs_pandas.read_tfs(kright_path)
    k_file_right["TIME"] = k_file_right["TIME"].astype(long)
    k_file_right.set_index("TIME", inplace=True)

    orbit_paths = {LEFT: orbit_path_left, RIGHT: orbit_path_right}
    k_files = {LEFT: k_file_left, RIGHT: k_file_right}

    k = {}
    _apply_to_beam_side_plane(lambda beam, side, plane: k.__setitem__(
        (beam, side, plane), []))

    bpm = {}
    _apply_to_beam_side_plane(lambda beam, side, plane: bpm.__setitem__(
        (beam, side, plane), []))
    for side in SIDES:
        orbit_path = orbit_paths[side]
        k_file = k_files[side]
        for filename in os.listdir(orbit_path):
            orbit_data = tfs_pandas.read_tfs(os.path.join(
                orbit_path, filename))
            timestamp = _date_str_to_timestamp(orbit_data.headers["OrbitDate"])
            if filename.startswith('LHCB1'):
                beam = BEAM1
            elif filename.startswith('LHCB2'):
                beam = BEAM2
            try:
                k[(beam, side, HOR)].append(
                    _get_sign(beam, HOR) * k_file.loc[timestamp, "K"])
                k[(beam, side, VER)].append(
                    _get_sign(beam, VER) * k_file.loc[timestamp, "K"])
                orbit_x = orbit_data.loc[:, "X"]
                bpm[(beam, side, HOR)].append(orbit_x - np.mean(orbit_x))
                orbit_y = orbit_data.loc[:, "Y"]
                bpm[(beam, side, VER)].append(orbit_y - np.mean(orbit_y))
            except KeyError:
                continue
    return k, bpm
Exemplo n.º 14
0
def load_result(path):
    getllm_result = GetLLMResult(path)
    getllm_result.betax = _load_beta_plane(path, "x")
    getllm_result.betay = _load_beta_plane(path, "y")
    if os.path.isfile(os.path.join(path, 'getlobster.out')):
        getllm_result.lobster = tfs_pandas.read_tfs(os.path.join(
            path, 'getlobster.out'),
                                                    index="NAME")

    return getllm_result
Exemplo n.º 15
0
def get_ip_positions(path):
    """ Returns a dict of IP positions from tfs-file of path.

    Args:
        path (str): Path to the tfs-file containing IP-positions
    """
    df = tfs.read_tfs(path).set_index('NAME')
    ip_names = ["IP" + str(i) for i in range(1, 9)]
    ip_pos = df.loc[ip_names, 'S'].values
    return dict(zip(ip_names, ip_pos))
Exemplo n.º 16
0
def find_model(outputfolder):

    tfsmodel = None

    if os.path.isfile(os.path.join(outputfolder, "themodel")):
        with open(os.path.join(outputfolder, "themodel")) as themodel:
            line = themodel.readline()
            print "model found in 'themodel' file"
            print "path = {}".format(line)
            tfsmodel = tfs_pandas.read_tfs(os.path.join(
                line, "twiss_elements.dat"),
                                           index="NAME")
        return tfsmodel

    output_filepath = os.path.join(outputfolder, "getbetax_free.out")
    i = 0
    while not os.path.isfile(output_filepath) and i < len(OFILELIST):
        output_filepath = os.path.join(outputfolder, OFILELIST[i])
        i = i + 1

    output_file = tfs_pandas.read_tfs(output_filepath)

    print ". . . . . . . . . ."
    print ". . . . .command: ."
    print output_file.headers["Command"]
    print ". . . . . . . . . ."

    commandargs = output_file.headers["Command"].split("' '")
    for i, comm in enumerate(commandargs):
        words = comm.split("=")
        if words[0] == "--model":
            if len(words) >= 2:
                words1 = words[1]
            else:
                words1 = commandargs[i + 1]
            modelpath = os.path.dirname(words1) + "/twiss_elements.dat"
            print "looking for model in ", modelpath
            if os.path.isfile(modelpath):
                tfsmodel = tfs_pandas.read_tfs(modelpath, index="NAME")
                print "model found in input file 1. "
                return tfsmodel
    return None
Exemplo n.º 17
0
def _write_chromatic_coupling_files(meas_path, cor_path):
    LOG.debug("Calculating chromatic coupling diff.")
    # TODO: Add Cf1010
    try:
        twiss_plus = read_tfs(join(split(cor_path)[0], TWISS_CORRECTED_PLUS),
                              index='NAME')
        twiss_min = read_tfs(join(split(cor_path)[0], TWISS_CORRECTED_MINUS),
                             index='NAME')
        deltap = np.abs(twiss_plus.DELTAP - twiss_min.DELTAP)
        plus = TwissOptics(twiss_plus,
                           quick_init=True).get_coupling(method='cmatrix')
        minus = TwissOptics(twiss_min,
                            quick_init=True).get_coupling(method='cmatrix')
        model = pd.merge(plus,
                         minus,
                         how='inner',
                         left_index=True,
                         right_index=True,
                         suffixes=('_p', '_m'))
        model['NAME'] = model.index.values
        if exists(join(meas_path, "chromcoupling_free.out")):
            meas = read_tfs(join(meas_path, "chromcoupling_free.out"))
        else:
            meas = read_tfs(join(meas_path, "chromcoupling.out"))
        tw = pd.merge(meas, model, how='inner', on='NAME')
        cf1001 = (tw.loc[:, 'F1001_p'] - tw.loc[:, 'F1001_m']) / deltap
        tw['Cf1001r_model'] = np.real(cf1001)
        tw['Cf1001i_model'] = np.imag(cf1001)
        tw['Cf1001r_prediction'] = tw.loc[:,
                                          'Cf1001r'] - tw.loc[:,
                                                              'Cf1001r_model']
        tw['Cf1001i_prediction'] = tw.loc[:,
                                          'Cf1001i'] - tw.loc[:,
                                                              'Cf1001i_model']
        write_tfs(
            join(meas_path, 'chromatic_coupling.out'), tw.loc[:, [
                'NAME', 'S', 'Cf1001r', 'Cf1001rERR', 'Cf1001i', 'Cf1001iERR',
                'Cf1001r_model', 'Cf1001i_model', 'Cf1001r_prediction',
                'Cf1001i_prediction'
            ]])
    except IOError:
        LOG.debug("Chromatic coupling measurements not found. Skipped.")
Exemplo n.º 18
0
def _create_columns(file, twiss):
    bpm_tfs_data = tfs_pandas.read_tfs(file).set_index("NAME")
    model_tfs_data = tfs_pandas.read_tfs(twiss).set_index("NAME").loc[
        bpm_tfs_data.index]

    ph_adv_bpm_data = (bpm_tfs_data.loc[:, "MUX"] -
                       np.roll(bpm_tfs_data.loc[:, "MUX"], 1)) % 1.
    ph_adv_model = (model_tfs_data.loc[:, "MUX"] -
                    np.roll(model_tfs_data.loc[:, "MUX"], 1)) % 1.

    bpm_tfs_data.loc[:, "PH_ADV_MDL"] = ph_adv_model

    bpm_tfs_data.loc[:, "PH_ADV_BEAT"] = (ph_adv_bpm_data - ph_adv_model)
    bpm_tfs_data.PH_ADV_BEAT[
        bpm_tfs_data.PH_ADV_BEAT >
        0.5] = 1 - bpm_tfs_data.PH_ADV_BEAT[bpm_tfs_data.PH_ADV_BEAT > 0.5]

    bpm_tfs_data.loc[:, "PH_ADV"] = ph_adv_bpm_data
    bpm_tfs_data.loc[:, "BETX"] = model_tfs_data.loc[:, "BETX"]
    return bpm_tfs_data
Exemplo n.º 19
0
 def plot(self):
     self._figure.clear()
     self._axes_to_data = {}
     axes_x = self._figure.add_subplot(2, 1, 1)
     axes_x.set_title(self._matcher_model.get_name())
     file_horizontal = tfs_pandas.read_tfs(
         os.path.join(
             self._matcher_model.get_output_path(), "sbs",
             "sbsphasext_" + str(self._matcher_model.get_label()) + ".out"))
     self._axes_to_data[axes_x] = file_horizontal
     self._plot_match(axes_x, file_horizontal, "X")
     file_vertical = tfs_pandas.read_tfs(
         os.path.join(
             self._matcher_model.get_output_path(), "sbs",
             "sbsphaseyt_" + str(self._matcher_model.get_label()) + ".out"))
     axes_y = self._figure.add_subplot(2, 1, 2)
     self._axes_to_data[axes_y] = file_vertical
     self._plot_match(axes_y, file_vertical, "Y")
     self._figure.tight_layout()
     self._figure.patch.set_visible(False)
     self._figure.canvas.draw()
Exemplo n.º 20
0
def clean_files(list_of_files, replace=False):
    for filepath in list_of_files:
        try:
            df = tfs.read_tfs(filepath)
            LOG.info("Read file {:s}".format(filepath))
        except (IOError, tfs.TfsFormatError):
            LOG.info("Skipped file {:s}".format(filepath))
        else:
            df = df.dropna(axis='index')
            if not replace:
                filepath += ".dropna"
            tfs.write_tfs(filepath, df)
Exemplo n.º 21
0
def remove_bpms_from_file(path, bad_bpm_names):
    #copy and rename original file
    src_dir = os.path.abspath(os.path.join(path, os.pardir))
    filename = os.path.basename(path)
    new_filename = os.path.join(src_dir, filename + ".notcleaned")
    os.rename(path, new_filename)
    #take the content of renamed file, remove bpms and write new file with the name of original file
    original_file_tfs = tfs_pandas.read_tfs(new_filename).set_index("NAME",
                                                                    drop=False)
    original_file_tfs = original_file_tfs.loc[~original_file_tfs.index.
                                              isin(bad_bpm_names)]
    tfs_pandas.write_tfs(path, original_file_tfs)
Exemplo n.º 22
0
def generate(
    infile,
    nturns=6600,
    plane='X',  #single plane with no coupling
    max_dpoverp=0.0002,  #amplitude of dp over p modulation
    chroma=5,  #Chroma not taken from the file, since it is generally not matched, and may have crazy values
    rf_modulation_freqency=5.0,  #Hz frequency of RF modulation, i. e. change of beam energy
    deltaQ=-0.01,  # difference between AC-Dipole driven tune and the natural tune, if 0.0 -> kicker case
    strength=1.02,  # of ac-dipole or kicker
    sigmaQ=0.0001,  # width of tune ... sigma
    damp_time=1500.0,  # damping time of kicker induced betatron oscillations 
    bpm_noise=0.1  #BPM noise in mm
):
    twiss = read_tfs(infile)
    if plane == 'X':
        Q = np.remainder(twiss.headers['Q1'], 1)
    else:
        Q = np.remainder(twiss.headers['Q2'], 1)
    nBPMs = len(twiss.index.values)
    dims = (nturns, nBPMs)
    phase0z = np.random.rand()  #initial phase of synchrotron oscilation
    phase0 = np.random.rand(
    )  #initial phase of betatron oscilation at the beginning of the lattice
    Qz = rf_modulation_freqency * twiss.headers['LENGTH'] / C  #synchrotron tune
    dpp = max_dpoverp * np.cos(PI2 * Qz * np.arange(nturns, dtype=float) +
                               PI2 * phase0z)  #dpoverp for all turns
    tune = dpp * chroma + Q  #betatron tune for all turns
    dQ = 1 / np.abs(dpp * chroma - deltaQ +
                    _get_noise(PI2 * sigmaQ, d1=nturns))

    signal = 1000 * np.outer(twiss.loc[:, 'D' + plane].values,
                             dpp)  #synchrotron motion nBPMs x nturns
    nat_tune = np.transpose(
        0.01 * np.sqrt(twiss.loc[:, 'BET' + plane].values) * np.cos(
            _get_tbt_phases(tune, twiss, plane, phase0, nturns, nBPMs,
                            sigmaQ)))  #

    if not deltaQ:  # kicker case
        nat_tune = nat_tune * 10 * np.exp(
            -1.0 / damp_time * np.arange(nturns, dtype=float))
    else:
        dQ = 1 / np.abs(dpp * chroma - deltaQ +
                        _get_noise(PI2 * sigmaQ, d1=nturns))
        dQ[np.where(dQ > 10000.0)] = 10000.0
        dr_tune = dQ * np.transpose(0.0006 * strength * np.sqrt(
            twiss.loc[:, 'BET' + plane].values) * np.cos(
                _get_tbt_phases_ac(Q + deltaQ, twiss, plane, phase0, nturns,
                                   nBPMs, sigmaQ)))
        signal += dr_tune
    signal = signal + nat_tune + _get_noise(bpm_noise, d1=nBPMs, d2=nturns)
    #plot_bpm_tbt('', signal, 40)
    return signal
def plot_bbq_data(opt):
    """ Plot BBQ wrapper. """
    LOG.info("Plotting BBQ.")
    if isinstance(opt.input, basestring):
        bbq_df = tfs.read_tfs(opt.input, index=COL_TIME())
    else:
        bbq_df = opt.input
    opt.pop("input")

    bbq_tools.plot_bbq_data(bbq_df, **opt)

    if opt.show:
        plt.show()
Exemplo n.º 24
0
def _plot_beta_function(twiss, data_features, labels):
    twiss = tfs_pandas.read_tfs(twiss).set_index("NAME")
    unique_labels = set(labels)
    for k, col in zip(unique_labels, COLORS[:len(unique_labels)]):
        if k == -1:  # Is noise
            col = "black"
        class_member_mask = (labels == k)
        class_members = data_features.iloc[class_member_mask].index
        plt.plot(twiss.loc[class_members, "S"],
                 twiss.loc[class_members, "BETX"],
                 'o',
                 markerfacecolor=col)
    plt.show()
Exemplo n.º 25
0
def _collect_orbit_data(orbit_path_left, orbit_path_right, kleft_path,
                        kright_path):
    k_file_left = tfs_pandas.read_tfs(kleft_path)
    k_file_left["TIME"] = k_file_left["TIME"].astype(long)
    k_file_left.set_index("TIME", inplace=True)

    k_file_right = tfs_pandas.read_tfs(kright_path)
    k_file_right["TIME"] = k_file_right["TIME"].astype(long)
    k_file_right.set_index("TIME", inplace=True)

    orbit_paths = {LEFT: orbit_path_left, RIGHT: orbit_path_right}
    k_files = {LEFT: k_file_left, RIGHT: k_file_right}

    k = {}
    _apply_to_beam_side_plane(lambda beam, side, plane: k.__setitem__(
        (beam, side, plane), []))

    bpm = {}
    _apply_to_beam_side_plane(lambda beam, side, plane: bpm.__setitem__(
        (beam, side, plane), []))
    for side in SIDES:
        orbit_path = orbit_paths[side]
        k_file = k_files[side]
        for filename in os.listdir(orbit_path):
            orbit_data = tfs_pandas.read_tfs(os.path.join(
                orbit_path, filename))
            timestamp = _date_str_to_timestamp(orbit_data.headers["OrbitDate"])
            if filename.startswith('LHCB1'):
                beam = BEAM1
            elif filename.startswith('LHCB2'):
                beam = BEAM2
            for plane in (HOR, VER):
                closest = np.argmin(np.abs(timestamp - k_file.index.values))
                val = _get_sign(beam, plane) * k_file.K.iloc[closest]
                k[(beam, side, plane)].append(val)
                orbit = orbit_data.loc[:, PLANE_STR[plane]]
                bpm[(beam, side, plane)].append(orbit - np.mean(orbit))
    return k, bpm
Exemplo n.º 26
0
def _create_base_file(source_dir, source_file, meas, error, expect, outname):
    """ Copy Measurement into a base-file. """
    data = tfs_pandas.read_tfs(source_file)

    if error == "":
        new_data = data.loc[:, ["S", "NAME", meas]]
        new_data.columns = ["S", "NAME", expect]
    else:
        new_data = data.loc[:, ["S", "NAME", meas, error]]
        new_data.columns = ["S", "NAME", expect, error]

    path_out = os.path.join(source_dir, outname + BASE_ID)
    tfs_pandas.write_tfs(path_out, new_data)
    return path_out
Exemplo n.º 27
0
 def _get_model_df(model_path_or_tfs):
     """ Check if DataFrame given, if not load model from file  """
     if isinstance(model_path_or_tfs, basestring):
         LOG.debug(
             "Creating TwissOptics from '{:s}'".format(model_path_or_tfs))
         df = tfs.read_tfs(model_path_or_tfs, index="NAME")
     else:
         LOG.debug("Creating TwissOptics from input DataFrame")
         df = model_path_or_tfs
         if (len(df.index.values)
                 == 0) or not isinstance(df.index.values[0], basestring):
             raise IndexError(
                 "Index of DataFrame needs to be the element names."
                 "This does not seem to be the case.")
     return df
Exemplo n.º 28
0
    def read_tfs(self, filename):
        """Actually reads the TFS file from self.directory with filename.

        This function can be ovewriten to use something instead of tfs_pandas
        to load the files.

        Arguments:
            filename: The name of the file to load.
        Returns:
            A tfs_pandas instance of the requested file.
        """
        tfs_data = tfs_pandas.read_tfs(os.path.join(self.directory, filename))
        if "NAME" in tfs_data:
            tfs_data = tfs_data.set_index("NAME", drop=False)
        return tfs_data
def revert_forest_cleaning(files):
    """
    Reverts the cleaning. The backup files are renamed back to the original names (e.g .linx.notcleaned --> .linx)
    :param paths: list of files, where bad bpms identified by iForest are removed
    """
    files_list = files.split(',')
    for path in files_list:
        src_dir = os.path.abspath(os.path.join(path, os.pardir))
        filename = os.path.basename(path)
        notcleaned_file = os.path.join(src_dir, filename + ".notcleaned")
        original_file_tfs = tfs_pandas.read_tfs(notcleaned_file).set_index(
            "NAME", drop=False)
        os.remove(path)
        lin_file = os.path.join(src_dir,
                                notcleaned_file.replace(".notcleaned", ""))
        os.rename(notcleaned_file, lin_file)
        tfs_pandas.write_tfs(lin_file, original_file_tfs)
Exemplo n.º 30
0
def _load_beta_plane(path, plane):
    if os.path.isfile(os.path.join(path, 'getbeta{}_free.out'.format(plane))):
        xfilename = os.path.join(path, 'getbeta{}_free.out'.format(plane))
    elif os.path.isfile(os.path.join(path, 'getbeta{}.out'.format(plane))):
        xfilename = os.path.join(path, 'getbeta{}.out'.format(plane))
    else:
        return None
    bbx = tfs_pandas.read_tfs(xfilename, index="NAME")
    plane_ = plane.upper()

    dfx = bbx.loc[:, ["S", "BET{}MDL".format(plane_), "BET" + plane_]]
    dfx.loc[:, "BBEAT"] = (bbx.loc[:, "BET" + plane_] /
                           bbx.loc[:, "BET{}MDL".format(plane_)] - 1) * 100.0
    #dfx.loc[:, "BBEAT"] = (bbx.loc[:, "BBEAT"] ) * 100.0
    dfx.loc[:, "ERR"] = (bbx.loc[:, "ERRBET" + plane_] /
                         bbx.loc[:, "BET{}MDL".format(plane_)]) * 100.0
    return dfx