1
+ #find the factors of a number |to add extra dimension for CNN|
2
+ def factors (n ):
3
+ return [i for i in range (1 , n + 1 ) if not n % i ]
4
+
5
+ ##Setup TensorFlow
6
+ def SetupTensorFlow ():
7
+
8
+ #run tensorboard
9
+ LOG_DIR = './log'
10
+ get_ipython ().system_raw (
11
+ 'tensorboard --logdir {} --host 0.0.0.0 --port 6007 &'
12
+ .format (LOG_DIR )
13
+ )
14
+
15
+ #run ngork
16
+ get_ipython ().system_raw ('./ngrok http 6007 &' )
17
+
18
+ #get url
19
+ ! curl - s http :// localhost :4040 / api / tunnels | python3 - c \
20
+ "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
21
+
22
+
23
+ #load in recorder data files
24
+ def load_data (filename ,data_type = 'muse' ,plot_sensors = True ,plot_raw = True ,plot_raw_psd = True ,stim_channel = False , ):
25
+
26
+
27
+ #load .vhdr files from brain vision recorder
28
+ raw = io .read_raw_brainvision (filename ,
29
+ montage = 'standard_1020' ,
30
+ eog = ('HEOG' , 'VEOG' ),
31
+ preload = True ,stim_channel = stim_channel )
32
+
33
+ #set sampling rate
34
+ sfreq = raw .info ['sfreq' ]
35
+ print ('Sampling Rate = ' + str (sfreq ))
36
+
37
+ #load channel locations
38
+ print ('Loading Channel Locations' )
39
+ if plot_sensors :
40
+ raw .plot_sensors (show_names = 'True' )
41
+
42
+ ##Plot raw data
43
+ if plot_raw :
44
+ raw .plot (n_channels = 16 , block = True )
45
+
46
+ #plot raw psd
47
+ if plot_raw_psd :
48
+ raw .plot_psd (fmin = .1 , fmax = 100 )
49
+
50
+ return raw , sfreq
51
+
52
+
53
+ #from eeg-notebooks
54
+ def load_muse_csv_as_raw (filename , sfreq = 256. , ch_ind = [0 , 1 , 2 , 3 ],
55
+ stim_ind = 5 , replace_ch_names = None , verbose = 1 ):
56
+ """Load CSV files into a Raw object.
57
+
58
+ Args:
59
+ filename (str or list): path or paths to CSV files to load
60
+
61
+ Keyword Args:
62
+ subject_nb (int or str): subject number. If 'all', load all
63
+ subjects.
64
+ session_nb (int or str): session number. If 'all', load all
65
+ sessions.
66
+ sfreq (float): EEG sampling frequency
67
+ ch_ind (list): indices of the EEG channels to keep
68
+ stim_ind (int): index of the stim channel
69
+ replace_ch_names (dict or None): dictionary containing a mapping to
70
+ rename channels. Useful when an external electrode was used.
71
+
72
+ Returns:
73
+ (mne.io.array.array.RawArray): loaded EEG
74
+ """
75
+ n_channel = len (ch_ind )
76
+
77
+ raw = []
78
+ for fname in filename :
79
+ # read the file
80
+ data = pd .read_csv (fname , index_col = 0 )
81
+
82
+ # name of each channels
83
+ ch_names = list (data .columns )[0 :n_channel ] + ['Stim' ]
84
+
85
+ if replace_ch_names is not None :
86
+ ch_names = [c if c not in replace_ch_names .keys ()
87
+ else replace_ch_names [c ] for c in ch_names ]
88
+
89
+ # type of each channels
90
+ ch_types = ['eeg' ] * n_channel + ['stim' ]
91
+ montage = read_montage ('standard_1005' )
92
+
93
+ # get data and exclude Aux channel
94
+ data = data .values [:, ch_ind + [stim_ind ]].T
95
+
96
+ # convert in Volts (from uVolts)
97
+ data [:- 1 ] *= 1e-6
98
+
99
+ # create MNE object
100
+ info = create_info (ch_names = ch_names , ch_types = ch_types ,
101
+ sfreq = sfreq , montage = montage , verbose = verbose )
102
+ raw .append (RawArray (data = data , info = info , verbose = verbose ))
103
+
104
+ # concatenate all raw objects
105
+ raws = concatenate_raws (raw , verbose = verbose )
106
+
107
+ return raws
108
+
109
+ #from eeg-notebooks load_data
110
+ def muse_load_data (data_dir , subject_nb = 1 , session_nb = 1 , sfreq = 256. ,
111
+ ch_ind = [0 , 1 , 2 , 3 ], stim_ind = 5 , replace_ch_names = None , verbose = 1 ):
112
+ """Load CSV files from the /data directory into a Raw object.
113
+
114
+ Args:
115
+ data_dir (str): directory inside /data that contains the
116
+ CSV files to load, e.g., 'auditory/P300'
117
+
118
+ Keyword Args:
119
+ subject_nb (int or str): subject number. If 'all', load all
120
+ subjects.
121
+ session_nb (int or str): session number. If 'all', load all
122
+ sessions.
123
+ sfreq (float): EEG sampling frequency
124
+ ch_ind (list): indices of the EEG channels to keep
125
+ stim_ind (int): index of the stim channel
126
+ replace_ch_names (dict or None): dictionary containing a mapping to
127
+ rename channels. Useful when an external electrode was used.
128
+
129
+ Returns:
130
+ (mne.io.array.array.RawArray): loaded EEG
131
+ """
132
+ if subject_nb == 'all' :
133
+ subject_nb = '*'
134
+ if session_nb == 'all' :
135
+ session_nb = '*'
136
+
137
+ data_path = os .path .join (
138
+ 'eeg-notebooks/data' , data_dir ,
139
+ 'subject{}/session{}/*.csv' .format (subject_nb , session_nb ))
140
+ fnames = glob (data_path )
141
+
142
+ return load_muse_csv_as_raw (fnames , sfreq = sfreq , ch_ind = ch_ind ,
143
+ stim_ind = stim_ind ,
144
+ replace_ch_names = replace_ch_names , verbose = verbose )
145
+
0 commit comments