Skip to content

Commit

Permalink
MAINT: Fix Azure (mne-tools#11815)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Jul 21, 2023
1 parent 57146ee commit 1336961
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Bugs
- Fix bug with :func:`mne.io.read_raw_fil` where datasets without sensor positions would not import (:gh:`11733` by `George O'Neill`_)
- Fix bug with :func:`mne.chpi.compute_chpi_snr` where cHPI being off for part of the recording or bad channels being defined led to an error or incorrect behavior (:gh:`11754`, :gh:`11755` by `Eric Larson`_)
- Allow int-like for the argument ``id`` of `~mne.make_fixed_length_events` (:gh:`11748` by `Mathieu Scheltienne`_)
- Fix bug where :func:`mne.io.read_raw_egi` did not properly set the EEG reference location for the reference channel itself (:gh:`11822` by `Eric Larson`_)
- Fix bug with legacy :meth:`~mne.io.Raw.plot_psd` method where passed axes were not used (:gh:`11778` by `Daniel McCloy`_)
- blink :class:`mne.Annotations` read in by :func:`mne.io.read_raw_eyelink` now begin with ``'BAD_'``, i.e. ``'BAD_blink'``, because ocular data are missing during blinks. (:gh:`11746` by `Scott Huberty`_)
- Fix bug where :ref:`mne show_fiff` could fail with an ambiguous error if the file is corrupt (:gh:`11800` by `Eric Larson`_)
Expand Down
23 changes: 11 additions & 12 deletions mne/channels/montage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,25 +1171,22 @@ def _backcompat_value(pos, ref_pos):
chs = [info["chs"][ii] for ii in picks]
non_names = [info["chs"][ii]["ch_name"] for ii in non_picks]
del picks
ref_pos = [ch["loc"][3:6] for ch in chs]
ref_pos = np.array([ch["loc"][3:6] for ch in chs])

# keep reference location from EEG-like channels if they
# already exist and are all the same.
custom_eeg_ref_dig = False
# Note: ref position is an empty list for fieldtrip data
if ref_pos:
if (
all([np.equal(ref_pos[0], pos).all() for pos in ref_pos])
and not np.equal(ref_pos[0], [0, 0, 0]).all()
):
eeg_ref_pos = ref_pos[0]
# since we have an EEG reference position, we have
# to add it into the info['dig'] as EEG000
custom_eeg_ref_dig = True
if not custom_eeg_ref_dig:
if len(ref_pos) and ref_pos[0].any() and (ref_pos[0] == ref_pos).all():
eeg_ref_pos = ref_pos[0]
# since we have an EEG reference position, we have
# to add it into the info['dig'] as EEG000
custom_eeg_ref_dig = True
else:
refs = set(ch_pos) & {"EEG000", "REF"}
assert len(refs) <= 1
eeg_ref_pos = np.zeros(3) if not refs else ch_pos.pop(refs.pop())
custom_eeg_ref_dig = False
del ref_pos

# This raises based on info being subset/superset of montage
info_names = [ch["ch_name"] for ch in chs]
Expand Down Expand Up @@ -1306,6 +1303,7 @@ def _backcompat_value(pos, ref_pos):

# determine if needed to add an extra EEG REF DigPoint
if custom_eeg_ref_dig:
print(len(digpoints))
# ref_name = 'EEG000' if match_case else 'eeg000'
ref_dig_dict = {
"kind": FIFF.FIFFV_POINT_EEG,
Expand All @@ -1318,6 +1316,7 @@ def _backcompat_value(pos, ref_pos):
# in the old dig
if ref_dig_point in old_dig:
digpoints.append(ref_dig_point)
print(len(digpoints))
# Next line modifies info['dig'] in place
with info._unlock():
info["dig"] = _format_dig_points(digpoints, enforce_order=True)
Expand Down
7 changes: 1 addition & 6 deletions mne/io/egi/egimff.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,9 @@ def __init__(
ref_idx = ref_idx.item()
ref_coords = info["chs"][int(ref_idx)]["loc"][:3]
for chan in info["chs"]:
is_eeg = chan["kind"] == FIFF.FIFFV_EEG_CH
is_not_ref = chan["ch_name"] not in REFERENCE_NAMES
if is_eeg and is_not_ref:
if chan["kind"] == FIFF.FIFFV_EEG_CH:
chan["loc"][3:6] = ref_coords

# Cz ref was applied during acquisition, so mark as already set.
with info._unlock():
info["custom_ref_applied"] = FIFF.FIFFV_MNE_CUSTOM_REF_ON
file_bin = op.join(input_fname, egi_info["eeg_fname"])
egi_info["egi_events"] = egi_events

Expand Down
29 changes: 20 additions & 9 deletions mne/io/egi/tests/test_egi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# simplified BSD-3 license


from copy import deepcopy
from pathlib import Path
import os
import shutil
Expand All @@ -12,7 +13,7 @@
import pytest
from scipy import io as sio

from mne import find_events, pick_types, pick_channels
from mne import find_events, pick_types
from mne.io import read_raw_egi, read_evokeds_mff, read_raw_fif
from mne.io.constants import FIFF
from mne.io.egi.egi import _combine_triggers
Expand Down Expand Up @@ -161,14 +162,12 @@ def test_io_egi_mff():
assert raw.info["dig"][0]["kind"] == FIFF.FIFFV_POINT_CARDINAL
assert raw.info["dig"][3]["kind"] == FIFF.FIFFV_POINT_EEG
assert raw.info["dig"][-1]["ident"] == 129
assert raw.info["custom_ref_applied"] == FIFF.FIFFV_MNE_CUSTOM_REF_ON
# This is not a custom reference, it's consistent across all channels
assert raw.info["custom_ref_applied"] == FIFF.FIFFV_MNE_CUSTOM_REF_OFF
ref_loc = raw.info["dig"][-1]["r"]
eeg_picks = pick_types(raw.info, eeg=True)
assert len(eeg_picks) == n_eeg + n_ref # 129
# ref channel doesn't store its own loc as ref location
# so don't test it
ref_pick = pick_channels(raw.info["ch_names"], ["VREF"])
eeg_picks = np.setdiff1d(eeg_picks, ref_pick)
# ref channel should store its own loc as ref location, so't test it
for i in eeg_picks:
loc = raw.info["chs"][i]["loc"]
assert loc[:3].any(), loc[:3]
Expand Down Expand Up @@ -523,13 +522,25 @@ def test_meas_date(fname, timestamp, utc_offset):
(egi_mff_pns_fname, "GSN-HydroCel-257"), # 257 chan EGI file
],
)
def test_set_standard_montage(fname, standard_montage):
def test_set_standard_montage_mff(fname, standard_montage):
"""Test setting a standard montage."""
raw = read_raw_egi(fname, verbose="warning")
dig_before_mon = raw.info["dig"]
n = int(standard_montage.split("-")[-1]) + 3
dig_before_mon = deepcopy(raw.info["dig"])
assert len(dig_before_mon) == n
ref_loc = dig_before_mon[-1]["r"]
picks = pick_types(raw.info, eeg=True)
assert len(picks) == n
for pick in picks:
assert_allclose(raw.info["chs"][pick]["loc"][3:6], ref_loc)

raw.set_montage(standard_montage, match_alias=True, on_missing="ignore")
dig_after_mon = raw.info["dig"]

# No dig entries should have been dropped while setting montage
assert len(dig_before_mon) == len(dig_after_mon)
assert len(dig_before_mon) == n
assert len(dig_after_mon) == n

# Check that the reference remained
for pick in picks:
assert_allclose(raw.info["chs"][pick]["loc"][3:6], ref_loc)

0 comments on commit 1336961

Please sign in to comment.