|
a |
|
b/tests/cli/test_train.py |
|
|
1 |
"""Test bpnet train |
|
|
2 |
""" |
|
|
3 |
import os |
|
|
4 |
import pytest |
|
|
5 |
from bpnet.cli.train import bpnet_train |
|
|
6 |
from pathlib import Path |
|
|
7 |
from bpnet.seqmodel import SeqModel |
|
|
8 |
from concise.preprocessing import encodeDNA |
|
|
9 |
import gin |
|
|
10 |
import keras.backend as K |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def test_output_files(trained_model): |
|
|
14 |
K.clear_session() |
|
|
15 |
output_files = os.listdir(str(trained_model)) |
|
|
16 |
expected_files = [ |
|
|
17 |
'config.gin', |
|
|
18 |
'config.gin.json', |
|
|
19 |
'bpnet-train.kwargs.json', |
|
|
20 |
'dataspec.yml', |
|
|
21 |
'evaluate.ipynb', |
|
|
22 |
'evaluate.html', |
|
|
23 |
'evaluation.valid.json', |
|
|
24 |
'history.csv', |
|
|
25 |
'model.h5', |
|
|
26 |
'seq_model.pkl', |
|
|
27 |
'note_params.json', |
|
|
28 |
] |
|
|
29 |
for f in expected_files: |
|
|
30 |
assert f in output_files |
|
|
31 |
|
|
|
32 |
m = SeqModel.load(trained_model / 'seq_model.pkl') |
|
|
33 |
m.predict(encodeDNA(["A" * 200])) |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
def test_output_files_model_w_bias(trained_model_w_bias): |
|
|
37 |
K.clear_session() |
|
|
38 |
output_files = os.listdir(str(trained_model_w_bias)) |
|
|
39 |
expected_files = [ |
|
|
40 |
'config.gin', |
|
|
41 |
'config.gin.json', |
|
|
42 |
'bpnet-train.kwargs.json', |
|
|
43 |
'dataspec.yml', |
|
|
44 |
'evaluate.ipynb', |
|
|
45 |
'evaluate.html', |
|
|
46 |
'evaluation.valid.json', |
|
|
47 |
'history.csv', |
|
|
48 |
'model.h5', |
|
|
49 |
'seq_model.pkl', |
|
|
50 |
'note_params.json', |
|
|
51 |
] |
|
|
52 |
for f in expected_files: |
|
|
53 |
assert f in output_files |
|
|
54 |
|
|
|
55 |
m = SeqModel.load(trained_model_w_bias / 'seq_model.pkl') |
|
|
56 |
m.predict(encodeDNA(["A" * 200])) |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
def test_trained_model_bed6(tmp_path, data_dir, config_gin, dataspec_bed6): |
|
|
60 |
K.clear_session() |
|
|
61 |
gin.clear_config() |
|
|
62 |
bpnet_train(dataspec=str(dataspec_bed6), |
|
|
63 |
output_dir=str(tmp_path), |
|
|
64 |
premade='bpnet9', |
|
|
65 |
config=str(config_gin), |
|
|
66 |
override='seq_width=100;train.batch_size=8', |
|
|
67 |
num_workers=2 |
|
|
68 |
) |
|
|
69 |
|
|
|
70 |
|
|
|
71 |
def test_trained_model_override_in_memory(tmp_path, data_dir, config_gin, dataspec_bias): |
|
|
72 |
K.clear_session() |
|
|
73 |
gin.clear_config() |
|
|
74 |
bpnet_train(dataspec=str(dataspec_bias), |
|
|
75 |
output_dir=str(tmp_path), |
|
|
76 |
premade='bpnet9', |
|
|
77 |
config=str(config_gin), |
|
|
78 |
in_memory=True, |
|
|
79 |
override='seq_width=190;train.batch_size=8', |
|
|
80 |
num_workers=2 |
|
|
81 |
) |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
def test_train_regions(tmp_path, data_dir, config_gin, dataspec_bias, regions): |
|
|
85 |
K.clear_session() |
|
|
86 |
gin.clear_config() |
|
|
87 |
bpnet_train(dataspec=str(dataspec_bias), |
|
|
88 |
output_dir=str(tmp_path), |
|
|
89 |
premade='bpnet9', |
|
|
90 |
config=str(config_gin), |
|
|
91 |
override=f'bpnet_data.intervals_file="{regions}"', |
|
|
92 |
num_workers=2 |
|
|
93 |
) |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
def test_trained_model_premade_pyspec(tmp_path, data_dir, config_gin, dataspec_bias): |
|
|
97 |
K.clear_session() |
|
|
98 |
gin.clear_config() |
|
|
99 |
bpnet_train(dataspec=str(dataspec_bias), |
|
|
100 |
output_dir=str(tmp_path), |
|
|
101 |
premade='bpnet9-pyspec', |
|
|
102 |
config=str(config_gin), |
|
|
103 |
num_workers=2 |
|
|
104 |
) |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
def test_trained_model_vmtouch(tmp_path, data_dir, config_gin, dataspec_bias): |
|
|
108 |
K.clear_session() |
|
|
109 |
gin.clear_config() |
|
|
110 |
bpnet_train(dataspec=str(dataspec_bias), |
|
|
111 |
output_dir=str(tmp_path), |
|
|
112 |
premade='bpnet9', |
|
|
113 |
config=str(config_gin), |
|
|
114 |
vmtouch=True, |
|
|
115 |
num_workers=1 |
|
|
116 |
) |