Coverage for tests / tests_references / test_spherical_heart.py: 100%
51 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
1# SPDX-FileCopyrightText: Copyright INRIA
2#
3# SPDX-License-Identifier: LGPL-3.0-only
4#
5# Copyright INRIA
6#
7# This file is part of PhysioBlocks, a library mostly developed by the
8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA.
9#
10# Authors:
11# - Colin Drieu
12# - Dominique Chapelle
13# - François Kimmig
14# - Philippe Moireau
15#
16# PhysioBlocks is free software: you can redistribute it and/or modify it under the
17# terms of the GNU Lesser General Public License as published by the Free Software
18# Foundation, version 3 of the License.
19#
20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY
21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
23#
24# You should have received a copy of the GNU Lesser General Public License along with
25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>.
27import logging
28from copy import copy
30import pandas as pd
32from physioblocks.configuration.aliases import unwrap_aliases
33from physioblocks.configuration.functions import load
34from physioblocks.io.configuration import read_json
35from physioblocks.simulation.runtime import ForwardSimulation
36from physioblocks.simulation.time_manager import TIME_QUANTITY_ID
37from physioblocks.utils.gradient_test_utils import gradient_test_from_file
39from .io import read_reference, results_close_to_data
41_logger = logging.getLogger()
42spherical_heart_gradient_path = (
43 "tests/tests_references/spherical_heart/spherical_heart_sim_gradient_test.json"
44)
46spherical_heart_path = "references/spherical_heart_sim.jsonc"
47spherical_heart_reference_path = (
48 "tests/tests_references/spherical_heart/ref_spherical_heart_sim.csv"
49)
51spherical_heart_respiration_path = "references/spherical_heart_respiration_sim.jsonc"
52spherical_heart_respiration_reference_path = (
53 "tests/tests_references/spherical_heart/ref_spherical_heart_respiration_sim.csv"
54)
57def test_spherical_heart_gradient():
58 assert gradient_test_from_file(spherical_heart_gradient_path)
61def test_spherical_heart_ref():
62 sim_config = read_json(spherical_heart_path)
63 sim_config = unwrap_aliases(sim_config)
64 sim: ForwardSimulation = load(sim_config)
65 sim.time_manager.duration = 5.0 # Shorten simulation time to avoid test too long
66 results = sim.run()
68 ref_df = read_reference(spherical_heart_reference_path)
69 ref_df = ref_df.set_index(TIME_QUANTITY_ID)
71 matching_ids = {data_id: data_id for data_id in results[0] if data_id != "time"}
73 tol_factors = copy(sim.magnitudes)
74 tol_factors["cavity.volume"] = 1.0e-3
75 tol_factors["atrial.blood_pressure"] = 1.0e2
76 tol_factors["active_law.activation"] = 1.0
78 results_df = pd.DataFrame(results)
79 results_df = results_df.set_index(TIME_QUANTITY_ID)
81 assert results_close_to_data(
82 results_df,
83 ref_df,
84 matching_ids,
85 1e-9,
86 tol_factors,
87 (sim.time_manager.start, sim.time_manager.start + sim.time_manager.duration),
88 )
91def test_spherical_heart_respiration_ref():
92 sim_config = read_json(spherical_heart_respiration_path)
93 sim_config = unwrap_aliases(sim_config)
94 # Shorten simulation time to avoid test too long
95 sim: ForwardSimulation = load(sim_config)
96 sim.time_manager.duration = 5.0
97 results = sim.run()
99 ref_df = read_reference(spherical_heart_respiration_reference_path)
100 ref_df = ref_df.set_index(TIME_QUANTITY_ID)
102 matching_ids = {data_id: data_id for data_id in results[0] if data_id != "time"}
103 tol_factors = copy(sim.magnitudes)
104 tol_factors["cavity.volume"] = 1.0e-3
105 tol_factors["atrial.blood_pressure"] = 1.0e2
106 tol_factors["active_law.activation"] = 1.0
107 tol_factors["pleural.pressure"] = 1.0e2
109 results_df = pd.DataFrame(results)
110 results_df = results_df.set_index(TIME_QUANTITY_ID)
111 assert results_close_to_data(
112 results_df,
113 ref_df,
114 matching_ids,
115 1e-9,
116 tol_factors,
117 (sim.time_manager.start, sim.time_manager.start + sim.time_manager.duration),
118 )