/
limo.py
373 lines (345 loc) · 12.8 KB
/
limo.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Authors: Jose C. Garcia Alanis <alanis.jcg@gmail.com>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os.path as op
import time
from pathlib import Path
import numpy as np
from scipy.io import loadmat
from ..._fiff.meas_info import create_info
from ...channels import make_standard_montage
from ...epochs import EpochsArray
from ...utils import _check_pandas_installed, logger, verbose
from ..utils import _do_path_update, _downloader_params, _get_path, _log_time_size
# root url for LIMO files
root_url = "https://files.de-1.osf.io/v1/resources/52rea/providers/osfstorage/"
@verbose
def data_path(
subject, path=None, force_update=False, update_path=None, *, verbose=None
):
"""Get path to local copy of LIMO dataset URL.
This is a low-level function useful for getting a local copy of the
remote LIMO dataset :footcite:`Rousselet2016`. The complete dataset is
available at datashare.is.ed.ac.uk/.
Parameters
----------
subject : int
Subject to download. Must be of :class:`ìnt` in the range from 1
to 18 (inclusive).
path : None | str
Location of where to look for the LIMO data storing directory.
If None, the environment variable or config parameter
``MNE_DATASETS_LIMO_PATH`` is used. If it doesn't exist, the
"~/mne_data" directory is used. If the LIMO dataset
is not found under the given path, the data
will be automatically downloaded to the specified folder.
force_update : bool
Force update of the dataset even if a local copy exists.
update_path : bool | None
If True, set the MNE_DATASETS_LIMO_PATH in mne-python
config to the given path. If None, the user is prompted.
%(verbose)s
Returns
-------
path : str
Local path to the given data file.
Notes
-----
For example, one could do:
>>> from mne.datasets import limo
>>> limo.data_path(subject=1, path=os.getenv('HOME') + '/datasets') # doctest:+SKIP
This would download the LIMO data file to the 'datasets' folder,
and prompt the user to save the 'datasets' path to the mne-python config,
if it isn't there already.
References
----------
.. footbibliography::
""" # noqa: E501
import pooch
t0 = time.time()
downloader = pooch.HTTPDownloader(**_downloader_params())
# local storage patch
config_key = "MNE_DATASETS_LIMO_PATH"
name = "LIMO"
subj = f"S{subject}"
path = _get_path(path, config_key, name)
base_path = op.join(path, "MNE-limo-data")
subject_path = op.join(base_path, subj)
# the remote URLs are in the form of UUIDs:
urls = dict(
S18={
"Yr.mat": "5cf839833a4d9500178a6ff8",
"LIMO.mat": "5cf83907e650a2001ad592e4",
},
S17={
"Yr.mat": "5cf838e83a4d9500168aeb76",
"LIMO.mat": "5cf83867a542b80019c87602",
},
S16={
"Yr.mat": "5cf83857e650a20019d5778f",
"LIMO.mat": "5cf837dc3a4d9500188a64fe",
},
S15={
"Yr.mat": "5cf837cce650a2001ad591e8",
"LIMO.mat": "5cf83758a542b8001ac7d11d",
},
S14={
"Yr.mat": "5cf837493a4d9500198a938f",
"LIMO.mat": "5cf836e4a542b8001bc7cc53",
},
S13={
"Yr.mat": "5cf836d23a4d9500178a6df7",
"LIMO.mat": "5cf836543a4d9500168ae7cb",
},
S12={
"Yr.mat": "5cf83643d4c7d700193e5954",
"LIMO.mat": "5cf835193a4d9500178a6c92",
},
S11={
"Yr.mat": "5cf8356ea542b8001cc81517",
"LIMO.mat": "5cf834f7d4c7d700163daab8",
},
S10={
"Yr.mat": "5cf833b0e650a20019d57454",
"LIMO.mat": "5cf83204e650a20018d59eb2",
},
S9={
"Yr.mat": "5cf83201a542b8001cc811cf",
"LIMO.mat": "5cf8316c3a4d9500168ae13b",
},
S8={
"Yr.mat": "5cf8326ce650a20017d60373",
"LIMO.mat": "5cf8316d3a4d9500198a8dc5",
},
S7={
"Yr.mat": "5cf834a03a4d9500168ae59b",
"LIMO.mat": "5cf83069e650a20017d600d7",
},
S6={
"Yr.mat": "5cf830e6a542b80019c86a70",
"LIMO.mat": "5cf83057a542b80019c869ca",
},
S5={
"Yr.mat": "5cf8115be650a20018d58041",
"LIMO.mat": "5cf80c0bd4c7d700193e213c",
},
S4={
"Yr.mat": "5cf810c9a542b80019c8450a",
"LIMO.mat": "5cf80bf83a4d9500198a6eb4",
},
S3={
"Yr.mat": "5cf80c55d4c7d700163d8f52",
"LIMO.mat": "5cf80bdea542b80019c83cab",
},
S2={
"Yr.mat": "5cde827123fec40019e01300",
"LIMO.mat": "5cde82682a50c4001677c259",
},
S1={
"Yr.mat": "5d6d3071536cf5001a8b0c78",
"LIMO.mat": "5d6d305f6f41fc001a3151d8",
},
)
# these can't be in the registry file (mne/data/dataset_checksums.txt)
# because of filename duplication
hashes = dict(
S18={
"Yr.mat": "md5:87f883d442737971a80fc0a35d057e51",
"LIMO.mat": "md5:8b4879646f65d7876fa4adf2e40162c5",
},
S17={
"Yr.mat": "md5:7b667ec9eefd7a9996f61ae270e295ee",
"LIMO.mat": "md5:22eaca4e6fad54431fd61b307fc426b8",
},
S16={
"Yr.mat": "md5:c877afdb4897426421577e863a45921a",
"LIMO.mat": "md5:86672d7afbea1e8c39305bc3f852c8c2",
},
S15={
"Yr.mat": "md5:eea9e0140af598fefc08c886a6f05de5",
"LIMO.mat": "md5:aed5cb71ddbfd27c6a3ac7d3e613d07f",
},
S14={
"Yr.mat": "md5:8bd842cfd8588bd5d32e72fdbe70b66e",
"LIMO.mat": "md5:1e07d1f36f2eefad435a77530daf2680",
},
S13={
"Yr.mat": "md5:d7925d2af7288b8a5186dfb5dbb63d34",
"LIMO.mat": "md5:ba891015d2f9e447955fffa9833404ca",
},
S12={
"Yr.mat": "md5:0e1d05beaa4bf2726e0d0671b78fe41e",
"LIMO.mat": "md5:423fd479d71097995b6614ecb11df9ad",
},
S11={
"Yr.mat": "md5:1b0016fb9832e43b71f79c1992fcbbb1",
"LIMO.mat": "md5:1a281348c2a41ee899f42731d30cda70",
},
S10={
"Yr.mat": "md5:13c66f60e241b9a9cc576eaf1b55a417",
"LIMO.mat": "md5:3c4b41e221eb352a21bbef1a7e006f06",
},
S9={
"Yr.mat": "md5:3ae1d9c3a1d9325deea2f2dddd1ab507",
"LIMO.mat": "md5:5e204e2a4bcfe4f535b4b1af469b37f7",
},
S8={
"Yr.mat": "md5:7e9adbca4e03d8d7ce8ea07ccecdc8fd",
"LIMO.mat": "md5:88313c21d34428863590e586b2bc3408",
},
S7={
"Yr.mat": "md5:6b5290a6725ecebf1022d5d2789b186d",
"LIMO.mat": "md5:8c769219ebc14ce3f595063e84bfc0a9",
},
S6={
"Yr.mat": "md5:420c858a8340bf7c28910b7b0425dc5d",
"LIMO.mat": "md5:9cf4e1a405366d6bd0cc6d996e32fd63",
},
S5={
"Yr.mat": "md5:946436cfb474c8debae56ffb1685ecf3",
"LIMO.mat": "md5:241fac95d3a79d2cea081391fb7078bd",
},
S4={
"Yr.mat": "md5:c8216af78ac87b739e86e57b345cafdd",
"LIMO.mat": "md5:8e10ef36c2e075edc2f787581ba33459",
},
S3={
"Yr.mat": "md5:ff02e885b65b7b807146f259a30b1b5e",
"LIMO.mat": "md5:59b5fb3a9749003133608b5871309e2c",
},
S2={
"Yr.mat": "md5:a4329022e57fd07ceceb7d1735fd2718",
"LIMO.mat": "md5:98b284b567f2dd395c936366e404f2c6",
},
S1={
"Yr.mat": "md5:076c0ae78fb71d43409c1877707df30e",
"LIMO.mat": "md5:136c8cf89f8f111a11f531bd9fa6ae69",
},
)
# create the download manager
fetcher = pooch.create(
path=subject_path,
base_url="",
version=None, # Data versioning is decoupled from MNE-Python version.
registry=hashes[subj],
urls={key: f"{root_url}{uuid}" for key, uuid in urls[subj].items()},
retry_if_failed=2, # 2 retries = 3 total attempts
)
# use our logger level for pooch's logger too
pooch.get_logger().setLevel(logger.getEffectiveLevel())
# fetch the data
sz = 0
for fname in ("LIMO.mat", "Yr.mat"):
destination = Path(subject_path, fname)
if destination.exists():
if force_update:
destination.unlink()
else:
continue
if sz == 0: # log once
logger.info("Downloading LIMO data")
# fetch the remote file (if local file missing or has hash mismatch)
fetcher.fetch(fname=fname, downloader=downloader)
sz += destination.stat().st_size
# update path in config if desired
_do_path_update(path, update_path, config_key, name)
if sz > 0:
_log_time_size(t0, sz)
return base_path
@verbose
def load_data(subject, path=None, force_update=False, update_path=None, verbose=None):
"""Fetch subjects epochs data for the LIMO data set.
Parameters
----------
subject : int
Subject to use. Must be of class ìnt in the range from 1 to 18.
path : str
Location of where to look for the LIMO data.
If None, the environment variable or config parameter
``MNE_DATASETS_LIMO_PATH`` is used. If it doesn't exist, the
"~/mne_data" directory is used.
force_update : bool
Force update of the dataset even if a local copy exists.
update_path : bool | None
If True, set the MNE_DATASETS_LIMO_PATH in mne-python
config to the given path. If None, the user is prompted.
%(verbose)s
Returns
-------
epochs : instance of Epochs
The epochs.
""" # noqa: E501
pd = _check_pandas_installed()
# subject in question
if isinstance(subject, int) and 1 <= subject <= 18:
subj = "S%i" % subject
else:
raise ValueError("subject must be an int in the range from 1 to 18")
# set limo path, download and decompress files if not found
limo_path = data_path(subject, path, force_update, update_path)
# -- 1) import .mat files
# epochs info
fname_info = op.join(limo_path, subj, "LIMO.mat")
data_info = loadmat(fname_info)
# number of epochs per condition
design = data_info["LIMO"]["design"][0][0]["X"][0][0]
data_info = data_info["LIMO"]["data"][0][0][0][0]
# epochs data
fname_eeg = op.join(limo_path, subj, "Yr.mat")
data = loadmat(fname_eeg)
# -- 2) get epochs information from structure
# sampling rate
sfreq = data_info["sampling_rate"][0][0]
# tmin and tmax
tmin = data_info["start"][0][0]
# create events matrix
sample = np.arange(len(design))
prev_id = np.zeros(len(design))
ev_id = design[:, 1]
events = np.array([sample, prev_id, ev_id]).astype(int).T
# event ids, such that Face B == 1
event_id = {"Face/A": 0, "Face/B": 1}
# -- 3) extract channel labels from LIMO structure
# get individual labels
labels = data_info["chanlocs"]["labels"]
labels = [label for label, *_ in labels[0]]
# get montage
montage = make_standard_montage("biosemi128")
# add external electrodes (e.g., eogs)
ch_names = montage.ch_names + ["EXG1", "EXG2", "EXG3", "EXG4"]
# match individual labels to labels in montage
found_inds = [ind for ind, name in enumerate(ch_names) if name in labels]
missing_chans = [name for name in ch_names if name not in labels]
assert labels == [ch_names[ind] for ind in found_inds]
# -- 4) extract data from subjects Yr structure
# data is stored as channels x time points x epochs
# data['Yr'].shape # <-- see here
# transpose to epochs x channels time points
data = np.transpose(data["Yr"], (2, 0, 1))
# initialize data in expected order
temp_data = np.empty((data.shape[0], len(ch_names), data.shape[2]))
# copy over the non-missing data
for source, target in enumerate(found_inds):
# avoid copy when fancy indexing
temp_data[:, target, :] = data[:, source, :]
# data to V (to match MNE's format)
data = temp_data / 1e6
# create list containing channel types
types = ["eog" if ch.startswith("EXG") else "eeg" for ch in ch_names]
# -- 5) Create custom info for mne epochs structure
# create info
info = create_info(ch_names, sfreq, types).set_montage(montage)
# get faces and noise variables from design matrix
event_list = list(events[:, 2])
faces = ["B" if event else "A" for event in event_list]
noise = list(design[:, 2])
# create epochs metadata
metadata = {"face": faces, "phase-coherence": noise}
metadata = pd.DataFrame(metadata)
# -- 6) Create custom epochs array
epochs = EpochsArray(
data, info, events, tmin, event_id, metadata=metadata, verbose=False
)
epochs.info["bads"] = missing_chans # missing channels are marked as bad.
return epochs