Skip to content

Commit 1ef16d7

Browse files
MAINT: Replace defusedxml with mffpy in _get_ep_info and _get_signalfname (mne-tools#13991)
1 parent b770a02 commit 1ef16d7

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

doc/changes/dev/13991.other.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace ``defusedxml``-backed implementations of ``_get_ep_info`` and ``_get_signalfname`` in the EGI MFF reader with ``mffpy``-backed parsing via :func:`mne.io.read_raw_egi`, by `Pragnya Khandelwal`_.

mne/io/egi/general.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,16 @@ def _get_gains(filepath):
5252

5353
def _get_ep_info(filepath):
5454
"""Get epoch info."""
55-
_soft_import("defusedxml", "reading EGI MFF data")
56-
from defusedxml.minidom import parse
55+
from mffpy.xml_files import XML
5756

58-
epochfile = filepath + "/epochs.xml"
59-
epochlist = parse(epochfile)
60-
epochs = epochlist.getElementsByTagName("epoch")
57+
ep_obj = XML.from_file(os.path.join(filepath, "epochs.xml"))
6158
keys = ("first_samps", "last_samps", "first_blocks", "last_blocks")
6259
epoch_info = {key: list() for key in keys}
63-
for epoch in epochs:
64-
ep_begin = int(epoch.getElementsByTagName("beginTime")[0].firstChild.data)
65-
ep_end = int(epoch.getElementsByTagName("endTime")[0].firstChild.data)
66-
first_block = int(epoch.getElementsByTagName("firstBlock")[0].firstChild.data)
67-
last_block = int(epoch.getElementsByTagName("lastBlock")[0].firstChild.data)
68-
epoch_info["first_samps"].append(ep_begin)
69-
epoch_info["last_samps"].append(ep_end)
70-
epoch_info["first_blocks"].append(first_block)
71-
epoch_info["last_blocks"].append(last_block)
60+
for ep in ep_obj.epochs:
61+
epoch_info["first_samps"].append(ep.beginTime)
62+
epoch_info["last_samps"].append(ep.endTime)
63+
epoch_info["first_blocks"].append(ep.firstBlock)
64+
epoch_info["last_blocks"].append(ep.lastBlock)
7265
# Don't turn into ndarray here, keep native int because it can deal with
7366
# huge numbers (could use np.uint64 but it's more work)
7467
return epoch_info
@@ -132,8 +125,7 @@ def _get_blocks(filepath):
132125

133126
def _get_signalfname(filepath):
134127
"""Get filenames."""
135-
_soft_import("defusedxml", "reading EGI MFF data")
136-
from defusedxml.minidom import parse
128+
from mffpy.xml_files import XML
137129

138130
listfiles = os.listdir(filepath)
139131
binfiles = list(
@@ -145,16 +137,14 @@ def _get_signalfname(filepath):
145137
bin_num_str = re.search(r"\d+", binfile).group()
146138
infofile = "info" + bin_num_str + ".xml"
147139
infofiles.append(infofile)
148-
infobjfile = os.path.join(filepath, infofile)
149-
infobj = parse(infobjfile)
150-
if len(infobj.getElementsByTagName("EEG")):
151-
signal_type = "EEG"
152-
elif len(infobj.getElementsByTagName("PNSData")):
153-
signal_type = "PNS"
154-
all_files[signal_type] = {
155-
"signal": f"signal{bin_num_str}.bin",
156-
"info": infofile,
157-
}
140+
info_obj = XML.from_file(os.path.join(filepath, infofile))
141+
channel_type = (
142+
info_obj.get_content().get("generalInformation", {}).get("channel_type", "")
143+
)
144+
if channel_type == "EEG":
145+
all_files["EEG"] = {"signal": f"signal{bin_num_str}.bin", "info": infofile}
146+
elif channel_type == "PNSData":
147+
all_files["PNS"] = {"signal": f"signal{bin_num_str}.bin", "info": infofile}
158148
if "EEG" not in all_files:
159149
infofiles_str = "\n".join(infofiles)
160150
raise FileNotFoundError(

0 commit comments

Comments
 (0)