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 --------- """ description = """Given a covariance matrix in a column of the input file(s), make it square""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("ifname", action="store", nargs='+', type=ap.FileType('r'), 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_replace1(p) p, group = apc.overwrite_or_skip(p) p.add_argument("-c", "--column", action="store", type=int, default=4, help="Column number in the input file containing the covariance.") 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 = """Subtract from the first three columns of the input files the three floats. If they are not provided they will be computed from the files themselves. """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument( "ifname", action="store", nargs='+', type=ap.FileType('r'), 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("--rescale", action="store", type=float, nargs=3, help="""Values to subtract from the first three columns""") p.add_argument("-o", "--offset", action="store", type=float, default=0, help="""Extra offset to add to 'rescale'""") 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 = """ 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 --------- """ description = """The weights w are preplaced by w/w_fpk, with w_fkp = 1/(1+ n(z) * pw )""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("pw", action="store", type=float, help="Value of 'pw' to use for the FKP weight") p.add_argument( "ifname", action="store", nargs='+', type=ap.FileType('r'), help= "Input file name(s), containing ra and dec in the first two columns") p = apc.version_verbose(p, '0.1') p, group = apc.insert_or_replace(p) p, group = apc.overwrite_or_skip(p) p.add_argument("-n", "--n-col", type=int, default=5, help="""Column containin the number density""") p.add_argument("-w", "--w-col", type=int, default=3, help="""Column containin the weights""") 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 --------- """ description = """Given a set of catalogues with x, y and z in the first three columns, compute the redshift corresponding to the objects and write the output in an extra column or in an existing one""" p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("ifname", action="store", nargs='+', type=ap.FileType('r'), 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.add_argument("-z", "--z-col", type=int, help="""If given the redshifts are substituted in '%(dest)s' column. Otherwise an extra column is appended in the output file""") p.add_argument("-f", "--zdist-file", type=ap.FileType('r'), help="""File containing a table with resdshift and comoving distance. If not given, this table is computed using the cosmological parameters that can be set with the options.""") description="""If the file with the table of z and D_c(z) is not given, this is compute using the following parameters""" p, cosmo = apc.cosmology_group( p, description=description ) cosmo.add_argument("--zrange", type=float, nargs=2, action="store", default=[0.4, 0.8], help="Maximum and minimum redshift where to compute the comoving distance") cosmo.add_argument("--nbins", action="store", type=int, default='500', help='Number of bins in redshift.') 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 --------- """ description = """ Given a file (list) containing power spectra with structure k, P(k), P(K)+shotnoise, and a fraction 'f', saves a file with k, P(k)-f*shotnoise """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("fraction", action="store", type=float, help="Fraction for rescaling the shot noise") p.add_argument( "ifname", action="store", nargs='+', type=ap.FileType('r'), help= "Input file name(s), containing k, P(k) and P(k)+shotnoise (maybe something else)." ) p = apc.version_verbose(p, '0.1') p, group = apc.insert_or_replace1(p, print_def=True) p, group = apc.overwrite_or_skip(p) p.add_argument("--fmt", default="%7.6e", action=apc.store_fmt, help="Format of the output files. (default: %(default)s)") 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 optsparse: allows nice command line option handling import argparse_custom as apc p.add_argument("") p = apc.version_verbose(p, '1') return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser instance 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 = """Draw 1D histograms for parameters from a list of MCMC chains. File name and structure is base on CosmoMC. ifroot+ext-chain must have the following structure: 'counter, likelihood, parameters'. ifroot+ext-paramn: are the paramname files """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument("ifroot", action="store", nargs='+', help="Input file name(s)") p = apc.version_verbose(p, '2') # columns p.add_argument( "-c", "--columns", action="store", nargs='+', default=['omegam', 'omegal', 'H0'], help="""Name of the columns as they appear in the first column of each parameter file name.""") # file related options pf = p.add_argument_group(description='Input-output file options') pf.add_argument("--ext-chain", action="store", default=".0.5_total.dat", help="""Extension to create the chain file names""") pf.add_argument("--ext-parmn", action="store", default=".paramnames", help="""Extension of the parameter file names""") pf.add_argument('-o', '--output', help="""Save the contour plot into file '%(dest)s' instead of plotting it""") # plot options pp = p.add_argument_group(description="Plot related options") pp.add_argument("--set-MNRAS", action='store_true', help='Use MNRAS presets for plotting') pp.add_argument("--figure-figsize", nargs=2, default=[10., 10.], type=float, action=apc.Cm2Inch, help="Figure size in cm") pp.add_argument("-n", "--n-cols", type=int, default=3, help='Number of subplot columns') pp.add_argument("--bounds", action="store", type=float, nargs=4, default=[0, 0, 1, 1], help="""(left, bottom, right, top) in the normalized figure coordinate passed to 'plt.tight_layout'""") pp.add_argument("--x-lim", nargs='+', action=apc.multiple_of(3, reshape=True), default=[], help="""Set x axis range in subplot '%(dest)s[0]' to '%(dest)s[1:3]'. The subplot is identified with the short name from the parameter file.""") pp.add_argument("--x-label", nargs='+', action=apc.multiple_of(2, reshape=True), default=[], help="""Set x axis label in subplot '%(dest)s[0]' to '%(dest)s[1]'. The sublot is identified with the short name from the parameter file. Multiple labels can be drawn providing couple of subplot-label.""") pp.add_argument("--y-label", default="$P/P_{max}$", help='x axis label') pp.add_argument("-w", "--lines-linewidth", action="store", type=float, help="Line width for the contour plots") pp.add_argument("--vertical", nargs='+', action=apc.multiple_of(2, reshape=True), default=[], help="""Plot a vertical line in subplot '%(dest)s[0]' at x='%(dest)s[1]'. The subplot is identified with the short name from the parameter file. Multiple lines can be drawn providing couple of subplot-line.""") # 1D histograms options p1D = p.add_argument_group(description="""Histogram creation options""") p1D.add_argument("-b", "--num-bins", action="store", type=int, default="80", help="Number of bins for the 2D histogram") #text and font options pfo = p.add_argument_group(description="Text and font options") pfo.add_argument("--axes-labelsize", type=float, help="Axis font size") pfo.add_argument("-t", "--text", nargs='+', action=apc.multiple_of(4, reshape=True), default=[], help="""Writes the text '%(dest)s[3]' at coordinates x='%(dest)s[1]', y='%(dest)s[2]' in axes '%(dest)s[0]'. Multiple text can be given providing, giving triplet of x,y, and text""") pfo.add_argument("--font-size", type=float, help="""Texts font size.""") pfo.add_argument("--text-bkgr", help="Text background") #legend options pl = p.add_argument_group(description='Legend options') pl.add_argument("--legend", nargs='+', default=[], help="""Legend tags. If given, the number of elements must be the same as the number of input root and in the same order""") pl.add_argument( "--legend-plot", action="store", help="""Put the legend in plot '%(dest)s'. The subplot is identified with the short name from the parameter file. If '%(dest)s' is not in the list of parameters, the legend is drawn in the first empty subplot, if any, otherwise the figure legend is drawn. If not given, the legend is drawn in a plot (depends on the dictionary hashing)""") pl.add_argument("--loc", type=apc.int_or_str, default=0, help='Legend location (see matplotlib legend help)') pl.add_argument("--legend-fontsize", type=float, help="""Legend tags font size.""") pl.add_argument("--color-text", action="store_true", help="""Legend text with the same color as the lines in the contour plots.""") pl.add_argument("--invert-legend", action="store_true", help="Invert the legend entries") pl.add_argument("--legend-frameon", action="store_true", help='Legend frame') 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 optsparse: allows nice command line option handling import argparse_custom as apc description = """Plot the rows of the window matrices """ p = ap.ArgumentParser(description=description, formatter_class=ap.ArgumentDefaultsHelpFormatter) p.add_argument( "ifroot", action="store", nargs='+', help= "Root of the file name(s) containing the window matrix and the values of k for the plots" ) p = apc.version_verbose(p, '1') p.add_argument( "-w", "--win-ext", default=".Wij.dat", action="store", help="""Extension for the window matrix file with dimension NxM. The full name must be 'ifroot+%(dest)s'.""") p.add_argument("--k-ext", default=".kj.dat", action="store", help="""Extension for the file with the k with dimension M. The full name must be 'ifroot+%(dest)s'.""") p.add_argument("-k", "--krange", nargs=2, default=[-1, -1], type=float, action="store", help="""Range of k to plot. If -1 minimum and/or maximum from the file of k.""") p.add_argument("--rows", nargs=3, default=[-1, -1, 1], type=int, action="store", help="""Rows of the window matrix to plot: begin, end and stride. If %(dest)s[0]<0, set to 0; if %(dest)s[1]<0 or >N, set to 0.""" ) p.add_argument("--shift", type=int, default=0, action="store", help="""Shift the starting point of the rows for each of the input window matrices""") p.add_argument("--lw", type=float, default=1.7, action="store", help="Line width") p.add_argument("-l", "--legend", action="store", nargs="+", help="""Enable the legene in position '%(dest)s[0]' with tags '%(dest)s[1:]'. Must be len(%(dest)s)=len(ifroot)+1.""") p.add_argument("--cmap", action=apc.store_cycle, default="Paired", nargs="+", help="""Colormap(s) to use cyclically. http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html """ ) p.add_argument("-y", "--y-lim", action="store", type=float, nargs=2, default=[0, 0.3], help="Limits on the y axis") p.add_argument("-o", "--ofile", action="store", type=ap.FileType('w'), help="Output file name. If not given, plot shown.") return p.parse_args(args=argv)
def parse(argv): """ This function accept a list of strings, create and fill a parser instance 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 import textwrap as tw description = tw.dedent("""\ Given a list of file roots and of corresponding tags, reads the parameter name files and the chain files. Then computes the mean and either the standard deviation (default) or the given percentile of the parameters for each chain (chains assumed to have the following structure: chain[:,0]=weight; chain[:,1]=likelihood]; chain[:,2:]=parameters). The mean and the stddev or percentile are printed in a latex table with the following format: -------------------------------------------------------------------------------------- tag1 tag2 ... tagn -------------------------------------------------------------------------------------- parameter1 m1+-s1(p1)(file1) m1+-s1(p1)(file2) ... m1+-s1(p1)(filen) parameter2 m2+-s2(p2)(file1) m2+-s2(p2)(file2) ... m2+-s2(p2)(filen) ... ... ... ... ... parameterm mm+-sm(pm)(file1) mm+-sm(pm)(file2) ... mm+-sm(p1)(filen) -------------------------------------------------------------------------------------- """) p = ap.ArgumentParser(description=description, formatter_class=apc.RawDescrArgDefHelpFormatter) p.add_argument( "froottag", metavar="froot-tag", nargs='+', action=apc.multiple_of(2, reshape=True), help="Input file name(s) and header tags. They must be given in pairs") p = apc.version_verbose(p, '2') # columns p.add_argument( "-c", "--columns", action="store", nargs='+', help="""Name of the columns as they appear in the first column of each parameter file name.""") class What(ap.Action): def __call__(self, parser, namespace, values, option_string=None): if values[0] not in ['s', 'p']: msg = "Only 's' or 'p' accecpted as first argument" raise ap.ArgumentTypeError(msg) else: try: values[1] = float(values[1]) except ValueError: msg = "The second argument must be a float" raise ap.ArgumentTypeError(msg) setattr(namespace, self.dest, values) p.add_argument( "-w", "--what", nargs=2, default=["s", 1.], action=What, help="""Decide what to do: '%(dest)s[0]= s' (standard deviation) or 'p' (percentile); if 's' the '%(dest)s[1]*sigma' values saved, if 'p' the '%(dest)s[1] percentile values around the mean computed and saved.""" ) p.add_argument("-r", "--rescale", nargs='+', default=[], action=apc.multiple_of(2, reshape=True), help="""Rescale the values of variable '%(dest)s[0]' by '%(dest)s[1]'. The same rescaling factor is shown in the parameter label. The variable is identified with the short name from the parameter file. Multiple rescaling can be drawn providing couple of variable-rescaling.""") p.add_argument("--label", nargs='+', default=[], action=apc.multiple_of(2, reshape=True), help="""Set the label of the variable '%(dest)s[0]' to '%(dest)s[1]'. The variable is identified with the short name from the parameter file. Multiple labels can be given providing couple of variable-label.""") p.add_argument("--comment", action="store_true", help="""Comment rows of the matrix when all the elements have null variance""") # file related options pf = p.add_argument_group(description='Input-output file options') pf.add_argument("--ext-chain", action="store", default=".0.5_total.dat", help="""Extension to create the chain file names""") pf.add_argument("--ext-parmn", action="store", default=".paramnames", help="""Extension of the parameter file names""") pf.add_argument("-o", "--output", default="latextable.tex", help="Output file name") pf.add_argument( "-l", "--landscape", action="store_true", help="""Save the table in the 'landscape' environment instead of 'table'. Remember to use '\\usepackage{lscape}'""") pf.add_argument("-f", "--format", default=[], nargs='+', action=apc.multiple_of(2, reshape=True), help="""Set the format for the variable '%(dest)s' to '%(dest)s'. The variable is identified with the short name from the parameter file. Multiple formats can be given providing couple of variable-format. All the variables without a specified format will have '%%.3f' assigned""") pf.add_argument("-s", "--spacing", help="""Spacing between table rows. If not set, default used. If given, must be something 4pt. See http://en.wikibooks.org/wiki/LaTeX/Lengths for more info""") 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_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)
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 = """ 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 as ap import argparse_custom as apc import textwrap as tw description = tw.dedent("""\ This program plot the values of the given parameters as function of the maximum scale used to perform the fit. To build the file names first the maximum scale is computed: k_max=numpy.arange(range[0],range[1]+1,stride)/100. Then the full file name is build if the following way: 1) if 'froot' contains only one '{}', this is substituted with k_max and the extension is appended 2) otherwise the file name is build as froot+kmax+extension If more than a file root given and all the columns are plotted, the files must have the same structure. If only selected columns are desired, then they must be present in all the files. """) p = ap.ArgumentParser(description=description, formatter_class=apc.RawTextArgDefHelpFormatter) p.add_argument('range', nargs=2, type=float, action='store', help="Maximum and minimum wavenumber multiplied by 100") p.add_argument('froot', nargs='+', action='store', help="Root of the file names") p = apc.version_verbose(p, '2') p.add_argument("--paramlist", action="store_true", default=False, help=tw.dedent("""\ Read the parameter list from the fist file, print it out and close the program""")) # file related options pf = p.add_argument_group(description='Input-output file options') pf.add_argument("--stride", action="store", type=int, default="1", help="""Stride in 'range'""") pf.add_argument( "--ext-chain", action="store", default="0.5_total.txt", help="""Extension of the input file containing the chain.""") pf.add_argument( "--ext-parmn", action="store", default="paramnames", help="""Extension of the input file containing the parameters' names.""" ) pf.add_argument("-s", "--skip", action="store_true", default=False, help="Skip non existing files") pf.add_argument( "-o", "--ofile", action="store", help="Output file name. If not given, the plot is shown on screen.") # plot options pp = p.add_argument_group(description="Plot related options") pp.add_argument("--variance-only", action="store_true", help="""Plot only the variance.""") pp.add_argument("-c", "--columns", action="store", nargs='+', help=tw.dedent("""\ Name of the columns to plot, as appears in the first column of each parameter file name. If not given, all columns are read""" )) pp.add_argument("-n", "--nsigma", action="store", type=int, default="1", help="Plot the ns-sigma error bars.") pp.add_argument("--set-MNRAS", action='store_true', help='Use MNRAS presets for plotting') pp.add_argument("-m", "--marker-size", action="store", type=float, default="4", help="Marker size.") pp.add_argument("--font-size", action="store", type=int, default="15", help="Axis font size.") pp.add_argument("-w", "--line-width", action="store", type=float, default=1, help="Line width.") pp.add_argument("--shift", action="store", type=float, default=0, help=tw.dedent("""\ Displace the errorbars by '%(dest)s' to avoid overlap. Ignored if 'fill-between' is used""")) pp.add_argument("-f", "--fill", action=apc.required_range(0, 1), type=float, nargs=2, help=tw.dedent("""\ Substitute errorbars with matplotlib 'fill_between' with transparencies in the range [0,1]. No transparency for eps.""")) pp.add_argument("--horizontal", nargs='+', action=apc.multiple_of(2, reshape=True), help=tw.dedent("""\ Plot an horizontal line in subplot '%(dest)s[0]' at y='%(dest)s[1]'. The sublot is identified with the short name from the parameter file. Multiple lines can be drawn providing couple of subplot-line.""")) pp.add_argument("--figsize", type=float, nargs=2, action=apc.Cm2Inch, help="Figure size in cm") pp.add_argument("-b", "--bounds", action="store", type=float, nargs=4, default=[0, 0, 1, 1], help=tw.dedent("""\ (left, bottom, right, top) in the normalized figure coordinate passed to 'plt.tight_layout'""")) pp.add_argument("-r", "--rescale", nargs='+', action=apc.multiple_of(2, reshape=True), help=tw.dedent("""\ Rescale the abscissa in subplot '%(dest)s[0]' by '%(dest)s[1]'. The same rescaling factor is shown in the y label. The sublot is identified with the short name from the parameter file. Multiple rescaling can be drawn providing couple of subplot-rescaling.""")) pp.add_argument("--x-label", default="$k_{\mathrm{max}}\,[h/Mpc]$", help='x axis label') pp.add_argument("--y-label", nargs='+', action=apc.multiple_of(2, reshape=True), help=tw.dedent("""\ Set y axis label in subplot '%(dest)s[0]' to '%(dest)s[1]'. The sublot is identified with the short name from the parameter file. Multiple labels can be drawn providing couple of subplot-label.""")) pp.add_argument("--y-range", nargs='+', action=apc.multiple_of(3, reshape=True), help=tw.dedent("""\ Set y axis range in subplot '%(dest)s[0]' to '%(dest)s[1]'. The sublot is identified with the short name from the parameter file. Multiple ranges can be set providing couple of subplot-range.""")) #legend options pl = p.add_argument_group(description='Legend options') pl.add_argument("-l", "--legend", nargs='+', help=tw.dedent("""\ Legend tags. If given, the number of elements must be the same as the number of input root and in the same order.""")) pl.add_argument("--legend-plot", action="store", help=tw.dedent("""\ Put the legend in plot '%(dest)s'. The sublot is identified with the short name from the parameter file. If '%(dest)s' is not in the list of parameters, the figure legend is drawn. If not given, the legend is drawn in the first plot""")) pl.add_argument("--loc", type=apc.int_or_str, default=0, help='Legend location (see matplotlib legend help)') pl.add_argument( "--legend-fsize", type=float, help="""Legend tags font size. Defaults to axis font size""") #correction of the variance due to number of mocks and bins: Percival ... 2013 description = tw.dedent("""\ Correct the estimated variance by sqrt(m1) or sqrt(m2), as in Percival et al. 2013""") pc = p.add_argument_group(description=description) lines = get_ini().replace('%', '%%') lines = ["Inifile with the following structure:", lines] lines = "".join(lines) pc.add_argument('--ini', type=ap.FileType('r'), help=lines) class Save_Ini(ap.Action): def __call__(self, parser, namespace, values, option_string=None): lines = get_ini() lines = lines.split('\n') lines = [l[4:] for l in lines[1:]] lines = '\n'.join(lines) with open("sample.ini", 'w') as f: f.write(lines) print("'sample.ini' written in the current directory") sys.exit(0) pc.add_argument( '--print-ini', nargs=0, action=Save_Ini, help="Save the inifile to file 'sample.ini' in the current directory") 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)