def prepare_data_script(): warn_deprecation() logger_setup() if "CONTAINED" not in os.environ: log.error("This command should only be run within a container. " "Run 'cobaya-install' instead.") raise HandledException parser = argparse.ArgumentParser( description= "Cobaya's installation tool for the data needed by a container.") parser.add_argument( "-f", "--force", action="store_true", default=False, help="Force re-installation of apparently installed modules.") arguments = parser.parse_args() try: info = load_input(requirements_file_path) except IOError: log.error( "Cannot find the requirements file. This should not be happening.") raise HandledException install(info, path=_modules_path, force=arguments.force, **{ _code: False, _data: True })
def bib_script(): from cobaya.mpi import is_main_process if not is_main_process(): return warn_deprecation() # Parse arguments and launch import argparse parser = argparse.ArgumentParser( prog="cobaya bib", description= "Prints bibliography to be cited for a component or input file.") parser.add_argument( "components_or_files", action="store", nargs="+", metavar="component_name or input_file.yaml", help="Component(s) or input file(s) whose bib info is requested.") kind_opt, kind_opt_ishort = "kind", 0 parser.add_argument( "-" + kind_opt[kind_opt_ishort], "--" + kind_opt, action="store", nargs=1, default=None, metavar="component_kind", help=("If component name given, " "kind of component whose bib is requested: " + ", ".join(['%s' % kind for kind in kinds]) + ". " + "Use only when component name is not unique (it would fail).")) arguments = parser.parse_args() # Case of files are_yaml = [(os.path.splitext(f)[1] in _yaml_extensions) for f in arguments.components_or_files] if all(are_yaml): infos = [load_input(f) for f in arguments.components_or_files] print(prettyprint_bib(*get_bib_info(*infos))) elif not any(are_yaml): if arguments.kind: arguments.kind = arguments.kind[0].lower() for component in arguments.components_or_files: try: print( create_banner(component, symbol=_default_symbol, length=_default_length)) print(get_bib_component(component, arguments.kind)) return except: if not arguments.kind: print( "Specify its kind with '--%s [component_kind]'." % kind_opt + "(NB: all requested components must have the same kind, " "or be requested separately).") print("") else: print("Give either a list of input yaml files, " "or of component names (not a mix of them).") return 1 return
def create_docker_image(filenames, MPI_version=None): log.info("Creating Docker image...") if not MPI_version: MPI_version = "3.2" # log.warning("You have not specified an MPICH version. " # "It is strongly encouraged to request the one installed in your cluster," # " using '--mpi-version X.Y'. Defaulting to MPICH v%s.", MPI_version) dc = get_docker_client() modules = yaml_dump(get_modules(*[load_input(f) for f in filenames])).strip() echos_reqs = "RUN " + " && \\ \n ".join([ r'echo "%s" >> %s' % (block, requirements_file_path) for block in modules.split("\n") ]) echos_help = "RUN " + " && \\ \n ".join([ r'echo "%s" >> %s' % (line, help_file_path) for line in image_help("docker").split("\n") ]) recipe = r""" FROM cobaya/base_mpich_%s:latest %s RUN cobaya-install %s --%s %s --just-code --force ### NEEDS PYTHON UPDATE! --no-progress-bars %s CMD ["cat", "%s"] """ % (MPI_version, echos_reqs, requirements_file_path, _modules_path_arg, _modules_path, echos_help, help_file_path) image_name = "cobaya:" + uuid.uuid4().hex[:6] with StringIO(recipe) as stream: dc.images.build(fileobj=stream, tag=image_name) log.info( "Docker image '%s' created! " "Do 'docker save %s | gzip > some_name.tar.gz'" "to save it to the current folder.", image_name, image_name)
def bib_script(args=None): """Command line script for the bibliography.""" warn_deprecation() # Parse arguments and launch import argparse parser = argparse.ArgumentParser( prog="cobaya bib", description= ("Prints bibliography to be cited for one or more components or input files." )) parser.add_argument( "files_or_components", action="store", nargs="+", metavar="input_file.yaml|component_name", help="Component(s) or input file(s) whose bib info is requested.") arguments = parser.parse_args(args) # Configure the logger ASAP logger_setup() logger = get_logger("bib") # Gather requests infos: List[Union[Dict, str]] = [] for f in arguments.files_or_components: if os.path.splitext(f)[1].lower() in Extension.yamls: infos += [load_input(f)] else: # a single component name, no kind specified infos += [f] if not infos: logger.info( "Nothing to do. Pass input files or component names as arguments.") return print(pretty_repr_bib(*get_bib_info(*infos, logger=logger)))
def citation_script(): from cobaya.mpi import am_single_or_primary_process if am_single_or_primary_process(): # Configure the logger ASAP from cobaya.log import logger_setup logger_setup() # Parse arguments and launch import argparse parser = argparse.ArgumentParser(description="Cobaya's citation tool.") parser.add_argument("files", action="store", nargs="+", metavar="input_file.yaml", help="One or more input files.") from cobaya.input import load_input infos = [load_input(f) for f in parser.parse_args().files] print(prettyprint_citation(citation(*infos)))
def citation_script(): from cobaya.mpi import get_mpi_rank if not get_mpi_rank(): # Configure the logger ASAP from cobaya.log import logger_setup logger_setup() # Parse arguments and launch import argparse parser = argparse.ArgumentParser(description="Cobaya's citation tool.") parser.add_argument("files", action="store", nargs="+", metavar="input_file.yaml", help="One or more input files.") from cobaya.input import load_input infos = [load_input(f) for f in parser.parse_args().files] citation(*infos)
def create_singularity_image(filenames, MPI_version=None): log.info("Creating Singularity image...") if not MPI_version: MPI_version = "2.1.1" # log.warning("You have not specified an OpenMPI version. " # "It is strongly encouraged to request the one installed in your cluster," # " using '--mpi-version X.Y.Z'. Defaulting to OpenMPI v%s.", MPI_version) components = yaml_dump( get_used_components(*[load_input(f) for f in filenames])).strip() echos_reqs = "\n " + "\n ".join([""] + [ 'echo "%s" >> %s' % (block, requirements_file_path) for block in components.split("\n") ]) recipe = ( dedent(""" Bootstrap: docker From: cobaya/base_openmpi_%s:latest\n %%post\n""" % MPI_version) + dedent(echos_reqs) + dedent(""" export CONTAINED=TRUE cobaya-install %s --%s %s --just-code --force ### --no-progress-bars mkdir $COBAYA_PRODUCTS %%help %s """ % ( requirements_file_path, # TODO: this looks wrong? packages_path_input, os.path.join(packages_path_arg, packages_path_input, data_path), "\n ".join(image_help("singularity").split("\n")[1:])))) with NamedTemporaryFile(delete=False) as recipe_file: recipe_file.write(recipe.encode('utf-8')) recipe_file_name = recipe_file.name image_name = "cobaya_" + uuid.uuid4().hex[:6] + ".simg" process_build = Popen( ["singularity", "build", image_name, recipe_file_name], stdout=PIPE, stderr=PIPE) out, err = process_build.communicate() if process_build.returncode: log.info(out) log.info(err) raise LoggedError(log, "Image creation failed! See error message above.") log.info("Singularity image '%s' created!", image_name)
def install_script(): from cobaya.mpi import am_single_or_primary_process if not am_single_or_primary_process(): # Configure the logger ASAP logger_setup() log = logging.getLogger(__name__.split(".")[-1]) # Parse arguments import argparse parser = argparse.ArgumentParser( description="Cobaya's installation tool for external modules.") parser.add_argument( "files", action="store", nargs="+", metavar="input_file.yaml", help="One or more input files " "(or 'cosmo' for a basic collection of cosmological modules)") parser.add_argument( "-" + _modules_path_arg[0], "--" + _modules_path_arg, action="store", nargs=1, required=True, metavar="/install/path", help="Desired path where to install external modules.") parser.add_argument( "-f", "--force", action="store_true", default=False, help="Force re-installation of apparently installed modules.") parser.add_argument( "--no-progress-bars", action="store_true", default=False, help="No progress bars shown. Shorter logs (used in Travis).") group_just = parser.add_mutually_exclusive_group(required=False) group_just.add_argument("-C", "--just-code", action="store_false", default=True, help="Install code of the modules.", dest=_data) group_just.add_argument("-D", "--just-data", action="store_false", default=True, help="Install data of the modules.", dest=_code) arguments = parser.parse_args() if arguments.files == ["cosmo"]: log.info( "Installing cosmological modules (input files will be ignored") from cobaya.cosmo_input import install_basic infos = [install_basic] else: from cobaya.input import load_input infos = [load_input(f) for f in arguments.files] # Launch installer install(*infos, path=getattr(arguments, _modules_path_arg)[0], **{ arg: getattr(arguments, arg) for arg in ["force", _code, _data, "no_progress_bars"] })
def install_script(): set_mpi_disabled(True) warn_deprecation() # Parse arguments import argparse parser = argparse.ArgumentParser( description="Cobaya's installation tool for external packages.") parser.add_argument( "files_or_components", action="store", nargs="+", metavar="input_file.yaml|component_name", help="One or more input files or component names " "(or simply 'cosmo' to install all the requisites for basic" " cosmological runs)") parser.add_argument( "-" + _packages_path_arg[0], "--" + _packages_path_arg_posix, action="store", nargs=1, required=False, metavar="/packages/path", default=[None], help="Desired path where to install external packages. " "Optional if one has been set globally or as an env variable" " (run with '--show_%s' to check)." % _packages_path_arg_posix) # MARKED FOR DEPRECATION IN v3.0 modules = "modules" parser.add_argument("-" + modules[0], "--" + modules, action="store", nargs=1, required=False, metavar="/packages/path", default=[None], help="To be deprecated! " "Alias for %s, which should be used instead." % _packages_path_arg_posix) # END OF DEPRECATION BLOCK -- CONTINUES BELOW! output_show_packages_path = resolve_packages_path() if output_show_packages_path and os.environ.get(_packages_path_env): output_show_packages_path += " (from env variable %r)" % _packages_path_env elif output_show_packages_path: output_show_packages_path += " (from config file)" else: output_show_packages_path = "(Not currently set.)" parser.add_argument( "--show-" + _packages_path_arg_posix, action="version", version=output_show_packages_path, help="Prints default external packages installation folder " "and exits.") parser.add_argument( "-" + _force[0], "--" + _force, action="store_true", default=False, help="Force re-installation of apparently installed packages.") parser.add_argument( "--skip", action="store", nargs="*", metavar="keyword", help="Keywords of components that will be skipped during " "installation.") parser.add_argument( "--no-progress-bars", action="store_true", default=False, help="No progress bars shown. Shorter logs (used in Travis).") parser.add_argument("--just-check", action="store_true", default=False, help="Just check whether components are installed.") parser.add_argument( "--no-set-global", action="store_true", default=False, help="Do not store the installation path for later runs.") group_just = parser.add_mutually_exclusive_group(required=False) group_just.add_argument("-C", "--just-code", action="store_false", default=True, help="Install code of the components.", dest=_data) group_just.add_argument("-D", "--just-data", action="store_false", default=True, help="Install data of the components.", dest=_code) arguments = parser.parse_args() # Configure the logger ASAP logger_setup() logger = logging.getLogger(__name__.split(".")[-1]) # Gather requests infos = [] for f in arguments.files_or_components: if f.lower() == "cosmo": logger.info("Installing basic cosmological packages.") from cobaya.cosmo_input import install_basic infos += [install_basic] elif f.lower() == "cosmo-tests": logger.info("Installing *tested* cosmological packages.") from cobaya.cosmo_input import install_tests infos += [install_tests] elif os.path.splitext(f)[1].lower() in _yaml_extensions: from cobaya.input import load_input infos += [load_input(f)] else: try: kind = get_kind(f) infos += [{kind: {f: None}}] except Exception: logger.warning("Could not identify component %r. Skipping.", f) if not infos: logger.info("Nothing to install.") return # MARKED FOR DEPRECATION IN v3.0 if getattr(arguments, modules) != [None]: logger.warning( "*DEPRECATION*: -m/--modules will be deprecated in favor of " "-%s/--%s in the next version. Please, use that one instead.", _packages_path_arg[0], _packages_path_arg_posix) # BEHAVIOUR TO BE REPLACED BY ERROR: if getattr(arguments, _packages_path_arg) == [None]: setattr(arguments, _packages_path_arg, getattr(arguments, modules)) # END OF DEPRECATION BLOCK # Launch installer install(*infos, path=getattr(arguments, _packages_path_arg)[0], **{ arg: getattr(arguments, arg) for arg in [ "force", _code, _data, "no_progress_bars", "just_check", "no_set_global", "skip" ] })
def post(info, sample=None): logger_setup(info.get(_debug), info.get(_debug_file)) log = logging.getLogger(__name__.split(".")[-1]) try: info_post = info[_post] except KeyError: log.error("No 'post' block given. Nothing to do!") raise HandledException if get_mpi_rank(): log.warning( "Post-processing is not yet MPI-able. Doing nothing for rank > 1 processes." ) return # 1. Load existing sample output_in = Output(output_prefix=info.get(_output_prefix), resume=True) info_in = load_input(output_in.file_full) if output_in else deepcopy(info) dummy_model_in = DummyModel(info_in[_params], info_in[_likelihood], info_in.get(_prior, None), info_in.get(_theory, None)) if output_in: i = 0 while True: try: collection = Collection(dummy_model_in, output_in, name="%d" % (1 + i), load=True, onload_skip=info_post.get("skip", 0), onload_thin=info_post.get("thin", 1)) if i == 0: collection_in = collection else: collection_in._append(collection) i += 1 except IOError: break elif sample: if isinstance(sample, Collection): sample = [sample] collection_in = deepcopy(sample[0]) for s in sample[1:]: try: collection_in._append(s) except: log.error("Failed to load some of the input samples.") raise HandledException i = len(sample) else: log.error( "Not output from where to load from or input collections given.") raise HandledException log.info("Loaded %d chain%s. Will process %d samples.", i, "s" if i - 1 else "", collection_in.n()) if collection_in.n() <= 1: log.error( "Not enough samples for post-processing. Try using a larger sample, " "or skipping or thinning less.") raise HandledException # 2. Compare old and new info: determine what to do add = info_post.get("add", {}) remove = info_post.get("remove", {}) # Add a dummy 'one' likelihood, to absorb unused parameters if not add.get(_likelihood): add[_likelihood] = odict() add[_likelihood].update({"one": None}) # Expand the "add" info add = get_full_info(add) # 2.1 Adding/removing derived parameters and changes in priors of sampled parameters out = {_params: deepcopy(info_in[_params])} for p in remove.get(_params, {}): pinfo = info_in[_params].get(p) if pinfo is None or not is_derived_param(pinfo): log.error( "You tried to remove parameter '%s', which is not a derived paramter. " "Only derived parameters can be removed during post-processing.", p) raise HandledException out[_params].pop(p) mlprior_names_add = [] for p, pinfo in add.get(_params, {}).items(): pinfo_in = info_in[_params].get(p) if is_sampled_param(pinfo): if not is_sampled_param(pinfo_in): # No added sampled parameters (de-marginalisation not implemented) if pinfo_in is None: log.error( "You added a new sampled parameter %r (maybe accidentaly " "by adding a new likelihood that depends on it). " "Adding new sampled parameters is not possible. Try fixing " "it to some value.", p) raise HandledException else: log.error( "You tried to change the prior of parameter '%s', " "but it was not a sampled parameter. " "To change that prior, you need to define as an external one.", p) raise HandledException if mlprior_names_add[:1] != _prior_1d_name: mlprior_names_add = ( [_minuslogprior + _separator + _prior_1d_name] + mlprior_names_add) elif is_derived_param(pinfo): if p in out[_params]: log.error( "You tried to add derived parameter '%s', which is already " "present. To force its recomputation, 'remove' it too.", p) raise HandledException elif is_fixed_param(pinfo): # Only one possibility left "fixed" parameter that was not present before: # input of new likelihood, or just an argument for dynamical derived (dropped) if ((p in info_in[_params] and pinfo[_p_value] != (pinfo_in or {}).get(_p_value, None))): log.error( "You tried to add a fixed parameter '%s: %r' that was already present" " but had a different value or was not fixed. This is not allowed. " "The old info of the parameter was '%s: %r'", p, dict(pinfo), p, dict(pinfo_in)) raise HandledException else: log.error("This should not happen. Contact the developers.") raise HandledException out[_params][p] = pinfo # For the likelihood only, turn the rest of *derived* parameters into constants, # so that the likelihoods do not try to compute them) # But be careful to exclude *input* params that have a "derived: True" value # (which in "full info" turns into "derived: 'lambda [x]: [x]'") out_params_like = deepcopy(out[_params]) for p, pinfo in out_params_like.items(): if ((is_derived_param(pinfo) and not (_p_value in pinfo) and p not in add.get(_params, {}))): out_params_like[p] = {_p_value: np.nan, _p_drop: True} parameterization_like = Parameterization(out_params_like, ignore_unused_sampled=True) # 2.2 Manage adding/removing priors and likelihoods warn_remove = False for level in [_prior, _likelihood]: out[level] = getattr(dummy_model_in, level) if level == _prior: out[level].remove(_prior_1d_name) for pdf in info_post.get("remove", {}).get(level, []) or []: try: out[level].remove(pdf) warn_remove = True except ValueError: log.error( "Trying to remove %s '%s', but it is not present. " "Existing ones: %r", level, pdf, out[level]) raise HandledException if warn_remove: log.warning("You are removing a prior or likelihood pdf. " "Notice that if the resulting posterior is much wider " "than the original one, or displaced enough, " "it is probably safer to explore it directly.") if _prior in add: mlprior_names_add += [ _minuslogprior + _separator + name for name in add[_prior] ] out[_prior] += list(add[_prior]) prior_recompute_1d = (mlprior_names_add[:1] == [ _minuslogprior + _separator + _prior_1d_name ]) # Don't initialise the theory code if not adding/recomputing theory, # theory-derived params or likelihoods recompute_theory = info_in.get(_theory) and not (list( add[_likelihood]) == ["one"] and not any([ is_derived_param(pinfo) for pinfo in add.get(_params, {}).values() ])) if recompute_theory: # Inherit from the original chain (needs input|output_params, renames, etc theory = list(info_in[_theory].keys())[0] info_theory_out = odict([[ theory, recursive_update(deepcopy(info_in[_theory][theory]), add.get(_theory, {theory: {}})[theory]) ]]) else: info_theory_out = None chi2_names_add = [ _chi2 + _separator + name for name in add[_likelihood] if name is not "one" ] out[_likelihood] += [l for l in add[_likelihood] if l is not "one"] if recompute_theory: log.warn( "You are recomputing the theory, but in the current version this does " "not force recomputation of any likelihood or derived parameter, " "unless explicitly removed+added.") for level in [_prior, _likelihood]: for i, x_i in enumerate(out[level]): if x_i in list(out[level])[i + 1:]: log.error( "You have added %s '%s', which was already present. If you " "want to force its recomputation, you must also 'remove' it.", level, x_i) raise HandledException # 3. Create output collection if "suffix" not in info_post: log.error("You need to provide a 'suffix' for your chains.") raise HandledException # Use default prefix if it exists. If it does not, produce no output by default. # {post: {output: None}} suppresses output, and if it's a string, updates it. out_prefix = info_post.get(_output_prefix, info.get(_output_prefix)) if out_prefix not in [None, False]: out_prefix += "_" + _post + "_" + info_post["suffix"] output_out = Output(output_prefix=out_prefix, force_output=info.get(_force)) info_out = deepcopy(info) info_out[_post] = info_post # Updated with input info and extended (full) add info info_out.update(info_in) info_out[_post]["add"] = add dummy_model_out = DummyModel(out[_params], out[_likelihood], info_prior=out[_prior]) if recompute_theory: theory = list(info_theory_out.keys())[0] if _input_params not in info_theory_out[theory]: log.error( "You appear to be post-processing a chain generated with an older " "version of Cobaya. For post-processing to work, please edit the " "'[root]__full.info' file of the original chain to add, inside the " "theory code block, the list of its input parameters. E.g.\n----\n" "theory:\n %s:\n input_params: [param1, param2, ...]\n" "----\nIf you get strange errors later, it is likely that you did not " "specify the correct set of theory parameters.\n" "The full set of input parameters are %s.", theory, list(dummy_model_out.parameterization.input_params())) raise HandledException prior_add = Prior(dummy_model_out.parameterization, add.get(_prior)) likelihood_add = Likelihood(add[_likelihood], parameterization_like, info_theory=info_theory_out, modules=info.get(_path_install)) # Remove auxiliary "one" before dumping -- 'add' *is* info_out[_post]["add"] add[_likelihood].pop("one") if likelihood_add.theory: # Make sure that theory.needs is called at least once, for adjustments likelihood_add.theory.needs() collection_out = Collection(dummy_model_out, output_out, name="1") output_out.dump_info({}, info_out) # 4. Main loop! log.info("Running post-processing...") last_percent = 0 for i, point in enumerate(collection_in.data.itertuples()): log.debug("Point: %r", point) sampled = [ getattr(point, param) for param in dummy_model_in.parameterization.sampled_params() ] derived = odict( [[param, getattr(point, param, None)] for param in dummy_model_out.parameterization.derived_params()]) inputs = odict([[ param, getattr( point, param, dummy_model_in.parameterization.constant_params().get( param, dummy_model_out.parameterization.constant_params().get( param, None))) ] for param in dummy_model_out.parameterization.input_params()]) # Solve inputs that depend on a function and were not saved # (we don't use the Parameterization_to_input method in case there are references # to functions that cannot be loaded at the moment) for p, value in inputs.items(): if value is None: func = dummy_model_out.parameterization._input_funcs[p] args = dummy_model_out.parameterization._input_args[p] inputs[p] = func(*[getattr(point, arg) for arg in args]) # Add/remove priors priors_add = prior_add.logps(sampled) if not prior_recompute_1d: priors_add = priors_add[1:] logpriors_add = odict(zip(mlprior_names_add, priors_add)) logpriors_new = [ logpriors_add.get(name, -getattr(point, name, 0)) for name in collection_out.minuslogprior_names ] if log.getEffectiveLevel() <= logging.DEBUG: log.debug("New set of priors: %r", dict(zip(dummy_model_out.prior, logpriors_new))) if -np.inf in logpriors_new: continue # Add/remove likelihoods output_like = [] if likelihood_add: # Notice "one" (last in likelihood_add) is ignored: not in chi2_names loglikes_add = odict( zip(chi2_names_add, likelihood_add.logps(inputs, _derived=output_like))) output_like = dict(zip(likelihood_add.output_params, output_like)) else: loglikes_add = dict() loglikes_new = [ loglikes_add.get(name, -0.5 * getattr(point, name, 0)) for name in collection_out.chi2_names ] if log.getEffectiveLevel() <= logging.DEBUG: log.debug("New set of likelihoods: %r", dict(zip(dummy_model_out.likelihood, loglikes_new))) if output_like: log.debug("New set of likelihood-derived parameters: %r", output_like) if -np.inf in loglikes_new: continue # Add/remove derived parameters and change priors of sampled parameters for p in add[_params]: if p in dummy_model_out.parameterization._directly_output: derived[p] = output_like[p] elif p in dummy_model_out.parameterization._derived_funcs: func = dummy_model_out.parameterization._derived_funcs[p] args = dummy_model_out.parameterization._derived_args[p] derived[p] = func(*[ getattr(point, arg, output_like.get(arg, None)) for arg in args ]) if log.getEffectiveLevel() <= logging.DEBUG: log.debug( "New derived parameters: %r", dict([[ p, derived[p] ] for p in dummy_model_out.parameterization.derived_params() if p in add[_params]])) # Save to the collection (keep old weight for now) collection_out.add(sampled, derived=derived.values(), weight=getattr(point, _weight), logpriors=logpriors_new, loglikes=loglikes_new) # Display progress percent = np.round(i / collection_in.n() * 100) if percent != last_percent and not percent % 5: last_percent = percent progress_bar(log, percent, " (%d/%d)" % (i, collection_in.n())) if not collection_out.data.last_valid_index(): log.error( "No elements in the final sample. Possible causes: " "added a prior or likelihood valued zero over the full sampled domain, " "or the computation of the theory failed everywhere, etc.") raise HandledException # Reweight -- account for large dynamic range! # Prefer to rescale +inf to finite, and ignore final points with -inf. # Remove -inf's (0-weight), and correct indices difflogmax = max(collection_in[_minuslogpost] - collection_out[_minuslogpost]) collection_out.data[_weight] *= np.exp(collection_in[_minuslogpost] - collection_out[_minuslogpost] - difflogmax) collection_out.data = ( collection_out.data[collection_out.data.weight > 0].reset_index( drop=True)) collection_out._n = collection_out.data.last_valid_index() + 1 # Write! collection_out._out_update() log.info("Finished! Final number of samples: %d", collection_out.n()) return info_out, {"sample": collection_out}
def install_script(args=None): set_mpi_disabled() warn_deprecation() # Parse arguments import argparse parser = argparse.ArgumentParser( prog="cobaya install", description="Cobaya's installation tool for external packages.") parser.add_argument( "files_or_components", action="store", nargs="+", metavar="input_file.yaml|component_name", help="One or more input files or component names " "(or simply 'cosmo' to install all the requisites for basic" " cosmological runs)") parser.add_argument( "-" + packages_path_arg[0], "--" + packages_path_arg_posix, action="store", required=False, metavar="/packages/path", default=None, help="Desired path where to install external packages. " "Optional if one has been set globally or as an env variable" " (run with '--show_%s' to check)." % packages_path_arg_posix) # MARKED FOR DEPRECATION IN v3.0 modules = "modules" parser.add_argument("-" + modules[0], "--" + modules, action="store", required=False, metavar="/packages/path", default=None, help="Deprecated! Use %s instead." % packages_path_arg_posix) # END OF DEPRECATION BLOCK -- CONTINUES BELOW! output_show_packages_path = resolve_packages_path() if output_show_packages_path and os.environ.get(packages_path_env): output_show_packages_path += " (from env variable %r)" % packages_path_env elif output_show_packages_path: output_show_packages_path += " (from config file)" else: output_show_packages_path = "(Not currently set.)" parser.add_argument( "--show-" + packages_path_arg_posix, action="version", version=output_show_packages_path, help="Prints default external packages installation folder " "and exits.") parser.add_argument( "-" + "f", "--" + "force", action="store_true", default=False, help="Force re-installation of apparently installed packages.") parser.add_argument( "--skip", action="store", nargs="*", metavar="keyword", help="Keywords of components that will be skipped during " "installation.") parser.add_argument( "--no-progress-bars", action="store_true", default=False, help="No progress bars shown. Shorter logs (used in Travis).") parser.add_argument("--%s" % "test", action="store_true", default=False, help="Just check whether components are installed.") # MARKED FOR DEPRECATION IN v3.0 parser.add_argument("--just-check", action="store_true", default=False, help="Just check whether components are installed.") # END OF DEPRECATION BLOCK -- CONTINUES BELOW! parser.add_argument( "--no-set-global", action="store_true", default=False, help="Do not store the installation path for later runs.") parser.add_argument( "--skip-global", action="store_true", default=False, help="Skip installation of already-available Python modules.") parser.add_argument("-" + "d", "--" + "debug", action="store_true", help="Produce verbose debug output.") group_just = parser.add_mutually_exclusive_group(required=False) group_just.add_argument("-C", "--just-code", action="store_false", default=True, help="Install code of the components.", dest=data_path) group_just.add_argument("-D", "--just-data", action="store_false", default=True, help="Install data of the components.", dest=code_path) arguments = parser.parse_args(args) # Configure the logger ASAP logger_setup() logger = get_logger("install") # Gather requests infos: List[InputDict] = [] for f in arguments.files_or_components: if f.lower() == "cosmo": logger.info("Installing basic cosmological packages.") from cobaya.cosmo_input import install_basic infos += [install_basic] elif f.lower() == "cosmo-tests": logger.info("Installing *tested* cosmological packages.") from cobaya.cosmo_input import install_tests infos += [install_tests] elif os.path.splitext(f)[1].lower() in Extension.yamls: from cobaya.input import load_input infos += [load_input(f)] else: try: kind = get_kind(f) infos += [{kind: {f: None}}] except Exception: logger.warning("Could not identify component %r. Skipping.", f) if not infos: logger.info("Nothing to install.") return # List of deprecation warnings, to be printed *after* installation deprecation_warnings = [] # MARKED FOR DEPRECATION IN v3.0 if getattr(arguments, modules) is not None: raise LoggedError( logger, "-m/--modules has been deprecated in favor of " "-%s/--%s", packages_path_arg[0], packages_path_arg_posix) # END OF DEPRECATION BLOCK # MARKED FOR DEPRECATION IN v3.0 if arguments.just_check is True: raise LoggedError(logger, "--just-check has been deprecated in favor of --%s", "test") # END OF DEPRECATION BLOCK # Launch installer install(*infos, path=getattr(arguments, packages_path_arg), **{ arg: getattr(arguments, arg) for arg in [ "force", code_path, data_path, "no_progress_bars", "test", "no_set_global", "skip", "skip_global", "debug" ] }) # MARKED FOR DEPRECATION IN v3.0 for warning_msg in deprecation_warnings: logger.warning(warning_msg)
def install_script(): from cobaya.mpi import get_mpi_rank if not get_mpi_rank(): # Configure the logger ASAP logger_setup() log = logging.getLogger(__name__.split(".")[-1]) # Parse arguments import argparse parser = argparse.ArgumentParser( description="Cobaya's installation tool for external modules.") parser.add_argument("files", action="store", nargs="+", metavar="input_file.yaml", help="One or more input files.") parser.add_argument( "-p", "--path", action="store", nargs=1, required=True, metavar="/install/path", help="Desired path where to install external modules.") parser.add_argument( "-f", "--force", action="store_true", default=False, help="Force re-installation of apparently installed modules.") parser.add_argument( "--no-progress-bars", action="store_true", default=False, help="No progress bars shown. Shorter logs (used in Travis).") group_just = parser.add_mutually_exclusive_group(required=False) group_just.add_argument("-c", "--just-code", action="store_false", default=True, help="Install code of the modules.", dest=_data) group_just.add_argument("-d", "--just-data", action="store_false", default=True, help="Install data of the modules.", dest=_code) arguments = parser.parse_args() from cobaya.input import load_input try: infos = [load_input(f) for f in arguments.files] except HandledException: log.error("Maybe you meant to pass an installation path? " "In that case, use '--path=/path/to/modules'.") raise HandledException # Launch installer install(*infos, path=arguments.path[0], **{ arg: getattr(arguments, arg) for arg in ["force", _code, _data, "no_progress_bars"] })
def install_script(args=None): """Command line script for the installer.""" set_mpi_disabled() warn_deprecation() # Parse arguments import argparse parser = argparse.ArgumentParser( prog="cobaya install", description="Cobaya's installation tool for external packages.") parser.add_argument( "files_or_components", action="store", nargs="+", metavar="input_file.yaml|component_name", help="One or more input files or component names " "(or simply 'cosmo' to install all the requisites for basic" " cosmological runs)") parser.add_argument( "-" + packages_path_arg[0], "--" + packages_path_arg_posix, action="store", required=False, metavar="/packages/path", default=None, help="Desired path where to install external packages. " "Optional if one has been set globally or as an env variable" " (run with '--show_%s' to check)." % packages_path_arg_posix) output_show_packages_path = resolve_packages_path() if output_show_packages_path and os.environ.get(packages_path_env): output_show_packages_path += " (from env variable %r)" % packages_path_env elif output_show_packages_path: output_show_packages_path += " (from config file)" else: output_show_packages_path = "(Not currently set.)" parser.add_argument( "--show-" + packages_path_arg_posix, action="version", version=output_show_packages_path, help="Prints default external packages installation folder " "and exits.") parser.add_argument( "-" + "f", "--" + "force", action="store_true", default=False, help="Force re-installation of apparently installed packages.") parser.add_argument("--%s" % "test", action="store_true", default=False, help="Just check whether components are installed.") parser.add_argument("--upgrade", action="store_true", default=False, help="Force upgrade of obsolete components.") parser.add_argument("--skip", action="store", nargs="*", metavar="keyword", help=("Keywords of components that will be " "skipped during installation.")) parser.add_argument( "--skip-global", action="store_true", default=False, help="Skip installation of already-available Python modules.") parser.add_argument("-" + "d", "--" + "debug", action="store_true", help="Produce verbose debug output.") group_just = parser.add_mutually_exclusive_group(required=False) group_just.add_argument("-C", "--just-code", action="store_false", default=True, help="Install code of the components.", dest=data_path) group_just.add_argument("-D", "--just-data", action="store_false", default=True, help="Install data of the components.", dest=code_path) parser.add_argument( "--no-progress-bars", action="store_true", default=False, help=("No progress bars shown; use when output is saved into a " "text file (e.g. when running on a cluster).")) parser.add_argument( "--no-set-global", action="store_true", default=False, help="Do not store the installation path for later runs.") arguments = parser.parse_args(args) # Configure the logger ASAP logger_setup(arguments.debug) logger = get_logger("install") # Gather requests infos: List[Union[InputDict, str]] = [] for f in arguments.files_or_components: if f.lower() == "cosmo": logger.info("Installing basic cosmological packages.") from cobaya.cosmo_input import install_basic infos += [install_basic] elif f.lower() == "cosmo-tests": logger.info("Installing *tested* cosmological packages.") from cobaya.cosmo_input import install_tests infos += [install_tests] elif os.path.splitext(f)[1].lower() in Extension.yamls: from cobaya.input import load_input infos += [load_input(f)] else: # a single component name, no kind specified infos += [f] # Launch installer install(*infos, path=getattr(arguments, packages_path_arg), logger=logger, **{ arg: getattr(arguments, arg) for arg in [ "force", code_path, data_path, "no_progress_bars", "test", "no_set_global", "skip", "skip_global", "debug", "upgrade" ] })