def fooofmodel(): import sys import numpy as np from scipy.io import loadmat, savemat from fooof import FOOOFGroup import matplotlib.pyplot as plt data = loadmat('ModelPowSpctraForFOOOF.mat') # Unpack data from dictionary, and squeeze numpy arrays freqs = np.squeeze(data['fx']).astype('float') psds = np.squeeze(data['avgpwr']).astype('float') # ^Note: this also explicitly enforces type as float (type casts to float64, instead of float32) # This is not strictly necessary for fitting, but is for saving out as json from FOOOF, if you want to do that # Transpose power spectra, to have the expected orientation for FOOOF #psds = psds.T fg = FOOOFGroup(peak_threshold=7, peak_width_limits=[3, 14]) #, aperiodic_mode='knee' #fg.report(freqs, psds, [1, 290]) #fg.fit(freqs,psds,[0.2,290]) fg.fit(freqs, psds, [1, 290]) fg.plot() fg.save_report('modelfits') slp = fg.get_params('aperiodic_params', 'exponent') off = fg.get_params('aperiodic_params', 'offset') r = fg.get_params('r_squared') savemat('slp.mat', {'slp': slp}) savemat('off.mat', {'off': off}) savemat('r.mat', {'r': r}) return
def test_plot_fg(tfg, skip_if_no_mpl): plot_fg(tfg) # Test error if no data available to plot tfg = FOOOFGroup() with raises(NoModelError): tfg.plot()
def indiv_fooofcode(): import sys import numpy as np from scipy.io import loadmat, savemat from fooof import FOOOFGroup import matplotlib.pyplot as plt patientcount = 16 alloffset = [] allr2 = [] allexps = [] allcfs = [] for k in range(1,patientcount+1): matfile = 'indiv_' + str(k) + '.mat' data = loadmat(matfile) # Unpack data from dictionary, and squeeze numpy arrays freqs = np.squeeze(data['indiv_frq']).astype('float') psds = np.squeeze(data['indiv_pow']).astype('float') # ^Note: this also explicitly enforces type as float (type casts to float64, instead of float32) # This is not strictly necessary for fitting, but is for saving out as json from FOOOF, if you want to do that fg = FOOOFGroup(peak_threshold=7,peak_width_limits=[3, 14])#, aperiodic_mode='knee' #fg.report(freqs, psds, [30, 300]) #fg.fit(freqs,psds,[float(sys.argv[1]),float(sys.argv[2])]) fg.fit(freqs,psds,[float(sys.argv[1]),float(sys.argv[2])]) fg.plot() reportname = str(k) + '_indiv result' fg.save_report(reportname) #print(fg.group_results) r2 = fg.get_params('r_squared') allr2.append(r2) exps = fg.get_params('aperiodic_params', 'exponent') allexps.append(exps) centerfrq = fg.get_params('peak_params','CF') allcfs.append(centerfrq) offset = fg.get_params('aperiodic_params','offset') alloffset.append(offset) #knee = fg.get_params('aperiodic_params','knee') #savemat('knee_allpeeps.mat', {'knee' : knee}) #NOW OUTSIDE OF BIG FORLOOP! #concat everythin. savemat('all_offset.mat',{'all_offset' : alloffset}) savemat('all_r2.mat', {'all_r2' : allr2}) savemat('all_exps.mat', {'all_exps' : allexps}) savemat('all_cfs.mat',{'all_cfs' : allcfs}) #these are cell arrays! I didn't even mean for them to be but heck yea useful
def test_plot_fg(tfg, skip_if_no_mpl): plot_fg(tfg, save_fig=True, file_path=TEST_PLOTS_PATH, file_name='test_plot_fg.png') # Test error if no data available to plot tfg = FOOOFGroup() with raises(NoModelError): tfg.plot()
def fooof_channel_rejection(eeg, psds, freqs, f_low, f_high, participant, session_name): from scipy.stats import bayes_mvs n_bads = 0 fooof_group = FOOOFGroup(max_n_peaks=6, min_peak_amplitude=0.1, peak_width_limits=[1, 12], background_mode='knee') fooof_group.fit(freqs, psds, freq_range=[f_low, f_high / 2], n_jobs=-1) fooof_group_fig = fooof_group.plot(save_fig=True, file_name='FOOOF_group_' + participant + '_' + session_name, file_path=data_dir + '/results/') bg_slope = fooof_group.get_all_data('background_params', col='slope') mean_cntr, var_cntr, std_cntr = bayes_mvs(bg_slope, alpha=0.9) lower_slope = mean_cntr[1][0] - std_cntr[1][1] upper_slope = mean_cntr[1][1] + std_cntr[1][1] print('upper and lower slope range (mean, std)', lower_slope, upper_slope, np.mean(bg_slope), np.std(bg_slope)) for channel_idx, slope in enumerate(bg_slope): if slope < lower_slope or slope > upper_slope: eeg.info['bads'].append(eeg.ch_names[channel_idx]) n_bads += 1 eeg.interpolate_bads(reset_bads=True) return eeg, n_bads
def fooof_perps(): import sys import numpy as np from scipy.io import loadmat, savemat from fooof import FOOOFGroup import matplotlib.pyplot as plt data = loadmat('perps_proper.mat') # Unpack data from dictionary, and squeeze numpy arrays freqs = np.squeeze(data['fx_regular']).astype('float') psds = np.squeeze(data['powa_regular']).astype('float') # ^Note: this also explicitly enforces type as float (type casts to float64, instead of float32) # This is not strictly necessary for fitting, but is for saving out as json from FOOOF, if you want to do that # Transpose power spectra, to have the expected orientation for FOOOF #psds = psds.T fg = FOOOFGroup(peak_threshold=4,peak_width_limits=[3, 14])#, aperiodic_mode='knee' #fg.report(freqs, psds, [1, 290]) #fg.fit(freqs,psds,[0.2,290]) fg.fit(freqs,psds,[float(sys.argv[1]),float(sys.argv[2])]) fg.plot() fg.save_report('perps') #print(fg.group_results) r2 = fg.get_params('r_squared') savemat('p_r2.mat', {'p_r2' : r2}) exps = fg.get_params('aperiodic_params', 'exponent') centerfrq = fg.get_params('peak_params','CF') peakheight = fg.get_params('peak_params','PW') savemat('p_exps.mat', {'p_exps' : exps}) savemat('p_cfs.mat', {'p_cfs' : centerfrq}) savemat('p_peakheight.mat',{'p_peakheight' : peakheight}) offset = fg.get_params('aperiodic_params','offset') savemat('p_offs.mat', {'p_offs' : offset})
################################################################################################### # Fit a group of power spectra with the .fit() method # The key difference (compared to FOOOF) is that it takes a 2D array of spectra # This matrix should have the shape of [n_spectra, n_freqs] fg.fit(freqs, spectra, [3, 30]) ################################################################################################### # Print out results fg.print_results() ################################################################################################### # Plot a summary of the results across the group fg.plot() ################################################################################################### # # Just as with the FOOOF object, you can call the convenience method # :meth:`fooof.FOOOFGroup.report` to run the fitting, and then print the results and plots. # ################################################################################################### # You can also save out PDF reports of the FOOOFGroup fits, same as with FOOOF fg.save_report('FOOOFGroup_report') ################################################################################################### # FOOOFGroup Results # ------------------
def test_plot_fg_error(skip_if_no_mpl): tfg = FOOOFGroup() with raises(RuntimeError): tfg.plot()