Skip to content

Commit

Permalink
Fix mne.io.edf.edf._parse_prefilter_string to add prefilter infor…
Browse files Browse the repository at this point in the history
…mation as ``np.nan`` when the prefilter information does not exist
  • Loading branch information
rcmdnk committed Dec 28, 2023
1 parent 6790426 commit 9fe75a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions doc/changes/devel/12329.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``mne.io.edf.edf._parse_prefilter_string`` to add prefilter information as ``np.nan`` when the prefilter information does not exist, by `Michiru Kaneda`_
28 changes: 10 additions & 18 deletions mne/io/edf/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,26 +758,18 @@ def _get_info(
return info, edf_info, orig_units


def _extract_filter_value(s, prefix, suffix):
if prefix in s:
start = s.find(prefix) + len(prefix)
end = s.find(suffix, start)
return s[start:end].strip()
return np.nan


def _parse_prefilter_string(prefiltering):
"""Parse prefilter string from EDF+ and BDF headers."""
highpass = np.array(
[
v
for hp in [
re.findall(r"HP:\s*([0-9]+[.]*[0-9]*)", filt) for filt in prefiltering
]
for v in hp
]
)
lowpass = np.array(
[
v
for hp in [
re.findall(r"LP:\s*([0-9]+[.]*[0-9]*)", filt) for filt in prefiltering
]
for v in hp
]
)
highpass = [_extract_filter_value(s, "HP:", "Hz") for s in prefiltering]
lowpass = [_extract_filter_value(s, "LP:", "Hz") for s in prefiltering]
return highpass, lowpass


Expand Down
9 changes: 7 additions & 2 deletions mne/io/edf/tests/test_edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,19 @@ def test_edf_prefilter_parse():

prefilter_unfiltered_ch = prefilter_normal_multi_ch + [""]
highpass, lowpass = _parse_prefilter_string(prefilter_unfiltered_ch)
assert_array_equal(highpass, ["1"] * 10)
assert_array_equal(lowpass, ["30"] * 10)
assert_array_equal(highpass, ["1"] * 10 + [np.nan])
assert_array_equal(lowpass, ["30"] * 10 + [np.nan])

prefilter_edf_specs_doc = ["HP:0.1Hz LP:75Hz N:50Hz"]
highpass, lowpass = _parse_prefilter_string(prefilter_edf_specs_doc)
assert_array_equal(highpass, ["0.1"])
assert_array_equal(lowpass, ["75"])

prefilter_edf_specs_doc = ["", "HP:0.1Hz LP:75Hz N:50Hz", ""]
highpass, lowpass = _parse_prefilter_string(prefilter_edf_specs_doc)
assert_array_equal(highpass, [np.nan, "0.1", np.nan])
assert_array_equal(lowpass, [np.nan, "75", np.nan])


@testing.requires_testing_data
@pytest.mark.parametrize("fname", [test_generator_edf, test_generator_bdf])
Expand Down

0 comments on commit 9fe75a6

Please sign in to comment.