Skip to content

Commit 3442039

Browse files
committed
simplify unit parser
1 parent 97d6925 commit 3442039

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

mne/io/bci2k/bci2k.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
from ...utils import verbose
1212
from ..base import BaseRaw
1313

14-
_VOLT_SCALE = {"v": 1.0, "mv": 1e-3, "muv": 1e-6, "uv": 1e-6, "nv": 1e-9}
15-
_FREQ_SCALE = {"hz": 1.0, "khz": 1e3}
14+
_VOLT_SCALE = {"v": 1.0, "mv": 1e-3, "muv": 1e-6, "uv": 1e-6, "nv": 1e-9, "": 1e-6}
15+
_FREQ_SCALE = {"hz": 1.0, "khz": 1e3, "": 1.0}
1616

1717

18-
def _parse_value_with_unit(token, unit_scale=None):
18+
def _parse_value_with_unit(token, unit_scale):
1919
"""Split a numeric token with optional unit into value and scale."""
2020
text = str(token).strip().replace("µ", "u")
21-
m = re.search(r"([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s*([a-zA-Z]*)", text)
22-
if m is None:
23-
raise ValueError(f"Could not parse numeric value from {token!r}")
24-
num = float(m.group(1))
25-
unit = m.group(2).lower()
26-
if unit_scale is None:
27-
scale = 1.0
21+
m = re.search(r"[a-zA-Z]+$", text)
22+
if m:
23+
num = float(text[:m.start()])
24+
unit = m.group().lower()
2825
else:
29-
scale = unit_scale.get(unit, 1.0)
30-
return num, scale
26+
num = float(text)
27+
unit = ""
28+
if unit not in unit_scale:
29+
raise ValueError(f"Unrecognized unit '{unit}' in token '{token}'.")
30+
return num, unit_scale[unit]
3131

3232

3333
def _parse_bci2k_header(fname):
@@ -182,7 +182,7 @@ def _read_bci2k_data(fname, info_dict):
182182
raise ValueError(
183183
"Expected SourceChOffset and SourceChGain lengths to match SourceCh."
184184
)
185-
offsets_arr = np.array([_parse_value_with_unit(val)[0] for val in offsets])
185+
offsets_arr = np.array([float(val) for val in offsets])
186186
gain_parsed = [_parse_value_with_unit(val, unit_scale=_VOLT_SCALE) for val in gains]
187187
gains_arr = np.array([val for val, _ in gain_parsed])
188188
gain_scales = np.array([scale for _, scale in gain_parsed])

0 commit comments

Comments
 (0)