Coverage for pySDC / helpers / testing.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 07:06 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 07:06 +0000
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Created on Fri Feb 9 14:41:43 2024
6Helpers module for testing utilities
7"""
9import os
10import json
11import warnings
12import numpy as np
15class DataChecker:
16 """
17 Object allowing to quickly generate and check generated data from scripts.
19 Here is an example of use for one given script:
21 >>> from pySDC.helpers.testing import DataChecker
22 >>>
23 >>> data = DataChecker(__file__)
24 >>> # ... some computations ...
25 >>> data.storeAndCheck('solution', solution)
26 >>> # ... some other computations ...
27 >>> data.storeAndCheck('errors', errors)
28 >>> # ... ploting some figures
29 >>> data.writeToJSON() # end of script
31 The `storeAndCheck` method use a unique key as first argument, and the
32 data as second argument, into list format.
33 Calling this method will store the data into a cache variable, and compare
34 with reference data stored in `_dataRef.json` (if it exists, see below ...).
35 Finally, the `writeToJSON` method saves the cache variable into a json
36 file `_data.json`.
38 When there is no `_dataRef.json` in the script directory,
39 executing the script output some warning telling that there is no
40 reference data to compare with.
41 To remove those warnings (and properly test data), just rename the
42 `_data.json` into `_dataRef.json`.
43 Then re-running the script will then compare the newly generated data with
44 the reference data stored into `_dataRef.json`, and raise an error if there
45 is some differences.
47 Important
48 ---------
50 - using the `__file__` built-in variable is necessary to save data in the
51 script directory, independently of the working directory.
52 - several script in the same directory can use the DataChecker, which
53 implies that the key provided to `storeAndCheck` must be unique across
54 all directory scripts. If not, the same key from previous directory will
55 simply be overwritten in the cached variable and the `_data.ref` file.
56 """
58 def __init__(self, filePath):
59 path = '/' + os.path.join(*filePath.split('/')[:-1])
60 self._data = {} # cache for data
61 self._dataRef = None # cache for reference data
62 self._dataFile = os.path.join(path, '_data.json')
63 self._dataRefFile = os.path.join(path, '_dataRef.json')
65 def storeAndCheck(self, key, data, rtol=1e-5, atol=1e-8):
66 """
67 Store data into cache, and check with eventual reference data
69 Parameters
70 ----------
71 key : str
72 Unique key (project wide) for the data.
73 data : list or array-like
74 The data that has to be stored.
75 rtol : float
76 Relative tolerance
77 atol : float
78 Absolute tolerance
79 """
80 self._data[key] = list(data)
81 if self._dataRef is None:
82 try:
83 with open(self._dataRefFile, "r") as f:
84 self._dataRef = json.load(f)
85 except FileNotFoundError:
86 warnings.warn(f"no reference data to check key:{key}")
87 return # pragma: no cover
89 assert key in self._dataRef, f"key:{key} not in reference data"
91 data = self._data[key]
92 ref = self._dataRef[key]
94 assert len(data) == len(ref), f"data with len:{len(data)}) cannot be compared to ref with len:{len(ref)})"
95 assert np.allclose(
96 data, ref, atol=atol, rtol=rtol, equal_nan=True
97 ), f"{key}, difference between data:{data} and ref:{ref}"
99 def writeToJSON(self):
100 """Write cached data into a json file"""
101 if os.path.isfile(self._dataFile):
102 with open(self._dataFile, "r") as f:
103 self._data.update(json.load(f))
104 with open(self._dataFile, "w") as f:
105 json.dump(self._data, f)