|
11 | 11 | from contextlib import contextmanager |
12 | 12 | import os |
13 | 13 | import shutil |
| 14 | +import json |
14 | 15 |
|
| 16 | +import numpy as np |
| 17 | +import numpy.testing as npt |
15 | 18 | import pytest |
16 | 19 | import pytest_check as check |
17 | 20 |
|
@@ -215,3 +218,56 @@ def test_get_tails(): |
215 | 218 | check.equal(len(tails), 4) |
216 | 219 | check.is_in("sn-1 1", tails) |
217 | 220 | check.is_in("sn-2 1", tails) |
| 221 | + |
| 222 | + |
| 223 | +def test_json_encoder(tmpdir): |
| 224 | + """Fixture for a mock experiment path with no data files.""" |
| 225 | + from fairmd.lipids.auxiliary import CompactJSONEncoder |
| 226 | + |
| 227 | + exp_dir = tmpdir.mkdir("jsonenc") |
| 228 | + |
| 229 | + data = { |
| 230 | + "compact_object": {"first": "element", "second": 2}, |
| 231 | + "compact_list": ["first", "second"], |
| 232 | + "long_list": [ |
| 233 | + "this", |
| 234 | + "is", |
| 235 | + "a", |
| 236 | + "rather", |
| 237 | + "long\nlist", |
| 238 | + "and should be broken up because of its width", |
| 239 | + ], |
| 240 | + "non_ascii": "汉语", |
| 241 | + 1: 2, |
| 242 | + } |
| 243 | + with open(exp_dir.join("test.yaml"), "w", encoding="utf-8") as fd: |
| 244 | + json.dump(data, fd, cls=CompactJSONEncoder, ensure_ascii=False) |
| 245 | + |
| 246 | + |
| 247 | +def test_average_block(): |
| 248 | + from fairmd.lipids.auxiliary import block_average_time_series |
| 249 | + |
| 250 | + # times 0..9, values = times |
| 251 | + t = np.arange(10, dtype=float) |
| 252 | + x = t.copy() |
| 253 | + arr = np.column_stack((t, x)) |
| 254 | + |
| 255 | + out = block_average_time_series(arr, blocksize=2.0) |
| 256 | + # expected bins: [0,2), [2,4), [4,6), [6,8), [8,10] |
| 257 | + expected_times = np.array([1, 3, 5, 7, 9], dtype=float) |
| 258 | + expected_vals = np.array( |
| 259 | + [ |
| 260 | + (0 + 1) / 2, |
| 261 | + (2 + 3) / 2, |
| 262 | + (4 + 5) / 2, |
| 263 | + (6 + 7) / 2, |
| 264 | + (8 + 9) / 2, |
| 265 | + ], |
| 266 | + dtype=float, |
| 267 | + ) |
| 268 | + |
| 269 | + npt.assert_allclose(out[:, 0], expected_times) |
| 270 | + npt.assert_allclose(out[:, 1], expected_vals) |
| 271 | + |
| 272 | + arr = arr[:-1, :] |
| 273 | + out = block_average_time_series(arr, blocksize=2.0) |
0 commit comments