1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os
from functools import partial
import numpy as np
import mne
from mne.utils import object_diff
info_ignored_fields = (
"file_id",
"hpi_results",
"hpi_meas",
"meas_id",
"meas_date",
"highpass",
"lowpass",
"subject_info",
"hpi_subsystem",
"experimenter",
"description",
"proj_id",
"proj_name",
"line_freq",
"gantry_angle",
"dev_head_t",
"bads",
"ctf_head_t",
"dev_ctf_t",
"dig",
)
ch_ignore_fields = (
"logno",
"cal",
"range",
"scanno",
"coil_type",
"kind",
"loc",
"coord_frame",
"unit",
)
info_long_fields = ("hpi_meas", "projs")
system_to_reader_fn_dict = {
"neuromag306": mne.io.read_raw_fif,
"CNT": partial(mne.io.read_raw_cnt),
"CTF": partial(mne.io.read_raw_ctf, clean_names=True),
"BTI": partial(
mne.io.read_raw_bti,
head_shape_fname=None,
rename_channels=False,
sort_by_ch_name=False,
),
"EGI": mne.io.read_raw_egi,
"eximia": mne.io.read_raw_eximia,
}
ignore_channels_dict = {"BTI": ["MUz", "MLx", "MLy", "MUx", "MUy", "MLz"]}
drop_extra_chans_dict = {
"EGI": ["STI 014", "DIN1", "DIN3", "DIN7", "DIN4", "DIN5", "DIN2"],
"eximia": ["GateIn", "Trig1", "Trig2"],
}
system_decimal_accuracy_dict = {"CNT": 2}
pandas_not_found_warning_msg = (
"The Pandas library is not installed. Not "
"returning the original trialinfo matrix as "
"metadata."
)
testing_path = mne.datasets.testing.data_path(download=False)
def _remove_ignored_ch_fields(info):
if "chs" in info:
for cur_ch in info["chs"]:
for cur_field in ch_ignore_fields:
if cur_field in cur_ch:
del cur_ch[cur_field]
def _remove_long_info_fields(info):
for cur_field in info_long_fields:
if cur_field in info:
del info[cur_field]
def _remove_ignored_info_fields(info):
for cur_field in info_ignored_fields:
if cur_field in info:
del info[cur_field]
_remove_ignored_ch_fields(info)
def get_data_paths(system):
"""Return common paths for all tests."""
return testing_path / "fieldtrip" / "ft_test_data" / system
def get_cfg_local(system):
"""Return cfg_local field for the system."""
from pymatreader import read_mat
cfg_local = read_mat(
os.path.join(get_data_paths(system), "raw_v7.mat"), ["cfg_local"]
)["cfg_local"]
return cfg_local
def get_raw_info(system):
"""Return the info dict of the raw data."""
cfg_local = get_cfg_local(system)
raw_data_file = os.path.join(testing_path, cfg_local["file_name"])
reader_function = system_to_reader_fn_dict[system]
info = reader_function(raw_data_file, preload=False).info
with info._unlock():
info["comps"] = []
return info
def get_raw_data(system, drop_extra_chs=False):
"""Find, load and process the raw data."""
cfg_local = get_cfg_local(system)
raw_data_file = os.path.join(testing_path, cfg_local["file_name"])
reader_function = system_to_reader_fn_dict[system]
raw_data = reader_function(raw_data_file, preload=True)
crop = min(cfg_local["crop"], np.max(raw_data.times))
if system == "eximia":
crop -= 0.5 * (1.0 / raw_data.info["sfreq"])
raw_data.crop(0, crop)
raw_data.del_proj("all")
with raw_data.info._unlock():
raw_data.info["comps"] = []
raw_data.drop_channels(cfg_local["removed_chan_names"])
if system in ["EGI"]:
raw_data._data[0:-1, :] = raw_data._data[0:-1, :] * 1e6
if system in ["CNT"]:
raw_data._data = raw_data._data * 1e6
if system in ignore_channels_dict:
raw_data.drop_channels(ignore_channels_dict[system])
if system in drop_extra_chans_dict and drop_extra_chs:
raw_data.drop_channels(drop_extra_chans_dict[system])
return raw_data
def get_epochs(system):
"""Find, load and process the epoched data."""
cfg_local = get_cfg_local(system)
raw_data = get_raw_data(system)
if cfg_local["eventtype"] in raw_data.ch_names:
stim_channel = cfg_local["eventtype"]
else:
stim_channel = "STI 014"
if system == "CNT":
events, event_id = mne.events_from_annotations(raw_data)
events[:, 0] = events[:, 0] + 1
else:
events = mne.find_events(raw_data, stim_channel=stim_channel, shortest_event=1)
if isinstance(cfg_local["eventvalue"], np.ndarray):
event_id = list(cfg_local["eventvalue"].astype("int"))
else:
event_id = [int(cfg_local["eventvalue"])]
event_id = [id_ for id_ in event_id if id_ in events[:, 2]]
epochs = mne.Epochs(
raw_data,
events=events,
event_id=event_id,
tmin=-cfg_local["prestim"],
tmax=cfg_local["poststim"],
baseline=None,
)
return epochs
def get_evoked(system):
"""Find, load and process the avg data."""
epochs = get_epochs(system)
return epochs.average(picks=np.arange(len(epochs.ch_names)))
def check_info_fields(expected, actual, has_raw_info):
"""
Check if info fields are equal.
Some fields are ignored.
"""
expected = expected.info.copy()
actual = actual.info.copy()
if not has_raw_info:
_remove_ignored_info_fields(expected)
_remove_ignored_info_fields(actual)
_remove_long_info_fields(expected)
_remove_long_info_fields(actual)
# we annoyingly have two ways of representing this, so just always use
# an empty list here
for obj in (expected, actual):
if obj.get("dig", None) is None:
with obj._unlock():
obj["dig"] = []
d = object_diff(actual, expected, allclose=True)
assert d == "", d
def check_data(expected, actual, system):
"""Check data for equality."""
decimal = 7
if system in system_decimal_accuracy_dict:
decimal = system_decimal_accuracy_dict[system]
np.testing.assert_almost_equal(expected, actual, decimal=decimal)
def assert_warning_in_record(warning_message, warn_record):
"""Assert that a warning message is in the records."""
all_messages = [str(w.message) for w in warn_record]
assert warning_message in all_messages
|