-
Notifications
You must be signed in to change notification settings - Fork 183
/
noplot_bids_conversion.py
134 lines (114 loc) · 4.98 KB
/
noplot_bids_conversion.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
"""
===============================
Convert a MOABB dataset to BIDS
===============================
The Brain Imaging Data Structure (BIDS) format
is standard for storing neuroimaging data.
It follows fixed principles to facilitate the
sharing of neuroimaging data between researchers.
The MOABB library allows to convert any MOABB dataset to
BIDS [1]_ and [2]_.
In this example, we will convert the AlexMI dataset to BIDS using the
option ``cache_config=dict(path=temp_dir, save_raw=True)`` of the ``get_data``
method from the dataset object.
This will automatically save the raw data in the BIDS format and allow to use
a cache for the next time the dataset is used.
We will use the AlexMI dataset [3]_, one of the smallest in
people and one that can be downloaded quickly.
"""
# Authors: Pierre Guetschel <[email protected]>
#
# License: BSD (3-clause)
import shutil
import tempfile
from pathlib import Path
import mne
from moabb import set_log_level
from moabb.datasets import AlexMI
set_log_level("info")
###############################################################################
# Basic usage
# -----------
#
# Here, we will save the BIDS version of the dataset in a temporary folder
temp_dir = Path(tempfile.mkdtemp())
# The conversion of any MOABB dataset to a BIDS-compliant structure can be done
# by simply calling its ``get_data`` method and using the ``cache_config``
# parameter. This parameter is a dictionary.
dataset = AlexMI()
_ = dataset.get_data(cache_config=dict(path=temp_dir, save_raw=True))
###############################################################################
# Before / after folder structure
# -----------------------------
#
# To investigate what was saved, we will first define a function to print
# the folder structure of a given path:
def print_tree(p: Path, last=True, header=""):
elbow = "└──"
pipe = "│ "
tee = "├──"
blank = " "
print(header + (elbow if last else tee) + p.name)
if p.is_dir():
children = list(p.iterdir())
for i, c in enumerate(children):
print_tree(
c, header=header + (blank if last else pipe), last=i == len(children) - 1
)
###############################################################################
# Now, we will retrieve the location of the original dataset. It is stored
# in the MNE data directory, which can be found with the ``"MNE_DATA"`` key:
mne_data = Path(mne.get_config("MNE_DATA"))
print(f"MNE data directory: {mne_data}")
###############################################################################
# Now, we can print the folder structure of the original dataset:
print("Before conversion:")
print_tree(mne_data / "MNE-alexeeg-data")
###############################################################################
# As we can see, before conversion, all the data (i.e. from all subjects,
# sessions and runs) is stored in a single folder. This follows no particular
# standard and can vary from one dataset to another.
#
# After conversion, the data is stored in a BIDS-compliant way:
print("After conversion:")
print_tree(temp_dir / "MNE-BIDS-alexandre-motor-imagery")
###############################################################################
# In the BIDS version of our dataset, the raw files are saved in EDF.
# The data is organized in a hierarchy of folders,
# starting with the subjects, then the sessions, and then the runs. Metadata
# files are stored to describe the data. For more details on the BIDS
# structure, please refer to the `BIDS website <https://bids.neuroimaging.io>`_
# and the `BIDS spec <https://bids-specification.readthedocs.io/en/stable/>`_.
#
# Under the hood, saving datasets to BIDS is done through the caching system
# of MOABB. Only raw EEG files are officially supported by the BIDS
# specification.
# However, MOABB's caching mechanism also offers the possibility to save
# the data in a pseudo-BIDS after different preprocessing steps.
# In particular, we can save :class:`mne.Epochs` and ``np.ndarray`` objects.
# For more details on the caching system,
# please refer to the tutorial :doc:`./plot_disk_cache`.
#
# Cleanup
# -------
#
# Finally, we can delete the temporary folder:
shutil.rmtree(temp_dir)
###############################################################################
# References
# -----------
#
# .. [1] Pernet, C.R., Appelhoff, S., Gorgolewski, K.J. et al. EEG-BIDS,
# An extension to the brain imaging data structure for
# electroencephalography. Sci Data 6, 103 (2019).
# https://doi.org/10.1038/s41597-019-0104-8
#
# .. [2] Appelhoff et al., (2019). MNE-BIDS: Organizing electrophysiological
# data into the BIDS format and facilitating their analysis.
# Journal of Open Source Software, 4(44), 1896,
# https://doi.org/10.21105/joss.01896
#
# .. [3] Barachant, A., 2012. Commande robuste d'un effecteur par une
# interface cerveau machine EEG asynchrone (Doctoral dissertation,
# Université de Grenoble).
# https://tel.archives-ouvertes.fr/tel-01196752