def run_fooof(freq_path,loc_path,save_report_path,save_variables_path,save_variable_prefix,save_variables = True ,save_report=True,freq_range=[1,30],channel = 'Oz'): freqIterator = folderIterator(freq_path) chan_inds_iterator = chanIndIterator(loc_path) # Constant bg_ppts= []# background parameters/ppts peak_ppts = [] errors = [] for chan_ind, freq_var in zip(chan_inds_iterator,freqIterator) if True: # if files[0].endswith(".mat"): TODO: set the conditions for safety power,spectrum = getFreqValuesVec(freq_var) if chan_ind != -1 power = 10**np.array(power[chan_ind]) spectrum=np.array(spectrum).flatten() # Initialize FOOOF object fm = FOOOF(peak_width_limits=[0.5,8]) # Model the power spectrum with FOOOF, and print out a report fm.fit(spectrum, power, freq_range) bg_ppts.append(fm.background_params_) peak_ppts.append(fm.peak_params_) errors.append(fm.error_) if save_report: fm.save_report(str(freq_var[-7:-4]),save_report_path) if save_variables : with cd(save_variables_path): save_file(save_variable_prefix+'bg_ppts.npy',bg_ppts) save_file(save_variable_prefix+'peak_ppts.npy',peak_ppts) save_file(save_variable_prefix+'errors.npy',errors)
def plot_all_epochs(power_path, cond, loc_path, color, end_freq=30, chan='Oz', subplot=False): ''' In the future, allow subplot optino and ppt option ''' power_itr = folderIterator(power_path) loc_itr = folderIterator(loc_path) subplot_values = 1 for power_file, loc_file in zip(power_itr, loc_itr): print(power_file) features, freqVec = readOneFeatures(power_file, end_freq) print('Dimension of all of the powers') print(features.shape) # sanity check plt.figure(subplot_values) chanlocs = getChanLocs(loc_file) if (chan in chanlocs): chan_ind = chanlocs.index(chan) # chan_powers = normalize_power(features[:,chan_ind,:]) chan_powers = features[:, chan_ind, :] labels_count = features.shape[0] mean = np.mean(chan_powers, 0) std = np.std(chan_powers, axis=0) plt.plot(freqVec, mean, color, label=cond + ':' + str(labels_count) + 'Epochs') plt.plot(freqVec, mean - std, color) plt.plot(freqVec, mean + std, color) plt.legend(loc='upper right', shadow=True, fontsize='x-large') power_file = power_file.decode("utf-8") plt.title('Participant: ' + power_file[-7:-4] + '\n Chan: ' + chan) plt.xlabel('Frequency (Hz)') plt.ylabel('Power') else: print(files[0], ' did not have ', chan) input('understood?') subplot_values += 1
def plot_all_ppt(cond_dir, conds, loc_path, color, label_itr, end_freq): ''' Input: cond_dir: conds: loc_path: color: label_itr: end_freq: Output: Warnings. It can only do 2 conditions now ''' loc_itr = folderIterator(loc_path) c0_itr = folderIterator(cond_dir + '/' + conds[0]) c1_itr = folderIterator(cond_dir + '/' + conds[1]) for cond0, cond1 in zip(c0_itr, c1_itr): pass
loc_dir = '/Volumes/SHard/Tsuchiya_Lab_Data/Probes/chanLocs/' conds = ['Off', 'On'] ########## # Script # ########## with cd(fooof_vars_path): peak_ppts = np.load('basepeak_ppts.npy') # Functions to apply for list alpha = lambda arr: float('nan') if arr.size == 0 else findAlpha(arr) peaks = list(map(alpha, peak_ppts)) peaks = [round(peak, 1) for peak in peaks] # round it to 0.1 scale folder_iter_wrapper = lambda cond: folderIterator(freq_path + '/' + cond) #TODO: ughhhh, generalize it... cond1 cond2... # Get folder iterators for conditions cond0_itr = folder_iter_wrapper(conds[0]) cond1_itr = folder_iter_wrapper(conds[1]) chan_inds_itr = chanIndIterator(loc_dir) plt.figure() # The difference of conditions with respect to alpha(cond1 - cond0) alpha_diffs = getDiffs(cond0_itr, cond1_itr, chan_inds_itr, peaks, avoid_flickers=True) plot_scatter(list(range(len(alpha_diffs))),