Skip to content

Commit

Permalink
Limit montage units to feasible choices. Add dm support to unit estim…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
jackz314 committed Aug 28, 2023
1 parent b04087a commit 640476b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
21 changes: 8 additions & 13 deletions mne/io/eeglab/eeglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

import numpy as np
from mne.utils.check import _check_option

from ._eeglab import _readmat
from ..base import BaseRaw
Expand Down Expand Up @@ -273,24 +274,18 @@ def _handle_montage_units(montage_units, eeg):
is_nan_locs = np.isnan(xyz).any(axis=1)
mean_radius = np.mean(np.linalg.norm(xyz[~is_nan_locs], axis=1))
# radius should be between 0.05 and 0.11 meters
if mean_radius < 1: # m
if mean_radius < 0.25: # m
prefix = ""
elif mean_radius > 20: # mm
elif mean_radius < 2.5: # dm
prefix = "d"
elif mean_radius > 25: # mm
prefix = "m"
else: # cm
else: # 2.5 <= mean_radius <= 25, cm
prefix = "c"
else:
raise ValueError(
"Could not determine the units of the channel positions "
"automatically. Please specify the ``montage_units`` parameter."
)
prefix = "m" # assume mm if no channel positions are available
else:
n_char_unit = len(montage_units)
if montage_units[-1:] != "m" or n_char_unit > 2:
raise ValueError(
'``montage_units`` has to be in prefix + "m" format'
f', got "{montage_units}"'
)
_check_option("montage_units", montage_units, ("m", "dm", "cm", "mm"))

prefix = montage_units[:-1]
scale_units = 1 / DEFAULTS["prefixes"][prefix]
Expand Down
10 changes: 5 additions & 5 deletions mne/io/eeglab/tests/test_eeglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,16 @@ def test_degenerate(tmp_path):
pytest.raises(NotImplementedError, read_epochs_eeglab, bad_epochs_fname)

# error when montage units incorrect
with pytest.raises(ValueError, match=r'prefix \+ "m" format'):
with pytest.raises(ValueError, match=r'Invalid value'):
read_epochs_eeglab(epochs_fname_mat, montage_units="mV")

# warning when head radius too small
# warning when head radius too large
with pytest.warns(RuntimeWarning, match="is above"):
read_raw_eeglab(raw_fname_chanloc, montage_units="km")
read_raw_eeglab(raw_fname_chanloc, montage_units="m")

# warning when head radius too large
# warning when head radius too small
with pytest.warns(RuntimeWarning, match="is below"):
read_raw_eeglab(raw_fname_chanloc, montage_units="µm")
read_raw_eeglab(raw_fname_chanloc_meter, montage_units="mm")


@pytest.mark.parametrize(
Expand Down

0 comments on commit 640476b

Please sign in to comment.