Ejemplo n.º 1
0
def make_tpl(cfg, tpl_name, filled_tpl_name):
    """
    Combines the dictionary and template file to create the new file(s)
    @param cfg: configuration for the run
    @param tpl_name: the cfg key for the template file name
    @param filled_tpl_name: the cfg key for the filled template file name
    """

    tpl_str = read_tpl(tpl_name)
    tpl_vals_dict = {}

    for value_set in itertools.product(*cfg[TPL_VALS].values()):
        for param, val in zip(cfg[TPL_VALS].keys(), value_set):
            tpl_vals_dict[param] = val

        for eq_param in cfg[TPL_EQ_PARAMS]:
            try:
                string_to_eval = tpl_vals_dict[eq_param].format(
                    **tpl_vals_dict)
            except KeyError as e:
                raise KeyError(
                    "Missing parameter value {} needed to evaluate '{}' for the parameter '{}'."
                    "".format(e, tpl_vals_dict[eq_param], eq_param))
            try:
                tpl_vals_dict[eq_param] = eval(string_to_eval)
            except NameError:
                raise InvalidDataError(
                    "Could not evaluate the string '{}' specifying the value for the parameter "
                    "'{}'. Check order of equation entry and/or input parameter values."
                    "".format(string_to_eval, eq_param))

        fill_save_tpl(cfg, tpl_str, tpl_vals_dict, tpl_name, filled_tpl_name)
Ejemplo n.º 2
0
def rmsd_split(meta_file,
               steps,
               tpl_dir=DEF_TPL_DIR,
               overwrite=False,
               base_dir=None):
    """
    Reads the given meta file, fetches the RMSD files in the inventory, and creates a succession
    of directories that split the original RMSD files into a larger number of chunks for each step
    such that step 1 will create a split of 2 in 01_01 and 01_02, etc.

    :param meta_file: The initial meta file.
    :param steps: The number of averaging steps to perform.
    :param tpl_dir: The directory that contains the submit templates.
    :param overwrite: Whether to overwrite existing files.
    :param base_dir: The base directory to write to (defaults to the meta file's dir)
    """
    meta = read_meta(meta_file)
    rmsd = read_meta_rmsd(meta)
    sub_tpl_base = read_tpl(os.path.join(tpl_dir, DEF_BASE_SUBMIT_TPL))
    sub_tpl_line = read_tpl(os.path.join(tpl_dir, DEF_PART_LINE_SUBMIT_TPL))

    if not base_dir:
        base_dir = meta[DIR_KEY]
    for step in range(1, steps + 1):
        for rmsd_fname, data in rmsd.items():
            data_len = len(data)
            chunk_num = step + 1
            chunk_size = math.floor(data_len / chunk_num)
            logger.debug(STEP_DBG_MSG, step, data_len, rmsd_fname, chunk_num,
                         chunk_size)

            rmsd_chunks = [ch for ch in chunk(data, chunk_size, list)]
            for step_part in range(1, chunk_num + 1):
                rmsd_tgt_dir = os.path.join(
                    base_dir, SPLIT_DIR_FMT.format(step, step_part))
                if not os.path.exists(rmsd_tgt_dir):
                    os.makedirs(rmsd_tgt_dir)
                f_name = os.path.join(rmsd_tgt_dir, rmsd_fname)
                if allow_write(f_name, overwrite=overwrite):
                    write_rmsd(rmsd_chunks[step_part - 1], f_name)

        write_meta(base_dir, meta, step, overwrite)
        write_submit(base_dir, sub_tpl_base, sub_tpl_line, step, overwrite)
Ejemplo n.º 3
0
def main(argv=None):
    """
    Runs the main program.

    :param argv: The command line arguments.
    :return: The return code for the program's termination.
    """
    args, ret = parse_cmdline(argv)
    if ret != GOOD_RET or args is None:
        return ret

    cfg = args.config

    try:
        tpl_str = read_tpl(cfg[PAR_TPL])
        tpl_dict = dict(cfg[TPL_VALS])
        for f_name_key in [BEST_PARAMS_FNAME, FITTING_SUM_FNAME]:
            if cfg[f_name_key] is not None:
                move_existing_file(cfg[f_name_key])
        if len(cfg[OPT_PARAMS]) == 0:
            warning(
                "No parameters will be optimized, as no parameters were listed for the keyword '{}' "
                "in the '{}' section of the configuration file.".format(
                    OPT_PARAMS, MAIN_SEC))
            eval_eqs(cfg, tpl_dict)
            fill_save_tpl(cfg,
                          tpl_str,
                          tpl_dict,
                          cfg[PAR_TPL],
                          cfg[PAR_FILE_NAME],
                          print_info=cfg[PRINT_INFO])
            trial_result = float(
                check_output([cfg[BASH_DRIVER], tpl_dict[NEW_FNAME]]).strip())
            tpl_dict[RESID] = round(trial_result, cfg[NUM_PARAM_DECIMALS])
            if cfg[PAR_COPY_NAME] is not None or cfg[RESULT_COPY] is not None:
                copy_par_result_file(cfg, tpl_dict)
            print("Result without optimizing parameters: {}".format(
                trial_result))
        else:
            min_params(cfg, tpl_dict, tpl_str)

    except (TemplateNotReadableError, IOError) as e:
        warning("Problems reading file: {}".format(e))
        return IO_ERROR
    except (KeyError, InvalidDataError, ValueError) as e:
        warning(e)
        return IO_ERROR

    return GOOD_RET