|
18 | 18 | ) |
19 | 19 | from mne.transforms import apply_trans, _get_trans |
20 | 20 | from mne._fiff.constants import FIFF |
| 21 | +from mne.utils import catch_logging |
21 | 22 |
|
22 | 23 |
|
23 | 24 | testing_path = data_path(download=False) |
@@ -471,7 +472,11 @@ def test_annotation_duration_from_stim_groups(): |
471 | 472 |
|
472 | 473 | def test_birthday(tmp_path, monkeypatch): |
473 | 474 | """Test birthday parsing.""" |
474 | | - snirf = pytest.importorskip("snirf") |
| 475 | + try: |
| 476 | + snirf = pytest.importorskip("snirf") |
| 477 | + except AttributeError as exc: |
| 478 | + # Until https://github.com/BUNPC/pysnirf2/pull/43 is released |
| 479 | + pytest.skip(f"snirf import error: {exc}") |
475 | 480 | fname = tmp_path / "test.snirf" |
476 | 481 | with snirf.Snirf(str(fname), "w") as a: |
477 | 482 | a.nirs.appendGroup() |
@@ -503,3 +508,40 @@ def test_birthday(tmp_path, monkeypatch): |
503 | 508 |
|
504 | 509 | raw = read_raw_snirf(fname) |
505 | 510 | assert raw.info["subject_info"]["birthday"] == (1950, 1, 1) |
| 511 | + |
| 512 | + |
| 513 | +@requires_testing_data |
| 514 | +def test_sample_rate_jitter(tmp_path): |
| 515 | + """Test handling of jittered sample times.""" |
| 516 | + from shutil import copy2 |
| 517 | + |
| 518 | + # Create a clean copy and ensure it loads without error |
| 519 | + new_file = tmp_path / "snirf_nirsport2_2019.snirf" |
| 520 | + copy2(snirf_nirsport2_20219, new_file) |
| 521 | + read_raw_snirf(new_file) |
| 522 | + |
| 523 | + # Edit the file and add jitter within tolerance (0.99%) |
| 524 | + with h5py.File(new_file, "r+") as f: |
| 525 | + orig_time = np.array(f.get("nirs/data1/time")) |
| 526 | + acceptable_time_jitter = orig_time.copy() |
| 527 | + average_time_diff = np.mean(np.diff(orig_time)) |
| 528 | + acceptable_time_jitter[-1] += 0.0099 * average_time_diff |
| 529 | + del f["nirs/data1/time"] |
| 530 | + f.flush() |
| 531 | + f.create_dataset("nirs/data1/time", data=acceptable_time_jitter) |
| 532 | + with catch_logging("info") as log: |
| 533 | + read_raw_snirf(new_file) |
| 534 | + lines = "\n".join(line for line in log.getvalue().splitlines() if "jitter" in line) |
| 535 | + assert "Found jitter of 0.9" in lines |
| 536 | + |
| 537 | + # Add jitter of 1.01%, which is greater than allowed tolerance |
| 538 | + with h5py.File(new_file, "r+") as f: |
| 539 | + unacceptable_time_jitter = orig_time |
| 540 | + unacceptable_time_jitter[-1] = unacceptable_time_jitter[-1] + ( |
| 541 | + 0.0101 * average_time_diff |
| 542 | + ) |
| 543 | + del f["nirs/data1/time"] |
| 544 | + f.flush() |
| 545 | + f.create_dataset("nirs/data1/time", data=unacceptable_time_jitter) |
| 546 | + with pytest.warns(RuntimeWarning, match="non-uniformly-sampled data"): |
| 547 | + read_raw_snirf(new_file, verbose=True) |
0 commit comments