def parse_cmdline(argv): """ Returns the parsed argument list and return code. :param argv: is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Calculates the proton dissociation constant ' '(PKA) for the given radially-corrected free ' 'energy data for a set of coordinates.') parser.add_argument("-d", "--base_dir", help="The starting point for a file search " "(defaults to current directory)", default=os.getcwd()) parser.add_argument("-f", "--src_file", help="The single file to read from (takes precedence " "over base_dir)") parser.add_argument('-p', "--pattern", help="The file pattern to search for " "(defaults to '{}')".format(DEF_FILE_PAT), default=DEF_FILE_PAT) parser.add_argument('-o', "--overwrite", help='Overwrite existing target file', action='store_true') parser.add_argument('-c', "--coord_ts", help='Manually entered coordinate of TS. ' 'Used in place of first local maximum.', type=float) parser.add_argument("temp", help="The temperature in Kelvin for the simulation", type=float) try: args = parser.parse_args(argv) except SystemExit as e: warning(e) parser.print_help() return [], INPUT_ERROR return args, GOOD_RET
def atom_distances(rst, atom_pairs): """Finds the distance between the each of the atom pairs in the given LAMMPS dump file. :param rst: A file in the LAMMPS dump format. :param atom_pairs: Zero or more pairs of atom IDs to compare. :returns: Nested dicts keyed by time step, then pair, with the distance as the value. """ results = OrderedDict() flat_ids = set(itertools.chain.from_iterable(atom_pairs)) tstep_atoms, tstep_box = find_atom_data(rst, flat_ids) for tstep, atoms in tstep_atoms.items(): pair_dist = OrderedDict({FILENAME: os.path.basename(rst)}) for pair in atom_pairs: try: row1 = atoms[pair[0]] row2 = atoms[pair[1]] pair_dist[pair] = pbc_dist(row1[-3:], row2[-3:], tstep_box[tstep]) except KeyError as e: warning(MISSING_TSTEP_ATOM_MSG.format(rst, tstep, e)) return results[tstep] = pair_dist return results
def parse_cmdline(argv): """ Returns the parsed argument list and return code. :param argv: is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Calculates the average and standard deviation ' 'for the given radially-corrected free ' 'energy data for a set of coordinates.') parser.add_argument("-d", "--base_dir", help="The starting point for a file search " "(defaults to current directory)", default=os.getcwd()) parser.add_argument('-p', "--pattern", help="The file pattern to search for " "(defaults to '{}')".format(DEF_FILE_PAT), default=DEF_FILE_PAT) parser.add_argument('-o', "--overwrite", help='Overwrite existing target file', action='store_true') try: args = parser.parse_args(argv) except SystemExit as e: warning(e) parser.print_help() return [], INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv=None): """ Returns the parsed argument list and return code. :param argv: A list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Block averages input data for WHAM') parser.add_argument("-d", "--base_dir", help="The starting point for a meta file search " "(defaults to current directory)", default=os.getcwd()) parser.add_argument('-p', "--pattern", help="The meta file pattern to search for " "(defaults to '{}')".format(DEF_FILE_PAT), default=DEF_FILE_PAT) parser.add_argument('-s', "--steps", help="The number of averaging steps to take " "(defaults to '{}')".format(DEF_STEPS_NUM), type=int, default=DEF_STEPS_NUM) parser.add_argument('-o', "--overwrite", help='Overwrite existing locations', action='store_true') args = None try: args = parser.parse_args(argv) except SystemExit as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Reads in a file and counts the number of columns on the first line.') parser.add_argument("-f", "--file", help="The location of the file to be analyzed.") parser.add_argument("-n", "--new_name", help="Name of amended file.", default=DEF_NEW_FNAME) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description="Creates a new version of a psf file. " "Options include renumbering molecules." ) parser.add_argument( "-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg, ) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (KeyError, InvalidDataError) as e: warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
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 found_files = find_files_by_dir(args.base_dir, args.pattern) print("Found {} dirs with files to combine".format(len(found_files))) for f_dir, files in found_files.items(): if not files: logger.warn("No files with pattern '{}' found for dir '{}'".format(args.pattern, f_dir)) continue combo_file = os.path.join(f_dir, args.target_file) if os.path.exists(combo_file) and not args.overwrite: warning("Target file already exists: '{}' \n" "Skipping dir '{}'".format(combo_file, f_dir)) continue combo = combine([os.path.join(f_dir, tgt) for tgt in files]) write_combo(extract_header(os.path.join(f_dir, files[0])), combo, combo_file) return GOOD_RET # success
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Grabs selected info from the designated file. ' 'The required input file provides the location of the file. ' 'Optional info is an atom index for the last atom not to consider.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Creates lammps data files from pdb files, given a template data file.' 'The required input file provides the name/location of the ' 'template file and a file with a list of data files to convert.') parser.add_argument("-c", "--config", help="The location of the configuration file. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Creates a new version of a pdb file. Atoms will be numbered ' 'starting from one. Options include renumbering molecules.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning(e) parser.print_help() return args, IO_ERROR except (KeyError, InvalidDataError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Compares parts of data files to determine differences.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Compares parameters (i.e. bond coeffs) of data files to ' 'determine differences. The first is read to align with the first' 'column in the dictionary; the rest to the second.') parser.add_argument("-c", "--config", help='The location of the configuration file in ini format. See the ' 'example file /test/test_data/evbd2d/compare_data_types.ini. ' 'The default file name is compare_data_types.ini, located in the ' 'base directory where the program as run.', default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
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 found_files = find_files_by_dir(args.base_dir, args.pattern) print("Found {} dirs with files to combine".format(len(found_files))) for f_dir, files in found_files.items(): if not files: logger.warn("No files with pattern '{}' found for dir '{}'".format( args.pattern, f_dir)) continue combo_file = os.path.join(f_dir, args.target_file) if os.path.exists(combo_file) and not args.overwrite: warning("Target file already exists: '{}' \n" "Skipping dir '{}'".format(combo_file, f_dir)) continue combo = combine([os.path.join(f_dir, tgt) for tgt in files]) write_combo(extract_header(os.path.join(f_dir, files[0])), combo, combo_file) return GOOD_RET # success
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET: return ret # Read template and data files cfg = args.config try: data_tpl_content = process_data_tpl(cfg) old_new_atom_num_dict = {} old_new_atom_type_dict = {} # Will return an empty dictionary for one of them if that one is not true if cfg[MAKE_ATOM_NUM_DICT] or cfg[MAKE_ATOM_TYPE_DICT]: make_atom_dict(cfg, data_tpl_content, old_new_atom_num_dict, old_new_atom_type_dict) # Will return empty dicts if no file if not cfg[MAKE_ATOM_TYPE_DICT]: old_new_atom_type_dict = read_csv_dict(cfg[ATOM_TYPE_DICT_FILE]) process_data_files(cfg, data_tpl_content, old_new_atom_type_dict) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except InvalidDataError as e: warning("Problems reading data:", e) return INVALID_DATA return GOOD_RET # success
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET or args is None: return ret # Read template and data files cfg = args.config try: chk_file_list = file_rows_to_list(cfg[CHK_FILE_LIST]) if len(cfg[REL_E_SEC]) > 0: extracted_data = get_ene_data(cfg, chk_file_list) ref_e_dict = read_csv_dict(cfg[REF_E_FILE], one_to_one=False, str_float=True) find_rel_e(extracted_data, cfg, ref_e_dict) else: for chK_file in chk_file_list: get_evb_atoms(cfg, chK_file) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except InvalidDataError as e: warning("Problems reading data:", e) return INVALID_DATA # print(psf_data_content[ATOMS_CONTENT]) return GOOD_RET # success
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Creates data files from lammps data in the format of a template data ' 'file. The required input file provides the location of the ' 'template file, a file with a list of data files to convert, and ' '(optionally) dictionaries mapping old data number or types to new, ' 'to reorder and/or check that the atom type order ' 'is the same in the files to convert and the template file. \n' 'Note: Dictionaries of data types can be made, **assuming the atom ' 'numbers correspond**. The check on whether they do can be used to ' 'make a list of which atom numbers require remapping.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Reads in a file and counts the number of columns on the first line.') parser.add_argument("-f", "--file", help="The location of the file to be analyzed.") parser.add_argument("-n", "--new_name", help="Name of amended file.", default=DEF_NEW_FNAME) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET or args is None: return ret len_buffer = None try: if args.buffer is not None: try: len_buffer = float(args.buffer) except ValueError: raise InvalidDataError("Input for buffer ({}) could not be converted to a float.".format(args.buffer)) if args.out_dir is None: args.out_dir = os.path.dirname(args.file) if args.min_max_file is None: min_max_dict = None else: min_max_dict = read_csv(args.min_max_file, quote_style=csv.QUOTE_NONNUMERIC) process_file(args.file, args.out_dir, len_buffer, args.delimiter, min_max_dict, header=args.names, make_hist=args.histogram) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except InvalidDataError as e: warning("Problems reading data:", e) return INVALID_DATA return GOOD_RET # success
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Changes a lammps data file by implementing options such as: ' 'reorder atom ids in a lammps data file, given a dictionary to ' 'reorder the atoms (a csv of old_index,new_index), and/or ' 'change the atom, bond, angle, dihedral, and/or improper types,' 'given a dictionary to do so. Can also ' 'print info for selected atom ids. ') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format." "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (InvalidDataError, KeyError, MissingSectionHeaderError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='For each timestep, find the highest protonated state ci^2 and ' 'highest ci^2 for a hydronium. ' 'Currently, this script expects only one protonatable residue.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (KeyError, InvalidDataError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description='Grabs selected info from the designated file. ' 'The required input file provides the location of the file. ' 'Optional info is an atom index for the last atom not to consider.') parser.add_argument( "-c", "--config", help="The location of the configuration file in ini " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Compares sequential lines of files. If two consecutive lines are ' 'equal, keeps only the first.') parser.add_argument("-f", "--src_file", help="The location of the file to be processed.") args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def read_cfg(floc, cfg_proc=process_cfg): """ Reads the given configuration file, returning a dict with the converted values supplemented by default values. :param floc: The location of the file to read. :param cfg_proc: The processor to use for the raw configuration values. Uses default values when the raw value is missing. :return: A dict of the processed configuration file's data. """ config = ConfigParser() try: good_files = config.read(floc) if not good_files: raise IOError('Could not read file {}'.format(floc)) main_proc = cfg_proc(dict(config.items(MAIN_SEC)), DEF_CFG_VALS, REQ_KEYS, int_list=False) except (ParsingError, KeyError) as e: raise InvalidDataError(e) # Check the config file does not have sections that will be ignored for section in config.sections(): if section not in SECTIONS: warning( "Found section '{}', which will be ignored. Expected section names are: {}" .format(section, ", ".join(SECTIONS))) # # Validate conversion input for conv in [COL1_CONV, COL2_CONV]: if main_proc[conv]: main_proc[conv] = conv_str_to_func(main_proc[conv]) return main_proc
def parse_cmdline(argv=None): """ Returns the parsed argument list and return code. :param argv: A list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description='Compresses duplicate rows in a ' 'given file based on values from ' 'a given column') parser.add_argument( '-c', '--column', default=DEF_COL_NAME, help="Specify dupe column. (defaults to {})".format(DEF_COL_NAME), metavar="DUPE_COL") parser.add_argument("file", help="The CSV file to process") args = [] try: args = parser.parse_args(argv) except SystemExit as e: warning(e) return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Creates pdb files from lammps data, given a template pdb file.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (KeyError, InvalidDataError, MissingSectionHeaderError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def read_cfg(floc, cfg_proc=process_cfg): """ Reads the given configuration file, returning a dict with the converted values supplemented by default values. :param floc: The location of the file to read. :param cfg_proc: The processor to use for the raw configuration values. Uses default values when the raw value is missing. :return: A dict of the processed configuration file's data. """ config = ConfigParser() try: good_files = config.read(floc) if not good_files: raise IOError('Could not read file {}'.format(floc)) main_proc = cfg_proc(dict(config.items(MAIN_SEC)), DEF_CFG_VALS, REQ_KEYS, int_list=False) except (ParsingError, KeyError) as e: raise InvalidDataError(e) # Check the config file does not have sections that will be ignored for section in config.sections(): if section not in SECTIONS: warning("Found section '{}', which will be ignored. Expected section names are: {}" .format(section, ", ".join(SECTIONS))) # # Validate conversion input for conv in [COL1_CONV, COL2_CONV]: if main_proc[conv]: main_proc[conv] = conv_str_to_func(main_proc[conv]) return main_proc
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description='Compares parameters (i.e. bond coeffs) of data files to ' 'determine differences. The first is read to align with the first' 'column in the dictionary; the rest to the second.') parser.add_argument( "-c", "--config", help='The location of the configuration file in ini format. See the ' 'example file /test/test_data/evbd2d/compare_data_types.ini. ' 'The default file name is compare_data_types.ini, located in the ' 'base directory where the program as run.', default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def read_cfg(floc, cfg_proc=process_cfg): """ Reads the given configuration file, returning a dict with the converted values supplemented by default values. :param floc: The location of the file to read. :param cfg_proc: The processor to use for the raw configuration values. Uses default values when the raw value is missing. :return: A dict of the processed configuration file's data. """ config = ConfigParser() try: good_files = config.read(floc) except ParsingError as e: raise InvalidDataError(e) if not good_files: raise IOError('Could not read file {}'.format(floc)) main_proc = cfg_proc(dict(config.items(MAIN_SEC)), DEF_CFG_VALS, REQ_KEYS, int_list=False) # Check that there is a least one subsection, or this script won't do anything. Check that all sections given # are expected or alert user that a given section is ignored (thus catches types, etc.) no_work_to_do = True for section in config.sections(): if section in SECTIONS: if section in SUB_SECTIONS: if len(config.items(section)) > 0: no_work_to_do = False else: warning("Found section '{}', which will be ignored. Expected section names are: {}" .format(section, ", ".join(SECTIONS))) if no_work_to_do: warning("No filtering will be applied as no criteria were found for the expected subsections ({})." "".format(", ".join(SUB_SECTIONS))) for section in [MAX_SEC, MIN_SEC]: main_proc[section] = check_vals(config, section) main_proc[BIN_SEC] = get_bin_data(config, BIN_SEC) return main_proc
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Compares sequential lines of files. If two consecutive lines are ' 'equal, keeps only the first.') parser.add_argument("-f", "--src_file", help="The location of the file to be processed.") args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. :param argv: is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Sorts the data in given XYZ input file ' 'into bins of the given interval, producing ' 'a VMD-formatted file containing the average ' 'XYZ coordinate, plus a detailed log file.') parser.add_argument("-s", "--bin_size", help="The size interval for each bin in Angstroms", default=0.1, type=float) parser.add_argument("-c", "--bin_coordinate", help='The xyz coordinate to use for bin sorting', default='z', choices=COORDS) parser.add_argument("infile", help="A three-field-per-line XYZ coordinate file to process") args = None try: args = parser.parse_args(argv) except SystemExit as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Grabs selected info rom the psf file.' 'The required input file provides the location of the ' 'file and the data requested.') parser.add_argument("-c", "--config", help="The location of the configuration file in ini " "format. See the example file /test/test_data/data2data/data_get_info. " "The default file name is data_get_info.ini, located in the " "base directory where the program as run.", default=DEF_CFG_FILE, type=read_cfg) args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except KeyError as e: warning("Input data missing:", e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv=None): """ Returns the parsed argument list and return code. :param argv: A list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Compresses duplicate rows in a ' 'given file based on values from ' 'a given column') parser.add_argument('-c', '--column', default=DEF_COL_NAME, help="Specify dupe column. (defaults to {})".format(DEF_COL_NAME), metavar="DUPE_COL") parser.add_argument("file", help="The CSV file to process") args = [] try: args = parser.parse_args(argv) except SystemExit as e: warning(e) return args, INPUT_ERROR return args, GOOD_RET
def comp_files(cfg, atom_id_dict, type_dicts): """ Compares each section of data files @param cfg: configuration information for current run @param atom_id_dict: dictionary for changing the atom id @param type_dicts: dictionary for changing atom and interaction types @return: """ first_content, first_section_order = proc_data_file( cfg, cfg[DATA_FILE], atom_id_dict, type_dicts, ) second_content, second_section_order = proc_data_file( cfg, cfg[DATA_COMP], atom_id_dict, type_dicts, ) for section in second_section_order: if section not in first_section_order: warning("Skipping section '{}'; section found in the file: {}\n" " but not in file: {}".format(section, cfg[DATA_COMP], cfg[DATA_FILE])) diffs = ["Differences in head section:"] compare_heads(first_content[SEC_HEAD], second_content[SEC_HEAD], diffs) for section in first_section_order: if section not in second_section_order: warning("Skipping section '{}'; section found in the file: {}\n" " but not in file: {}".format(section, cfg[DATA_FILE], cfg[DATA_COMP])) elif section in [SEC_VELOS]: diffs.append("\nSkipping section '{}'".format(section)) elif section in COMP_ORD_SEC_COL_DICT: diffs.append("\nDifferences in section '{}':".format(section)) num_col_to_compare = COMP_ORD_SEC_COL_DICT[section] compare_lists(first_content[section], second_content[section], 0, num_col_to_compare, diffs, SEC_FORMAT_DICT[section][0], SEC_FORMAT_DICT[section][1]) elif section in NUM_SEC_DICT: diffs.append("\nDifferences in section '{}':".format(section)) num_col_to_compare = NUM_SEC_DICT[section][1] compare_lists(first_content[section], second_content[section], 1, num_col_to_compare, diffs, SEC_FORMAT_DICT[section][0], SEC_FORMAT_DICT[section][1]) else: print("Encountered unexpected section '{}'".format(section)) f_name = create_out_fname(cfg[DATA_COMP], prefix='diffs_', ext='.txt') list_to_file(diffs, f_name) print('Completed writing {}'.format(f_name))
def process_raw_cfg(raw_cfg): cfgs = {} param_sections = [] # Process raw values for section in raw_cfg: section_dict = {} if section == MAIN_SEC: for entry in raw_cfg[section]: section_dict[entry[0]] = entry[1] else: param_sections.append(section) section_dict[SEC_PARAMS] = [] for entry in raw_cfg[section]: if entry[0] == GROUP_NAMES: section_dict[entry[0]] = entry[1] else: section_dict[SEC_PARAMS].append(entry[0]) vals = [x.strip() for x in entry[1].split(',')] if len(vals) == 2: vals.append(DEF_DESCRIP) try: section_dict[entry[0]] = {LOW: float(vals[0]), HIGH: float(vals[1]), DESCRIP: vals[2]} except ValueError: warning("In configuration file section {}, expected comma-separated numerical lower range " "value, upper-range value, and (optional) description (i.e. '-10,10,d_OO') for key {}. " "Found {}. Please check input.".format(section, entry[0], entry[1])) cfgs[section] = section_dict # Check for defaults for section in cfgs: if section == MAIN_SEC: for cfg in MAIN_SEC_DEF_CFG_VALS: if cfg not in cfgs[section]: cfgs[section][cfg] = MAIN_SEC_DEF_CFG_VALS[cfg] else: if section in PARAM_SECS: for cfg in PARAM_SEC_DEF_CFG_VALS: if cfg not in cfgs[section]: cfgs[section][cfg] = PARAM_SEC_DEF_CFG_VALS[cfg] else: warning("This program currently expects only the sections {} and an optional section {}. Read section " "{}, which will be ignored.".format(PARAM_SECS, MAIN_SEC, section)) # Add main section with defaults if this optional section is missing; make sure required sections and parameters # have been read. if MAIN_SEC not in cfgs: cfgs[MAIN_SEC] = MAIN_SEC_DEF_CFG_VALS for section in PARAM_SECS: if section in cfgs: for param in FIT_PARAMS[section]: if param not in cfgs[section]: raise InvalidDataError('The configuration file is missing parameter {} in section {}. ' 'Check input.'.format(param, section)) else: raise InvalidDataError('The configuration file is missing section {}. Check input.'.format(section)) return cfgs
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Reads in a file containing a header with columns of data. Using ' 'specifications from a configuration file, it changes values in rows ' 'based on column min and/or max values, and overwrites the original ' 'file.') parser.add_argument( "-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) parser.add_argument( "-d", "--delimiter", help="Delimiter separating columns in the FILE to be edited. " "The default is: '{}'".format(DEF_DELIMITER), default=DEF_DELIMITER) parser.add_argument("-b", "--base_dir", help="The starting point for a file search " "(defaults to current directory)", default=os.getcwd()) parser.add_argument("-f", "--src_file", help="The single file to read from (takes precedence " "over base_dir)") args = None try: args = parser.parse_args(argv) except IOError as e: warning(e) parser.print_help() return args, IO_ERROR except (InvalidDataError, SystemExit) as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def find_atom_data(lammps_f, atom_ids): """Searches and returns the given file location for atom data for the given IDs. :param lammps_f: The LAMMPS data file to search. :param atom_ids: The set of atom IDs to collect. :return: A nested dict of the atoms found keyed first by time step, then by atom ID. :raises: InvalidDataError If the file is missing atom data or is otherwise malformed. """ tstep_atoms = OrderedDict() tstep_box = {} atom_count = len(atom_ids) empty_dims = np.full(3, np.nan) with open(lammps_f) as lfh: file_name = os.path.basename(lammps_f) tstep_id = None box_dim = np.copy(empty_dims) tstep_val = "(no value)" for line in lfh: if line.startswith(TSTEP_LINE): try: tstep_val = next(lfh).strip() tstep_id = int(tstep_val) # Todo: remove if never used except ValueError as e: raise InvalidDataError( "Invalid timestep value {}: {}".format(tstep_val, e)) elif line.startswith(NUM_ATOM_LINE): # not needed, so just move along next(lfh) elif line.startswith(BOX_LINE): try: for coord_id in range(len(box_dim)): box_vals = list(map(float, next(lfh).strip().split())) if len(box_vals) == 2: box_dim[coord_id] = box_vals[1] - box_vals[0] except (ValueError, KeyError) as e: raise InvalidDataError( "Invalid PBC value read on timestep {}: {}".format( tstep_val, e)) elif tstep_id is not None: atom_lines = find_atom_lines(lfh, atom_ids, tstep_id, file_name) if len(atom_lines) != atom_count: try: missing_atoms_err(atom_ids, atom_lines, tstep_id, file_name) except InvalidDataError as e: warning(e) warning("Skipping timestep and continuing.") else: tstep_atoms[tstep_id] = atom_lines tstep_box[tstep_id] = box_dim tstep_id = None box_dim = empty_dims return tstep_atoms, tstep_box
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Reads in a file containing a header with columns of data. Using ' 'specifications from a configuration file, it filters rows based ' 'on column min and/or max values, and prints a file of the filtered ' 'data.') parser.add_argument( "-f", "--file", help= "The location of the file with the dimensions with one line per vector, " "space-separated, containing at least two lines. The default file is {}, " "located in the current directory".format(DEF_ARRAY_FILE), default=DEF_ARRAY_FILE) parser.add_argument( "-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) parser.add_argument( "-d", "--delimiter", help="Delimiter separating columns in the FILE to be filtered. " "The default is: '{}'".format(DEF_DELIMITER), default=DEF_DELIMITER) args = None try: args = parser.parse_args(argv) except IOError as e: warning(e) parser.print_help() return args, IO_ERROR except (InvalidDataError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Make combine output from multiple files, with a common column ' 'name, printing only data from common column values. ') parser.add_argument("-d", "--delimiter", help="The delimiter separating the file names in each row of the" "compare_file_list. The default delimiter is '{}'.".format(DEF_DELIM), default=DEF_DELIM) parser.add_argument("-f", "--compare_file_list", help="The location of the file that lists the files to be " "combined. Each row should contain a list of files to be " "combined by aligning on the col_name. " "The default file name is {}, located in the " "directory where the program as run.".format(DEF_CMP_FILE), default=DEF_CMP_FILE, type=read_cmp_file) parser.add_argument("-l", "--output_location", help="The location (directory) for output files. The default is the " "directory from which the program was called.", default=None) parser.add_argument("-n", "--col_name", help="The common column name in the files used as the key to combine " "files. The default file name is {}.".format(DEF_ALIGN_COL_NAME), default=DEF_ALIGN_COL_NAME) parser.add_argument("-o", "--out_name", help="The output file name. The default is {}.".format(DEF_OUT_FILE), default=DEF_OUT_FILE) parser.add_argument("-s", "--sep_out", help="A flag to specify a separate output files should be created for " "the aligned files from each row of the compare_file_list. If this " "is specified, the out_name will be used as a suffix. The base name " "will be based on the common part of the names of the files to be " "combined. If there is no common string, the output files will be " "numbered based on their row number in the compare_file_list. Separate " "output files will also be created if the column names from files on " "different lines to not match.", action='store_true') args = None try: args = parser.parse_args(argv) except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (KeyError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'For each timestep, gather the energy information output by LAMMPS ' 'in the log file.') parser.add_argument("-f", "--file", help="The log file to be processed.", default=None) parser.add_argument( "-l", "--list_file", help="The a file with a list of log files to be processes.", default=None) args = None try: args = parser.parse_args(argv) if args.file is None: args.file_list = [] else: if os.path.isfile(args.file): args.file_list = [args.file] args.source_name = args.file else: raise IOError("Could not find specified log file: {}".format( args.file)) if args.list_file is not None: args.file_list += file_rows_to_list(args.list_file) args.source_name = args.list_file if len(args.file_list) < 1: raise InvalidDataError( "Found no log file names to process. Specify one or more files as specified in " "the help documentation ('-h').") except IOError as e: warning("Problems reading file:", e) parser.print_help() return args, IO_ERROR except (KeyError, InvalidDataError, SystemExit) as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def process_cfg_conv(raw_cfg, def_cfg_vals=None, req_keys=None, int_list=True): """ Converts the given raw configuration, filling in defaults and converting the specified value (if any) to the default value's type. @param raw_cfg: The configuration map. @param def_cfg_vals: dictionary of default values @param req_keys: dictionary of required types @param int_list: flag to specify if lists should converted to a list of integers @return: The processed configuration. """ proc_cfg = {} for key in raw_cfg: if not (key in def_cfg_vals or key in req_keys): raise InvalidDataError( "Unexpected key '{}' in configuration ('ini') file.".format( key)) key = None try: for key, def_val in def_cfg_vals.items(): proc_cfg[key] = conv_raw_val(raw_cfg.get(key), def_val, int_list) for key, type_func in req_keys.items(): proc_cfg[key] = type_func(raw_cfg[key]) except KeyError as e: raise KeyError("Missing config val for key '{}'".format(key, e)) except Exception as e: raise InvalidDataError('Problem with config vals on key {}: {}'.format( key, e)) if proc_cfg[SCIPY_OPT_METHOD] != DEF_OPT_METHOD: proc_cfg[SCIPY_OPT_METHOD] = proc_cfg[SCIPY_OPT_METHOD].lower() if proc_cfg[SCIPY_OPT_METHOD] not in TESTED_SCIPY_MIN: warning( "Only the following optimization methods have been tested: scipy.optimize.minimize with {}." "".format(TESTED_SCIPY_MIN)) for int_key in [TEMP, NITER_SUCCESS]: if proc_cfg[int_key] is not None: proc_cfg[int_key] = float(proc_cfg[int_key]) # Remove any repeated parameters, or zero-character-length params (can happen if accidentally an additional comma) if len(proc_cfg[OPT_PARAMS]) > 0: filtered_opt_params = [] for param in proc_cfg[OPT_PARAMS]: if len(param) > 0: if param in filtered_opt_params: warning("'{}' repeated in '{}'; skipping repeated entry". format(param, OPT_PARAMS)) else: filtered_opt_params.append(param) proc_cfg[OPT_PARAMS] = filtered_opt_params return proc_cfg
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser(description='Reads in space-separated columns and returns the min, max, avg, and ' 'std dev for each column.') parser.add_argument("-f", "--file", help="The location of the file with the dimensions with one line per vector, " "space-separated, containing at least two lines. The default file is {}, " "located in the current directory".format(DEF_ARRAY_FILE), default=DEF_ARRAY_FILE) parser.add_argument("-b", "--buffer", help="If specified, the program will output only the max dimension" "in each column plus an additional buffer amount (float).", default=None) parser.add_argument("-d", "--delimiter", help="Delimiter. Default is '{}'".format(DEF_DELIMITER), default=DEF_DELIMITER) parser.add_argument("-m", "--min_max_file", help="CSV file with column names (first line), " "initial values (second line), min values " "(third line), and max values (fourth line), used to further " "analyze the data file.", default=None) parser.add_argument("-n", "--names", help="File contains column names (header) (default is false). " "Note: lines beginning with '#' are ignored.", action='store_true') parser.add_argument("-o", "--out_dir", help="Output folder. Default is the directory of the file to be processed.", default=None) parser.add_argument("-s", "--histogram", help="Create histograms of the non-numerical data (default is false).", action='store_true') args = None try: args = parser.parse_args(argv) except SystemExit as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv=None): """ Returns the parsed argument list and return code. :param argv: A list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description='Finds the distances between each pair ' 'of atoms listed in the pair file for ' 'each time step in the given LAMMPS dump ' 'file.') parser.add_argument( "-p", "--pair_files", action="append", default=[], help="One or more files containing atom pairs (default {0})".format( DEF_PAIRS_FILE)) parser.add_argument("-f", "--file", help="The dump file to process", default=None) parser.add_argument("-l", "--list_file", help="The file with a list of dump files to process", default=None) args = None try: args = parser.parse_args(argv) if not args.pair_files: args.pair_files.append(DEF_PAIRS_FILE) if not os.path.isfile(DEF_PAIRS_FILE): raise InvalidDataError( "No pair file specified and did not find the default " "pair file: {}".format(DEF_PAIRS_FILE)) if (args.file is None) and (args.list_file is None): raise InvalidDataError( "Specify either a file or list of files to process.") except (KeyError, InvalidDataError, SystemExit) as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def print_gofr(cfg, gofr_data): g_dr = cfg[GOFR_DR] dr_array = gofr_data[GOFR_BINS][1:] - g_dr / 2 gofr_out_fieldnames = [GOFR_R] gofr_output = dr_array if cfg[CALC_HO_GOFR]: normal_fac = np.square( dr_array) * gofr_data[HO_STEPS_COUNTED] * 4 * np.pi * g_dr gofr_ho = np.divide(gofr_data[HO_BIN_COUNT], normal_fac) gofr_out_fieldnames.append(GOFR_HO) gofr_output = np.column_stack((gofr_output, gofr_ho)) if cfg[CALC_OO_GOFR]: normal_fac = np.square( dr_array) * gofr_data[OO_STEPS_COUNTED] * 4 * np.pi * g_dr gofr_oo = np.divide(gofr_data[OO_BIN_COUNT], normal_fac) gofr_out_fieldnames.append(GOFR_OO) gofr_output = np.column_stack((gofr_output, gofr_oo)) if cfg[CALC_HH_GOFR]: normal_fac = np.square( dr_array) * gofr_data[HH_STEPS_COUNTED] * 4 * np.pi * g_dr gofr_hh = np.divide(gofr_data[HH_BIN_COUNT], normal_fac) gofr_out_fieldnames.append(GOFR_HH) gofr_output = np.column_stack((gofr_output, gofr_hh)) if cfg[CALC_OH_GOFR]: normal_fac = np.square( dr_array) * gofr_data[OH_STEPS_COUNTED] * 4 * np.pi * g_dr gofr_oh = np.divide(gofr_data[OH_BIN_COUNT], normal_fac) gofr_out_fieldnames.append(GOFR_OH) gofr_output = np.column_stack((gofr_output, gofr_oh)) if cfg[CALC_TYPE_GOFR]: if gofr_data[TYPE_STEPS_COUNTED] > 0: normal_fac = np.square( dr_array) * gofr_data[TYPE_STEPS_COUNTED] * 4 * np.pi * g_dr gofr_type = np.divide(gofr_data[TYPE_BIN_COUNT], normal_fac) gofr_out_fieldnames.append(GOFR_TYPE) gofr_output = np.column_stack((gofr_output, gofr_type)) else: warning("Did not find any timesteps with the pairs in {}. " "This output will not be printed.".format(CALC_TYPE_GOFR)) f_out = create_out_fname(cfg[DUMP_FILE_LIST], suffix='_gofrs', ext='.csv', base_dir=cfg[OUT_BASE_DIR]) # am not using the dict writer because the gofr output is a np.array list_to_csv([gofr_out_fieldnames] + gofr_output.tolist(), f_out, print_message=cfg[PRINT_PROGRESS], round_digits=ROUND_DIGITS)
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Reads in files containing a header with columns of data. Using ' 'specifications from a configuration file, it compares rows and ' 'adds an RMSD to the comparison file.') parser.add_argument( "-f", "--file", help= "The location of the file with the dimensions with one line per vector, " "space-separated, containing at least two lines. The default file is {}, " "located in the current directory".format(DEF_ARRAY_FILE), default=DEF_ARRAY_FILE) parser.add_argument( "-b", "--base_file", help="The location of the file values used for comparison. There should " "be two lines: a column headers followed by values. " "The default file is {}, " "located in the current directory".format(DEF_BASE_FILE), default=DEF_BASE_FILE) args = None try: args = parser.parse_args(argv) except IOError as e: warning(e) parser.print_help() return args, IO_ERROR except (InvalidDataError, SystemExit) as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def parse_cmdline(argv): """ Returns the parsed argument list and return code. :param argv: is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description='Creates a radial correction value for each line ' 'of the target file(s). \n' 'The output is a three-column file: original coord, original free' ' energy, radially-corrected free-energy (with zero set to equal' ' the furthest (highest) coordinate corrected free energy).') parser.add_argument("-d", "--base_dir", help="The starting point for a file search " "(defaults to current directory)", default=os.getcwd()) parser.add_argument("-f", "--src_file", help="The single file to read from (takes precedence " "over base_dir)") parser.add_argument('-p', "--pattern", help="The file pattern to search for " "(defaults to '{}')".format(DEF_FILE_PAT), default=DEF_FILE_PAT) parser.add_argument('-o', "--overwrite", help='Overwrite existing target file', action='store_true') parser.add_argument("temp", help="The temperature in Kelvin for the simulation", type=float) args = None try: args = parser.parse_args(argv) except SystemExit as e: if hasattr(e, 'code') and e.code == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def pair_avg(vals): """ Returns a list of the average between pairs of numbers in the input list. If there is an odd final input value, it is dropped. :param vals: A list of floats to pair and average. :return: The average of adjacent pairs in the given input list. """ results = [] for pair in chunk(vals, 2, list): if len(pair) == 2: results.append(sum(pair) / 2) else: warning("'{}' is not a pair".format(pair)) return results
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET or args is None: return ret try: process_file(args.base_file, args.file) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except (ValueError, InvalidDataError) as e: warning("Problems reading data:", e) return INVALID_DATA return GOOD_RET # success
def read_2int_dict(dict_file): """ Reads a two-field csv of integer values from dictionary and passes back as a dictionary """ two_item_dict = {} with open(dict_file) as csv_file: for line in csv.reader(csv_file): if len(line) == 0: continue if len(line) != 2: raise ValueError('Expected two entries per line on dictionary file file {}'.format(csv_file)) try: two_item_dict[int(line[0])] = int(line[1]) except ValueError: warning("Could not convert line the following line to two integers: {}\n " "in file: {}".format(line, csv_file)) return two_item_dict
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET: return ret try: process_file(args.file, args.begin, args.end, args.new_name) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except InvalidDataError as e: warning("Problems reading data:", e) return INVALID_DATA return GOOD_RET # success
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: return ret kbt = calc_kbt(args.temp) if args.coord_ts is not None: logger.info("Read TS coordinate value: '{:8.3f}'".format(args.coord_ts)) try: if args.src_file is not None: file_data = read_csv(args.src_file, data_conv=KEY_CONV) f_base_name = os.path.basename(args.src_file) try: pka, cur_corr, cur_coord = calc_pka(file_data, kbt, args.coord_ts) result = [{SRC_KEY: f_base_name, PKA_KEY: pka, MAX_VAL: cur_corr, MAX_LOC: cur_coord}] except NoMaxError: result = [{SRC_KEY: f_base_name, PKA_KEY: NO_MAX_RET, MAX_VAL: NO_MAX_RET, MAX_LOC: NO_MAX_RET}] write_result(result, args.src_file, args.overwrite) else: found_files = find_files_by_dir(args.base_dir, args.pattern) logger.debug("Found '{}' dirs with files to process".format(len(found_files))) if len(found_files) == 0: raise IOError("No files found in specified directory '{}'".format(args.base_dir)) for f_dir, files in found_files.items(): results = [] for pmf_path, fname in ([(os.path.join(f_dir, tgt), tgt) for tgt in sorted(files)]): file_data = read_csv(pmf_path, data_conv=KEY_CONV) try: pka, cur_corr, cur_coord = calc_pka(file_data, kbt, args.coord_ts) results.append({SRC_KEY: fname, PKA_KEY: pka, MAX_VAL: cur_corr, MAX_LOC: cur_coord}) except NoMaxError: results.append({SRC_KEY: fname, PKA_KEY: NO_MAX_RET, MAX_VAL: NO_MAX_RET, MAX_LOC: NO_MAX_RET}) write_result(results, os.path.basename(f_dir), args.overwrite, basedir=os.path.dirname(f_dir)) except IOError as e: warning(e) return IO_ERROR return GOOD_RET # success
def parse_cmdline(argv): """ Returns the parsed argument list and return code. `argv` is a list of arguments, or `None` for ``sys.argv[1:]``. """ if argv is None: argv = sys.argv[1:] # initialize the parser object: parser = argparse.ArgumentParser( description= 'Reads in a file containing a header with columns of data. Using ' 'specifications from a configuration file, it combines data from ' 'two columns per row and outputs that to a new row, adding a ' 'prefix, middle (string joining the two columns), and suffix, if ' 'supplied. The user can also specify that the column information be ' 'converted (i.e. to an int).') parser.add_argument( "-c", "--config", help="The location of the configuration file in ini format. " "The default file name is {}, located in the " "base directory where the program as run.".format(DEF_CFG_FILE), default=DEF_CFG_FILE, type=read_cfg) parser.add_argument( "-f", "--file", help="The location of the file with the column data to combine. " "The default file name is {}, " "located in the current directory".format(DEF_FILE), default=DEF_FILE) args = None try: args = parser.parse_args(argv) except IOError as e: warning(e) parser.print_help() return args, IO_ERROR except (InvalidDataError, SystemExit) as e: if e.message == 0: return args, GOOD_RET warning(e) parser.print_help() return args, INPUT_ERROR return args, GOOD_RET
def main(argv=None): # Read input args, ret = parse_cmdline(argv) if ret != GOOD_RET or args is None: return ret cfg = args.config try: process_file(args.file, cfg) except IOError as e: warning("Problems reading file:", e) return IO_ERROR except (ValueError, InvalidDataError) as e: warning("Problems reading data:", e) return INVALID_DATA return GOOD_RET # success
def process_data_files(cfg, data_tpl_content): # Don't want to change the original template data when preparing to print the new file: chk_atom_type = cfg[CHECK_ATOM_TYPE] data_dict = {} if chk_atom_type: try: with open(cfg[ATOM_TYPE_DICT_FILE], 'r') as d_file: data_dict = json.load(d_file) except IOError as e: warning("Problems reading dictionary file: {}\n" "The program will continue without checking atom types.".format(cfg[ATOM_TYPE_DICT_FILE]), e) chk_atom_type = False for data_file in cfg[DATA_FILES]: process_data_file(cfg, chk_atom_type, data_dict, data_file, data_tpl_content)