11"""
22Using fooof with MNE
3- =======================
3+ ====================
44
5- This examples illustrates how to use fooof with ` MNE
6- <https://mne-tools.github.io/>`_ and create topographical plots
5+ This examples illustrates how to use fooof with MNE
6+ and create topographical plots
77
8- This tutorial does require that you have MNE installed. If you don't already have
8+ This tutorial does require that you have `MNE
9+ <https://mne-tools.github.io/>`_ installed. If you don't already have
910MNE, you can follow instructions to get it `here
1011<https://mne-tools.github.io/stable/getting_started.html>`_.
1112"""
5051event_fname = sample .data_path () + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
5152
5253# Load the file of example MNE data
53- raw = mne .io .read_raw_fif (raw_fname , preload = True )
54+ raw = mne .io .read_raw_fif (raw_fname , preload = True , verbose = False )
5455
5556###################################################################################################
5657
6465
6566###################################################################################################
6667
67- # Explore the data
68- raw .info ()
69-
70- ###################################################################################################
71-
7268# Plot the channel locations and labels
7369raw .plot_sensors (show_names = True )
7470
7571###################################################################################################
7672
7773# Creating epochs
7874reject = dict (eeg = 180e-6 )
79- event_id , tmin , tmax = {'left/auditory' : 1 }, - 0.2 , 0.5
75+ event_id = {'left/auditory' : 1 }
8076events = mne .read_events (event_fname )
8177epochs = mne .Epochs (raw , events = events , event_id = event_id ,
82- tmin = 5 , tmax = 125 , baseline = None , preload = True )
78+ tmin = 5 , tmax = 125 , baseline = None , preload = True )
8379
8480###################################################################################################
8581
8682# Creating Power Spectra Densities
87- psd , freqs = mne .time_frequency .psd_welch (epochs , fmin = 1. , fmax = 50. , n_fft = 2000 , n_overlap = 250 , n_per_seg = 500 )
83+ psd , freqs = mne .time_frequency .psd_welch (epochs , fmin = 1. , fmax = 50. , n_fft = 2000 ,
84+ n_overlap = 250 , n_per_seg = 500 )
8885
8986###################################################################################################
9087# fooofgroup
91- # ------------------
88+ # ----------
9289#
9390# The FOOOFGroup object is used to fit
9491# FOOOF models across the power spectra. A list of FOOOFGroup objects is returned.
9794###################################################################################################
9895
9996# Initialize a FOOOF group object, with desired settings
100- fg = FOOOFGroup (peak_width_limits = [1 , 6 ], min_peak_amplitude = 0.075 ,
101- max_n_peaks = 6 , peak_threshold = 1 , verbose = False )
97+ fg = FOOOFGroup (peak_width_limits = [1 , 6 ], min_peak_height = 0.075 ,
98+ max_n_peaks = 6 , peak_threshold = 1 , verbose = False )
10299
103100###################################################################################################
104101
105102# Shaping the PSDs so that we can run fooofgroup
106- fooof_psd = np .squeeze (psd [4 ,:,:])
103+ fooof_psd = np .squeeze (psd [0 ,:,:])
107104n_channels , n_freq = fooof_psd .shape
108105num_blocks = len (mne .read_events (event_fname ))
109106
110107# Setting frequency range
111- freq_range = [3 , 32 ]
108+ freq_range = [3 , 35 ]
112109
113110# This returns a list of FOOOFGRoup objects
114111fg .fit (freqs , fooof_psd , freq_range )
115112fg .plot ()
116113
117114###################################################################################################
118115
116+ # Periodic features
117+ feats = ["CFS" , "PWS" , "BWS" ]
118+
119+ # Aperiodic features
120+ aperiodic_feats = ["Offset" ,"Exponent" ]
121+
119122# Define bands of interest
120- bands = {'theta' : [2 ,7 ],
121- 'alpha' : [8 ,14 ],
123+ bands = {'theta' : [3 ,7 ],
124+ 'alpha' : [7 ,14 ],
122125 'beta' : [15 ,30 ]}
123126
124127# Creating dictionaries to store all the aperiodic properties across frequencies
125128results = {}
126129for band_name in bands .keys ():
127- results [band_name ] = np .zeros (shape = [ num_blocks , n_channels , n_feats ])
130+ results [band_name ] = np .zeros (shape = [ num_blocks , n_channels , len ( feats ) ])
128131
129132# Creating dictionaries to store all the periodic properties across frequencies
130- slope_results = np .zeros (shape = [num_blocks , n_channels , 2 ])
133+ exponent_results = np .zeros (shape = [num_blocks , n_channels , len ( aperiodic_feats ) ])
131134
132135###################################################################################################
133-
134- # Populating periodic and aperiodic values
135- for block in range (0 , num_blocks ):
136- for ind , res in enumerate (fg ):
137- slope_results [block , ind , :] = res .aperiodic_params
138- for band_label , band_range in bands .items ():
139- results [band_label ][block , ind , :] = get_band_peak (res .peak_params , band_range , True )
140-
141- ###################################################################################################
142- # Plotting topologies
143- # --------------
136+ # Plotting topographies
137+ # ---------------------
144138#
145- # The following will illustrate the process of exploring the previoulsy generated
139+ # The following will illustrate the process of exploring the previously generated
146140# oscillatory values
147141
148142###################################################################################################
@@ -161,20 +155,20 @@ def plot_topo_colorbar(vmin, vmax, label):
161155 norm = colors .Normalize (vmin = vmin , vmax = vmax )
162156
163157 cb1 = colorbar .ColorbarBase (plt .gca (), cmap = cmap ,
164- norm = norm , orientation = 'vertical' )all_fg . plot ()
158+ norm = norm , orientation = 'vertical' )
165159
166160###################################################################################################
167161
168- # Settings: In this example, we will be plotting the Alpha Central Frequency
169- # and oscillatory slope
162+ # Settings: In this example, we will be plotting the Alpha Center Frequency
163+ # and oscillatory exponent
170164band = 'alpha'
171165cur_data = results [band ]
172- feats = [ "CFS" , "AMPS" , "BWS" ]
166+
173167topo_dat = np .mean (cur_data ,0 )
174168
175169###################################################################################################
176170
177- # Looking at the alpha Central Frequeuncy
171+ # Looking at the alpha Center Frequeuncy
178172print ('CURRENT FEATURE:' , feats [0 ])
179173disp_dat = topo_dat [:,0 ]
180174
@@ -191,11 +185,11 @@ def plot_topo_colorbar(vmin, vmax, label):
191185###################################################################################################
192186
193187# Looking at the Oscillatory
194- cur_data = slope_results
188+ cur_data = exponent_results
195189
196190topo_dat = np .mean (cur_data ,0 )
197191
198- print ('CURRENT FEATURE:' , slope_feat [1 ])
192+ print ('CURRENT FEATURE:' , exponent_feat [1 ])
199193disp_dat = topo_dat [:,1 ]
200194
201195inds = np .where (np .isnan (disp_dat ))
@@ -205,19 +199,20 @@ def plot_topo_colorbar(vmin, vmax, label):
205199vmin , vmax , = disp_dat .min () - vbuffer , disp_dat .max () + vbuffer
206200
207201fig , ax = plt .subplots ()
208- plot_topo_colorbar (vmin , vmax , slope_feat [1 ])
202+ plot_topo_colorbar (vmin , vmax , exponent_results [1 ])
209203mne .viz .plot_topomap (disp_dat , raw .info , vmin = vmin , vmax = vmax , cmap = cm .viridis , contours = 0 , axes = ax )
210204
211205
212206###################################################################################################
213207# Alternative epoching methods
214- # --------------
208+ # ----------------------------
215209#
216210# Antother way to epoch your data based on time by avaeraging across events as suppose to
217211# selecting specifc time sections
218212
219213###################################################################################################
220214
215+ tmin , tmax = - 0.2 , 0.5
221216epochs_params = dict (events = events , event_id = event_id , tmin = tmin , tmax = tmax ,
222217 reject = reject )
223- epochs = mne .Epochs (raw_no_ref , ** epochs_params ).average ()
218+ epochs = mne .Epochs (raw , ** epochs_params ).average ()
0 commit comments