Skip to content

Commit e41f6c6

Browse files
MAINT: Replace defusedxml with mffpy in _get_gains and remove _extract (mne-tools#14007)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a15d20c commit e41f6c6

2 files changed

Lines changed: 17 additions & 35 deletions

File tree

doc/changes/dev/14007.other.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace ``defusedxml``-backed ``_get_gains`` with ``mffpy``-backed parsing via :func:`mne.io.read_raw_egi` and remove the internal ``_extract`` helper, fully eliminating ``defusedxml`` from the EGI MFF reader, by `Pragnya Khandelwal`_.

mne/io/egi/general.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,26 @@
88

99
import numpy as np
1010

11-
from ...utils import _pl, _soft_import
12-
13-
14-
def _extract(tags, filepath=None, obj=None):
15-
"""Extract info from XML."""
16-
_soft_import("defusedxml", "reading EGI MFF data")
17-
from defusedxml.minidom import parse
18-
19-
if obj is not None:
20-
fileobj = obj
21-
elif filepath is not None:
22-
fileobj = parse(filepath)
23-
else:
24-
raise ValueError("There is not object or file to extract data")
25-
infoxml = dict()
26-
for tag in tags:
27-
value = fileobj.getElementsByTagName(tag)
28-
infoxml[tag] = []
29-
for i in range(len(value)):
30-
infoxml[tag].append(value[i].firstChild.data)
31-
return infoxml
11+
from ...utils import _pl
3212

3313

3414
def _get_gains(filepath):
3515
"""Parse gains."""
36-
_soft_import("defusedxml", "reading EGI MFF data")
37-
from defusedxml.minidom import parse
38-
39-
file_obj = parse(filepath)
40-
objects = file_obj.getElementsByTagName("calibration")
41-
gains = dict()
42-
for ob in objects:
43-
value = ob.getElementsByTagName("type")
44-
if value[0].firstChild.data == "GCAL":
45-
data_g = _extract(["ch"], obj=ob)["ch"]
46-
gains.update(gcal=np.asarray(data_g, dtype=np.float64))
47-
elif value[0].firstChild.data == "ICAL":
48-
data_g = _extract(["ch"], obj=ob)["ch"]
49-
gains.update(ical=np.asarray(data_g, dtype=np.float64))
16+
from mffpy.xml_files import XML
17+
18+
obj = XML.from_file(filepath)
19+
cals = obj.get_content().get("calibrations", {})
20+
gains = {}
21+
if "GCAL" in cals:
22+
ch_dict = cals["GCAL"]["channels"]
23+
gains["gcal"] = np.asarray(
24+
[ch_dict[k] for k in sorted(ch_dict)], dtype=np.float64
25+
)
26+
if "ICAL" in cals:
27+
ch_dict = cals["ICAL"]["channels"]
28+
gains["ical"] = np.asarray(
29+
[ch_dict[k] for k in sorted(ch_dict)], dtype=np.float64
30+
)
5031
return gains
5132

5233

0 commit comments

Comments
 (0)