-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspect_Data_Raw.py
149 lines (120 loc) · 4.89 KB
/
Inspect_Data_Raw.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
"""
Script to load the Raw data in the BIDS Format
"""
##
# ---------------------------------------------------------------------------------------------------------------------
# Cell to Import Libraries
# ---------------------------------------------------------------------------------------------------------------------
import matplotlib
import matplotlib.pyplot as plt
import pathlib
import mne
from mne import Epochs, pick_types, events_from_annotations
from mne.channels import make_standard_montage
from mne.io import concatenate_raws, read_raw_edf
from mne.datasets import eegbci
from mne.decoding import CSP
import mne_bids
# Define variable
from mne.datasets import eegbci
subject = 1
runs = [6, 10, 14] # motor imagery: hands vs feet
tmin, tmax = -0.5, 0.5
event_id = dict(rest=1,
hands=2,
feet=3)
##
# ---------------------------------------------------------------------------------------------------------------------
# Cell to Import dataset of the subject for the motor imagery: hands vs feet
# ---------------------------------------------------------------------------------------------------------------------
# Load the raw data after the fixing
raw_path = 'Data_Out/fixed_raw.fif'
raw = mne.io.read_raw_fif(raw_path, preload=True)
# Create Events
events, _ = events_from_annotations(raw, event_id=dict(T0=1, T1=2, T2=3))
# Define the picks
picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads')
# Select only the EEG channel (create a copy of our raw data)
raw_eeg = raw.copy().pick_types(meg=False, eeg=True, eog=False, exclude='bads')
# Filter the data between [7, 30] Hertz
raw_eeg_filtered = raw_eeg.copy().filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
##
# ---------------------------------------------------------------------------------------------------------------------
# Display the information about the raw file
# ---------------------------------------------------------------------------------------------------------------------
print(raw_eeg_filtered.info)
print(raw_eeg_filtered.ch_names)
##
# ---------------------------------------------------------------------------------------------------------------------
# Plot Raw data
# ---------------------------------------------------------------------------------------------------------------------
# Plot data channel
# raw_eeg_filtered.plot()
# plt.show()
# Plot Sensors eeg
# raw_eeg_filtered.plot_sensors(ch_type='eeg')
# plt.show()
# Plot Sensors eeg 3D
# raw_eeg_filtered.plot_sensors(kind='3d', ch_type='eeg')
# plt.show()
# Plot with events id
# raw_eeg_filtered.plot(events=events, event_id=event_id)
# plt.show()
##
# ---------------------------------------------------------------------------------------------------------------------
# TODO Save data in bids mode
# ---------------------------------------------------------------------------------------------------------------------
"""
out_path = pathlib.Path('Data_BIDS/sample_BIDS')
# Define the BIDS path, we need only to specify the entities.
bids_path = mne_bids.BIDSPath(subject='01',
session='01',
task='motor imagery: hands vs feet',
run='01',
root=out_path)
# Write the BIDS files
mne_bids.write_raw_bids(raw_eeg_filtered,
bids_path=bids_path,
events_data=events,
event_id=event_id,
overwrite=True,
allow_preload=True)
"""
##
# ---------------------------------------------------------------------------------------------------------------------
# Epoch and Evoked
# ---------------------------------------------------------------------------------------------------------------------
epochs = Epochs(raw_eeg_filtered,
events,
event_id,
tmin,
tmax,
proj=True,
baseline=None,
preload=True)
print(epochs)
# Plot Epochs
# epochs.plot()
# plt.show()
# epochs.plot_image()
# plt.show()
# Save the Epoch (note that _epo.fif is quite a convention)
epochs.save('Data_Out/epochs_epo.fif', overwrite=True)
#Create the Evoked
evoked_hands = epochs['hands'].average()
evoked_feet = epochs['feet'].average()
#Plot Evoked
# evoked_hands.plot(spatial_colors=True)
# plt.show()
# evoked_hands.plot_topomap()
# plt.show()
# evoked_hands.plot_joint()
# plt.show()
# mne.viz.plot_compare_evokeds([evoked_hands, evoked_feet])
# plt.show()
# Save the evoked
mne.write_evokeds('Data_Out/evokeds_ave.fif', evoked=[evoked_hands, evoked_feet])
##
# ---------------------------------------------------------------------------------------------------------------------
# TODO Preprocessing
# ---------------------------------------------------------------------------------------------------------------------