def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Rebin the power spectra for the input files. The structure of the input files must be like the following: # sum(w) sum(w^2n(z)) sum(w^2) #data ### ### ### #random ### ### ### # k P(k) P(k)+noise n_modes [...] The output files will have the same structure If the number of bins in the input file are not multiples of 'n_bins', the last bins will be discarded """ p = ap.ArgumentParser(description=description, formatter_class=apc.RawDescrArgDefHelpFormatter) p.add_argument('merge_bins', action='store', type=int, help="Number of bins to merge") p.add_argument("ifname", action=apc.file_exists(), nargs='+', help="""Input file name(s).""") p = apc.version_verbose(p, '1') p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p.add_argument('-f', '--from-bin', type=int, action='store', help='Ignore the first %(dest)s bins') p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Read one or more files and extract a part of the file according to the constraints given. Examples: %(prog)s 'c3 < 4' filename %(prog)s '(c3 < 4) | (c3 > 8)' filename %(prog)s '(c4 > 8) & (c4 < 4)' filename WARNING: The constraint is evaluated with 'eval' and no check is done upon local or global variables.""" p = ap.ArgumentParser(description=description, formatter_class=ap.RawDescriptionHelpFormatter) #p.add_argument("column", action="store", type=int, help="Column to check") p.add_argument("constr", action="store", help="""Constraints to be applied to the desired column""") p.add_argument("ifname", nargs='+', action=apc.file_exists(), help="Input file name(s)") p = apc.version_verbose(p, '1.0') p, group = apc.insert_or_replace(p, print_def=True) p, group = apc.overwrite_or_skip(p) p, pandas = apc.pandas_group(p) p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files. (default: %(default)s)") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = "Move or swap some column in a (list of) file(s)." p = ap.ArgumentParser(description=description, ) p.add_argument("from_cols", action="store", type=apc.int_or_list, help="""Column(s) to copy. (if more than one, give them as a string of integers separated by spaces)""") p.add_argument("to_cols", action="store", type=apc.int_or_list, help="""Where to copy the column(s). (if more than one, give them as a string of integers separated by spaces)""") p.add_argument("ifname", action=apc.file_exists(), nargs='+', help="""Input file name(s)""") p = apc.version_verbose(p, '0.1') p.add_argument("-s", "--swap", action="store_true", help="Swap 'from' with 'to'") p.add_argument("--substitute", action="store", type=float, help="""If given, substitutes the content of the columns 'from_cols' not contained in 'to_cols' with '%(dest)s'. This is executed only if 'swap' is *False* and after moving""") p, group = apc.insert_or_replace(p, print_def=True) p, group = apc.overwrite_or_skip(p) p, pandas = apc.pandas_group(p) p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files. (default: %(default)s)") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group( p, description=description ) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """Execute operations between columns and save in a column. Example: c3+c6-1: add column 3 to column 6 and subtract 1. """ p = ap.ArgumentParser( description=description, #formatter_class=apc.RawDescrArgDefHelpFormatter) formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("operation", action="store", help="""Operation to execute between columns.""") p.add_argument( "to_col", action="store", type=int, help="""Column where the operation is saved. If it is larger than the number of columns, the result is appended""") p.add_argument("ifname", nargs='+', action=apc.file_exists(), help="""Input file name(s)""") p = apc.version_verbose(p, '0.1') p.add_argument( "-s", "--substitute", action="store", type=float, help="""If given, substitutes the content of the columns used for the operation with '%(dest)s'. This is executed before copying the result of the operation to the desired column""") p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p, pandas = apc.pandas_group(p) p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files.") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """Given a file with the fraction for downsample as function of redshift and one or more catalogues, downsample them according to the given fractions""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument( "downsample", action=apc.file_exists(), help="""File with the fraction to downsample as function of redshift.""" ) p.add_argument( "ifname", action=apc.file_exists(), nargs='+', help="Input file name(s), containing z in one of the columns") p = apc.version_verbose(p, '0.1') p.add_argument("-z", "--z-column", action="store", type=int, default=2, help="Column containing the redshift") p.add_argument( "-k", "--kind", action="store", type=select_interp, default="linear", help="""Specifies the kind of interpolation of the downsample file as a string {} or as an integer specifying the order of the spline interpolator to use. Default is ‘linear’.""".format(interp_type)) p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p, pandas = apc.pandas_group(p) p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """Given a table of 'z,n(z)', subsitute a column in the catalogue(s) with the value of n(z), using the values of z from an other column of the same file """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("noz", action="store", type=ap.FileType('r'), help="File containing a table of z, n(z)") p.add_argument( "ifname", nargs='+', action=apc.file_exists(), help="""Input file name(s), containing z in one of the columns""") p = apc.version_verbose(p, '1') p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p.add_argument("-z", "--z-column", action="store", type=int, default="-1", help="Column in the catalogue with the redshift.") p.add_argument("-n", "--nz-column", action="store", type=int, default="5", help="Column in the catalogue in which n(z) is saved.") p.add_argument("--zn-file-col", action="store", type=int, nargs=2, default=[0, 1], help="""Columns in 'noz' containing z and n(z).""") p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Convert the file from ra, dec, redshift into cartesian coordinates assuming a cosmology. The name of the output file name is derived from the input one and the modification can be set with the options. ra, dec and redshift are by default assumed to be in the first three columns, and all the following columns are by default copied after x, y and z. If the number of columns in the input file (or in the ones read) is more than 8 the exceding ones are cut. Redshift is copied in the last column. The output file has the structure x y z w bias n(x,y,z) n_b(z) M redshift (The columns that are not present in the input file are filled with 1, except the redshift)""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("ifname", nargs='+', action=apc.file_exists(), help="""Input file name(s), containing ra and dec in the first two columns""") p = apc.version_verbose( p, '1' ) p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p, pandas = apc.pandas_group(p) description="""Cosmology to use to convert ra, dec and z in distances. h0=1 is equivalent to have the distance in Mpc/h with any other value of h0""" p, cosmo = apc.cosmology_group( p, description=description, h0_def=1. ) cosmo.add_argument("--zrange", action="store", nargs=2, type=float, help="""Lower and upper limit for the redshift. If this option is given the distance is computed only ones and then interpolated in the values in the files""") cosmo.add_argument("--nbins", action="store", type=int, default='500', help='Number of bins in redshift.') p.add_argument('--negative-z', action='store', choices=[None, 'skip', 'tozero'], default=None, help="""If *None* no check on negative redshifts, otherwise either skip the corresponding lines or set to 0""") p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") p.add_argument("--usecols", action="store", nargs="+", type=int, help="""Read the selected columns. By default read all the columns.If thi option is used, make sure the the first three columns read in are ra, dec and redshift.""") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group( p, description=description ) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Computes N(z) and n(z) from one column in a (set of) file(s) and save it (them) for each file and/or computes the mean and save it. Weights can be applied. When the 'area' is not specified, a standard histogram is produced. The output files have the following structure: bin_center bin_lower bin_upper hist (std) std is added only if the mean is required """ p = ap.ArgumentParser(description=description, formatter_class=apc.RawDescrArgDefHelpFormatter) int_or_oper = """If integer, the given column is considered. Operations between columns can be performed preceding the column number with a c: e.g. c3+c6-1 add columns 3 and 6 and subtract 1 from the result. only after the histogram is created""" p.add_argument("column", action="store", type=apc.int_or_str, help="""Columns containing the redshift (or whatever variable for the histogram). """ + int_or_oper) p.add_argument("ifname", nargs='+', action=apc.file_exists(), help="""Input file name(s)""") p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p = apc.version_verbose(p, '0.1') p.add_argument( "-w", "--weight", action="store", type=apc.int_or_str, help="""Weight for the histogram. Operation permitted as for 'column' If boolean expressions like '(c3==1)&(c6==0)', the weight is interpreted as 1 if *True*, as 0 if *False*""") p.add_argument("-n", "--nbins", action="store", type=int, default='50', help="Number of bins per histogram.") p.add_argument( "--range", action="store", nargs=2, type=float, help="""Lower and upper range of the bins. Computed from the first file if any of the following are required: i) the mean; ii) n(z) [so the area needs to be computed]; iii) pandas and chunks """) p.add_argument("--mean", action="store", type=apc.outfile, help="""Save the mean in file '%(dest)s'""") p.add_argument("--mean-only", action="store_true", help="Only the mean of the histograms is saved") p, pandas = apc.pandas_group(p) description = """ If the area of the survey is given, the effective volume per bin is computed and the histogram of n(z) is returned. 'h0=1' is equivalent to requiring the volume to be in (Mpc/h)^3. For performance reasons, if the range is not given it will be computed as for when the mean is requested""" p, cosmo = apc.cosmology_group(p, description=description, h0_def=1) cosmo.add_argument("-a", "--area", type=float, action="store", help="Area of the survey in steradians") p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Merge the input window matrices or power spectra files. The first two columns must be k and W(k) or P(k). The files are ordered according to k[0] and all k>k[-1]*fkN are ignored. When two or more window functions/power spectra overlap the one with smaller k[0] will be considered. """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("ofname", action='store', help="Output file name") p.add_argument("ifnames", action=apc.file_exists(), nargs='+', help="Input file name(s)") p = apc.version_verbose(p, '0.1') p.add_argument("-o", "--overwrite", action="store_true", help="Overwrite the output file name") p.add_argument("--fraction-kN", action="store", type=float, default=0.65, dest='fkN', help="All the modes larger than fkN*k[-1] are discarded.") p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") smooth = p.add_argument_group( title='Smoothing', description="""Smooth (or fit) the merged files""") excsmooth = smooth.add_mutually_exclusive_group() excsmooth.add_argument( '-m', '--mean', action='store_true', help="""Subsitute the window function with its mean""") excsmooth.add_argument('-s', '--smooth', action='store_true', help="""Smooth the window function""") excsmooth.add_argument('-f', '--fit', action='store_true', help="""Perfom a power law fit""") smooth.add_argument('-k', '-k-sub', action='store', type=float, nargs=2, default=[-1, -1], help="""Minimum and maximum wavenumbers to substitute. If negative numbers are give the minimum and/or maximum of k after merging are used.""") smooth.add_argument( '--k-fit', action='store', type=float, nargs=2, help="""Minimum and maximum wavenumbers to use to compute the mean or do the fit. If not given, use 'k_sub'. If 'smooth' is used, '%(dest)s' must include 'k'.""") wchoises = ['flat', 'hanning', 'hamming', 'bartlett', 'blackman'] smooth.add_argument('-w', '--window', action='store', choices=wchoises, default='hanning', help="Only for 'smooth'. Window type") smooth.add_argument( '-l', '--window-len', action='store', default=11, type=int, help= "Only for 'smooth'. Dimension of the smoothing window; should be an odd integer" ) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """Read two columns c1, c2 from 'replacement' file, then replace in 'column' of the input file(s) c1 -> c2.""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("column", action="store", type=int, help="Column to substitute") p.add_argument("replacement", action=apc.file_exists(), help="""File containing the substitution to do.""") p.add_argument("ifname", nargs='+', action=apc.file_exists(), help="Input file name(s)") p = apc.version_verbose(p, '1.0') p, group = apc.insert_or_replace(p, print_def=True) p, group = apc.overwrite_or_skip(p) p.add_argument("-c", "--repl-columns", nargs='+', action=apc.required_length(1, 2), type=int, default=[0, 1], help="""Columns to read from 'replacement' file. If only one column is read, the content of 'column' in 'ifname' is casted to int.""") p, pandas = apc.pandas_group(p) p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files. (default: %(default)s)") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """Give a list of fits files, read some of the columns into a numpy array and then save them into ascii files. Operations between colums permitted but numbers are interpreted as such and not columns. Permitted operations: {0} """.format(permitted_operations) p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument( "ifname", nargs='+', action=apc.file_exists(), help="Input file name(s), containing z in one of the columns") p = apc.version_verbose(p, '1') p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) finfo = p.add_argument_group(title="file information", description="""Get information from the fits files""") finfo.add_argument("--show-columns", action="store_true", help="""Print on screen the info for the first file in 'ifname'""") finfo.add_argument("--all-files", action="store_true", help="""Show the info of all the files. Ignored if '--show-columns' not used.""") finfo.add_argument( "--tofile", action="store", type=ap.FileType("w"), help="""Write the output to file '%(dest)s' instead than to stdout. Ignored if '--show-columns' not used.""") p.add_argument( "-c", "--columns", nargs='+', default=["RA", "DEC", "Z"], help="""Read the given column(s). Can be: integer, string and lists. Operations permitted for string entry like 'a+b-1': 'a' and 'b' are column name and '1' a number.""") p.add_argument("--fmt", action=apc.StoreFmt, nargs='+', default="%7.6e", help="Format of the output files.") description = """Parameters related to the parallel computation""" p, parallel = apc.parallel_group(p, description=description) return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse as ap import argparse_custom as apc description = """ Computes the mean, standard deviation and, if reqired, the covariance from a list of power spectra. The power spectra are assumed to be computed from the same FFT grid and in the wavenumbers. The structure of the input files must be like the following: # sum(w) sum(w^2n(z)) sum(w^2) #data ### ### ### #random ### ### ### # k P(k) P(k)+noise n_modes [...] The output files with the mean and standard deviation will have the following structure # sum(w) sum(w^2n(z)) sum(w^2) #mean_data ### ### ### #mean_random ### ### ### #stddev_data ### ### ### #stddev_random ### ### ### # k mean P(k) stddev P(k) n_modes [...] """ p = ap.ArgumentParser(description=description, formatter_class=apc.RawDescrArgDefHelpFormatter) p.add_argument("ofname", action="store", help="""Output file name. If more that one element in '--norm_sh' is given, the string associated to the various cases is inserted after '%(dest)s.'""") p.add_argument("ifname", action=apc.file_exists(), nargs='+', help="Input file name(s). The files are ordered, to be able to use 'correct_sh'") p = apc.version_verbose(p, '1') p.add_argument("-o", "--overwrite", action="store_true", help="Overwrite the output file name") p.add_argument("--before", action="store", help="""Insert a dot and the string of selected cases before '%(dest)s' instead of the end of the output file name""") p.add_argument("-c", "--covariance", action='store', help="""Computes and saves the covariance matrix to file '%(dest)s'. If more that one element in '--norm_sh' is given, the string associated to the various cases is inserted after '%(dest)s.'""") p.add_argument( "--norm-sh", nargs="+", choices=ns_choices, default=ns_choices[0], metavar='CHOICES', help="""Cases to work on. The part before the '_' regards the power spectrum normalisation, the one after the shot noise. The choices are CHOISES={0}. The input power spectra are for case '{0[0]}'. If 'all' given, all the cases done.""".format(ns_choices)) p.add_argument("--correct-sh", nargs=3, help="""Corrects the shot noise estimated from the randoms (i.e. for the cases '*_ran') substituting 'P_SN' with 'n_tot/n_redshift * P_SN': 'n_tot' and 'n_redshift' are the number of objects that should have redshift and the number of objects with measured redshifts. Those two numbers are in columns '%(dest)s[1]' and '%(dest)s[2]' of file '%(dest)s[0]'. The number of lines in '%(dest)s[0]' must be the same as the number of input files and the order assumed to be the same as the ordered list of files.""") p.add_argument("-t", "--total-number", type=int, help="""If given and smaller than the number of input file names, randomly remove enought file, and rows in 'correct_sh', in order to get the the desired number""") p.add_argument("--fmt-ps", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output mean files") p.add_argument("--fmt-cov", default="%7.6e", help="Format of the output covariance files") return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser istance and return a populated namespace Parameters ---------- argv: list of strings list to be parsed output: namespace --------- """ import argparse_custom as apc description = """ Change the shot noise and/or the amplitude of the input files. The structure of the input files must be like the following: # sum(w) sum(w^2n(z)) sum(w^2) #data ### ### ### #random ### ### ### # k P(k) P(k)+noise n_modes [...] The output files will have the same structure """ p = ap.ArgumentParser(description=description, formatter_class=apc.RawDescrArgDefHelpFormatter) p.add_argument("ifname", action=apc.file_exists(), nargs='+', help="""Input file name(s).""") p = apc.version_verbose(p, '1') p.add_argument("--before", action="store", help="""Insert a dot and the string of selected cases before '%(dest)s' instead of the end of the output file name""") p, group = apc.overwrite_or_skip(p) p.add_argument("--norm-sh", nargs="+", choices=ps_me.ns_choices, default=ps_me.ns_choices[0], metavar='CHOICES', help="""Cases to work on. The part before the '_' regards the power spectrum normalisation, the one after the shot noise. The choices are CHOISES={0}. The input power spectra are for case '{0[0]}'. If 'all' given, all the cases done.""".format(ps_me.ns_choices)) p.add_argument( "--correct-sh", nargs=3, action=read_n, help="""'%(dest)s[0]' contains one line (header ignored) whose '%(dest)s[1]' and '%(dest)s[2]' elements are 'n_tot' and 'n_redshift', used to correct the shot noise estimated from the randoms substituting 'P_SN' with 'n_tot/n_redshift * P_SN'. If given, the file is read directly and the two numbers are returned. Any line besides the first id ignored""") p.add_argument("--fmt", default="%7.6e", action=apc.StoreFmt, nargs='+', help="Format of the output files") return p.parse_args(args=argv)