Skip to content

Commit

Permalink
MRG: Fix bug with writing ctc/cal for emptyroom (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Nov 8, 2023
1 parent b96c489 commit 61b26aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Detailed list of changes
^^^^^^^^^^^^

- Fix reading when the channel order differs between ``*_channels.tsv`` and the raw data file, which would previously throw an error, by `Richard Höchenberger`_ (:gh:`1171`)
- Fix bug with writing crosstalk and calibration files when subject is ``"emptyroom"``, by `Eric Larson`_ (:gh:`1189`)
- Make ``recording`` entity available for :func:`mne_bids.get_entity_vals`, by `Stefan Appelhoff`_ (:gh:`1182`)

:doc:`Find out what was new in previous releases <whats_new_previous_releases>`
Expand Down
12 changes: 10 additions & 2 deletions mne_bids/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,16 @@ def _check(self):

# perform deeper check if user has it turned on
if self.check:
if self.subject == "emptyroom":
_check_empty_room_basename(self)
_check_empty_room_basename(self)

if (
self.acquisition in ("calibration", "crosstalk")
and self.task is not None
):
raise ValueError(
f'task must be None if the acquisition is "calibration" or '
f'"crosstalk", but received: {self.task}'
)

# ensure extension starts with a '.'
extension = self.extension
Expand Down
13 changes: 12 additions & 1 deletion mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,10 @@ def test_write_meg_calibration(_bids_validate, tmp_path):
write_meg_calibration(calibration=calibration, bids_path=bids_path)
_bids_validate(bids_root)

# subject emptyroom
bids_path_erm = bids_path.copy().update(subject="emptyroom", task="noise")
write_meg_calibration(calibration=fine_cal_fname, bids_path=bids_path_erm)

# Test passing in incompatible dict.
calibration = mne.preprocessing.read_fine_calibration(fine_cal_fname)
del calibration["locs"]
Expand All @@ -2686,7 +2690,7 @@ def test_write_meg_calibration(_bids_validate, tmp_path):
def test_write_meg_crosstalk(_bids_validate, tmp_path):
"""Test writing of the Elekta/Neuromag fine-calibration file."""
bids_root = tmp_path / "bids1"
bids_path = _bids_path.copy().update(root=bids_root)
bids_path = _bids_path.copy().update(root=bids_root, suffix="meg")

raw_fname = op.join(data_path, "MEG", "sample", "sample_audvis_trunc_raw.fif")
raw = _read_raw_fif(raw_fname, verbose=False)
Expand All @@ -2697,6 +2701,13 @@ def test_write_meg_crosstalk(_bids_validate, tmp_path):
write_meg_crosstalk(fname=crosstalk_fname, bids_path=bids_path)
_bids_validate(bids_root)

# subject emptyroom
bids_path_erm = bids_path.copy().update(subject="emptyroom", task="noise")
write_meg_crosstalk(fname=crosstalk_fname, bids_path=bids_path_erm)

# bad path when task is provided for crosstalk
with pytest.raises(ValueError, match="task must be None if the acquisition is"):
assert bids_path_erm.copy().update(acquisition="crosstalk").task == "noise"
# subject not set.
bids_path = bids_path.copy().update(root=bids_root, subject=None)
with pytest.raises(ValueError, match="must have root and subject set"):
Expand Down
25 changes: 12 additions & 13 deletions mne_bids/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,19 @@ def _scale_coord_to_meters(coord, unit):
return coord


def _check_empty_room_basename(bids_path, on_invalid_er_task="raise"):
def _check_empty_room_basename(bids_path):
if bids_path.subject != "emptyroom":
return
# only check task entity for emptyroom when it is the sidecar/MEG file
if bids_path.suffix == "meg":
if bids_path.task != "noise":
msg = (
f'task must be "noise" if subject is "emptyroom", but '
f"received: {bids_path.task}"
)
if on_invalid_er_task == "raise":
raise ValueError(msg)
elif on_invalid_er_task == "warn":
logger.critical(msg)
else:
pass
if bids_path.suffix != "meg":
return
if bids_path.acquisition in ("calibration", "crosstalk"):
return
if bids_path.task != "noise":
raise ValueError(
f'task must be "noise" if subject is "emptyroom", but '
f"received: {bids_path.task}"
)


def _check_anonymize(anonymize, raw, ext):
Expand Down

0 comments on commit 61b26aa

Please sign in to comment.