forked from NeuroTechX/moabb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasets.py
392 lines (339 loc) · 14 KB
/
datasets.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import inspect
import logging
import shutil
import tempfile
import unittest
import mne
import moabb.datasets as db
import moabb.datasets.compound_dataset as db_compound
from moabb.datasets import Cattan2019_VR, Shin2017A, Shin2017B
from moabb.datasets.base import BaseDataset, is_abbrev, is_camel_kebab_case
from moabb.datasets.compound_dataset import CompoundDataset
from moabb.datasets.compound_dataset.utils import compound_dataset_list
from moabb.datasets.fake import FakeDataset, FakeVirtualRealityDataset
from moabb.datasets.utils import block_rep, dataset_list
from moabb.paradigms import P300
from moabb.utils import aliases_list
_ = mne.set_log_level("CRITICAL")
def _run_tests_on_dataset(d):
for s in d.subject_list:
data = d.get_data(subjects=[s])
# we should get a dict
assert isinstance(data, dict)
# We should get a raw array at the end
rawdata = data[s]["session_0"]["run_0"]
assert issubclass(type(rawdata), mne.io.BaseRaw), type(rawdata)
# print events
print(mne.find_events(rawdata))
print(d.event_id)
class TestRegex(unittest.TestCase):
def test_is_abbrev(self):
assert is_abbrev("a", "a-")
assert is_abbrev("a", "a0")
assert is_abbrev("a", "ab")
assert not is_abbrev("a", "aA")
assert not is_abbrev("a", "Aa")
assert not is_abbrev("a", "-a")
assert not is_abbrev("a", "0a")
assert not is_abbrev("a", "ba")
assert not is_abbrev("a", "a ")
def test_is_camell_kebab_case(self):
assert is_camel_kebab_case("Aa")
assert is_camel_kebab_case("aAa")
assert is_camel_kebab_case("Aa-a")
assert is_camel_kebab_case("1Aa-1a1")
assert is_camel_kebab_case("AB")
assert not is_camel_kebab_case("A ")
assert not is_camel_kebab_case(" A")
assert not is_camel_kebab_case("A A")
assert not is_camel_kebab_case("A_")
assert not is_camel_kebab_case("_A")
assert not is_camel_kebab_case("A_A")
class Test_Datasets(unittest.TestCase):
def test_fake_dataset(self):
"""This test will insure the basedataset works."""
n_subjects = 3
n_sessions = 2
n_runs = 2
for paradigm in ["imagery", "p300", "ssvep"]:
ds = FakeDataset(
n_sessions=n_sessions,
n_runs=n_runs,
n_subjects=n_subjects,
paradigm=paradigm,
)
data = ds.get_data()
# we should get a dict
self.assertTrue(isinstance(data, dict))
# we get the right number of subject
self.assertEqual(len(data), n_subjects)
# right number of session
self.assertEqual(len(data[1]), n_sessions)
# right number of run
self.assertEqual(len(data[1]["session_0"]), n_runs)
# We should get a raw array at the end
self.assertIsInstance(data[1]["session_0"]["run_0"], mne.io.BaseRaw)
# bad subject id must raise error
self.assertRaises(ValueError, ds.get_data, [1000])
def test_cache_dataset(self):
tempdir = tempfile.mkdtemp()
for paradigm in ["imagery", "p300", "ssvep"]:
dataset = FakeDataset(paradigm=paradigm)
# Save cache:
with self.assertLogs(
logger="moabb.datasets.bids_interface", level="INFO"
) as cm:
_ = dataset.get_data(
subjects=[1],
cache_config=dict(
save_raw=True,
use=True,
overwrite_raw=False,
path=tempdir,
),
)
print("\n".join(cm.output))
expected = [
"Attempting to retrieve cache .* datatype-eeg", # empty pipeline
"No cache found at",
"Starting caching .* datatype-eeg",
"Finished caching .* datatype-eeg",
]
self.assertEqual(len(expected), len(cm.output))
for i, regex in enumerate(expected):
self.assertRegex(cm.output[i], regex)
# Load cache:
with self.assertLogs(
logger="moabb.datasets.bids_interface", level="INFO"
) as cm:
_ = dataset.get_data(
subjects=[1],
cache_config=dict(
save_raw=True,
use=True,
overwrite_raw=False,
path=tempdir,
),
)
print("\n".join(cm.output))
expected = [
"Attempting to retrieve cache .* datatype-eeg",
"Finished reading cache .* datatype-eeg",
]
self.assertEqual(len(expected), len(cm.output))
for i, regex in enumerate(expected):
self.assertRegex(cm.output[i], regex)
# Overwrite cache:
with self.assertLogs(
logger="moabb.datasets.bids_interface", level="INFO"
) as cm:
_ = dataset.get_data(
subjects=[1],
cache_config=dict(
save_raw=True,
use=True,
overwrite_raw=True,
path=tempdir,
),
)
print("\n".join(cm.output))
expected = [
"Starting erasing cache .* datatype-eeg",
"Finished erasing cache .* datatype-eeg",
"Starting caching .* datatype-eeg",
"Finished caching .* datatype-eeg",
]
self.assertEqual(len(expected), len(cm.output))
for i, regex in enumerate(expected):
self.assertRegex(cm.output[i], regex)
shutil.rmtree(tempdir)
def test_dataset_accept(self):
"""Verify that accept licence is working."""
# Only BaseShin2017 (bbci_eeg_fnirs) for now
for ds in [Shin2017A(), Shin2017B()]:
# if the data is already downloaded:
if mne.get_config("MNE_DATASETS_BBCIFNIRS_PATH") is None:
self.assertRaises(AttributeError, ds.get_data, [1])
def test_datasets_init(self):
codes = []
logger = logging.getLogger("moabb.datasets.base")
deprecated_list, _, _ = zip(*aliases_list)
for ds in dataset_list:
kwargs = {}
if inspect.signature(ds).parameters.get("accept"):
kwargs["accept"] = True
with self.assertLogs(logger="moabb.datasets.base", level="WARNING") as cm:
# We test if the is_abrev does not throw a warning.
# Trick needed because assertNoLogs only inrtoduced in python 3.10:
logger.warning(f"Testing {ds.__name__}")
obj = ds(**kwargs)
if type(obj).__name__ not in deprecated_list:
self.assertEqual(len(cm.output), 1)
self.assertIsNotNone(obj)
if type(obj).__name__ not in deprecated_list:
codes.append(obj.code)
# Check that all codes are unique:
self.assertEqual(len(codes), len(set(codes)))
def test_depreciated_datasets_init(self):
depreciated_names, _, _ = zip(*aliases_list)
for ds in db.__dict__.values():
if ds in dataset_list:
continue
if not (inspect.isclass(ds) and issubclass(ds, BaseDataset)):
continue
kwargs = {}
if inspect.signature(ds).parameters.get("accept"):
kwargs["accept"] = True
with self.assertLogs(logger="moabb.utils", level="WARNING"):
# We test if depreciated_alias throws a warning.
obj = ds(**kwargs)
self.assertIsNotNone(obj)
self.assertIn(ds.__name__, depreciated_names)
def test_dataset_list(self):
if aliases_list:
depreciated_list, _, _ = zip(*aliases_list)
else:
pass
all_datasets = [
c
for c in db.__dict__.values()
if (
inspect.isclass(c)
and issubclass(c, BaseDataset)
# and c.__name__ not in depreciated_list
)
]
assert len(dataset_list) == len(all_datasets)
assert set(dataset_list) == set(all_datasets)
class Test_VirtualReality_Dataset(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def test_canary(self):
assert Cattan2019_VR() is not None
def test_warning_if_parameters_false(self):
with self.assertWarns(UserWarning):
Cattan2019_VR(virtual_reality=False, screen_display=False)
def test_data_path(self):
ds = Cattan2019_VR(virtual_reality=True, screen_display=True)
data_path = ds.data_path(1)
assert len(data_path) == 2
assert "subject_01_VR.mat" in data_path[0]
assert "subject_01_PC.mat" in data_path[1]
def test_get_block_repetition(self):
ds = FakeVirtualRealityDataset()
subject = 5
block = 3
repetition = 4
_, _, ret = ds.get_block_repetition(P300(), [subject], [block], [repetition])
assert ret.subject.unique()[0] == subject
assert ret.run.unique()[0] == block_rep(block, repetition)
class Test_CompoundDataset(unittest.TestCase):
def __init__(self, *args, **kwargs):
self.paradigm = "p300"
self.n_sessions = 2
self.n_subjects = 2
self.n_runs = 2
self.ds = FakeDataset(
n_sessions=self.n_sessions,
n_runs=self.n_runs,
n_subjects=self.n_subjects,
event_list=["Target", "NonTarget"],
paradigm=self.paradigm,
)
super().__init__(*args, **kwargs)
def test_fake_dataset(self):
"""This test will insure the basedataset works."""
param_list = [(None, None), ("session_0", "run_0"), (["session_0"], ["run_0"])]
for sessions, runs in param_list:
with self.subTest():
subjects_list = [(self.ds, 1, sessions, runs)]
compound_data = CompoundDataset(
subjects_list,
events=dict(Target=2, NonTarget=1),
code="CompoundDataset-test",
interval=[0, 1],
paradigm=self.paradigm,
)
data = compound_data.get_data()
# Check data type
self.assertTrue(isinstance(data, dict))
self.assertIsInstance(data[1]["session_0"]["run_0"], mne.io.BaseRaw)
# Check data size
self.assertEqual(len(data), 1)
expected_session_number = self.n_sessions if sessions is None else 1
self.assertEqual(len(data[1]), expected_session_number)
expected_runs_number = self.n_runs if runs is None else 1
self.assertEqual(len(data[1]["session_0"]), expected_runs_number)
# bad subject id must raise error
self.assertRaises(ValueError, compound_data.get_data, [1000])
def test_compound_dataset_composition(self):
# Test we can compound two instance of CompoundDataset into a new one.
# Create an instance of CompoundDataset with one subject
subjects_list = [(self.ds, 1, None, None)]
compound_dataset = CompoundDataset(
subjects_list,
events=dict(Target=2, NonTarget=1),
code="CompoundDataset-test",
interval=[0, 1],
paradigm=self.paradigm,
)
# Add it two time to a subjects_list
subjects_list = [compound_dataset, compound_dataset]
compound_data = CompoundDataset(
subjects_list,
events=dict(Target=2, NonTarget=1),
code="CompoundDataset-test",
interval=[0, 1],
paradigm=self.paradigm,
)
# Assert that the coumpouned dataset has two times more subject than the original one.
data = compound_data.get_data()
self.assertEqual(len(data), 2)
def test_get_sessions_per_subject(self):
# define a new fake dataset with two times more sessions:
self.ds2 = FakeDataset(
n_sessions=self.n_sessions * 2,
n_runs=self.n_runs,
n_subjects=self.n_subjects,
event_list=["Target", "NonTarget"],
paradigm=self.paradigm,
)
# Add the two datasets to a CompoundDataset
subjects_list = [(self.ds, 1, None, None), (self.ds2, 1, None, None)]
compound_dataset = CompoundDataset(
subjects_list,
events=dict(Target=2, NonTarget=1),
code="CompoundDataset",
interval=[0, 1],
paradigm=self.paradigm,
)
# Test private method _get_sessions_per_subject returns the minimum number of sessions per subjects
self.assertEqual(compound_dataset._get_sessions_per_subject(), self.n_sessions)
def test_datasets_init(self):
codes = []
for ds in compound_dataset_list:
kwargs = {}
if inspect.signature(ds).parameters.get("accept"):
kwargs["accept"] = True
obj = ds(**kwargs)
self.assertIsNotNone(obj)
codes.append(obj.code)
# Check that all codes are unique:
self.assertEqual(len(codes), len(set(codes)))
def test_dataset_list(self):
if aliases_list:
depreciated_list, _, _ = zip(*aliases_list)
else:
depreciated_list = []
all_datasets = [
c
for c in db_compound.__dict__.values()
if (
inspect.isclass(c)
and issubclass(c, CompoundDataset)
and c.__name__ not in depreciated_list
and c.__name__ != "CompoundDataset"
)
]
assert len(compound_dataset_list) == len(all_datasets)
assert set(compound_dataset_list) == set(all_datasets)