示例#1
0
def split_arguments(args, *param_list):
    """ Divide remaining arguments into a list of argument-dicts,
        fitting to the params in param_list.

        Args:
            args: Input arguments, either as list of strings or dict
            param_list: list of sets of entry-point parameters (either dict, or list)

        Returns:
            A list of dictionaries containing the arguments for each set of parameters,
            plus one more entry for unknown parameters.
            If the input was a list of argument-strings, the parameters will already be parsed.

        .. warning:: Unless you know what you are doing, run this function only on
                     remaining-arguments from entry point parsing, not on the actual arguments

        .. warning:: Adds each argument only once, to the set of params who claim it first!
    """
    split_args = []
    if isinstance(args, list):
        # strings of commandline parameters, has to be parsed twice
        # (as I don't know how to handle flags properly)
        for params in param_list:
            parser = argparse.ArgumentParser()
            parser = add_params_to_generic(parser, params)
            this_args, args = parser.parse_known_args(args)
            split_args.append(DotDict(this_args.__dict__))
        split_args.append(args)
    else:
        # should be a dictionary of params, so do it the manual way
        for params in param_list:
            params = param_names(params)
            split_args.append(DotDict([(key, args.pop(key)) for key in args if key in params]))
        split_args.append(DotDict(args))
    return split_args
def _get_method_opt(opt):
    """ Slightly unnecessary function to separate method-options
    for easier debugging and readability """
    meth_opt = DotDict(
        svd_cut=opt.svd_cut,
    )
    return meth_opt
示例#3
0
def line_to_struct(line):
    """ Converts a line to a dictionary structure of plotly-standard.
    See https://plot.ly/python/reference/#scatter
    """
    ls = DotDict()
    ls.name = line.get_name()
    ls.visible = line.get_visible()
    ls.x = line.get_xdata()
    ls.y = line.get_ydata()
    ls.mode = _get_mode(line)

    ls.line = DotDict()
    ls.line.color = line.get_color()
    ls.line.width = line.get_width()
    ls.line.dash = line.get_linestyle()
    def __init__(self, model_path_or_df, quick_init=True):
        self.twiss_df = self._get_model_df(model_path_or_df)
        self._ip_pos = self._find_ip_positions()

        self._results_df = self._make_results_dataframe()

        self._phase_advance = None
        if not quick_init:
            self._phase_advance = self.get_phase_adv()

        self._plot_options = DotDict(PLOT_DEFAULTS)
示例#5
0
    def __init__(self, model_path_or_df, quick_init=True, keep_all_elem=False):
        self.twiss_df = self._get_model_df(model_path_or_df)
        self._ip_pos = self._find_ip_positions()

        if not keep_all_elem:
            self.twiss_df = self._remove_nonnecessaries()

        self._results_df = self._make_results_dataframe()
        self._elements = self._sort_element_types()
        self._elements_mapped = self._map_element_types()

        self._phase_advance = None
        if not quick_init:
            self._phase_advance = self.get_phase_adv()

        self._plot_options = DotDict(PLOT_DEFAULTS)
示例#6
0
 def define_plot_style(self, **kwargs):
     """ Edit your desired plot style here """
     if not kwargs:
         options = DotDict(PLOT_DEFAULTS)
     else:
         options = DotDict(kwargs)
         if "style" not in options:
             options.style = PLOT_DEFAULTS["style"]
         if "manual" not in options:
             options.manual = PLOT_DEFAULTS["manual"]
     self._plot_options = options
示例#7
0
def line_to_struct(line):
    """ Converts a line to a dictionary structure of plotly-standard.
    See https://plot.ly/python/reference/#scatter
    """
    ls = DotDict()
    ls.name = line.get_name()
    ls.visible = line.get_visible()
    ls.x = line.get_xdata()
    ls.y = line.get_ydata()
    ls.mode = _get_mode(line)

    ls.line = DotDict()
    ls.line.color = line.get_color()
    ls.line.width = line.get_width()
    ls.line.dash = line.get_linestyle()
示例#8
0
 def _handle_commandline(self, args=None):
     """ No input to function """
     try:
         # check for config file first
         with silence():
             options = self.configarg.parse_args(args)
     except SystemExit:
         # parse regular options
         options, unknown_opts = self.argparse.parse_known_args(args)
         options = DotDict(vars(options))
         if self.strict:
             if unknown_opts:
                 raise ArgumentError("Unknown options: {:s}".format(unknown_opts))
             return options
         else:
             if unknown_opts:
                 LOG.debug("Unknown options: {:s}".format(unknown_opts))
             return options, unknown_opts
     else:
         # parse config file
         return self.dictparse.parse_config_items(self._read_config(vars(options)[ID_CONFIG]))
示例#9
0
def main(**kwargs):
    """ getsuper main function

    Keyword Args:
        source_files (list): list of strings with file_paths to TFS files
        twissfile (str): path to TFS model file
        output_path (str): Path to store created files
        algorithm (str): Used Turn-by-turn data analysis algorithm: 'SUSSIX', 'SVD' or 'HA'
        accel_cls (accelerator): Accelerator class object
        deltap_scaling_factor (float): Scaling factor for deltap,
                                       remember final value must be in MAD units
    """

    options = check_input(DotDict(kwargs))

    files_dict = {}  # dpp --> files with corresponding dpp

    for f_name in options.source_files:

        datax, datay = _load_from_file(f_name)

        dppx = datax.DPP * options.deltap_scaling_factor  # scaling factor hack for old files
        dppy = datay.DPP * options.deltap_scaling_factor

        if dppx != dppy:
            raise ValueError("Discrepancy between horizontal"
                             "{:f} and vertical {:f} dpp".format(dppx, dppy))
        else:
            dpp = float(dppx)

        if dpp not in files_dict:
            LOG.debug("Adding dpp {:f}".format(dpp))
            files_dict[dpp] = [f_name]
        else:
            files_dict[dpp].append(f_name)

    if len(files_dict.keys()) < 2:
        raise ValueError(
            "Less than two DPP-Values found. Cannot do W-Analysis.")

    if 0 not in files_dict:
        raise ValueError(
            "NO DPP=0.0. Provide at least one source file with DPP=0.0.")

    accel_inst = _create_accel_instance(options.accel_cls, files_dict,
                                        options.output_path, options.twissfile)

    _create_models_by_madx(accel_inst, files_dict.keys())

    for dpp in files_dict:
        files = files_dict[dpp]
        twiss_dpp_path = _join_with_output_path(options.output_path,
                                                "twiss_{:f}.dat".format(dpp))
        _rungetllm(twiss_dpp_path, files, dpp, options.output_path, accel_inst,
                   options.algorithm)

    # The GUI wants the default files to have the names without _0.0
    _copy_default_outfiles(options.output_path)

    #TODO: HOPE THAT GETLLM DOES A BETTER JOB
    LOG.warn("Cleaning files of NAN! This should not be necessary!")
    all_files = os.listdir(options.output_path)
    tfs_utils.remove_nan_from_files(
        [os.path.join(options.output_path, f) for f in all_files],
        replace=True)

    # adding data
    betalistx = {}
    betalisty = {}
    couplelist = {}
    betalistxf = {}
    betalistyf = {}
    couplelistf = {}

    listx = []
    listxf = []
    listy = []
    listyf = []
    listc = []
    listcf = []

    for dpp in files_dict.keys():
        LOG.debug("Loading driven data for dpp {:f}".format(dpp))
        betx_path = _join_with_output_path(
            options.output_path, 'getbetax{ext:s}'.format(ext=_ext(dpp)))
        bety_path = _join_with_output_path(
            options.output_path, 'getbetay{ext:s}'.format(ext=_ext(dpp)))
        couple_path = _join_with_output_path(
            options.output_path, 'getcouple{ext:s}'.format(ext=_ext(dpp)))

        betx = metaclass.twiss(betx_path)
        bety = metaclass.twiss(bety_path)
        couple = metaclass.twiss(couple_path)

        betalistx[dpp] = betx
        betalisty[dpp] = bety
        couplelist[dpp] = couple

        if float(dpp) == 0.0:
            zerobx = betx
            zeroby = bety

        listx.append(betx)
        listy.append(bety)
        listc.append(couple)

        modeld = metaclass.twiss(options.twissfile)

        try:
            betaxf_path = _join_with_output_path(
                options.output_path,
                'getbetax_free{ext:s}'.format(ext=_ext(dpp)))
            betxf = metaclass.twiss(betaxf_path)
            LOG.debug("Loaded betax free data from '{:s}".format(betaxf_path))

            betayf_path = _join_with_output_path(
                options.output_path,
                'getbetay_free{ext:s}'.format(ext=_ext(dpp)))
            betyf = metaclass.twiss(betayf_path)
            LOG.debug("Loaded betay free data from '{:s}".format(betayf_path))

            couplef_path = _join_with_output_path(
                options.output_path,
                'getcouple_free{ext:s}'.format(ext=_ext(dpp)))
            couplef = metaclass.twiss(couplef_path)
            LOG.debug(
                "Loaded coupling free data from '{:s}".format(couplef_path))
        except IOError:
            use_free = False
            LOG.warn("WARNING: Could not open all of the free data files.")
        else:
            use_free = True
            betalistxf[dpp] = betxf
            betalistyf[dpp] = betyf
            couplelistf[dpp] = couplef

            listxf.append(betxf)
            listyf.append(betyf)
            listcf.append(couplef)

            modelf = modeld

            path_ac_file = options.twissfile.replace(".dat", "_ac.dat")
            if not os.path.isfile(path_ac_file):
                LOG.error(
                    "Ac file '{:s}' does not exist.".format(path_ac_file))
                LOG.error(
                    "  -> In GUI check 'Ac dipole' box to create a model with ac dipole."
                )
                sys.exit(1)
            modeld = metaclass.twiss(path_ac_file)
            if float(dpp) == 0.0:
                zerobxf = betalistxf[dpp]
                zerobyf = betalistyf[dpp]

    LOG.debug("Getting Driven beta")
    # H
    fileobj = _chromFileWriter(
        'beta',
        _join_with_output_path(options.output_path, "chrombetax" + _ext()),
        'H')
    bpms = bpm_util.intersect(listx)
    bpms = bpm_util.model_intersect(bpms, modeld)
    _do_lin_reg_bet(fileobj, files_dict.keys(), betalistx, bpms, "H", zerobx,
                    modeld)
    del fileobj

    # V
    fileobj = _chromFileWriter(
        'beta',
        _join_with_output_path(options.output_path, "chrombetay" + _ext()),
        'V')
    bpms = bpm_util.intersect(listy)
    bpms = bpm_util.model_intersect(bpms, modeld)
    _do_lin_reg_bet(fileobj, files_dict.keys(), betalisty, bpms, "V", zeroby,
                    modeld)
    del fileobj
    LOG.debug("Driven beta finished")

    LOG.debug("Getting Driven coupling")
    fileobj = _chromFileWriter(
        'coupling',
        _join_with_output_path(options.output_path, "chromcoupling" + _ext()),
        '')
    bpms = bpm_util.intersect(listc)
    bpms = bpm_util.model_intersect(bpms, modeld)
    _do_linreg_coupling(couplelist, bpms, files_dict.keys(), fileobj)
    del fileobj
    LOG.debug("Driven coupling finished")

    if use_free:
        #
        # free beta
        #
        LOG.debug("Getting Free beta")
        # H
        fileobj = _chromFileWriter(
            'beta',
            _join_with_output_path(options.output_path,
                                   "chrombetax_free" + _ext()), 'H')
        bpms = bpm_util.intersect(listxf)
        bpms = bpm_util.model_intersect(bpms, modelf)
        _do_lin_reg_bet(fileobj, files_dict.keys(), betalistxf, bpms, "H",
                        zerobxf, modelf)

        # V
        fileobj = _chromFileWriter(
            'beta',
            _join_with_output_path(options.output_path,
                                   "chrombetay_free" + _ext()), 'V')
        bpms = bpm_util.intersect(listyf)
        bpms = bpm_util.model_intersect(bpms, modelf)
        _do_lin_reg_bet(fileobj, files_dict.keys(), betalistyf, bpms, "V",
                        zerobyf, modelf)
        LOG.debug("Free beta finished")

        LOG.debug("GettingFree coupling")
        fileobj = _chromFileWriter(
            'coupling',
            _join_with_output_path(options.output_path,
                                   "chromcoupling_free" + _ext()), '')
        bpms = bpm_util.intersect(listcf)
        bpms = bpm_util.model_intersect(bpms, modelf)
        _do_linreg_coupling(couplelistf, bpms, files_dict.keys(), fileobj)
        LOG.debug("Free coupling finished")