Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow missing sessions #1000

Merged
merged 17 commits into from
Oct 4, 2024
6 changes: 6 additions & 0 deletions mne_bids_pipeline/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
BIDS dataset.
"""

allow_missing_sessions: bool = False
"""
Whether to continue processing the dataset if some combinations of `subjects` and
`sessions` are missing.
"""

task: str = ""
"""
The task to process.
Expand Down
25 changes: 25 additions & 0 deletions mne_bids_pipeline/_config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ def get_sessions(config: SimpleNamespace) -> list[None] | list[str]:
return sessions


def get_subjects_sessions(config: SimpleNamespace) -> dict[str, list[str]]:
subj_sessions = dict()
all_sessions = _get_entity_vals_cached(
config.bids_root,
entity_key="session",
ignore_datatypes=_get_ignore_datatypes(config),
)
for subject in get_subjects(config):
# Only traverse through the current subject's directory
valid_sessions_subj = _get_entity_vals_cached(
config.bids_root / f"sub-{subject}",
entity_key="session",
ignore_datatypes=_get_ignore_datatypes(config),
)
missing_sessions = set(all_sessions) - set(valid_sessions_subj)
if missing_sessions and not config.allow_missing_sessions:
raise RuntimeError(
f"Subject {subject} is missing session{_pl(missing_sessions)} "
f"{tuple(sorted(missing_sessions))}, and "
"`config.allow_missing_sessions` is False"
)
subj_sessions[subject] = valid_sessions_subj
return subj_sessions


def get_runs_all_subjects(
config: SimpleNamespace,
) -> dict[str, list[None] | list[str]]:
Expand Down
6 changes: 3 additions & 3 deletions mne_bids_pipeline/steps/init/_01_init_derivatives_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mne_bids.config import BIDS_VERSION
from mne_bids.utils import _write_json

from mne_bids_pipeline._config_utils import _bids_kwargs, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import _bids_kwargs, get_subjects_sessions
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._run import _prep_out_files, failsafe_run

Expand Down Expand Up @@ -76,8 +76,8 @@ def main(*, config):
init_dataset(cfg=get_config(config=config), exec_params=config.exec_params)
# Don't bother with parallelization here as I/O operations are generally
# not well parallelized (and this should be very fast anyway)
for subject in get_subjects(config):
for session in get_sessions(config):
for subject, sessions in get_subjects_sessions(config).items():
for session in sessions:
init_subject_dirs(
cfg=get_config(
config=config,
Expand Down
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/init/_02_find_empty_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
_pl,
get_datatype,
get_mf_reference_run,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import _empty_room_match_path
from mne_bids_pipeline._io import _write_json
Expand Down Expand Up @@ -127,8 +126,8 @@ def main(*, config) -> None:
# This will be I/O bound if the sidecar is not complete, so let's not run
# in parallel.
logs = list()
for subject in get_subjects(config):
for session in get_sessions(config):
for subject, sessions in get_subjects_sessions(config).items():
for session in sessions:
run = get_mf_reference_run(config=config)
logs.append(
find_empty_room(
Expand Down
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/preprocessing/_01_data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
get_mf_cal_fname,
get_mf_ctc_fname,
get_runs_tasks,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import (
_bads_path,
Expand Down Expand Up @@ -349,8 +348,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
6 changes: 3 additions & 3 deletions mne_bids_pipeline/steps/preprocessing/_02_head_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import mne

from mne_bids_pipeline._config_utils import get_runs_tasks, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import get_runs_tasks, get_subjects_sessions
from mne_bids_pipeline._import_data import (
_get_run_rest_noise_path,
_import_data_kwargs,
Expand Down Expand Up @@ -173,8 +173,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
11 changes: 5 additions & 6 deletions mne_bids_pipeline/steps/preprocessing/_03_maxfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
get_mf_cal_fname,
get_mf_ctc_fname,
get_runs_tasks,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import (
_get_mf_reference_run_path,
Expand Down Expand Up @@ -613,8 +612,8 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)

# Second: maxwell_filter
Expand All @@ -637,8 +636,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
6 changes: 3 additions & 3 deletions mne_bids_pipeline/steps/preprocessing/_04_frequency_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mne.io.pick import _picks_to_idx
from mne.preprocessing import EOGRegression

from mne_bids_pipeline._config_utils import get_runs_tasks, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import get_runs_tasks, get_subjects_sessions
from mne_bids_pipeline._import_data import (
_get_run_rest_noise_path,
_import_data_kwargs,
Expand Down Expand Up @@ -328,8 +328,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
6 changes: 3 additions & 3 deletions mne_bids_pipeline/steps/preprocessing/_05_regress_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mne.io.pick import _picks_to_idx
from mne.preprocessing import EOGRegression

from mne_bids_pipeline._config_utils import get_runs_tasks, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import get_runs_tasks, get_subjects_sessions
from mne_bids_pipeline._import_data import (
_get_run_rest_noise_path,
_import_data_kwargs,
Expand Down Expand Up @@ -159,8 +159,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/preprocessing/_06a1_fit_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
_bids_kwargs,
get_eeg_reference,
get_runs,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import annotations_to_events, make_epochs
from mne_bids_pipeline._logging import gen_log_kwargs, logger
Expand Down Expand Up @@ -377,7 +376,7 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
_bids_kwargs,
get_eeg_reference,
get_runs,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._parallel import get_parallel_backend, parallel_func
Expand Down Expand Up @@ -394,7 +393,7 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/preprocessing/_06b_run_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
_pl,
_proj_path,
get_runs,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._parallel import get_parallel_backend, parallel_func
Expand Down Expand Up @@ -279,7 +278,7 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/preprocessing/_07_make_epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
_bids_kwargs,
get_eeg_reference,
get_runs,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import annotations_to_events, make_epochs
from mne_bids_pipeline._logging import gen_log_kwargs, logger
Expand Down Expand Up @@ -355,7 +354,7 @@ def main(*, config) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
10 changes: 5 additions & 5 deletions mne_bids_pipeline/steps/preprocessing/_08a_apply_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from mne.preprocessing import read_ica
from mne_bids import BIDSPath

from mne_bids_pipeline._config_utils import get_runs_tasks, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import get_runs_tasks, get_subjects_sessions
from mne_bids_pipeline._import_data import _get_run_rest_noise_path, _import_data_kwargs
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._parallel import get_parallel_backend, parallel_func
Expand Down Expand Up @@ -273,8 +273,8 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
# Raw
parallel, run_func = parallel_func(
Expand All @@ -292,8 +292,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
11 changes: 5 additions & 6 deletions mne_bids_pipeline/steps/preprocessing/_08b_apply_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from mne_bids_pipeline._config_utils import (
_proj_path,
get_runs_tasks,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._import_data import _get_run_rest_noise_path, _import_data_kwargs
from mne_bids_pipeline._logging import gen_log_kwargs, logger
Expand Down Expand Up @@ -183,8 +182,8 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
# Raw
parallel, run_func = parallel_func(
Expand All @@ -202,8 +201,8 @@ def main(*, config: SimpleNamespace) -> None:
run=run,
task=task,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
for run, task in get_runs_tasks(
config=config,
subject=subject,
Expand Down
6 changes: 3 additions & 3 deletions mne_bids_pipeline/steps/preprocessing/_09_ptp_reject.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import numpy as np
from mne_bids import BIDSPath

from mne_bids_pipeline._config_utils import _bids_kwargs, get_sessions, get_subjects
from mne_bids_pipeline._config_utils import _bids_kwargs, get_subjects_sessions
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._parallel import get_parallel_backend, parallel_func
from mne_bids_pipeline._reject import _get_reject
Expand Down Expand Up @@ -275,7 +275,7 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
7 changes: 3 additions & 4 deletions mne_bids_pipeline/steps/sensor/_01_make_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
_restrict_analyze_channels,
get_all_contrasts,
get_eeg_reference,
get_sessions,
get_subjects,
get_subjects_sessions,
)
from mne_bids_pipeline._logging import gen_log_kwargs, logger
from mne_bids_pipeline._parallel import get_parallel_backend, parallel_func
Expand Down Expand Up @@ -198,7 +197,7 @@ def main(*, config: SimpleNamespace) -> None:
subject=subject,
session=session,
)
for subject in get_subjects(config)
for session in get_sessions(config)
for subject, sessions in get_subjects_sessions(config).items()
for session in sessions
)
save_logs(config=config, logs=logs)
Loading
Loading