Define an electrophysiological dataset using MNE-Python structures#

This example illustrates how to define a dataset using MNE-Python Epochs.

import numpy as np

from mne import EpochsArray, create_info
from frites.dataset import DatasetEphy
from frites import set_mpl_style

import matplotlib.pyplot as plt
set_mpl_style()

Create artificial data#

We start by creating some random data for several subjects. To do that, each subject is going have a 3 dimensional array of shape (n_epochs, n_channels, n_times). Then, all of the arrays are grouped together in a list of length (n_subjects,)

n_subjects = 5
n_epochs = 10
n_channels = 5
n_times = 100
sf = 512

x, ch = [], []
for k in range(n_subjects):
    # generate single subject data
    x_suj = np.random.rand(n_epochs, n_channels, n_times)
    # generate some random channel names
    ch_suj = np.array([f"ch_{r}" for r in range(n_channels)])
    # concatenate in a list
    x.append(x_suj)
    ch.append(ch_suj)
# finally lets create a time vector
times = np.arange(n_times) / sf

MNE conversion to Epoch#

Here, we convert our illustrative data into EpochsArray. With real data, you are probbaly going to have mne.Epochs objects which also going to work just the same

x_mne = []
for k in range(n_subjects):
    # create some informations
    info = create_info(ch[k].tolist(), sf)
    # create the Epoch of this subject
    epoch = EpochsArray(x[k], info, tmin=times[0], verbose=False)
    # finally, replace it in the original list
    x_mne.append(epoch)
print(x_mne[0])
<EpochsArray |  10 events (all good), 0 – 0.193359 s, baseline off, ~49 kB, data loaded,
 '1': 10>

Build the dataset#

Finally, we pass the data to the frites.dataset.DatasetEphy class in order to create the dataset

dt = DatasetEphy(x_mne)
print(dt)

print('Time vector : ', dt.times)
print('ROI DataFrame\n: ', dt.df_rs)
<xarray.DataArray 'subject_0' (trials: 10, roi: 5, times: 100)>
array([[[0.24392333, 0.65495435, 0.35150695, ..., 0.75384745,
         0.93627024, 0.3299055 ],
        [0.3110566 , 0.87868043, 0.06982022, ..., 0.90765657,
         0.67911187, 0.19209818],
        [0.36660264, 0.02974329, 0.95329504, ..., 0.48321024,
         0.60814371, 0.93922705],
        [0.79902297, 0.37618136, 0.58825882, ..., 0.43421206,
         0.33749481, 0.94168455],
        [0.76155977, 0.80324398, 0.25562594, ..., 0.24869778,
         0.9335653 , 0.22747919]],

       [[0.71605324, 0.92937511, 0.91069937, ..., 0.99355173,
         0.22700501, 0.49485814],
        [0.12091355, 0.10959777, 0.00516555, ..., 0.47622637,
         0.17127595, 0.68506362],
        [0.70785012, 0.38955529, 0.76906054, ..., 0.16161444,
         0.06978964, 0.23431213],
        [0.72875621, 0.87612937, 0.03940922, ..., 0.19273235,
         0.76165237, 0.43280645],
        [0.59328406, 0.80146893, 0.63416803, ..., 0.89278915,
...
         0.31650509, 0.03652929],
        [0.41709039, 0.90968837, 0.35487336, ..., 0.83509349,
         0.74766765, 0.17380476],
        [0.97159376, 0.7545753 , 0.10104604, ..., 0.93820243,
         0.62185655, 0.19636822],
        [0.21483059, 0.74461171, 0.2580803 , ..., 0.45927542,
         0.30866759, 0.39530782],
        [0.1791466 , 0.32694537, 0.15613618, ..., 0.89519947,
         0.07556917, 0.47114553]],

       [[0.1810615 , 0.54071795, 0.63001438, ..., 0.24662136,
         0.45418379, 0.80146086],
        [0.28820267, 0.12738988, 0.41164697, ..., 0.51239921,
         0.49802141, 0.18298122],
        [0.61265254, 0.27139124, 0.70616473, ..., 0.36432601,
         0.43102553, 0.70709125],
        [0.22926219, 0.66829409, 0.26836332, ..., 0.55201116,
         0.59011918, 0.46382726],
        [0.01851871, 0.38966844, 0.11513355, ..., 0.78412312,
         0.61925482, 0.15752893]]])
Coordinates:
  * trials   (trials) int64 0 1 2 3 4 5 6 7 8 9
  * roi      (roi) <U4 'ch_0' 'ch_1' 'ch_2' 'ch_3' 'ch_4'
    agg_ch   (roi) int64 0 0 0 0 0
  * times    (times) float64 0.0 0.001953 0.003906 ... 0.1895 0.1914 0.1934
    subject  (trials) int64 0 0 0 0 0 0 0 0 0 0
Attributes:
    __version__:   0.4.5
    modality:      electrophysiology
    dtype:         SubjectEphy
    y_dtype:       none
    z_dtype:       none
    mi_type:       none
    mi_repr:       none
    sfreq:         512.0
    agg_ch:        1
    multivariate:  0
<xarray.DataArray 'subject_1' (trials: 10, roi: 5, times: 100)>
array([[[0.01109675, 0.00176979, 0.15505512, ..., 0.45284762,
         0.59954199, 0.2719661 ],
        [0.96102724, 0.04345081, 0.72659793, ..., 0.52574985,
         0.00683546, 0.86238922],
        [0.12650748, 0.74559266, 0.72083238, ..., 0.0294278 ,
         0.53947405, 0.21383679],
        [0.61218365, 0.45715353, 0.35434214, ..., 0.16090124,
         0.87208857, 0.01881678],
        [0.97678488, 0.12174478, 0.16746949, ..., 0.29672356,
         0.04980363, 0.62885735]],

       [[0.53937919, 0.06376116, 0.03691362, ..., 0.94779467,
         0.85471542, 0.41874634],
        [0.55564718, 0.83965301, 0.23306488, ..., 0.4001393 ,
         0.7145485 , 0.29302891],
        [0.60144183, 0.29616752, 0.1655798 , ..., 0.62902752,
         0.93831673, 0.26722398],
        [0.94567553, 0.15444464, 0.78849142, ..., 0.42681579,
         0.37615727, 0.57259254],
        [0.10025302, 0.99448565, 0.87103453, ..., 0.72900585,
...
         0.5213406 , 0.43386368],
        [0.91233447, 0.88570529, 0.86468671, ..., 0.84050424,
         0.66784162, 0.17469976],
        [0.50634435, 0.12160911, 0.52991834, ..., 0.69923107,
         0.85486006, 0.98326732],
        [0.24818939, 0.3976172 , 0.06209891, ..., 0.88555227,
         0.33731101, 0.47106099],
        [0.5582356 , 0.57802422, 0.68296597, ..., 0.08616856,
         0.38576078, 0.75506298]],

       [[0.24089265, 0.76595472, 0.00714221, ..., 0.14817551,
         0.36155844, 0.40182818],
        [0.94620349, 0.07018871, 0.80118731, ..., 0.13793289,
         0.89391399, 0.67439013],
        [0.40669996, 0.68865767, 0.4992233 , ..., 0.13865445,
         0.73507301, 0.18129449],
        [0.18627529, 0.45228337, 0.42723941, ..., 0.4991425 ,
         0.60134917, 0.77386409],
        [0.26973761, 0.14448086, 0.78933364, ..., 0.59208601,
         0.45728648, 0.55747382]]])
Coordinates:
  * trials   (trials) int64 0 1 2 3 4 5 6 7 8 9
  * roi      (roi) <U4 'ch_0' 'ch_1' 'ch_2' 'ch_3' 'ch_4'
    agg_ch   (roi) int64 0 0 0 0 0
  * times    (times) float64 0.0 0.001953 0.003906 ... 0.1895 0.1914 0.1934
    subject  (trials) int64 1 1 1 1 1 1 1 1 1 1
Attributes:
    __version__:   0.4.5
    modality:      electrophysiology
    dtype:         SubjectEphy
    y_dtype:       none
    z_dtype:       none
    mi_type:       none
    mi_repr:       none
    sfreq:         512.0
    agg_ch:        1
    multivariate:  0
<xarray.DataArray 'subject_2' (trials: 10, roi: 5, times: 100)>
array([[[0.51581483, 0.95028891, 0.3888361 , ..., 0.10959219,
         0.47453192, 0.44606687],
        [0.65847037, 0.47130979, 0.67203554, ..., 0.62118374,
         0.46666992, 0.26672759],
        [0.64807381, 0.24777647, 0.11131784, ..., 0.93289054,
         0.46018623, 0.40699517],
        [0.90758875, 0.30291291, 0.24209164, ..., 0.03279871,
         0.75713367, 0.37236105],
        [0.02202746, 0.00932916, 0.01339035, ..., 0.46286979,
         0.85665419, 0.4872235 ]],

       [[0.7777711 , 0.55748914, 0.98574848, ..., 0.91798246,
         0.25489224, 0.50705835],
        [0.36248442, 0.50160319, 0.99962982, ..., 0.92314743,
         0.81968621, 0.46867419],
        [0.48178369, 0.18603546, 0.26248367, ..., 0.90772469,
         0.84959545, 0.48859922],
        [0.46675039, 0.58862497, 0.97976884, ..., 0.18438617,
         0.14433657, 0.98559311],
        [0.06389391, 0.42291312, 0.90768166, ..., 0.4520585 ,
...
         0.23274637, 0.85764766],
        [0.86262341, 0.36115707, 0.31575497, ..., 0.55765498,
         0.07969845, 0.24284618],
        [0.40113791, 0.36688194, 0.20798164, ..., 0.15687717,
         0.33361071, 0.73073486],
        [0.34390796, 0.08748721, 0.49332186, ..., 0.15175538,
         0.43770247, 0.88636801],
        [0.08106464, 0.25920657, 0.43460526, ..., 0.37084312,
         0.98002021, 0.38036088]],

       [[0.31787268, 0.33901538, 0.16138566, ..., 0.63222269,
         0.8402969 , 0.44492924],
        [0.59753713, 0.52374904, 0.00125058, ..., 0.38727654,
         0.9537937 , 0.83938407],
        [0.39440674, 0.48919191, 0.005361  , ..., 0.88669946,
         0.75097974, 0.40812447],
        [0.2985188 , 0.45829102, 0.12191161, ..., 0.46862567,
         0.51352981, 0.79236877],
        [0.43251311, 0.00900492, 0.18810681, ..., 0.56425734,
         0.60526059, 0.93545513]]])
Coordinates:
  * trials   (trials) int64 0 1 2 3 4 5 6 7 8 9
  * roi      (roi) <U4 'ch_0' 'ch_1' 'ch_2' 'ch_3' 'ch_4'
    agg_ch   (roi) int64 0 0 0 0 0
  * times    (times) float64 0.0 0.001953 0.003906 ... 0.1895 0.1914 0.1934
    subject  (trials) int64 2 2 2 2 2 2 2 2 2 2
Attributes:
    __version__:   0.4.5
    modality:      electrophysiology
    dtype:         SubjectEphy
    y_dtype:       none
    z_dtype:       none
    mi_type:       none
    mi_repr:       none
    sfreq:         512.0
    agg_ch:        1
    multivariate:  0
<xarray.DataArray 'subject_3' (trials: 10, roi: 5, times: 100)>
array([[[0.19112391, 0.05854766, 0.43681052, ..., 0.98285116,
         0.75960944, 0.56082868],
        [0.68743319, 0.95944606, 0.54110355, ..., 0.85549807,
         0.63384251, 0.12465387],
        [0.07699636, 0.52816128, 0.58944985, ..., 0.21595938,
         0.04935497, 0.848193  ],
        [0.85337132, 0.23613114, 0.85887024, ..., 0.63804154,
         0.57225554, 0.05314667],
        [0.86561985, 0.80783732, 0.73797733, ..., 0.72411739,
         0.96091459, 0.1326284 ]],

       [[0.42140534, 0.37790698, 0.00808216, ..., 0.13832723,
         0.52820552, 0.01815526],
        [0.83772931, 0.89204519, 0.85892773, ..., 0.27244483,
         0.64041396, 0.62111615],
        [0.39675014, 0.55016239, 0.03579929, ..., 0.21352248,
         0.16069066, 0.56689289],
        [0.92848866, 0.72602818, 0.57983457, ..., 0.79242554,
         0.34400905, 0.35637945],
        [0.26435496, 0.09152404, 0.41832919, ..., 0.30127874,
...
         0.47819327, 0.62576883],
        [0.27082248, 0.5636694 , 0.4572046 , ..., 0.21566352,
         0.20891019, 0.84209603],
        [0.80166917, 0.3996219 , 0.85685233, ..., 0.09643161,
         0.20417991, 0.11488457],
        [0.52324762, 0.63761337, 0.42255344, ..., 0.42335149,
         0.14261326, 0.44492949],
        [0.17777531, 0.20606367, 0.05732538, ..., 0.61222859,
         0.2601258 , 0.02038846]],

       [[0.95433104, 0.07217132, 0.09010197, ..., 0.40614839,
         0.2623648 , 0.8129389 ],
        [0.14624277, 0.16398797, 0.83541745, ..., 0.48746753,
         0.90240508, 0.92632065],
        [0.84035788, 0.9764717 , 0.85184826, ..., 0.34881195,
         0.32055504, 0.59203046],
        [0.5991912 , 0.96951956, 0.39432461, ..., 0.91030387,
         0.42970534, 0.73500405],
        [0.38844779, 0.0480029 , 0.15251854, ..., 0.49011981,
         0.0430546 , 0.64650278]]])
Coordinates:
  * trials   (trials) int64 0 1 2 3 4 5 6 7 8 9
  * roi      (roi) <U4 'ch_0' 'ch_1' 'ch_2' 'ch_3' 'ch_4'
    agg_ch   (roi) int64 0 0 0 0 0
  * times    (times) float64 0.0 0.001953 0.003906 ... 0.1895 0.1914 0.1934
    subject  (trials) int64 3 3 3 3 3 3 3 3 3 3
Attributes:
    __version__:   0.4.5
    modality:      electrophysiology
    dtype:         SubjectEphy
    y_dtype:       none
    z_dtype:       none
    mi_type:       none
    mi_repr:       none
    sfreq:         512.0
    agg_ch:        1
    multivariate:  0
<xarray.DataArray 'subject_4' (trials: 10, roi: 5, times: 100)>
array([[[0.96313608, 0.61881857, 0.11579477, ..., 0.10670696,
         0.37226264, 0.66481021],
        [0.71629345, 0.30617858, 0.84802591, ..., 0.02707331,
         0.32840594, 0.01588814],
        [0.88415412, 0.5712669 , 0.28411945, ..., 0.55713675,
         0.67094586, 0.86149949],
        [0.59120621, 0.56605371, 0.51685115, ..., 0.76287029,
         0.59916864, 0.51235271],
        [0.95963307, 0.3884158 , 0.15728345, ..., 0.75118109,
         0.55464832, 0.26593243]],

       [[0.02760775, 0.08173029, 0.293669  , ..., 0.9336578 ,
         0.45489323, 0.16932518],
        [0.04195368, 0.16490623, 0.25099848, ..., 0.29583988,
         0.49688914, 0.54716939],
        [0.52323549, 0.28077145, 0.20884221, ..., 0.92226945,
         0.58961377, 0.32022785],
        [0.85765427, 0.61893106, 0.43780174, ..., 0.58446624,
         0.18915154, 0.84550671],
        [0.54754623, 0.25526766, 0.64168355, ..., 0.12260603,
...
         0.13758326, 0.41077202],
        [0.84638027, 0.98380405, 0.74107143, ..., 0.81495978,
         0.78667031, 0.0113469 ],
        [0.15206914, 0.14219443, 0.60842978, ..., 0.18452566,
         0.67846034, 0.97917668],
        [0.53046042, 0.90742149, 0.57439819, ..., 0.69266066,
         0.09664642, 0.66421804],
        [0.61849359, 0.27357108, 0.41722557, ..., 0.35432996,
         0.48737692, 0.75225962]],

       [[0.52289232, 0.48686443, 0.24256897, ..., 0.39883038,
         0.8833534 , 0.7931012 ],
        [0.16060307, 0.7799293 , 0.1054113 , ..., 0.64910971,
         0.52654156, 0.02859812],
        [0.1755579 , 0.07195134, 0.6645303 , ..., 0.00219381,
         0.79661686, 0.15715099],
        [0.16175203, 0.20587649, 0.93604932, ..., 0.44172662,
         0.85701721, 0.17779312],
        [0.07849112, 0.27611425, 0.0851793 , ..., 0.81013284,
         0.55103228, 0.22575586]]])
Coordinates:
  * trials   (trials) int64 0 1 2 3 4 5 6 7 8 9
  * roi      (roi) <U4 'ch_0' 'ch_1' 'ch_2' 'ch_3' 'ch_4'
    agg_ch   (roi) int64 0 0 0 0 0
  * times    (times) float64 0.0 0.001953 0.003906 ... 0.1895 0.1914 0.1934
    subject  (trials) int64 4 4 4 4 4 4 4 4 4 4
Attributes:
    __version__:   0.4.5
    modality:      electrophysiology
    dtype:         SubjectEphy
    y_dtype:       none
    z_dtype:       none
    mi_type:       none
    mi_repr:       none
    sfreq:         512.0
    agg_ch:        1
    multivariate:  0
Time vector :  [0.         0.00195312 0.00390625 0.00585938 0.0078125  0.00976562
 0.01171875 0.01367188 0.015625   0.01757812 0.01953125 0.02148438
 0.0234375  0.02539062 0.02734375 0.02929688 0.03125    0.03320312
 0.03515625 0.03710938 0.0390625  0.04101562 0.04296875 0.04492188
 0.046875   0.04882812 0.05078125 0.05273438 0.0546875  0.05664062
 0.05859375 0.06054688 0.0625     0.06445312 0.06640625 0.06835938
 0.0703125  0.07226562 0.07421875 0.07617188 0.078125   0.08007812
 0.08203125 0.08398438 0.0859375  0.08789062 0.08984375 0.09179688
 0.09375    0.09570312 0.09765625 0.09960938 0.1015625  0.10351562
 0.10546875 0.10742188 0.109375   0.11132812 0.11328125 0.11523438
 0.1171875  0.11914062 0.12109375 0.12304688 0.125      0.12695312
 0.12890625 0.13085938 0.1328125  0.13476562 0.13671875 0.13867188
 0.140625   0.14257812 0.14453125 0.14648438 0.1484375  0.15039062
 0.15234375 0.15429688 0.15625    0.15820312 0.16015625 0.16210938
 0.1640625  0.16601562 0.16796875 0.16992188 0.171875   0.17382812
 0.17578125 0.17773438 0.1796875  0.18164062 0.18359375 0.18554688
 0.1875     0.18945312 0.19140625 0.19335938]
ROI DataFrame
:        #subjects         subjects  keep
roi
ch_0          5  [0, 1, 2, 3, 4]  True
ch_1          5  [0, 1, 2, 3, 4]  True
ch_2          5  [0, 1, 2, 3, 4]  True
ch_3          5  [0, 1, 2, 3, 4]  True
ch_4          5  [0, 1, 2, 3, 4]  True

Total running time of the script: (0 minutes 0.593 seconds)

Estimated memory usage: 9 MB

Gallery generated by Sphinx-Gallery