Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,7 @@ def test_convert_eeg_formats(dir_name, fmt, fname, reader, tmp_path):
# load channels.tsv; the unit should be Volts
channels_fname = bids_output_path.copy().update(suffix="channels", extension=".tsv")
channels_tsv = _from_tsv(channels_fname)
assert channels_tsv["units"][0] == "V"
assert channels_tsv["units"][0] == "µV"

if fmt == "BrainVision":
assert Path(raw2.filenames[0]).suffix == ".eeg"
Expand Down
21 changes: 17 additions & 4 deletions mne_bids/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _should_use_bti_pdf_suffix() -> bool:
return use_pdf_suffix


def _channels_tsv(raw, fname, overwrite=False):
def _channels_tsv(raw, fname, *, convert, overwrite=False):
"""Create a channels.tsv file and save it.
Parameters
Expand Down Expand Up @@ -193,10 +193,21 @@ def _channels_tsv(raw, fname, overwrite=False):
ch_type.append(map_chs[_channel_type])
description.append(map_desc[_channel_type])
low_cutoff, high_cutoff = (raw.info["highpass"], raw.info["lowpass"])
if raw._orig_units:
# if raw data is merely copied, check `raw._orig_units`.
if not convert and raw._orig_units:
units = [raw._orig_units.get(ch, "n/a") for ch in raw.ch_names]
# If `raw._orig_units` is missing (or if data are being *converted*),
# unit is determined by destination format:
# - `eeglabio.raw.export_set` always converts V to uV
# - `mne.export._edf_bdf._export_raw_edf_bdf` always converts V to uV
# - `pybv.write_brainvision` converts V to uV by default (and we don't alter that)
else:
units = [_unit2human.get(ch_i["unit"], "n/a") for ch_i in raw.info["chs"]]
units = [
"µV"
if ch_i["unit"] == FIFF.FIFF_UNIT_V
else _unit2human.get(ch_i["unit"], "n/a")
for ch_i in raw.info["chs"]
]
units = [u if u not in ["NA"] else "n/a" for u in units]

# Translate from MNE to BIDS unit naming
Expand Down Expand Up @@ -2229,7 +2240,6 @@ def write_raw_bids(
emptyroom_fname=associated_er_path,
overwrite=overwrite,
)
_channels_tsv(raw, channels_path.fpath, overwrite)

# create parent directories if needed
_mkdir_p(os.path.dirname(data_path))
Expand Down Expand Up @@ -2288,6 +2298,9 @@ def write_raw_bids(
f"for {datatype} datatype."
)

# this can't happen until after value of `convert` has been determined
_channels_tsv(raw, channels_path.fpath, convert=convert, overwrite=overwrite)

# raise error when trying to copy files (copyfile_*) into same location
# (src == dest, see https://github.com/mne-tools/mne-bids/issues/867)
if (
Expand Down
Loading