Exemple #1
0
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)))
Exemple #2
0
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
Exemple #3
0
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
            })
Exemple #4
0
def citation_script():
    warn_deprecation()
    print(
        create_banner(
            "\nThis command will be deprecated soon. Use `cobaya-bib` instead.\n"
        ))
    bib_script()
Exemple #5
0
def gui_script():
    warn_deprecation()
    try:
        app = QApplication(sys.argv)
    except NameError:
        # TODO: fix this long logger setup
        from cobaya.log import logger_setup, LoggedError
        logger_setup(0, None)
        raise LoggedError(
            "cosmo_generator", "PySide2 is not installed! "
            "Check Cobaya's documentation for the cosmo_generator "
            "('Basic cosmology runs').")
    clip = app.clipboard()
    window = MainWindow()
    window.clipboard = clip
    sys.exit(app.exec_())
Exemple #6
0
def citation_script():
    warn_deprecation()
    from cobaya.mpi import am_single_or_primary_process
    if am_single_or_primary_process():
        warn_deprecation()
        # 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)))
Exemple #7
0
def run_script(args=None):
    """Shell script wrapper for :func:`run.run` (including :func:`post.post`)"""
    warn_deprecation()
    import argparse
    # kwargs for flags that should be True|None, instead of True|False
    # (needed in order not to mistakenly override input file inside the run() function
    trueNone_kwargs = {"action": "store_true", "default": None}
    parser = argparse.ArgumentParser(
        prog="cobaya run", description="Cobaya's run script.")
    parser.add_argument("input_file", action="store", metavar="input_file.yaml",
                        help="An input file to run.")
    parser.add_argument("-" + packages_path_arg[0], "--" + packages_path_arg_posix,
                        action="store", metavar="/packages/path", default=None,
                        help="Path where external packages were installed.")
    parser.add_argument("-" + "o", "--" + "output",
                        action="store", metavar="/some/path", default=None,
                        help="Path and prefix for the text output.")
    parser.add_argument("-" + "d", "--" + "debug",
                        help="Produce verbose debug output.", **trueNone_kwargs)
    continuation = parser.add_mutually_exclusive_group(required=False)
    continuation.add_argument("-" + "r", "--" + "resume",
                              help=("Resume an existing chain if it has similar info "
                                    "(fails otherwise)."), **trueNone_kwargs)
    continuation.add_argument("-" + "f", "--" + "force",
                              help=("Overwrites previous output, if it exists "
                                    "(use with care!)"), **trueNone_kwargs)
    parser.add_argument("--%s" % "test",
                        help="Initialize model and sampler, and exit.", **trueNone_kwargs)
    parser.add_argument("-M", "--minimize",
                        help=("Replaces the sampler in the input and runs a minimization "
                              "process (incompatible with post-processing)."),
                        **trueNone_kwargs)
    parser.add_argument("--version", action="version", version=get_version())
    parser.add_argument("--no-mpi",
                        help=("disable MPI when mpi4py installed but MPI does "
                              "not actually work"),
                        **trueNone_kwargs)
    arguments = parser.parse_args(args)
    info = arguments.input_file
    del arguments.input_file
    run(info, **arguments.__dict__)
Exemple #8
0
def create_image_script():
    warn_deprecation()
    logger_setup()
    parser = argparse.ArgumentParser(
        prog="cobaya create-image",
        description=(
            "Cobaya's tool for preparing Docker (for Shifter) and Singularity images."))
    parser.add_argument("files", action="store", nargs="+", metavar="input_file.yaml",
                        help="One or more input files.")
    parser.add_argument("-v", "--mpi-version", action="store", default=None,
                        metavar="X.Y(.Z)", dest="version", help="Version of the MPI lib.")
    group_type = parser.add_mutually_exclusive_group(required=True)
    group_type.add_argument("-d", "--docker", action="store_const", const="docker",
                            help="Create a Docker image (for Shifter).", dest="type")
    group_type.add_argument("-s", "--singularity", action="store_const", dest="type",
                            const="singularity", help="Create a Singularity image.")
    arguments = parser.parse_args()
    if arguments.type == "docker":
        create_docker_image(arguments.files, MPI_version=arguments.version)
    elif arguments.type == "singularity":
        create_singularity_image(arguments.files, MPI_version=arguments.version)
Exemple #9
0
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"
                ]
            })
Exemple #10
0
def run():
    warn_deprecation()
    Opts = batchjob_args.batchArgs(
        'Submit jobs to run chains or importance sample',
        notExist=True,
        notall=True,
        converge=True)
    jobqueue.addArguments(Opts.parser, combinedJobs=True)
    Opts.parser.add_argument('--subitems',
                             action='store_true',
                             help='include sub-grid items')
    Opts.parser.add_argument('--not_queued', action='store_true')
    Opts.parser.add_argument('--minimize',
                             action='store_true',
                             help='Run minimization jobs')
    Opts.parser.add_argument('--importance_minimize',
                             action='store_true',
                             help=('Run minimization jobs for chains '
                                   'that are importance sampled'))
    Opts.parser.add_argument('--minimize_failed',
                             action='store_true',
                             help='run where minimization previously failed')
    Opts.parser.add_argument(
        '--checkpoint_run',
        nargs='?',
        default=None,
        const=0,
        type=float,
        help=('run if stopped and not finished; if optional value '
              'given then only run chains with convergence worse than '
              'the given value'))
    Opts.parser.add_argument(
        '--importance_ready',
        action='store_true',
        help='where parent chain has converged and stopped')
    Opts.parser.add_argument(
        '--importance_changed',
        action='store_true',
        help=('run importance jobs where the parent chain has '
              'changed since last run'))
    Opts.parser.add_argument(
        '--parent_converge',
        type=float,
        default=0,
        help='minimum R-1 convergence for importance job parent')
    Opts.parser.add_argument(
        '--parent_stopped',
        action='store_true',
        help='only run if parent chain is not still running')
    (batch, args) = Opts.parseForBatch()
    if args.not_queued:
        print('Getting queued names...')
        queued = jobqueue.queue_job_names(args.batchPath)

    def notQueued(name):
        for job in queued:
            if name in job:
                # print 'Already running:', name
                return False
        return True

    variant = ''
    if args.importance_minimize:
        variant = '_minimize'
        if args.importance is None:
            args.importance = []
    if args.minimize:
        args.noimportance = True
        variant = '_minimize'
    if args.importance is None:
        if args.importance_changed or args.importance_ready:
            args.importance = []
        else:
            args.noimportance = True
    isMinimize = args.importance_minimize or args.minimize
    if args.combine_one_job_name:
        print('Combining multiple (hopefully fast) into single job script: ' +
              args.combine_one_job_name)
    global iniFiles
    iniFiles = []
    jobqueue.checkArguments(**args.__dict__)

    def jobName():
        s = "-".join(
            [os.path.splitext(os.path.basename(ini))[0] for ini in iniFiles])
        if len(iniFiles) < 2 or len(s) < 70:
            return s
        base = os.path.basename(iniFiles[0])
        if len(base) > 70:
            base = base[:70]
        return base + '__' + hashlib.md5(s).hexdigest()[:16]

    def submitJob(ini):
        global iniFiles
        if not args.dryrun:
            print('Submitting...' + ini)
        else:
            print('... ' + ini)
        iniFiles.append(ini)
        if args.combine_one_job_name:
            return
        if len(iniFiles) >= args.runs_per_job:
            if args.runs_per_job > 1:
                print('--> jobName: ', jobName())
            jobqueue.submitJob(jobName(), iniFiles, **args.__dict__)
            iniFiles = []

    for jobItem in Opts.filteredBatchItems(wantSubItems=args.subitems):
        if ((not args.notexist or isMinimize and not jobItem.chainMinimumExists()
             or not isMinimize and not jobItem.chainExists()) and (
                    not args.minimize_failed or not jobItem.chainMinimumConverged())
            and (isMinimize or args.notall is None or not jobItem.allChainExists(args.notall))) \
                and (not isMinimize or getattr(jobItem, 'want_minimize', True)):
            if not args.parent_converge or not jobItem.isImportanceJob or jobItem.parent.hasConvergeBetterThan(
                    args.parent_converge):
                if args.converge == 0 or not jobItem.hasConvergeBetterThan(
                        args.converge, returnNotExist=True):
                    if args.checkpoint_run is None or jobItem.wantCheckpointContinue(
                            args.checkpoint_run) and jobItem.notRunning():
                        if (not jobItem.isImportanceJob or isMinimize
                                or (args.importance_ready
                                    and jobItem.parent.chainFinished()
                                    or not args.importance_ready
                                    and jobItem.parent.chainExists()) and
                            (not args.importance_changed
                             or jobItem.parentChanged()) and
                            (not args.parent_stopped
                             or jobItem.parent.notRunning())):
                            if not args.not_queued or notQueued(jobItem.name):
                                submitJob(jobItem.iniFile(variant))

    if len(iniFiles) > 0:
        if args.runs_per_job > 1: print('--> jobName: ', jobName())
        jobqueue.submitJob(args.combine_one_job_name or jobName(),
                           iniFiles,
                           sequential=args.combine_one_job_name is not None,
                           **args.__dict__)
Exemple #11
0
def doc_script():
    from cobaya.mpi import am_single_or_primary_process
    if not am_single_or_primary_process():
        return
    warn_deprecation()
    # Parse arguments
    import argparse
    parser = argparse.ArgumentParser(
        description="Prints defaults for Cobaya's internal modules.")
    parser.add_argument("module",
                        action="store",
                        nargs="?",
                        default="",
                        metavar="module_name",
                        help="The module whose defaults are 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="module_kind",
        help=("Kind of module whose defaults are requested: " +
              ", ".join(['%s' % kind for kind in _kinds]) + ". " +
              "Use only when module name is not unique (it would fail)."))
    parser.add_argument("-p",
                        "--python",
                        action="store_true",
                        default=False,
                        help="Request Python instead of YAML.")
    expand_flag, expand_flag_ishort = "expand", 1
    parser.add_argument("-" + expand_flag[expand_flag_ishort],
                        "--" + expand_flag,
                        action="store_true",
                        default=False,
                        help="Expand YAML defaults.")
    arguments = parser.parse_args()
    # Remove plurals (= name of src subfolders), for user-friendliness
    if arguments.module.lower() in subfolders.values():
        arguments.module = next(k for k in subfolders
                                if arguments.module == subfolders[k])
    # Kind given, list all
    if not arguments.module:
        msg = "Available modules: (some may need external code/data)"
        print(msg + "\n" + "-" * len(msg))
        for kind in _kinds:
            print("%s:" % kind)
            print(_indent + ("\n" + _indent).join(get_available_modules(kind)))
        return
    # Kind given: list all modules of that kind
    if arguments.module.lower() in _kinds:
        print("%s:" % arguments.module.lower())
        print(_indent +
              ("\n" +
               _indent).join(get_available_modules(arguments.module.lower())))
        return
    # Otherwise, check if it's a unique module name
    try:
        if arguments.kind:
            arguments.kind = arguments.kind[0].lower()
        to_print = get_default_info(arguments.module,
                                    arguments.kind,
                                    return_yaml=not arguments.python,
                                    yaml_expand_defaults=arguments.expand,
                                    fail_if_not_found=True)
        if arguments.python:
            print(import_odict + pformat(to_print))
        else:
            print(to_print)
            if "!defaults" in to_print:
                print("# This file contains defaults. "
                      "To populate them, use the flag --%s (or -%s)." %
                      (expand_flag, expand_flag[expand_flag_ishort]))
    except:
        if not arguments.kind:
            print("Specify its kind with '--%s [module_kind]'." % kind_opt)
        return 1
    return
Exemple #12
0
def run_script(args=None):
    warn_deprecation()
    import argparse
    parser = argparse.ArgumentParser(prog="cobaya run",
                                     description="Cobaya's run script.")
    parser.add_argument("input_file",
                        action="store",
                        metavar="input_file.yaml",
                        help="An input file to run.")
    parser.add_argument("-" + packages_path_arg[0],
                        "--" + packages_path_arg_posix,
                        action="store",
                        metavar="/packages/path",
                        default=None,
                        help="Path where external packages were installed.")
    # MARKED FOR DEPRECATION IN v3.0
    modules = "modules"
    parser.add_argument("-" + modules[0],
                        "--" + modules,
                        action="store",
                        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!
    parser.add_argument("-" + "o",
                        "--" + "output",
                        action="store",
                        metavar="/some/path",
                        default=None,
                        help="Path and prefix for the text output.")
    parser.add_argument("-" + "d",
                        "--" + "debug",
                        action="store_true",
                        help="Produce verbose debug output.")
    continuation = parser.add_mutually_exclusive_group(required=False)
    continuation.add_argument(
        "-" + "r",
        "--" + "resume",
        action="store_true",
        help="Resume an existing chain if it has similar info "
        "(fails otherwise).")
    continuation.add_argument("-" + "f",
                              "--" + "force",
                              action="store_true",
                              help="Overwrites previous output, if it exists "
                              "(use with care!)")
    parser.add_argument("--%s" % "test",
                        action="store_true",
                        help="Initialize model and sampler, and exit.")
    parser.add_argument("--version", action="version", version=get_version())
    parser.add_argument("--no-mpi",
                        action='store_true',
                        help="disable MPI when mpi4py installed but MPI does "
                        "not actually work")
    arguments = parser.parse_args(args)

    # MARKED FOR DEPRECATION IN v3.0
    if arguments.modules is not None:
        logger_setup()
        logger = get_logger("run")
        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) is None:
            setattr(arguments, packages_path_arg, arguments.modules)
    del arguments.modules
    # END OF DEPRECATION BLOCK
    info = arguments.input_file
    del arguments.input_file
    run(info, **arguments.__dict__)
Exemple #13
0
def run_script():
    warn_deprecation()
    import os
    import argparse
    parser = argparse.ArgumentParser(description="Cobaya's run script.")
    parser.add_argument("input_file",
                        nargs=1,
                        action="store",
                        metavar="input_file.yaml",
                        help="An input file to run.")
    parser.add_argument("-" + _packages_path_arg[0],
                        "--" + _packages_path_arg_posix,
                        action="store",
                        nargs=1,
                        metavar="/packages/path",
                        default=[None],
                        help="Path where external packages were installed.")
    # 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!
    parser.add_argument("-" + _output_prefix[0],
                        "--" + _output_prefix,
                        action="store",
                        nargs=1,
                        metavar="/some/path",
                        default=[None],
                        help="Path and prefix for the text output.")
    parser.add_argument("-" + _debug[0],
                        "--" + _debug,
                        action="store_true",
                        help="Produce verbose debug output.")
    continuation = parser.add_mutually_exclusive_group(required=False)
    continuation.add_argument(
        "-" + _resume[0],
        "--" + _resume,
        action="store_true",
        help="Resume an existing chain if it has similar info "
        "(fails otherwise).")
    continuation.add_argument("-" + _force[0],
                              "--" + _force,
                              action="store_true",
                              help="Overwrites previous output, if it exists "
                              "(use with care!)")
    parser.add_argument("--%s" % _test_run,
                        action="store_true",
                        help="Initialize model and sampler, and exit.")
    parser.add_argument("--version", action="version", version=__version__)
    parser.add_argument("--no-mpi",
                        action='store_true',
                        help="disable MPI when mpi4py installed but MPI does "
                        "not actually work")
    arguments = parser.parse_args()
    if arguments.no_mpi or getattr(arguments, _test_run, False):
        set_mpi_disabled()
    if any((os.path.splitext(f)[0] in ("input", "updated"))
           for f in arguments.input_file):
        raise ValueError("'input' and 'updated' are reserved file names. "
                         "Please, use a different one.")
    load_input = import_MPI(".input", "load_input")
    given_input = arguments.input_file[0]
    if any(given_input.lower().endswith(ext) for ext in _yaml_extensions):
        info = load_input(given_input)
        output_prefix_cmd = getattr(arguments, _output_prefix)[0]
        output_prefix_input = info.get(_output_prefix)
        info[_output_prefix] = output_prefix_cmd or output_prefix_input
    else:
        # Passed an existing output_prefix? Try to find the corresponding *.updated.yaml
        updated_file = get_info_path(*split_prefix(given_input),
                                     kind="updated")
        try:
            info = load_input(updated_file)
        except IOError:
            raise ValueError(
                "Not a valid input file, or non-existent run to resume")
        # We need to update the output_prefix to resume the run *where it is*
        info[_output_prefix] = given_input
        # If input given this way, we obviously want to resume!
        info[_resume] = True
    # solve packages installation path cmd > env > input
    # MARKED FOR DEPRECATION IN v3.0
    if getattr(arguments, modules) != [None]:
        logger_setup()
        logger = logging.getLogger(__name__.split(".")[-1])
        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))
    # BEHAVIOUR TO BE REPLACED BY ERROR:
    check_deprecated_modules_path(info)
    # END OF DEPRECATION BLOCK
    info[_packages_path] = \
        getattr(arguments, _packages_path_arg)[0] or info.get(_packages_path)
    info[_debug] = getattr(arguments, _debug) or info.get(
        _debug, _debug_default)
    info[_test_run] = getattr(arguments, _test_run, False)
    # If any of resume|force given as cmd args, ignore those in the input file
    resume_arg, force_arg = [
        getattr(arguments, arg) for arg in [_resume, _force]
    ]
    if any([resume_arg, force_arg]):
        info[_resume], info[_force] = resume_arg, force_arg
    if _post in info:
        post(info)
    else:
        run(info)
Exemple #14
0
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"
                ]
            })
Exemple #15
0
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)
Exemple #16
0
def run_script():
    warn_deprecation()
    import os
    import argparse
    parser = argparse.ArgumentParser(description="Cobaya's run script.")
    parser.add_argument("input_file",
                        nargs=1,
                        action="store",
                        metavar="input_file.yaml",
                        help="An input file to run.")
    parser.add_argument(
        "-" + _modules_path_arg[0],
        "--" + _modules_path_arg,
        action="store",
        nargs=1,
        metavar="/some/path",
        default=[None],
        help="Path where modules were automatically installed.")
    parser.add_argument("-" + _output_prefix[0],
                        "--" + _output_prefix,
                        action="store",
                        nargs=1,
                        metavar="/some/path",
                        default=[None],
                        help="Path and prefix for the text output.")
    parser.add_argument("-" + _debug[0],
                        "--" + _debug,
                        action="store_true",
                        help="Produce verbose debug output.")
    continuation = parser.add_mutually_exclusive_group(required=False)
    continuation.add_argument(
        "-" + _resume[0],
        "--" + _resume,
        action="store_true",
        help="Resume an existing chain if it has similar info "
        "(fails otherwise).")
    continuation.add_argument("-" + _force[0],
                              "--" + _force,
                              action="store_true",
                              help="Overwrites previous output, if it exists "
                              "(use with care!)")
    parser.add_argument("--version", action="version", version=__version__)
    args = parser.parse_args()
    if any([(os.path.splitext(f)[0] in ("input", "updated"))
            for f in args.input_file]):
        raise ValueError("'input' and 'updated' are reserved file names. "
                         "Please, use a different one.")
    load_input = import_MPI(".input", "load_input")
    given_input = args.input_file[0]
    if any(given_input.lower().endswith(ext) for ext in _yaml_extensions):
        info = load_input(given_input)
        output_prefix_cmd = getattr(args, _output_prefix)[0]
        output_prefix_input = info.get(_output_prefix)
        info[_output_prefix] = output_prefix_cmd or output_prefix_input
    else:
        # Passed an existing output_prefix? Try to find the corresponding *.updated.yaml
        updated_file = (
            given_input +
            (_separator_files if not given_input.endswith(os.sep) else "") +
            _updated_suffix + _yaml_extensions[0])
        try:
            info = load_input(updated_file)
        except IOError:
            raise ValueError(
                "Not a valid input file, or non-existent sample to resume")
        # We need to update the output_prefix to resume the sample *where it is*
        info[_output_prefix] = given_input
        # If input given this way, we obviously want to resume!
        info[_resume] = True
    # solve modules installation path cmd > env > input
    path_cmd = getattr(args, _modules_path_arg)[0]
    path_env = os.environ.get(_modules_path_env, None)
    path_input = info.get(_path_install)
    info[_path_install] = path_cmd or (path_env or path_input)
    info[_debug] = getattr(args, _debug) or info.get(_debug, _debug_default)
    info[_resume] = getattr(args, _resume, _resume_default)
    info[_force] = getattr(args, _force, False)
    if _post in info:
        post(info)
    else:
        run(info)
Exemple #17
0
def make_grid_script():
    warn_deprecation()
    args = getArgs()
    args.interactive = True
    makeGrid(**args.__dict__)
Exemple #18
0
def doc_script():
    from cobaya.mpi import is_main_process
    if not is_main_process():
        return
    warn_deprecation()
    # Parse arguments
    import argparse
    parser = argparse.ArgumentParser(
        description="Prints defaults for Cobaya's components.")
    parser.add_argument("component",
                        action="store",
                        nargs="?",
                        default="",
                        metavar="component_name",
                        help="The component whose defaults are 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=("Kind of component whose defaults are requested: " +
              ", ".join(['%s' % kind for kind in kinds]) + ". " +
              "Use only when component name is not unique (it would fail)."))
    parser.add_argument("-p",
                        "--python",
                        action="store_true",
                        default=False,
                        help="Request Python instead of YAML.")
    expand_flag, expand_flag_ishort = "expand", 1
    parser.add_argument("-" + expand_flag[expand_flag_ishort],
                        "--" + expand_flag,
                        action="store_true",
                        default=False,
                        help="Expand YAML defaults.")
    arguments = parser.parse_args()
    # Remove plurals (= name of src subfolders), for user-friendliness
    if arguments.component.lower() in subfolders.values():
        arguments.component = next(k for k in subfolders
                                   if arguments.component == subfolders[k])
    # Kind given, list all
    if not arguments.component:
        msg = "Available components: (some may need external code/data)"
        print(msg + "\n" + "-" * len(msg))
        for kind in kinds:
            print("%s:" % kind)
            print(_indent +
                  ("\n" +
                   _indent).join(get_available_internal_class_names(kind)))
        return
    # Kind given: list all components of that kind
    if arguments.component.lower() in kinds:
        print("%s:" % arguments.component.lower())
        print(_indent + ("\n" + _indent).join(
            get_available_internal_class_names(arguments.component.lower())))
        return
    # Otherwise, check if it's a unique component name
    try:
        if arguments.kind:
            arguments.kind = arguments.kind[0].lower()
            if arguments.kind not in kinds:
                print("Kind %r not recognized. Try one of %r" %
                      (arguments.kind, tuple(kinds)))
                raise ValueError
        else:
            arguments.kind = get_kind(arguments.component)
        to_print = get_default_info(arguments.component,
                                    arguments.kind,
                                    return_yaml=not arguments.python,
                                    yaml_expand_defaults=arguments.expand)
        if arguments.python:
            print(pformat({arguments.kind: {arguments.component: to_print}}))
        else:
            print(arguments.kind + ":\n" + _indent + arguments.component +
                  ":\n" + 2 * _indent +
                  ("\n" + 2 * _indent).join(to_print.split("\n")))
            if "!defaults" in to_print:
                print("# This file contains defaults. "
                      "To populate them, use the flag --%s (or -%s)." %
                      (expand_flag, expand_flag[expand_flag_ishort]))
    except Exception:
        if isinstance(Exception, LoggedError.__class__):
            pass
        else:
            if not arguments.kind:
                print("Specify its kind with '--%s [component_kind]'." %
                      kind_opt)
        return 1
    return
Exemple #19
0
def doc_script(args=None):
    """Command line script for the documentation."""
    warn_deprecation()
    logger_setup()
    logger = get_logger("doc")
    # Parse arguments
    import argparse
    parser = argparse.ArgumentParser(
        prog="cobaya doc",
        description="Prints defaults for Cobaya's components.")
    parser.add_argument(
        "component",
        action="store",
        nargs="?",
        default="",
        metavar="component_name",
        help=("The component whose defaults are requested. "
              "Pass a component kind (sampler, theory, likelihood) to "
              "list all available (internal) ones, pass nothing to list "
              "all available (internal) components of all kinds."))
    parser.add_argument("-p",
                        "--python",
                        action="store_true",
                        default=False,
                        help="Request Python instead of YAML.")
    expand_flag, expand_flag_ishort = "expand", 1
    parser.add_argument("-" + expand_flag[expand_flag_ishort],
                        "--" + expand_flag,
                        action="store_true",
                        default=False,
                        help="Expand YAML defaults.")
    arguments = parser.parse_args(args)
    # Nothing passed: list all
    if not arguments.component:
        msg = "Available components: (some may need external code/data)"
        print(msg + "\n" + "-" * len(msg))
        for kind in kinds:
            print("%s:" % kind)
            print(_indent +
                  ("\n" +
                   _indent).join(get_available_internal_class_names(kind)))
        return
    # A kind passed (plural or singular): list all of that kind
    if arguments.component.lower() in subfolders.values():
        arguments.component = next(k for k in subfolders
                                   if arguments.component == subfolders[k])
    if arguments.component.lower() in kinds:
        print("%s:" % arguments.component.lower())
        print(_indent + ("\n" + _indent).join(
            get_available_internal_class_names(arguments.component.lower())))
        return
    # Otherwise, try to identify the component
    try:
        cls = get_component_class(arguments.component, logger=logger)
    except ComponentNotFoundError:
        suggestions = similar_internal_class_names(arguments.component)
        logger.error(
            f"Could not identify component '{arguments.component}'. "
            f"Did you mean any of the following? {suggestions} (mind capitalization!)"
        )
        return 1
    to_print = get_default_info(cls,
                                return_yaml=not arguments.python,
                                yaml_expand_defaults=arguments.expand)
    if arguments.python:
        print(pformat({cls.get_kind(): {arguments.component: to_print}}))
    else:
        print(cls.get_kind() + ":\n" + _indent + arguments.component + ":\n" +
              2 * _indent + ("\n" + 2 * _indent).join(to_print.split("\n")))
        if "!defaults" in to_print:
            print("# This file contains defaults. "
                  "To populate them, use the flag --%s (or -%s)." %
                  (expand_flag, expand_flag[expand_flag_ishort]))
Exemple #20
0
def install_script():
    from cobaya.mpi import am_single_or_primary_process
    if am_single_or_primary_process():
        warn_deprecation()
        # 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 simply 'polychord', 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]
        elif arguments.files == ["polychord"]:
            infos = [{"sampler": {"polychord": None}}]
        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"]
                })