Exemple #1
0
def cli_create(cmd, raw_args):
    """Create a conda environment for a model
    """
    import uuid
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Create a conda environment for a specific model.'
    )
    add_env_args(parser)
    parser.add_argument('-e', '--env', default=None,
                        help="Special environment name. default: kipoi-<model>[-<dataloader>]")
    args = parser.parse_args(raw_args)

    # create the tmp dir
    tmpdir = "/tmp/kipoi/envfiles/" + str(uuid.uuid4())[:8]
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    # write the env file
    logger.info("Writing environment file: {0}".format(tmpdir))
    env, env_file = export_env(args.model,
                               args.dataloader,
                               args.source,
                               env_file=None,
                               env_dir=tmpdir,
                               env=args.env,
                               vep=args.vep,
                               gpu=args.gpu)

    # setup the conda env from file
    logger.info("Creating conda env from file: {0}".format(env_file))
    kipoi.conda.create_env_from_file(env_file)
    logger.info("Done!")
    print("\nActivate the environment via:")
    print("source activate {0}".format(env))
Exemple #2
0
def cli_remove(cmd, raw_args):
    """Remove a conda environment for a model
    """
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description=
        'Remove environment compatible with this model. If `--all` is not set then only remove the most '
        'recently generated environment compatible with this model.')
    add_env_args(parser)
    parser.add_argument(
        '-a',
        '--all',
        action='store_true',
        help=
        "Remove all environments every created that are compatible with this model"
    )
    parser.add_argument(
        '-y',
        '--yes',
        action='store_true',
        help="If set then do NOT ask before deleting environments.")
    args = parser.parse_args(raw_args)

    to_delete = get_envs_by_model(args.model,
                                  args.source,
                                  only_most_recent=not args.all)

    if len(to_delete) != 0:
        ask_and_delete_envs(to_delete, args)
    else:
        logger.info("Nothing to remove!")

    logger.info("Done!")
Exemple #3
0
def cli_create(cmd, raw_args):
    """Create a conda environment for a model
    """
    from kipoi.conda import get_kipoi_bin
    import uuid
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Create a conda environment for a specific model.')
    add_env_args(parser)
    parser.add_argument(
        '-e',
        '--env',
        default=None,
        help="Special environment name. default: kipoi-<model>[-<dataloader>]")
    parser.add_argument(
        '-t',
        '--tmpdir',
        default=None,
        help=
        "Temporary directory path where to create the conda environment file" +
        "Defaults to /tmp/kipoi/envfiles/<uuid>/")
    args = parser.parse_args(raw_args)

    # create the tmp dir
    if args.tmpdir is None:
        tmpdir = "/tmp/kipoi/envfiles/" + str(uuid.uuid4())[:8]
    else:
        tmpdir = args.tmpdir
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    # write the env file
    logger.info("Writing environment file: {0}".format(tmpdir))

    env, env_file = export_env(args.model,
                               args.dataloader,
                               args.source,
                               env_file=None,
                               env_dir=tmpdir,
                               env=args.env,
                               vep=args.vep,
                               gpu=args.gpu)

    env_db_entry = generate_env_db_entry(args, args_env_overload=env)
    envdb = get_model_env_db()
    envdb.append(env_db_entry)
    envdb.save()

    # setup the conda env from file
    logger.info("Creating conda env from file: {0}".format(env_file))
    kipoi.conda.create_env_from_file(env_file)
    env_db_entry.successful = True

    # env is environment name
    env_db_entry.cli_path = get_kipoi_bin(env)
    get_model_env_db().save()
    logger.info("Done!")
    print("\nActivate the environment via:")
    print("source activate {0}".format(env))
Exemple #4
0
def cli_name(cmd, raw_args):
    """Show the name of the environment
    """
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Show the name of the environment.')
    add_env_args(parser)
    args = parser.parse_args(raw_args)
    env = get_env_name(args.model, args.dataloader, args.source, gpu=args.gpu)
    print("\nEnvironment name: {0}".format(env))
Exemple #5
0
def cli_get_cli(cmd, raw_args):
    """Print a kipoi cli path for a model
    """
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Print kipoi cli path for specific model.'
    )
    add_env_args(parser)
    parser.add_argument('-a', '--all', action='store_true',
                        help="If set all environments compatible with this model will be printed!")
    args = parser.parse_args(raw_args)

    entries = get_envs_by_model(args.model, args.source, only_most_recent=not args.all, only_valid=True)

    print_env_cli_paths(entries)
Exemple #6
0
def cli_install(cmd, raw_args):
    """Install the required packages for the model
    """
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Install all the dependencies for a model into the current conda environment.'
    )
    add_env_args(parser)
    args = parser.parse_args(raw_args)

    deps = merge_deps(models=args.model,
                      dataloaders=args.dataloader,
                      source=args.source,
                      vep=args.vep,
                      gpu=args.gpu)
    deps.install()
    logger.info("Done!")
Exemple #7
0
def cli_export(cmd, raw_args):
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Export the environment.yaml file for a specific model.')
    add_env_args(parser)
    parser.add_argument('-o',
                        '--output',
                        default='environment.yaml',
                        required=True,
                        help="Output file name")
    parser.add_argument('-e', '--env', default=None, help="Environment name")
    args = parser.parse_args(raw_args)
    env, env_file = export_env(args.model,
                               args.dataloader,
                               args.source,
                               env_file=args.output,
                               env=args.env,
                               vep=args.vep,
                               gpu=args.gpu)

    print("Create the environment with:")
    print("conda env create --file {0}".format(env_file))
Exemple #8
0
def cli_create(cmd, raw_args):
    """Create a conda environment for a model
    """
    from kipoi_conda import get_kipoi_bin
    import uuid
    parser = argparse.ArgumentParser(
        'kipoi env {}'.format(cmd),
        description='Create a conda environment for a specific model.')
    add_env_args(parser)
    parser.add_argument(
        '-e',
        '--env',
        default=None,
        help="Special environment name. default: kipoi-<model>[-<dataloader>]")
    parser.add_argument('--dry-run',
                        action='store_true',
                        help="Don't actually create the environment")
    parser.add_argument(
        '-t',
        '--tmpdir',
        default=None,
        help=
        ("Temporary directory path where to create the conda environment file"
         "Defaults to /tmp/kipoi/envfiles/<uuid>/"))
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help="Increase output verbosity. Show conda stdout.")
    args = parser.parse_args(raw_args)

    # create the tmp dir
    if args.tmpdir is None:
        tmpdir = "/tmp/kipoi/envfiles/" + str(uuid.uuid4())[:8]
    else:
        tmpdir = args.tmpdir
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    # write the env file
    logger.info("Writing environment file: {0}".format(tmpdir))

    if args.model == ['all']:
        from kipoi.cli.source_test import get_common_env
        src = kipoi.get_source(args.source)
        model_envs = yaml.safe_load(
            open(
                os.path.join(src.local_path, SPECIAL_ENV_PREFIX,
                             "models.yaml")))

        # TODO - test this by mocking up the CLI command execution

        # setup the args for all the models
        df = kipoi.list_models()
        dfg = list_models_by_group(df, "")
        for model_group in dfg.group.unique().tolist():
            existing_envs = get_envs_by_model(model_group,
                                              args.source,
                                              only_valid=True)
            if existing_envs or existing_envs is None:
                # No need to create the environment
                existing_envs_str = "\n".join(
                    [e.create_args.env for e in existing_envs])
                logger.info(
                    "Environment for {} already exists ({}). Skipping installation"
                    .format(model_group, existing_envs_str))
                continue

            logger.info(
                "Environment doesn't exists for model group {}. Installing it".
                format(model_group))

            # Figure out which <model> to use for installation
            common_env = get_common_env(model_group, model_envs)
            if common_env is not None:
                # common environment exists for the model. Use it
                logger.info("Using common environment: {}".format(common_env))
                model_group = os.path.join(SPECIAL_ENV_PREFIX, common_env)

            # Run cli_create
            def optional_replace(x, ref, alt):
                if x == ref:
                    return alt
                else:
                    return x

            new_raw_args = [
                optional_replace(x, 'all', model_group) for x in raw_args
                if x is not None
            ]
            cli_create(cmd, new_raw_args)
        logger.info("Done installing all environments!")
        return None

    env, env_file = export_env(args.model,
                               args.dataloader,
                               args.source,
                               env_file=None,
                               env_dir=tmpdir,
                               env=args.env,
                               vep=args.vep,
                               interpret=args.interpret,
                               gpu=args.gpu)

    if not args.dry_run:
        env_db_entry = generate_env_db_entry(args, args_env_overload=env)
        envdb = get_model_env_db()
        envdb.append(env_db_entry)
        envdb.save()

        # setup the conda env from file
        logger.info("Creating conda env from file: {0}".format(env_file))
        kipoi_conda.create_env_from_file(env_file, use_stdout=args.verbose)
        env_db_entry.successful = True

        # env is environment name
        env_db_entry.cli_path = get_kipoi_bin(env)
        get_model_env_db().save()
        logger.info("Done!")
        print("\nActivate the environment via:")
        print("conda activate {0}".format(env))
    else:
        print("Dry run. Conda file path: {}".format(env_file))