From ae6275237558cd68895eeb768668193efd81106a Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 2 Oct 2024 15:34:40 -0500 Subject: [PATCH] almost there [ci skip] --- mne_bids_pipeline/tests/test_run.py | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/mne_bids_pipeline/tests/test_run.py b/mne_bids_pipeline/tests/test_run.py index 952be5f13..069fccf02 100644 --- a/mne_bids_pipeline/tests/test_run.py +++ b/mne_bids_pipeline/tests/test_run.py @@ -4,6 +4,7 @@ import shutil import sys from collections.abc import Collection +from contextlib import nullcontext from pathlib import Path from typing import TypedDict @@ -204,3 +205,66 @@ def test_run(dataset, monkeypatch, dataset_test, capsys, tmp_path): with capsys.disabled(): print() main() + + +@pytest.mark.parametrize("allow_missing_sessions", (False, True)) +def test_missing_sessions(tmp_path, monkeypatch, capsys, allow_missing_sessions): + """Test the `allow_missing_sessions` config variable.""" + dataset = "ds001810" + # exclude all except 1 session ("ses-anodalpost") + exclude = ( + "ses-anodalpre", + "ses-anodaltDCS", + "ses-cathodalpost", + "ses-cathodalpre", + "ses-cathodaltDCS", + ) + # subset the dataset by symlinking in a temp dir + src_dir = Path(f"~/mne_data/{dataset}").expanduser() + link_dir = tmp_path / dataset + link_dir.mkdir() + for dirpath, dirnames, filenames in src_dir.walk(): + if any(excl in dirpath.parts for excl in exclude): + continue + for dirname in dirnames: + if dirname not in exclude: + target = (dirpath / dirname).absolute() + link = link_dir / target.relative_to(src_dir) + link.mkdir() + for fname in filenames: + target = (dirpath / fname).absolute() + link = link_dir / target.relative_to(src_dir) + link.symlink_to(target) + # fake a config file + config = dict( + bids_root=f'"{link_dir}"', + deriv_root=f'"{tmp_path / "derivatives" / "mne-bids-pipeline" / dataset}"', + interactive=False, + subjects=["01"], + sessions=["ses-anodalpost", "ses-anodalpre"], + ch_types=["eeg"], + conditions=["61450", "61511"], + allow_missing_sessions=allow_missing_sessions, + ) + config_fname = "config_ds001810_missing_session.py" + config_path = tmp_path / config_fname + with open(config_path, "w") as fid: + for key, val in config.items(): + fid.write(f"{key} = {val}\n") + # set up the context handler + context = ( + nullcontext() + if allow_missing_sessions + else pytest.raises(RuntimeError, match="is missing session ('ses-anodalpre',)") + ) + # run + steps = ("init/_01_init_derivatives_dir",) + command = ["mne_bids_pipeline", str(config_path), f'--steps={",".join(steps)}'] + if "--pdb" in sys.argv: + command.append("--n_jobs=1") + monkeypatch.setenv("_MNE_BIDS_STUDY_TESTING", "true") + monkeypatch.setattr(sys, "argv", command) + with capsys.disabled(): + print() + with context: + main()