Repository navigation
Expand file tree
/
Copy pathtest_gui.py
More file actions
360 lines (284 loc) · 11.6 KB
/
Copy pathtest_gui.py
File metadata and controls
360 lines (284 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import sys
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
import torch
import yaml
from pyhdx.fileIO import csv_to_dataframe
from pyhdx.web.apps import main_app, rfu_app
from pyhdx.web.utils import load_state
cwd = Path(__file__).parent
input_dir = cwd / "test_data" / "input"
output_dir = cwd / "test_data" / "output"
test_port = 55432
np.random.seed(43)
torch.manual_seed(43)
@pytest.fixture
def ppix_spec() -> dict:
return yaml.safe_load(Path(input_dir / "PpiX_states.yaml").read_text())
@pytest.fixture
def secb_file_dict() -> dict:
filenames = ["ecSecB_apo.csv", "ecSecB_dimer.csv"]
file_dict = {fname: (input_dir / fname).read_bytes() for fname in filenames}
return file_dict
@pytest.fixture
def ppix_file_dict() -> dict:
filenames = ["PpiA_folding.csv", "PpiB_folding.csv"]
file_dict = {fname: (input_dir / fname).read_bytes() for fname in filenames}
return file_dict
@pytest.fixture
def secb_spec() -> dict:
return yaml.safe_load(Path(input_dir / "data_states.yaml").read_text())
def test_load_single_file():
with open(input_dir / "ecSecB_apo.csv", "rb") as f:
binary = f.read()
ctrl, tmpl = main_app()
src = ctrl.sources["main"]
input_control = ctrl.control_panels["PeptideFileInputControl"]
input_control.widgets["input_files"].filename = ["ecSecB_apo.csv"]
input_control.input_files = [binary]
assert input_control.fd_state == "Full deuteration control"
assert input_control.fd_exposure == 0.0
input_control.fd_state = "Full deuteration control"
input_control.fd_exposure = 10.020000000000001
input_control.exp_state = "SecB WT apo"
timepoints = list(np.array([0.167, 0.5, 1.0, 5.0, 10.0, 100.000008]) * 60)
assert input_control.exp_exposures == timepoints
input_control._add_single_dataset_spec()
input_control._action_load_datasets()
assert "SecB WT apo" in src.hdxm_objects
hdxm = src.hdxm_objects["SecB WT apo"]
assert hdxm.Nt == 6
assert hdxm.Np == 63
assert hdxm.Nr == 145
assert np.nanmean(hdxm.rfu_residues) == pytest.approx(0.6335831166442542)
def test_batch_input(secb_file_dict):
ctrl, tmpl = main_app()
input_control = ctrl.control_panels["PeptideFileInputControl"]
input_control.input_mode = "Batch"
input_control.widgets["input_files"].filename = list(secb_file_dict.keys())
input_control.input_files = list(secb_file_dict.values())
input_control.batch_file = Path(input_dir / "data_states.yaml").read_bytes()
input_control._action_load_datasets()
src = ctrl.sources["main"]
assert len(src.hdxm_objects) == 2
# ... additional tests
def test_web_fitting():
filenames = ["ecSecB_apo.csv", "ecSecB_dimer.csv"]
file_dict = {fname: (input_dir / fname).read_bytes() for fname in filenames}
ctrl, tmpl = main_app()
input_control = ctrl.control_panels["PeptideFileInputControl"]
# input_control.input_mode = "Batch"
input_control.widgets["input_files"].filename = list(file_dict.keys())
input_control.input_files = list(file_dict.values())
filenames = ["ecSecB_apo.csv", "ecSecB_dimer.csv"]
input_control.widgets["input_files"].filename = filenames
input_control.fd_state = "Full deuteration control"
input_control.fd_exposure = 0.167 * 60
input_control.exp_state = "SecB WT apo"
input_control.measurement_name = "testname_123"
input_control._add_single_dataset_spec()
input_control.exp_file = "ecSecB_dimer.csv"
input_control.exp_state = "SecB his dimer apo"
input_control.measurement_name = "SecB his dimer apo" # todo catch error duplicate name
input_control._add_single_dataset_spec()
input_control._action_load_datasets()
assert "testname_123" in ctrl.sources["main"].hdxm_objects.keys()
assert "SecB his dimer apo" in ctrl.sources["main"].hdxm_objects.keys()
rfu_df = ctrl.sources["main"].get_table("rfu")
assert rfu_df.shape == (145, 24)
assert rfu_df.columns.nlevels == 3
initial_guess = ctrl.control_panels["InitialGuessControl"]
initial_guess._action_fit()
@pytest.mark.skipif(
not sys.platform.startswith("win"), reason="output slightly different on other platforms"
)
def test_web_load(secb_spec, secb_file_dict):
ctrl, tmpl = main_app()
file_input = ctrl.control_panels["PeptideFileInputControl"]
states = ["SecB_tetramer", "SecB_dimer"]
load_state(file_input, secb_spec, data_dir=input_dir, states=states)
file_input._action_load_datasets()
assert len(file_input.src.hdxm_objects) == 2
file_export = ctrl.control_panels["FileExportControl"]
# Check table output
file_export.table = "peptides"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "main_web" / "peptides.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# Check rfu table output
file_export.table = "rfu"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "main_web" / "rfu.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# check color table output
sio = file_export.color_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "main_web" / "rfu_colors.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# Download HDX spec file
sio_hdx_spec = file_export.hdx_spec_callback()
del ctrl
del file_export
del file_input
# load a new instance of the main app, reload previous data through batch input
new_ctrl, tmpl = main_app()
input_control = new_ctrl.control_panels["PeptideFileInputControl"]
sio_hdx_spec.seek(0)
input_control.input_mode = "Batch"
input_control.widgets["input_files"].filename = list(secb_file_dict.keys())
input_control.input_files = list(secb_file_dict.values())
input_control.batch_file = sio_hdx_spec.read().encode("utf-8")
input_control._action_load_datasets()
file_export = new_ctrl.control_panels["FileExportControl"]
# check rfu table output
file_export.table = "rfu"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "main_web" / "rfu.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# check table output
file_export.table = "peptides"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "main_web" / "peptides.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
@pytest.mark.skipif(
not sys.platform.startswith("win"), reason="output slightly different on other platforms"
)
def test_rfu(ppix_spec, ppix_file_dict):
"""Test the RFU app"""
ctrl, tmpl = rfu_app()
file_input = ctrl.control_panels["PeptideFileInputControl"]
states = ["PpiA_Folding", "PpiB_Folding"]
load_state(file_input, ppix_spec, data_dir=input_dir, states=states)
file_input._action_load_datasets()
assert len(file_input.src.hdxm_objects) == 2
file_export = ctrl.control_panels["FileExportControl"]
# check table output
file_export.table = "peptides"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "rfu_web" / "peptides.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# check rfu table output
file_export.table = "rfu"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "rfu_web" / "rfu.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# Download HDX spec file
sio_hdx_spec = file_export.hdx_spec_callback()
del ctrl
del file_export
del file_input
# creata new app, input the same data via batch input
new_ctrl, tmpl = rfu_app()
input_control = new_ctrl.control_panels["PeptideFileInputControl"]
sio_hdx_spec.seek(0)
input_control.input_mode = "Batch"
input_control.widgets["input_files"].filename = list(ppix_file_dict.keys())
input_control.input_files = list(ppix_file_dict.values())
input_control.batch_file = sio_hdx_spec.read().encode("utf-8")
input_control._action_load_datasets()
file_export = new_ctrl.control_panels["FileExportControl"]
# check table output
file_export.table = "peptides"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "rfu_web" / "peptides.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# check rfu table output
file_export.table = "rfu"
sio = file_export.table_export_callback()
df_test = csv_to_dataframe(sio)
df_ref = csv_to_dataframe(output_dir / "rfu_web" / "rfu.csv")
pd.testing.assert_frame_equal(df_test, df_ref)
# with cluster() as (s, [a, b]):
# conf.set("cluster", "scheduler_address", s["address"])
#
# fit_control = ctrl.control_panels["FitControl"]
# fit_control.fit_mode = "Batch"
# fit_control.epochs = 10
#
# fit_control.fit_name = "testfit_1"
# fit_control._action_fit()
#
# table_names = [
# "loss",
# "peptide_mse",
# "rates",
# "dG_fits",
# "rfu_residues",
# "peptides",
# "d_calc",
# ]
# for name in table_names:
# ref = csv_to_dataframe(output_dir / "gui" / f"{name}.csv")
# test = ctrl.sources["main"].get_table(name)
#
# for test_col, ref_col in zip(test.columns, ref.columns):
# test_values = test[test_col].to_numpy()
# if np.issubdtype(test_values.dtype, np.number):
# assert np.allclose(
# test_values, ref[ref_col].to_numpy(), equal_nan=True
# )
#
# fit_control.guess_mode = "One-to-many"
# fit_control.fit_name = "testfit_2"
# fit_control._action_fit()
#
# fit_control.fit_mode = "Single"
# fit_control.guess_mode = "One-to-one"
# fit_control.fit_name = "testfit_3"
# fit_control._action_fit()
#
# fit_control.initial_guess = "testfit_2"
# fit_control.guess_mode = "One-to-many"
# fit_control.guess_state = "testname_123"
# fit_control.fit_name = "testfit_4"
# fit_control._action_fit()
#
# color_transform_control = ctrl.control_panels["ColorTransformControl"]
# color_transform_control._action_otsu()
#
# fit_result = ctrl.sources["main"].get_table("dG_fits")
# values = fit_result["testfit_1"]["testname_123"]["dG"]
# color_transform_control.quantity = "dG"
# cmap, norm = color_transform_control.get_cmap_and_norm()
# colors = cmap(norm(values), bytes=True)
#
# h = hash_array(colors, method="md5")
# assert h == "bac3602f877a53abd94be8bb5f9b72ec"
#
# color_transform_control.mode = "Continuous"
# color_transform_control._action_linear()
#
# cmap, norm = color_transform_control.get_cmap_and_norm()
# colors = cmap(norm(values), bytes=True)
#
# h = hash_array(colors, method="md5")
#
# assert h == "123085ba16b3a9374595b734f9e675e6"
#
# value_widget = color_transform_control.widgets["value_1"]
# value_widget.value = 25
# cmap, norm = color_transform_control.get_cmap_and_norm()
# assert norm.vmin == 25
#
# color_transform_control.mode = "Colormap"
# color_transform_control.library = "colorcet"
# color_transform_control.colormap = "CET_C1"
# cmap, norm = color_transform_control.get_cmap_and_norm()
#
# colors = cmap(norm(values), bytes=True)
#
# h = hash_array(colors, method="md5")
# assert h == "0628b46e7975ed57490e84c169bc81ad"
# Future tests: check if renderers are present in figures
# cov_figure = ctrl.figure_panels['CoverageFigure']
# renderer = cov_figure.figure.renderers[0]
# assert renderer.data_source.name == f'coverage_{self.series.state}'