Skip to content

Commit fffe29f

Browse files
authored
MAINT: Refactor _get_raw_paths (mne-tools#749)
1 parent 1fb04a9 commit fffe29f

File tree

15 files changed

+566
-557
lines changed

15 files changed

+566
-557
lines changed

docs/source/settings/general.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- eeg_template_montage
2929
- drop_channels
3030
- reader_extra_params
31+
- read_raw_bids_verbose
3132
- analyze_channels
3233
- plot_psd_for_runs
3334
- n_jobs

docs/source/v1.4.md.inc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
### :new: New features & enhancements
44

55
- Add movement compensation and cHPI filtering to the Maxwell filtering step, along with additional configuration options (#747 by @larsoner)
6-
- Add option to specify `ssp_ecg_channel` to override the default value (#747 by @larsoner)
6+
- Add option to specify [`ssp_ecg_channel`]([mne_bids_pipeline._config.ssp_ecg_channel) to override the default value (#747 by @larsoner)
7+
- Add option [`read_raw_bids_verbose`]([mne_bids_pipeline._config.read_raw_bids_verbose) to set the verbosity level when using `read_raw_bids` to suppress known warnings (#749 by @larsoner)
78

89
[//]: # (### :warning: Behavior changes)
910

1011
[//]: # (- Whatever (#000 by @whoever))
1112

1213
### :medical_symbol: Code health
1314

14-
- Refactor code to deduplicate keyword-passing (#746 by @larsoner)
15+
- Refactor code to deduplicate keyword-passing and improve internal logic (#746, #749 by @larsoner)
1516

1617
### :bug: Bug fixes
1718

18-
- Fix bug when `mf_reference_run != runs[0]` (#742 by @larsoner)
19+
- Fix bug when [`mf_reference_run != runs[0]`]([mne_bids_pipeline._config.mf_reference_run) (#742 by @larsoner)
1920
- Fix bug with too many JSON files found during empty room matching (#743 by @allermat)
2021
- Fix bug with outdated info on ch_types config option (#745 by @allermat)
2122
- Fix bug where SSP projectors were not added to the report (#747 by @larsoner)

mne_bids_pipeline/_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,14 @@
416416
```
417417
"""
418418

419+
read_raw_bids_verbose: Optional[Literal["error"]] = None
420+
"""
421+
Verbosity level to pass to `read_raw_bids(..., verbose=read_raw_bids_verbose)`.
422+
If you know your dataset will contain files that are not perfectly BIDS
423+
compliant (e.g., "Did not find any meg.json..."), you can set this to
424+
`'error'` to suppress warnings emitted by read_raw_bids.
425+
"""
426+
419427
###############################################################################
420428
# BREAK DETECTION
421429
# ---------------

mne_bids_pipeline/_config_utils.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,6 @@ def get_sessions(config: SimpleNamespace) -> Union[List[None], List[str]]:
118118
return sessions
119119

120120

121-
@functools.lru_cache(maxsize=None)
122-
def _get_runs_all_subjects_cached(
123-
**config_dict: Dict[str, Any],
124-
) -> Dict[str, Union[List[None], List[str]]]:
125-
config = SimpleNamespace(**config_dict)
126-
# Sometimes we check list equivalence for ch_types, so convert it back
127-
config.ch_types = list(config.ch_types)
128-
return get_runs_all_subjects(config)
129-
130-
131121
def get_runs_all_subjects(
132122
config: SimpleNamespace,
133123
) -> Dict[str, Union[List[None], List[str]]]:
@@ -139,7 +129,26 @@ def get_runs_all_subjects(
139129
for each subject asked in the configuration file
140130
(and not for each subject present in the bids_path).
141131
"""
142-
# We cannot use get_subjects() because if there is just one subject
132+
# Use caching under the hood for speed
133+
return copy.deepcopy(
134+
_get_runs_all_subjects_cached(
135+
bids_root=config.bids_root,
136+
data_type=config.data_type,
137+
ch_types=tuple(config.ch_types),
138+
subjects=tuple(config.subjects) if config.subjects != "all" else "all",
139+
exclude_subjects=tuple(config.exclude_subjects),
140+
exclude_runs=tuple(config.exclude_runs) if config.exclude_runs else None,
141+
)
142+
)
143+
144+
145+
@functools.lru_cache(maxsize=None)
146+
def _get_runs_all_subjects_cached(
147+
**config_dict: Dict[str, Any],
148+
) -> Dict[str, Union[List[None], List[str]]]:
149+
config = SimpleNamespace(**config_dict)
150+
# Sometimes we check list equivalence for ch_types, so convert it back
151+
config.ch_types = list(config.ch_types)
143152
subj_runs = dict()
144153
for subject in get_subjects(config):
145154
# Only traverse through the current subject's directory
@@ -193,15 +202,7 @@ def get_runs(
193202
return [None]
194203

195204
runs = copy.deepcopy(config.runs)
196-
197-
subj_runs = _get_runs_all_subjects_cached(
198-
bids_root=config.bids_root,
199-
data_type=config.data_type,
200-
ch_types=tuple(config.ch_types),
201-
subjects=tuple(config.subjects) if config.subjects != "all" else "all",
202-
exclude_subjects=tuple(config.exclude_subjects),
203-
exclude_runs=tuple(config.exclude_runs) if config.exclude_runs else None,
204-
)
205+
subj_runs = get_runs_all_subjects(config=config)
205206
valid_runs = subj_runs[subject]
206207

207208
if len(get_subjects(config)) > 1:
@@ -239,24 +240,28 @@ def get_runs_tasks(
239240
config: SimpleNamespace,
240241
subject: str,
241242
session: Optional[str],
243+
include_noise: bool = True,
242244
) -> List[Tuple[str]]:
243245
"""Get (run, task) tuples for all runs plus (maybe) rest."""
244-
from ._import_data import _get_bids_path_in
246+
from ._import_data import _get_noise_path, _get_rest_path
245247

246248
runs = get_runs(config=config, subject=subject)
247-
tasks = [config.task] * len(runs)
248-
if config.process_rest and not config.task_is_rest:
249-
run, task = None, "rest"
250-
bids_path_in = _get_bids_path_in(
251-
cfg=config,
252-
subject=subject,
253-
session=session,
254-
run=run,
255-
task=task,
256-
)
257-
if bids_path_in.fpath.exists():
249+
tasks = [get_task(config=config)] * len(runs)
250+
kwargs = dict(
251+
cfg=config,
252+
subject=subject,
253+
session=session,
254+
kind="orig",
255+
add_bads=False,
256+
)
257+
if _get_rest_path(**kwargs):
258+
runs.append(None)
259+
tasks.append("rest")
260+
if include_noise:
261+
mf_reference_run = get_mf_reference_run(config=config)
262+
if _get_noise_path(mf_reference_run=mf_reference_run, **kwargs):
258263
runs.append(None)
259-
tasks.append("rest")
264+
tasks.append("noise")
260265
return tuple(zip(runs, tasks))
261266

262267

@@ -600,3 +605,7 @@ def _bids_kwargs(*, config: SimpleNamespace) -> dict:
600605
bids_root=config.bids_root,
601606
deriv_root=config.deriv_root,
602607
)
608+
609+
610+
def _do_mf_autobad(*, cfg: SimpleNamespace) -> bool:
611+
return cfg.find_noisy_channels_meg or cfg.find_flat_channels_meg

0 commit comments

Comments
 (0)