Skip to content

Commit

Permalink
almost there [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
drammock committed Oct 2, 2024
1 parent 8072b3b commit ae62752
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions mne_bids_pipeline/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

0 comments on commit ae62752

Please sign in to comment.