Coverage for tests / tests_references / io.py: 59%
29 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
29import numpy as np
30import pandas as pd
31import pytest
33_logger = logging.getLogger(__name__)
36def read_reference(reference_file: str) -> pd.DataFrame:
37 data = pd.read_csv(reference_file, sep=";")
38 return data
41def results_close_to_data(
42 results: pd.DataFrame,
43 ref: pd.DataFrame,
44 matching_ids: dict[str, str],
45 tol: float,
46 tol_factors: dict[str, float],
47 interval: tuple[float, float] | None = None,
48) -> bool:
49 # if interval is provided, shorten the dataframes to
50 # the provided interval
51 results_df = results if interval is None else results[interval[0] : interval[1]]
52 ref_df = ref if interval is None else ref[interval[0] : interval[1]]
54 found_differences = False
56 for var_id, data_id in matching_ids.items():
57 result_array = results_df[var_id].to_numpy()
58 ref_array = ref_df[data_id].to_numpy()
60 if result_array != pytest.approx(ref_array, abs=tol * tol_factors[var_id]):
61 found_differences = True
62 message = str.format(
63 "Results differ from reference for outputs: {0}", var_id
64 )
65 _logger.info(message)
66 if result_array.shape != ref_array.shape:
67 _logger.debug(
68 str.format(
69 "Reference and result shape differs: got {0} and {1}",
70 ref_array.shape,
71 result_array.shape,
72 )
73 )
74 else:
75 absolute_error = abs(ref_array - result_array)
76 mean_abs_error = np.mean(absolute_error)
77 median_abs_error = np.median(absolute_error)
78 max_abs_error = absolute_error.max()
79 _logger.debug(str.format("absolute error mean: {0}", mean_abs_error))
80 _logger.debug(
81 str.format("absolute error median: {0}", median_abs_error)
82 )
83 _logger.debug(str.format("absolute error max: {0}", max_abs_error))
85 return not found_differences