Parser module¶
Defines the command line options and their help messages in
create_parser()
and read the input command line in parse()
, dealing
with different possible configurations.
The fancy short/long help formatting, as well as the automatic help creation from docstrings is entirely due to Francesco Montesano.
-
class
parser_mp.
MpArgumentParser
(prog=None, usage=None, description=None, epilog=None, version=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True)[source]¶ Bases:
argparse.ArgumentParser
Extension of the default ArgumentParser
-
error
(message)[source]¶ Override method to raise error :Parameters: message (string) –
error message
-
-
parser_mp.
add_subparser
(sp, name, **kwargs)[source]¶ Add a parser to the subparser sp with name.
All the logic common to all subparsers should go here
Parameters: - sp (subparser instance)
- name (str) – name of the subparser
- kwargs (dict) – keywords to pass to the subparser
- output
- ——
- sparser (Argparse instance) – new subparser
-
parser_mp.
create_parser
()[source]¶ Definition of the parser command line options
The main parser has so far two subparsers, corresponding to the two main modes of operating the code, namely run and info. If you simply call
python montepython/MontePython.py -h
, you will find only this piece of information. To go further, and find the command line options specific to these two submodes, one should then do:python montepython/MontePython.py run -h
, orinfo -h
.All command line arguments are defined below, for each of the two subparsers. This function create the automatic help command.
Each flag outputs the following argument to a destination variable, specified by the dest keyword argument in the source code. Please check there to understand the variable names associated with each option.
run
- -N : int
- number of steps in the chain (OBL). Note that when running on a cluster, your run might be stopped before reaching this number.
- -o : str
- output folder (OBL). For instance
-o chains/myexperiments/mymodel
. Note that in this example, the folderchains/myexperiments
must already exist. - -p : str
- input parameter file (OBL). For example
-p input/exoticmodel.param
. - -c : str
input covariance matrix (OPT). A covariance matrix is created when analyzing previous runs.
Note that the list of parameters in the input covariance matrix and in the run do not necessarily coincide.
- -j : str
jumping method (global (default), sequential or fast) (OPT).
With the global method the code generates a new random direction at each step, with the sequential one it cycles over the eigenvectors of the proposal density (= input covariance matrix).
The global method the acceptance rate is usually lower but the points in the chains are less correlated. We recommend using the sequential method to get started in difficult cases, when the proposal density is very bad, in order to accumulate points and generate a covariance matrix to be used later with the default jumping method.
The fast method implements the Cholesky decomposition presented in http://arxiv.org/abs/1304.4473 by Antony Lewis.
- -m : str
sampling method, by default ‘MH’ for Metropolis-Hastings, can be set to ‘NS’ for Nested Sampling (using Multinest wrapper PyMultiNest), ‘CH’ for Cosmo Hammer (using the Cosmo Hammer wrapper to emcee algorithm), and finally ‘IS’ for importance sampling.
Note that when running with Importance sampling, you need to specify a folder to start from.
- –update : int
- update frequency for Metropolis Hastings. If greater than zero, number of steps after which the proposal covariance matrix is updated automatically (OPT).
- -f : float
jumping factor (>= 0, default to 2.4) (OPT).
The proposal density is given by the input covariance matrix (or a diagonal matrix with elements given by the square of the input sigma’s) multiplied by the square of this factor. In other words, a typical jump will have an amplitude given by sigma times this factor.
The default is the famous factor 2.4, advertised by Dunkley et al. to be an optimal trade-off between high acceptance rate and high correlation of chain elements, at least for multivariate gaussian posterior probabilities. It can be a good idea to reduce this factor for very non-gaussian posteriors.
Using
-f 0 -N 1
is a convenient way to get the likelihood exactly at the starting point passed in input.- –conf : str
- configuration file (default to default.conf) (OPT). This file contains the path to your cosmological module directory.
- –chain-number : str
arbitrary number of the output chain, to overcome the automatic one (OPT).
By default, the chains are named
yyyy-mm-dd_N__i.txt
with year, month and day being extracted,N
being the number of steps, andi
an automatically updated index.This means that running several times the code with the same command will create different chains automatically.
This option is a way to enforce a particular number
i
. This can be useful when running on a cluster: for instance you may ask your script to use the job number asi
.- -r : str
restart from last point in chain, to avoid the burn-in stage (OPT).
At the beginning of the run, the previous chain will be deleted, and its content transfered to the beginning of the new chain.
- -b : str
- start a new chain from the bestfit file computed with analyze. (OPT)
- –fisher : None
- Calculates the inverse of the fisher matrix to use as proposal distribution
- –silent : None
- silence the standard output (useful when running on clusters)
- –Der-target-folder : str
- Add additional derived params to this folder. It has to be
used in conjunction with Der-param-list, and the method set to
Der:
-m Der
. (OPT) - –Der-param-list : str
- Specify a number of derived parameters to be added. A
complete example would be to add Omega_Lambda as a derived
parameter:
python montepython/MontePython.py run -o existing_folder -m Der --Der-target-folder non_existing_folder --Der-param-list Omega_Lambda
- –IS-starting-folder : str
- Perform Importance Sampling from this folder or set of chains (OPT)
For Nested Sampling and Cosmo Hammer arguments, see
nested_sampling
andcosmo_hammer
.info
Replaces the old -info command, which is deprecated but still available.- files : string/list of strings
you can specify either single files, or a complete folder, for example
info chains/my-run/2012-10-26*
, orinfo chains/my-run
.If you specify several folders (or set of files), a comparison will be performed.
- –minimal : None
- use this flag to avoid computing the posterior distribution. This will decrease the time needed for the analysis, especially when analyzing big folders.
- –bins : int
- number of bins in the histograms used to derive posterior probabilities and credible intervals (default to 20). Decrease this number for smoother plots at the expense of masking details.
- –no-mean : None
- remove the mean likelihood from the plot. By default, when plotting marginalised 1D posteriors, the code also shows the mean likelihood per bin with dashed lines; this flag switches off the dashed lines.
- –extra : str
extra file to customize the output plots. You can actually set all the possible options in this file, including line-width, ticknumber, ticksize, etc... You can specify four fields, info.redefine (dict with keys set to the previous variable, and the value set to a numerical computation that should replace this variable), info.to_change (dict with keys set to the old variable name, and value set to the new variable name), info.to_plot (list of variables with new names to plot), and info.new_scales (dict with keys set to the new variable names, and values set to the number by which it should be multiplied in the graph). For instance,
info.to_change={'oldname1':'newname1','oldname2':'newname2',...} info.to_plot=['name1','name2','newname3',...] info.new_scales={'name1':number1,'name2':number2,...}
- –noplot : bool
- do not produce any plot, simply compute the posterior (OPT) (flag)
- –noplot-2d : bool
- produce only the 1d posterior plot (OPT) (flag)
- –contours-only : bool
- do not fill the contours on the 2d plots (OPT) (flag)
- –all : None
- output every subplot and data in separate files (OPT) (flag)
- –ext : str
- change the extension for the output file. Any extension handled
by
matplotlib
can be used. (pdf (default), png (faster)) - –fontsize : int
- desired fontsize (default to 16)
- –ticksize : int
- desired ticksize (default to 14)
- –line-width : int
- set line width (default to 4)
- –decimal : int
- number of decimal places on ticks (default to 3)
- –ticknumber : int
- number of ticks on each axis (default to 3)
- –legend-style : str
- specify the style of the legend, to choose from sides or top.
- –keep-non-markovian : bool
- Use this flag to keep the non-markovian part of the chains produced at the beginning of runs with –update mode This option is only relevant when the chains were produced with –update (OPT) (flag)
- –keep-fraction : float
- after burn-in removal, analyze only last fraction of each chain. (between 0 and 1). Normally one would not use this for runs with –update mode, unless –keep-non-markovian is switched on (OPT)
- –want-covmat : bool
- calculate the covariant matrix when analyzing the chains. Warning: this will interfere with ongoing runs utilizing update mode (OPT) (flag)
Returns: args (NameSpace) – parsed input arguments
-
parser_mp.
custom_help
(split_string='<++>')[source]¶ Create a custom help action.
It expects split_string to appear in groups of three. If the option string is ‘-h’, then uses the short description between the first two split_string. If the option string is ‘-h’, then uses all that is between the first and the third split_string, stripping the first one.
Parameters: - split_string (str) – string to use to select the help string and how to select them. They must appear in groups of 3
- output
- ——
- CustomHelp (class definition)
-
parser_mp.
existing_file
(fname)[source]¶ Check if the file exists. If not raise an error
Parameters: fname (string) – file name to parse Returns: fname (string)
-
parser_mp.
get_dict_from_docstring
(key_symbol='<**>', description_symbol='<++>')[source]¶ Create the decorator
Parameters: - key_symbol (str) – identifies the key of a argument/option
- description_symbol (str) – identify the description of a argument/option
- Returns
- ——
- wrapper (function)
-
parser_mp.
initialise_parser
(**kwargs)[source]¶ Create the argument parser and returns it :Parameters: * kwargs (dictionary) –
keyword to pass to the parser- output
- ——
- p (MpArgumentParser instance) – parser with some keyword added
-
parser_mp.
parse
(custom_command='')[source]¶ Check some basic organization of the folder, and exit the program in case something goes wrong.
Keyword Arguments: custom_command (str) – For testing purposes, instead of reading the command line argument, read instead the given string. It should ommit the start of the command, so e.g.: ‘-N 10 -o toto/’
-
parser_mp.
parse_docstring
(docstring, key_symbol='<**>', description_symbol='<++>')[source]¶ Extract from the docstring the keys and description, return it as a dict
Parameters: - docstring (str)
- key_symbol (str) – identifies the key of an argument/option
- description_symbol (str) – identify the description of an argument/option
- output
- ——
- helpdic (dict) – help strings for the parser