22Using fooof with MNE
33====================
44
5- This examples illustrates how to use fooof with MNE
6- and create topographical plots
5+ This examples illustrates how to use fooof with MNE and create topographical plots.
76
87This tutorial does require that you have `MNE
98<https://mne-tools.github.io/>`_ installed. If you don't already have
1312
1413###################################################################################################
1514
16-
17- #General imports
15+ # General imports
1816import numpy as np
1917import pandas as pd
2018from matplotlib import cm , colors , colorbar
3028from fooof import FOOOF , FOOOFGroup
3129from fooof .analysis import *
3230
33-
3431###################################################################################################
3532# Load & Check MNE Data
3633# ---------------------
7471reject = dict (eeg = 180e-6 )
7572event_id = {'left/auditory' : 1 }
7673events = mne .read_events (event_fname )
77- epochs = mne .Epochs (raw , events = events , event_id = event_id ,
78- tmin = 5 , tmax = 125 , baseline = None , preload = True )
74+ epochs = mne .Epochs (raw , events = events , event_id = event_id , tmin = 5 , tmax = 125 ,
75+ baseline = None , preload = True , verbose = False )
7976
8077###################################################################################################
8178
8279# Creating Power Spectra Densities
83- psd , freqs = mne .time_frequency .psd_welch (epochs , fmin = 1. , fmax = 50. , n_fft = 2000 ,
84- n_overlap = 250 , n_per_seg = 500 )
80+ spectra , freqs = mne .time_frequency .psd_welch (epochs , fmin = 1. , fmax = 50. , n_fft = 2000 ,
81+ n_overlap = 250 , n_per_seg = 500 )
8582
8683###################################################################################################
87- # fooofgroup
84+ # FOOOFGroup
8885# ----------
8986#
90- # The FOOOFGroup object is used to fit
91- # FOOOF models across the power spectra. A list of FOOOFGroup objects is returned.
87+ # The FOOOFGroup object is used to fit FOOOF models across the power spectra.
9288#
9389
9490###################################################################################################
9591
96- # Initialize a FOOOF group object, with desired settings
92+ # Initialize a FOOOFGroup object, with desired settings
9793fg = FOOOFGroup (peak_width_limits = [1 , 6 ], min_peak_height = 0.075 ,
9894 max_n_peaks = 6 , peak_threshold = 1 , verbose = False )
9995
10096###################################################################################################
10197
102- # Shaping the PSDs so that we can run fooofgroup
103- fooof_psd = np .squeeze (psd [0 ,:,:])
104- n_channels , n_freq = fooof_psd .shape
98+ # Selecting the first epoch of data to FOOOF
99+ spectra = np .squeeze (spectra [0 ,:,:])
100+ n_channels , n_freq = spectra .shape
105101num_blocks = len (mne .read_events (event_fname ))
106102
107103# Setting frequency range
108104freq_range = [3 , 35 ]
109105
110- # This returns a list of FOOOFGRoup objects
111- fg .fit (freqs , fooof_psd , freq_range )
106+ # Fit the FOOOF model across all channels
107+ fg .fit (freqs , spectra , freq_range )
112108fg .plot ()
113109
114110###################################################################################################
115111
116- # Periodic features
112+ # Define labels for periodic & aperiodic features
117113feats = ["CFS" , "PWS" , "BWS" ]
118-
119- # Aperiodic features
120114aperiodic_feats = ["Offset" ,"Exponent" ]
121115
122116# Define bands of interest
123- bands = {'theta' : [3 ,7 ],
124- 'alpha' : [7 ,14 ],
125- 'beta' : [15 ,30 ]}
117+ bands = {'theta' : [3 , 7 ],
118+ 'alpha' : [7 , 14 ],
119+ 'beta' : [15 , 30 ]}
126120
127- # Creating dictionaries to store all the aperiodic properties across frequencies
121+ # Create dictionaries to store all the periodic properties across frequencies
128122results = {}
129123for band_name in bands .keys ():
130- results [band_name ] = np .zeros (shape = [ num_blocks , n_channels , len (feats )])
124+ results [band_name ] = np .zeros (shape = [num_blocks , n_channels , len (feats )])
131125
132- # Creating dictionaries to store all the periodic properties across frequencies
126+ # Creating dictionaries to store all the aperiodic properties across frequencies
133127exponent_results = np .zeros (shape = [num_blocks , n_channels , len (aperiodic_feats )])
134128
135-
136129###################################################################################################
137130
138131# Populating periodic and aperiodic values
142135 for band_label , band_range in bands .items ():
143136 results [band_label ][block , ind , :] = get_band_peak (res .peak_params , band_range , True )
144137
145-
146138###################################################################################################
147- # Plotting topographies
139+ # Plotting Topographies
148140# ---------------------
149141#
150- # The following will illustrate the process of exploring the previously generated
151- # oscillatory values
142+ # Now we can plot the extracted FOOOF features across all channels.
143+ #
152144
153145###################################################################################################
154- # This is the function required to create color bars for topographies
146+
155147def plot_topo_colorbar (vmin , vmax , label ):
156- """
157- Create a colorbar for the topography plots
158- vmin: int
159- vmax: int
160- label: str
161- """
148+ """Helper function to create colorbars for the topography plots."""
149+
162150 fig = plt .figure (figsize = (2 , 3 ))
163151 ax1 = fig .add_axes ([0.9 , 0.25 , 0.15 , 0.9 ])
164152
@@ -168,18 +156,21 @@ def plot_topo_colorbar(vmin, vmax, label):
168156 cb1 = colorbar .ColorbarBase (plt .gca (), cmap = cmap ,
169157 norm = norm , orientation = 'vertical' )
170158
159+ ###################################################################################################
160+ #
161+ # In this example, we will be plotting the alpha center frequency and oscillatory exponent.
162+
171163###################################################################################################
172164
173- # Settings: In this example, we will be plotting the Alpha Center Frequency
174- # and oscillatory exponent
165+ # Settings to grab the alpha center frequency
175166band = 'alpha'
176167cur_data = results [band ]
177168
178169topo_dat = np .mean (cur_data ,0 )
179170
180171###################################################################################################
181172
182- # Looking at the alpha Center Frequeuncy
173+ # Looking at the alpha center frequeuncy
183174print ('CURRENT FEATURE:' , feats [0 ])
184175disp_dat = topo_dat [:,0 ]
185176
@@ -195,7 +186,7 @@ def plot_topo_colorbar(vmin, vmax, label):
195186
196187###################################################################################################
197188
198- # Looking at the Oscillatory
189+ # Looking at the aperiodic exponent
199190cur_data = exponent_results
200191
201192topo_dat = np .mean (cur_data ,0 )
@@ -212,18 +203,3 @@ def plot_topo_colorbar(vmin, vmax, label):
212203fig , ax = plt .subplots ()
213204plot_topo_colorbar (vmin , vmax , exponent_results [1 ])
214205mne .viz .plot_topomap (disp_dat , raw .info , vmin = vmin , vmax = vmax , cmap = cm .viridis , contours = 0 , axes = ax )
215-
216-
217- ###################################################################################################
218- # Alternative epoching methods
219- # ----------------------------
220- #
221- # Antother way to epoch your data based on time by avaeraging across events as suppose to
222- # selecting specifc time sections
223-
224- ###################################################################################################
225-
226- tmin , tmax = - 0.2 , 0.5
227- epochs_params = dict (events = events , event_id = event_id , tmin = tmin , tmax = tmax ,
228- reject = reject )
229- epochs = mne .Epochs (raw , ** epochs_params ).average ()
0 commit comments