def load_delta_theta_cfc(hdf5_path, rois, subjects, sessions, outfile=None): bands_to_extract = [('BOLD bandpass', 'Delta'), ('Delta', 'Theta'), ('Theta', 'Gamma')] bands = ['BOLD bandpass', 'Delta', 'Theta', 'Alpha', 'Gamma'] # Getting metadata from hdf5_path # file = h5py.File(hdf5_path) # sessions = list(file) # layer = file.get(sessions[0] + '/' + subjects[0] + '/' + rois[0] + '/r_vals') # data = layer5[...] # file.close() tables = {} for band_pair in bands_to_extract: phase_band = band_pair[0] amp_band = band_pair[1] for b, band in enumerate(bands): # Getting indcies if phase_band in band: p = b if amp_band in band: a = b for session in sessions: table_name = "%s_%s_%s" % (phase_band, amp_band, session) print('Creating table %s' % table_name) cfc_output = np.ndarray(shape=(len(subjects), len(rois))) for s, subject in enumerate(subjects): for r, roi in enumerate(rois): cfc_key = session + '/' + subject + '/' + roi + '/r_vals' file = h5py.File(hdf5_path) cfc_data = file.get(cfc_key)[...] cfc_value = cfc_data[p, a] cfc_output[s, r] = cfc_value file.close() cfc_table = pd.DataFrame(cfc_output, index=subjects, columns=rois) tables[table_name] = cfc_table if outfile is not None: mf.save_xls(tables, outfile) return tables
return session_dict outfile = ddir + '/mPLSC_cfc.pkl' check = input('Run mPLSC? y/n ') if check == 'y': check = input('Load cfc data from hdf5? y/n ') if check == 'y': hdf5_file = ddir + '/MEG_BOLD_phase_amp_coupling.hdf5' cfc_tables = _load_cfc_tables_from_hdf5(hdf5_file, meg_sess, subjects, rois, bands) for session in list(cfc_tables): session_dict = cfc_tables[session] fname = ddir + '/MEG_BOLD_phase_amp_coupling_%s.xlsx' % session mf.save_xls(session_dict, fname) cfc_fnames = [ 'MEG_BOLD_phase_amp_coupling_%s.xlsx' % sess for sess in meg_sess ] cfc_filelist = [os.path.join(ddir, f) for f in cfc_fnames] cfc_tables = _load_cfc_subtables(cfc_filelist) x_tables = [] for sess in list(cfc_tables): for t in list(cfc_tables[sess]): x_tables.append(cfc_tables[sess][t]) # Creating list of y tables behavior_metadata = pd.read_csv(pdir + '/data/b_variables_mPLSC.txt', delimiter='\t', header=None)
y_saliences_zscores_thresh = {} for behavior_category in list(y_saliences_zscores): unthresh_df = y_saliences_zscores[behavior_category] unthresh_vals = unthresh_df.values thresh_vals = np.ndarray(shape=unthresh_vals.shape) for r in range(len(unthresh_df.index)): for c in range(len(list(unthresh_df))): if np.abs(unthresh_vals[r, c]) < 4: thresh_vals[r, c] = 0 else: thresh_vals[r, c] = np.abs(unthresh_vals[r, c]) thresh_df = pd.DataFrame(thresh_vals, index=unthresh_df.index, columns=list(unthresh_df)) y_saliences_zscores_thresh[behavior_category] = thresh_df mf.save_xls(y_saliences_zscores_thresh, fig_path + '/behavior_saliences_z.xlsx') print('%s: Averaging saliences within behavior categories' % pu.ctime()) behavior_avg = mf.average_subtable_saliences(y_saliences_zscores_thresh) behavior_avg.to_excel(fig_path + '/behavior_average_z.xlsx') print('%s: Creating quick histograms, getting means' % pu.ctime()) def _quick_hist(data, fname=None): import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.hist(data, bins=20) if fname is not None: fig.savefig(fname, bbox_inches='tight')
with open(output_file, 'wb') as file: pkl.dump(output, file) else: with open(output_file, 'rb') as file: output = pkl.load(file) res_perm = output['permutation_tests'] res_boot = output['bootstrap_tests'] y_saliences = output['y_saliences'] y_saliences_z = output['y_saliences_zscores'] x_saliences = output['x_saliences'] x_saliences_z = output['x_saliences_zscores'] fig_path = pdir + '/figures/mPLSC_power_all_sessions' mf.save_xls(y_saliences, fig_path + '/behavior_saliences.xlsx') mf.save_xls(x_saliences, fig_path + '/brain_saliences.xlsx') y_saliences_zscores_thresh = {} for behavior_category in list(y_saliences_z): unthresh_df = y_saliences_z[behavior_category] unthresh_vals = unthresh_df.values thresh_vals = np.ndarray(shape=unthresh_vals.shape) for r in range(len(unthresh_df.index)): for c in range(len(list(unthresh_df))): if np.abs(unthresh_vals[r, c]) < 4: thresh_vals[r, c] = 0 else: thresh_vals[r, c] = np.abs(unthresh_vals[r, c]) thresh_df = pd.DataFrame(thresh_vals, index=unthresh_df.index,