|
1 | 1 | import copy |
2 | 2 | import pickle |
3 | 3 | import tempfile |
| 4 | +from io import StringIO |
4 | 5 | from math import nan |
5 | 6 | from pathlib import Path |
| 7 | +from tempfile import TemporaryDirectory |
6 | 8 |
|
7 | 9 | import libsbml |
8 | 10 | import numpy as np |
9 | 11 | import pandas as pd |
10 | 12 | import petab |
11 | 13 | import pytest |
12 | 14 | from petab.C import * |
| 15 | +from yaml import safe_load |
13 | 16 |
|
14 | 17 |
|
15 | 18 | @pytest.fixture |
@@ -452,6 +455,33 @@ def test_concat_measurements(): |
452 | 455 | petab.measurements.get_measurement_df)) |
453 | 456 |
|
454 | 457 |
|
| 458 | +def test_concat_condition_df(): |
| 459 | + df1 = pd.DataFrame(data={ |
| 460 | + CONDITION_ID: ['condition1', 'condition2'], |
| 461 | + 'par1': [1.1, 1.2], |
| 462 | + 'par2': [2.1, 2.2], |
| 463 | + 'par3': [3.1, 3.2] |
| 464 | + }).set_index(CONDITION_ID) |
| 465 | + |
| 466 | + assert df1.equals(petab.concat_tables(df1, petab.get_condition_df)) |
| 467 | + |
| 468 | + df2 = pd.DataFrame(data={ |
| 469 | + CONDITION_ID: ['condition3'], |
| 470 | + 'par1': [1.3], |
| 471 | + 'par2': [2.3], |
| 472 | + }).set_index(CONDITION_ID) |
| 473 | + |
| 474 | + df_expected = pd.DataFrame(data={ |
| 475 | + CONDITION_ID: ['condition1', 'condition2', 'condition3'], |
| 476 | + 'par1': [1.1, 1.2, 1.3], |
| 477 | + 'par2': [2.1, 2.2, 2.3], |
| 478 | + 'par3': [3.1, 3.2, np.nan], |
| 479 | + }).set_index(CONDITION_ID) |
| 480 | + assert df_expected.equals( |
| 481 | + petab.concat_tables((df1, df2), petab.get_condition_df) |
| 482 | + ) |
| 483 | + |
| 484 | + |
455 | 485 | def test_get_observable_ids(petab_problem): # pylint: disable=W0621 |
456 | 486 | """Test if observable ids functions returns correct value.""" |
457 | 487 | assert set(petab_problem.get_observable_ids()) == {'observable_1'} |
@@ -535,3 +565,68 @@ def test_load_remote(): |
535 | 565 | assert petab_problem.sbml_model is not None |
536 | 566 | assert petab_problem.measurement_df is not None \ |
537 | 567 | and not petab_problem.measurement_df.empty |
| 568 | + |
| 569 | + |
| 570 | +def test_problem_from_yaml_v1_empty(): |
| 571 | + """Test loading PEtab version 1 yaml without any files""" |
| 572 | + yaml_config = """ |
| 573 | + format_version: 1 |
| 574 | + parameter_file: |
| 575 | + problems: |
| 576 | + - condition_files: [] |
| 577 | + measurement_files: [] |
| 578 | + observable_files: [] |
| 579 | + sbml_files: [] |
| 580 | + """ |
| 581 | + yaml_config = safe_load(StringIO(yaml_config)) |
| 582 | + petab.Problem.from_yaml(yaml_config) |
| 583 | + |
| 584 | + |
| 585 | +def test_problem_from_yaml_v1_multiple_files(): |
| 586 | + """Test loading PEtab version 1 yaml with multiple condition / measurement |
| 587 | + / observable files""" |
| 588 | + yaml_config = """ |
| 589 | + format_version: 1 |
| 590 | + parameter_file: |
| 591 | + problems: |
| 592 | + - condition_files: [conditions1.tsv, conditions2.tsv] |
| 593 | + measurement_files: [measurements1.tsv, measurements2.tsv] |
| 594 | + observable_files: [observables1.tsv, observables2.tsv] |
| 595 | + sbml_files: [] |
| 596 | + """ |
| 597 | + |
| 598 | + with TemporaryDirectory() as tmpdir: |
| 599 | + yaml_path = Path(tmpdir, "problem.yaml") |
| 600 | + with open(yaml_path, 'w') as f: |
| 601 | + f.write(yaml_config) |
| 602 | + |
| 603 | + for i in (1, 2): |
| 604 | + condition_df = pd.DataFrame({ |
| 605 | + CONDITION_ID: [f"condition{i}"], |
| 606 | + }) |
| 607 | + condition_df.set_index([CONDITION_ID], inplace=True) |
| 608 | + petab.write_condition_df(condition_df, |
| 609 | + Path(tmpdir, f"conditions{i}.tsv")) |
| 610 | + |
| 611 | + measurement_df = pd.DataFrame({ |
| 612 | + SIMULATION_CONDITION_ID: [f"condition{i}"], |
| 613 | + OBSERVABLE_ID: [f"observable{i}"], |
| 614 | + TIME: [i], |
| 615 | + MEASUREMENT: [1] |
| 616 | + }) |
| 617 | + petab.write_measurement_df(measurement_df, |
| 618 | + Path(tmpdir, f"measurements{i}.tsv")) |
| 619 | + |
| 620 | + observables_df = pd.DataFrame({ |
| 621 | + OBSERVABLE_ID: [f"observable{i}"], |
| 622 | + OBSERVABLE_FORMULA: [1], |
| 623 | + NOISE_FORMULA: [1], |
| 624 | + }) |
| 625 | + petab.write_observable_df(observables_df, |
| 626 | + Path(tmpdir, f"observables{i}.tsv")) |
| 627 | + |
| 628 | + petab_problem = petab.Problem.from_yaml(yaml_path) |
| 629 | + |
| 630 | + assert petab_problem.measurement_df.shape[0] == 2 |
| 631 | + assert petab_problem.observable_df.shape[0] == 2 |
| 632 | + assert petab_problem.condition_df.shape[0] == 2 |
0 commit comments