forked from PEtab-dev/libpetab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lint.py
More file actions
662 lines (542 loc) · 20.1 KB
/
test_lint.py
File metadata and controls
662 lines (542 loc) · 20.1 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import os
import subprocess
from math import nan
from unittest.mock import patch
import pandas as pd
import pytest
import petab
from petab import lint
from petab.C import *
# import fixtures
pytest_plugins = [
"tests.v1.test_petab",
]
def test_assert_measured_observables_present():
# create test model
measurement_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["non-existing1"],
}
)
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["obs1"],
}
)
observable_df.set_index(OBSERVABLE_ID, inplace=True)
with pytest.raises(AssertionError):
lint.assert_measured_observables_defined(measurement_df, observable_df)
def test_condition_table_is_parameter_free():
with patch(
"petab.v1.get_parametric_overrides"
) as mock_get_parametric_overrides:
mock_get_parametric_overrides.return_value = []
assert lint.condition_table_is_parameter_free(pd.DataFrame()) is True
mock_get_parametric_overrides.assert_called_once()
mock_get_parametric_overrides.reset_mock()
mock_get_parametric_overrides.return_value = ["p1"]
assert lint.condition_table_is_parameter_free(pd.DataFrame()) is False
mock_get_parametric_overrides.assert_called_once()
def test_measurement_table_has_timepoint_specific_mappings():
# Ensure we fail if we have time-point specific assignments
measurement_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["obs1", "obs1"],
SIMULATION_CONDITION_ID: ["condition1", "condition1"],
PREEQUILIBRATION_CONDITION_ID: [nan, nan],
TIME: [1.0, 2.0],
OBSERVABLE_PARAMETERS: ["obsParOverride", ""],
NOISE_PARAMETERS: ["1.0", 1.0],
}
)
assert (
lint.measurement_table_has_timepoint_specific_mappings(measurement_df)
is True
)
# both measurements different anyways
measurement_df.loc[1, OBSERVABLE_ID] = "obs2"
assert (
lint.measurement_table_has_timepoint_specific_mappings(measurement_df)
is False
)
# mixed numeric string
measurement_df.loc[1, OBSERVABLE_ID] = "obs1"
measurement_df.loc[1, OBSERVABLE_PARAMETERS] = "obsParOverride"
assert (
lint.measurement_table_has_timepoint_specific_mappings(measurement_df)
is False
)
# different numeric values
measurement_df.loc[1, NOISE_PARAMETERS] = 2.0
assert (
lint.measurement_table_has_timepoint_specific_mappings(measurement_df)
is True
)
assert (
lint.measurement_table_has_timepoint_specific_mappings(
measurement_df, allow_scalar_numeric_noise_parameters=True
)
is False
)
def test_observable_table_has_nontrivial_noise_formula():
# Ensure we fail if we have nontrivial noise formulas
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: [
"0obsPar1noisePar",
"2obsPar0noisePar",
"3obsPar0noisePar",
],
OBSERVABLE_FORMULA: ["1.0", "1.0", "1.0"],
NOISE_FORMULA: [
"noiseParameter1_0obsPar1noisePar + 3.0",
1e18,
"1e18",
],
}
)
assert (
lint.observable_table_has_nontrivial_noise_formula(observable_df)
is True
)
observable_df.loc[0, NOISE_FORMULA] = "sigma1"
assert (
lint.observable_table_has_nontrivial_noise_formula(observable_df)
is False
)
def test_assert_overrides_match_parameter_count():
# Ensure we recognize and fail if we have wrong number of overrides
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["0obsPar1noisePar", "2obsPar0noisePar"],
OBSERVABLE_FORMULA: [
"1.0",
"observableParameter1_2obsPar0noisePar + "
"observableParameter2_2obsPar0noisePar",
],
NOISE_FORMULA: ["noiseParameter1_0obsPar1noisePar", "1.0"],
}
)
observable_df.set_index(OBSERVABLE_ID, inplace=True)
measurement_df_orig = pd.DataFrame(
data={
OBSERVABLE_ID: ["0obsPar1noisePar", "2obsPar0noisePar"],
SIMULATION_CONDITION_ID: ["condition1", "condition1"],
PREEQUILIBRATION_CONDITION_ID: ["", ""],
TIME: [1.0, 2.0],
OBSERVABLE_PARAMETERS: ["", "override1;override2"],
NOISE_PARAMETERS: ["noiseParOverride", ""],
}
)
# valid
petab.assert_overrides_match_parameter_count(
measurement_df_orig, observable_df
)
# 0 noise parameters given, 1 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[0, NOISE_PARAMETERS] = ""
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 2 noise parameters given, 1 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[0, NOISE_PARAMETERS] = "noiseParOverride;oneTooMuch"
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 1 noise parameter given, 0 allowed
measurement_df = measurement_df_orig.copy()
measurement_df.loc[1, NOISE_PARAMETERS] = "oneTooMuch"
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 0 observable parameters given, 2 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[1, OBSERVABLE_PARAMETERS] = ""
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 1 observable parameters given, 2 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[1, OBSERVABLE_PARAMETERS] = "oneMissing"
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 3 observable parameters given, 2 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[
1, OBSERVABLE_PARAMETERS
] = "override1;override2;oneTooMuch"
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
# 1 observable parameters given, 0 expected
measurement_df = measurement_df_orig.copy()
measurement_df.loc[0, OBSERVABLE_PARAMETERS] = "oneTooMuch"
with pytest.raises(AssertionError):
petab.assert_overrides_match_parameter_count(
measurement_df, observable_df
)
def test_assert_no_leading_trailing_whitespace():
test_df = pd.DataFrame(
data={
"testId": ["name1 ", "name2"],
"testText ": [" name1", "name2"],
"testNumeric": [1.0, 2.0],
"testNone": None,
}
)
with pytest.raises(AssertionError):
lint.assert_no_leading_trailing_whitespace(
test_df.columns.values, "test"
)
with pytest.raises(AssertionError):
lint.assert_no_leading_trailing_whitespace(
test_df["testId"].values, "testId"
)
with pytest.raises(AssertionError):
lint.assert_no_leading_trailing_whitespace(
test_df["testText "].values, "testText"
)
lint.assert_no_leading_trailing_whitespace(
test_df["testNumeric"].values, "testNumeric"
)
lint.assert_no_leading_trailing_whitespace(
test_df["testNone"].values, "testNone"
)
def test_assert_model_parameters_in_condition_or_parameter_table():
from petab.models.sbml_model import SbmlModel
ant_model = """
parameter1 = 0.0
noiseParameter1_ = 0.0
observableParameter1_ = 0.0
"""
sbml_model = SbmlModel.from_antimony(ant_model)
assert sbml_model.is_valid()
lint.assert_model_parameters_in_condition_or_parameter_table(
sbml_model, pd.DataFrame(columns=["parameter1"]), pd.DataFrame()
)
lint.assert_model_parameters_in_condition_or_parameter_table(
sbml_model, pd.DataFrame(), pd.DataFrame(index=["parameter1"])
)
with pytest.raises(AssertionError):
lint.assert_model_parameters_in_condition_or_parameter_table(
sbml_model,
pd.DataFrame(columns=["parameter1"]),
pd.DataFrame(index=["parameter1"]),
)
lint.assert_model_parameters_in_condition_or_parameter_table(
sbml_model, pd.DataFrame(), pd.DataFrame()
)
sbml_model = SbmlModel.from_antimony(
ant_model + "\nparameter2 = 0\nparameter1 := parameter2"
)
assert sbml_model.is_valid()
lint.assert_model_parameters_in_condition_or_parameter_table(
sbml_model, pd.DataFrame(), pd.DataFrame()
)
def test_assert_noise_distributions_valid():
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["0obsPar1noisePar", "2obsPar0noisePar"],
NOISE_PARAMETERS: ["", ""],
NOISE_DISTRIBUTION: ["", ""],
}
)
observable_df.set_index([OBSERVABLE_ID], inplace=True)
lint.assert_noise_distributions_valid(observable_df)
observable_df[OBSERVABLE_TRANSFORMATION] = [LIN, LOG]
observable_df[NOISE_DISTRIBUTION] = [NORMAL, ""]
lint.assert_noise_distributions_valid(observable_df)
observable_df[NOISE_DISTRIBUTION] = ["Normal", ""]
with pytest.raises(ValueError):
lint.assert_noise_distributions_valid(observable_df)
observable_df.drop(columns=NOISE_DISTRIBUTION, inplace=True)
lint.assert_noise_distributions_valid(observable_df)
def test_check_measurement_df():
"""Check measurement (and observable) tables"""
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["0obsPar1noisePar", "2obsPar0noisePar"],
OBSERVABLE_FORMULA: ["", ""],
NOISE_FORMULA: ["", ""],
NOISE_DISTRIBUTION: ["", ""],
}
)
observable_df.set_index([OBSERVABLE_ID], inplace=True)
measurement_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["0obsPar1noisePar", "2obsPar0noisePar"],
SIMULATION_CONDITION_ID: ["condition1", "condition1"],
PREEQUILIBRATION_CONDITION_ID: ["", ""],
TIME: [1.0, 2.0],
MEASUREMENT: [1.0, 2.0],
OBSERVABLE_PARAMETERS: ["", ""],
NOISE_PARAMETERS: ["", ""],
}
)
lint.check_measurement_df(measurement_df, observable_df)
# Incompatible measurement and transformation
observable_df[OBSERVABLE_TRANSFORMATION] = [LOG, ""]
measurement_df[MEASUREMENT] = [-1.0, 0.0]
with pytest.raises(ValueError):
lint.check_measurement_df(measurement_df, observable_df)
def test_check_parameter_bounds():
lint.check_parameter_bounds(
pd.DataFrame({LOWER_BOUND: [1], UPPER_BOUND: [2], ESTIMATE: [1]})
)
with pytest.raises(AssertionError):
lint.check_parameter_bounds(
pd.DataFrame({LOWER_BOUND: [3], UPPER_BOUND: [2], ESTIMATE: [1]})
)
with pytest.raises(AssertionError):
lint.check_parameter_bounds(
pd.DataFrame(
{
LOWER_BOUND: [-1],
UPPER_BOUND: [2],
ESTIMATE: [1],
PARAMETER_SCALE: [LOG10],
}
)
)
with pytest.raises(AssertionError):
lint.check_parameter_bounds(
pd.DataFrame(
{
LOWER_BOUND: [-1],
UPPER_BOUND: [2],
ESTIMATE: [1],
PARAMETER_SCALE: [LOG],
}
)
)
def test_assert_parameter_prior_type_is_valid():
"""Check lint.assert_parameter_prior_type_is_valid."""
lint.assert_parameter_prior_type_is_valid(
pd.DataFrame(
{
INITIALIZATION_PRIOR_TYPE: [UNIFORM, LAPLACE, ""],
OBJECTIVE_PRIOR_TYPE: [NORMAL, LOG_NORMAL, ""],
}
)
)
lint.assert_parameter_prior_type_is_valid(pd.DataFrame())
with pytest.raises(AssertionError):
lint.assert_parameter_prior_type_is_valid(
pd.DataFrame({INITIALIZATION_PRIOR_TYPE: ["normel"]})
)
def test_assert_parameter_prior_parameters_are_valid():
"""Check lint.assert_parameter_prior_parameters_are_valid."""
parameter_df = pd.DataFrame(
{
INITIALIZATION_PRIOR_TYPE: [UNIFORM, "", ""],
INITIALIZATION_PRIOR_PARAMETERS: ["0;1", "10;20", ""],
OBJECTIVE_PRIOR_PARAMETERS: ["0;20", "10;20", ""],
}
)
lint.assert_parameter_prior_parameters_are_valid(parameter_df)
with pytest.raises(AssertionError):
lint.assert_parameter_prior_parameters_are_valid(
pd.DataFrame({INITIALIZATION_PRIOR_TYPE: [NORMAL]})
)
with pytest.raises(AssertionError):
lint.assert_parameter_prior_parameters_are_valid(
pd.DataFrame({OBJECTIVE_PRIOR_PARAMETERS: ["0;1;2"]})
)
def test_petablint_succeeds():
"""Run petablint and ensure we exit successfully for a file that should
contain no errors
"""
dir_isensee = "../../doc/example/example_Isensee/"
dir_fujita = "../../doc/example/example_Fujita/"
# run with measurement file
script_path = os.path.abspath(os.path.dirname(__file__))
measurement_file = os.path.join(
script_path, dir_isensee, "Isensee_measurementData.tsv"
)
result = subprocess.run(["petablint", "-m", measurement_file]) # noqa: S603,S607
assert result.returncode == 0
# run with yaml
yaml_file = os.path.join(script_path, dir_fujita, "Fujita.yaml")
result = subprocess.run(["petablint", "-v", "-y", yaml_file]) # noqa: S603,S607
assert result.returncode == 0
parameter_file = os.path.join(
script_path, dir_fujita, "Fujita_parameters.tsv"
)
result = subprocess.run(["petablint", "-v", "-p", parameter_file]) # noqa: S603,S607
assert result.returncode == 0
def test_assert_measurement_conditions_present_in_condition_table():
condition_df = pd.DataFrame(
data={
CONDITION_ID: ["condition1", "condition2"],
CONDITION_NAME: ["", "Condition 2"],
"fixedParameter1": [1.0, 2.0],
}
)
condition_df.set_index(CONDITION_ID, inplace=True)
measurement_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["", ""],
SIMULATION_CONDITION_ID: ["condition1", "condition1"],
TIME: [1.0, 2.0],
MEASUREMENT: [1.0, 2.0],
OBSERVABLE_PARAMETERS: ["", ""],
NOISE_PARAMETERS: ["", ""],
}
)
# check we can handle missing preeq condition
lint.assert_measurement_conditions_present_in_condition_table(
measurement_df=measurement_df, condition_df=condition_df
)
# check we can handle preeq condition
measurement_df[PREEQUILIBRATION_CONDITION_ID] = [
"condition1",
"condition2",
]
lint.assert_measurement_conditions_present_in_condition_table(
measurement_df=measurement_df, condition_df=condition_df
)
# check we detect missing condition
measurement_df[PREEQUILIBRATION_CONDITION_ID] = [
"missing_condition1",
"missing_condition2",
]
with pytest.raises(AssertionError):
lint.assert_measurement_conditions_present_in_condition_table(
measurement_df=measurement_df, condition_df=condition_df
)
def test_check_condition_df():
"""Check that we correctly detect errors in condition table"""
from petab.models.sbml_model import SbmlModel
model = SbmlModel.from_antimony("")
condition_df = pd.DataFrame(
data={
CONDITION_ID: ["condition1"],
"p1": [nan],
}
)
condition_df.set_index(CONDITION_ID, inplace=True)
# parameter missing in model
with pytest.raises(AssertionError):
lint.check_condition_df(condition_df, model)
# fix by adding output parameter
observable_df = pd.DataFrame(
{
OBSERVABLE_ID: ["obs1"],
OBSERVABLE_FORMULA: ["p1"],
}
)
lint.check_condition_df(condition_df, model, observable_df)
# fix by adding parameter
model = SbmlModel.from_antimony("p1 = 1")
lint.check_condition_df(condition_df, model)
# species missing in model
condition_df["s1"] = [3.0]
with pytest.raises(AssertionError):
lint.check_condition_df(condition_df, model)
# fix:
model = SbmlModel.from_antimony("p1 = 1; species s1 = 1")
lint.check_condition_df(condition_df, model)
# compartment missing in model
condition_df["c2"] = [4.0]
with pytest.raises(AssertionError):
lint.check_condition_df(condition_df, model)
# fix:
model = SbmlModel.from_antimony(
"p1 = 1; species s1 = 1; compartment c2 = 1"
)
lint.check_condition_df(condition_df, model)
def test_check_ids():
"""Test check_ids"""
lint.check_ids(["a1", "_1"])
with pytest.raises(ValueError):
lint.check_ids(["1"])
def test_check_parameter_df():
"""Check lint.check_parameter_df."""
parameter_df = pd.DataFrame(
{
PARAMETER_ID: ["par0", "par1", "par2"],
PARAMETER_SCALE: [LOG10, LOG10, LIN],
NOMINAL_VALUE: [1e-2, 1e-3, 1e-4],
ESTIMATE: [1, 1, 0],
LOWER_BOUND: [1e-5, 1e-6, 1e-7],
UPPER_BOUND: [1e5, 1e6, 1e7],
}
).set_index(PARAMETER_ID)
parameter_df[NOMINAL_VALUE] = parameter_df[NOMINAL_VALUE].astype("object")
lint.check_parameter_df(df=parameter_df)
# NOMINAL_VALUE empty, for non-estimated parameter
parameter_df.loc["par2", NOMINAL_VALUE] = ""
with pytest.raises(AssertionError):
lint.check_parameter_df(df=parameter_df)
# NOMINAL_VALUE column missing, but non-estimated parameter
del parameter_df[NOMINAL_VALUE]
with pytest.raises(AssertionError):
lint.check_parameter_df(df=parameter_df)
def test_check_observable_df():
"""Check that we correctly detect errors in observable table"""
observable_df = pd.DataFrame(
data={
OBSERVABLE_ID: ["obs1", "obs2"],
OBSERVABLE_FORMULA: ["x1", "x2"],
NOISE_FORMULA: ["sigma1", "sigma2"],
}
).set_index(OBSERVABLE_ID)
lint.check_observable_df(observable_df)
# Check that duplicated observables ids are detected
bad_observable_df = observable_df.copy()
bad_observable_df.index = ["obs1", "obs1"]
with pytest.raises(AssertionError):
lint.check_observable_df(bad_observable_df)
# Check that missing noiseFormula is detected
bad_observable_df = observable_df.copy()
bad_observable_df.loc["obs1", NOISE_FORMULA] = nan
with pytest.raises(AssertionError):
lint.check_observable_df(bad_observable_df)
def test_condition_ids_are_unique():
condition_df = pd.DataFrame(
data={
CONDITION_ID: ["condition1", "condition1"],
"parameter1": [1.0, 2.0],
}
)
condition_df.set_index(CONDITION_ID, inplace=True)
with pytest.raises(AssertionError):
lint.check_condition_df(condition_df)
condition_df.index = ["condition0", "condition1"]
condition_df.index.name = "conditionId"
lint.check_condition_df(condition_df)
def test_parameter_ids_are_unique():
parameter_df = pd.DataFrame(
{
PARAMETER_ID: ["par0", "par0"],
PARAMETER_SCALE: [LIN, LIN],
ESTIMATE: [1, 1],
LOWER_BOUND: [1e-5, 1e-6],
UPPER_BOUND: [1e5, 1e6],
}
).set_index(PARAMETER_ID)
with pytest.raises(AssertionError):
lint.check_parameter_df(parameter_df)
parameter_df.index = ["par0", "par1"]
parameter_df.index.name = "parameterId"
lint.check_parameter_df(parameter_df)
def test_check_positive_bounds_for_scaled_parameters():
parameter_df = pd.DataFrame(
{
PARAMETER_ID: ["par"],
PARAMETER_SCALE: [LOG10],
ESTIMATE: [1],
LOWER_BOUND: [0.0],
UPPER_BOUND: [1],
}
).set_index(PARAMETER_ID)
with pytest.raises(AssertionError, match="positive"):
lint.check_parameter_df(parameter_df)