Esempio n. 1
0
def test_test_example(example, tmpdir):
    """kipoi test ..., add also output file writing
    """
    if example in {"rbp", "non_bedinput_model", "iris_model_template"} \
            and sys.version_info[0] == 2:
        pytest.skip("example not supported on python 2 ")

    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)

    args = [
        "python", "./kipoi/__main__.py", "test", "--batch_size=4", example_dir
    ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args)
    assert returncode == 0

    if example == 'pyt':
        # python interface, write also the output file
        output_file = os.path.join(example_dir, 'preds.h5')
        kipoi.cli.main.cli_test("test", args[3:] + ["-o", output_file])

        assert os.path.exists(output_file)
        preds = HDF5Reader.load(output_file)
        assert 'inputs' in preds
        assert 'metadata' in preds
        assert 'preds' in preds
Esempio n. 2
0
def test_preproc_example(example, tmpdir):
    """kipoi preproc ...
    """
    if example in {"rbp", "non_bedinput_model", "iris_model_template"
                   } and sys.version_info[0] == 2:
        pytest.skip("example not supported on python 2 ")
    if example in {"extended_coda", "kipoi_dataloader_decorator"}:
        # extended_coda will anyway be tested in models
        pytest.skip(
            "randomly failing on circleci without any reason. Skipping this test."
        )

    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)
    # example_dir = "example/models/{0}".format(example)

    tmpfile = str(tmpdir.mkdir("output", ).join("out.h5"))

    # run the
    args = [
        "python",
        os.path.abspath("./kipoi/__main__.py"),
        "preproc",
        "../",  # directory
        "--source=dir",
        "--batch_size=4",
        "--num_workers=2",
        "--dataloader_args=test.json",
        "--output",
        tmpfile
    ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args,
                                 cwd=os.path.realpath(example_dir +
                                                      "/example_files"))

    assert returncode == 0

    assert os.path.exists(tmpfile)

    data = HDF5Reader.load(tmpfile)

    with open(example_dir + "/dataloader.yaml", "r") as f:
        ex_descr = yaml.load(f)

    if example not in {"pyt", "sklearn_iris"}:
        assert data["inputs"].keys(
        ) == ex_descr["output_schema"]["inputs"].keys()

    if example == 'pyt':
        args[-1] = tmpfile + "2.h5"
        with kipoi.utils.cd(os.path.join(example_dir, "example_files")):
            kipoi.cli.main.cli_preproc("preproc", args[3:])
Esempio n. 3
0
def test_predict_activation_example(example, tmpdir):
    """Kipoi predict --layer=x with a specific output layer specified
    """
    if example in {"rbp", "non_bedinput_model", "iris_model_template"
                   } and sys.version_info[0] == 2:
        pytest.skip("rbp example not supported on python 2 ")
    if example in {'kipoi_dataloader_decorator'}:
        pytest.skip(
            "Automatically-dowloaded input files skipped for prediction")

    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)
    # example_dir = "example/models/{0}".format(example)

    print(example)
    print("tmpdir: {0}".format(tmpdir))
    tmpfile = str(tmpdir.mkdir("output").join("out.h5"))

    # run the
    args = [
        "python",
        os.path.abspath("./kipoi/__main__.py"),
        "predict",
        "../",  # directory
        "--source=dir",
        "--layer",
        predict_activation_layers[example],
        "--batch_size=4",
        "--num_workers=2",
        "--dataloader_args=test.json",
        "--output",
        tmpfile
    ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args,
                                 cwd=os.path.realpath(example_dir +
                                                      "/example_files"))
    assert returncode == 0

    assert os.path.exists(tmpfile)

    data = HDF5Reader.load(tmpfile)
    assert {'metadata', 'preds'} <= set(data.keys())
    if example == 'pyt':
        args[-1] = tmpfile + "2.h5"
        with kipoi.utils.cd(os.path.join(example_dir, "example_files")):
            kipoi.cli.main.cli_predict("predict", args[3:])
Esempio n. 4
0
def test_cli_test_expect(tmpdir):
    """kipoi test - check that the expected predictions also match
    """
    example = 'pyt'
    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)

    # fail the test
    args = [
        "python", "./kipoi/__main__.py", "test", "--batch_size=4", "-e",
        os.path.join(example_dir, "wrong.pred.h5"), example_dir
    ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args)
    assert returncode == 1

    # succeed
    kipoi.cli.main.cli_test("test", [
        "--batch_size=4", "-e",
        os.path.join(example_dir, "expected.pred.h5"), example_dir
    ])
Esempio n. 5
0
def test_decorator_env_loading(tmpdir):
    mdir = cp_tmpdir("example/models/kipoi_dataloader_decorator", tmpdir)
    assert merge_deps([mdir], source='dir') == \
           Dependencies(conda=['python=2.7', 'scikit-learn'],
                        pip=['kipoi', 'scikit-learn', 'tqdm'],
                        conda_channels=['defaults'])
Esempio n. 6
0
def test_predict_example(example, tmpdir):
    """kipoi predict ...
    """
    # TODO - test -out
    # Traceback (most recent call last):
    #   File "/home/avsec/projects-work/kipoi/kipoi/__main__.py", line 60, in <module>
    #     main()
    #   File "/home/avsec/projects-work/kipoi/kipoi/__main__.py", line 56, in main
    #     command_fn(args.command, sys.argv[2:])
    #   File "/home/avsec/bin/anaconda3/lib/python3.6/site-packages/kipoi/pipeline.py", line 273, in cli_predict
    #     pred_batch = model.predict_on_batch(batch['inputs'])
    #   File "/home/avsec/bin/anaconda3/lib/python3.6/site-packages/kipoi/model.py", line 22, in predict_on_batch
    #     raise NotImplementedError
    # NotImplementedError
    # _________________________
    if example in {"rbp", "non_bedinput_model", "iris_model_template"
                   } and sys.version_info[0] == 2:
        pytest.skip("rbp example not supported on python 2 ")

    if example in {'kipoi_dataloader_decorator'}:
        pytest.skip(
            "Automatically-dowloaded input files skipped for prediction")

    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)
    # example_dir = "example/models/{0}".format(example)

    if example == "rbp":
        file_format = "tsv"
    else:
        file_format = "hdf5"

    print(example)
    print("tmpdir: {0}".format(tmpdir))
    tmpfile = str(tmpdir.mkdir("output").join("out.{0}".format(file_format)))

    # run the
    args = [
        "python",
        os.path.abspath("./kipoi/__main__.py"),
        "predict",
        "../",  # directory
        "--source=dir",
        "--batch_size=4",
        "--num_workers=2",
        "--dataloader_args=test.json",
        "--output",
        tmpfile
    ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args,
                                 cwd=os.path.realpath(example_dir +
                                                      "/example_files"))
    assert returncode == 0

    assert os.path.exists(tmpfile)

    if file_format == "hdf5":
        data = HDF5Reader.load(tmpfile)
        assert {'metadata', 'preds'} <= set(data.keys())
    else:
        data = pd.read_csv(tmpfile, sep="\t")
        assert list(data.columns) == [
            'metadata/ranges/chr', 'metadata/ranges/end', 'metadata/ranges/id',
            'metadata/ranges/start', 'metadata/ranges/strand', 'preds/0'
        ]
    if example == 'pyt':
        args[-1] = tmpfile + "out2.{0}".format(file_format)
        with kipoi.utils.cd(os.path.join(example_dir, "example_files")):
            kipoi.cli.main.cli_predict("predict", args[3:])
Esempio n. 7
0
def test_preproc_example(example, new_dataloader_kwargs_format, tmpdir):
    """kipoi preproc ...
    """
    if example in {"rbp", "non_bedinput_model", "iris_model_template"
                   } and sys.version_info[0] == 2:
        pytest.skip("example not supported on python 2 ")
    if example in {"extended_coda", "kipoi_dataloader_decorator"}:
        # extended_coda will anyway be tested in models
        pytest.skip(
            "randomly failing on circleci without any reason. Skipping this test."
        )

    example_dir = cp_tmpdir("example/models/{0}".format(example), tmpdir)
    # example_dir = "example/models/{0}".format(example)

    tmpfile = str(tmpdir.mkdir("output", ).join("out.h5"))

    if example in {"rbp"} and new_dataloader_kwargs_format:
        if example == "rbp":
            dataloader_args = [
                "intervals_file=intervals.tsv", "fasta_file=hg38_chr22.fa",
                "preproc_transformer=../dataloader_files/encodeSplines.pkl",
                "gtf_file=gencode_v25_chr22.gtf.pkl.gz",
                "tarOget_file=targets.tsv"
            ]
        elif example == "extended_coda":
            dataloader_args = [
                "intervals_file=intervals.tsv",
                "input_data_sources={'H3K27AC_subsampled':'H3K27AC_subsampled.bw'}",
                "batch_size=4"
            ]
        # run the
        args = [
            "python",
            os.path.abspath("./kipoi/__main__.py"),
            "preproc",
            "../",  # directory
            "--source=dir",
            "--batch_size=4",
            "--num_workers=2",
            "--dataloader_args"
        ] + dataloader_args + ["--output", tmpfile]

    else:
        # run the
        args = [
            "python",
            os.path.abspath("./kipoi/__main__.py"),
            "preproc",
            "../",  # directory
            "--source=dir",
            "--batch_size=4",
            "--num_workers=2",
            "--dataloader_args=test.json",
            "--output",
            tmpfile
        ]
    if INSTALL_FLAG:
        args.append(INSTALL_FLAG)
    returncode = subprocess.call(args=args,
                                 cwd=os.path.realpath(example_dir +
                                                      "/example_files"))

    assert returncode == 0

    assert os.path.exists(tmpfile)

    data = HDF5Reader.load(tmpfile)

    with open(example_dir + "/dataloader.yaml", "r") as f:
        ex_descr = yaml.load(f)

    if example not in {"pyt", "sklearn_iris"}:
        assert data["inputs"].keys(
        ) == ex_descr["output_schema"]["inputs"].keys()

    if example == 'pyt':
        args[-1] = tmpfile + "2.h5"
        with kipoi_utils.utils.cd(os.path.join(example_dir, "example_files")):
            kipoi.cli.main.cli_preproc("preproc", args[3:])