def compute_coherence(time_windows, TR, f_lb, f_ub, roi_names): n_timewindows = time_windows.shape[0] n_samples = time_windows.shape[1] n_rois = time_windows.shape[2] coherence_3Darray = np.zeros((n_timewindows, n_rois, n_rois)) if n_rois == len(roi_names): for time_index in range(n_timewindows): ts = time_windows[time_index, :, :] data = np.zeros((n_rois, n_samples)) for n_idx, roi in enumerate(roi_names): data[n_idx] = ts[:, n_idx] data = percent_change(data) T = TimeSeries(data, sampling_interval=TR) C = CoherenceAnalyzer(T) freq_idx = np.where( (C.frequencies > f_lb) * (C.frequencies < f_ub))[0] coh = np.mean(C.coherence[:, :, freq_idx], -1) coherence_3Darray[time_index] = coh else: raise Exception( "Number of ROIs in 3D Array do not match number of ROI names provided." ) return coherence_3Darray
# Get roi correlations and coherence for runNum in range(allROIS.shape[1]): #need to load timeseries by run fixTS=ts.TimeSeries(allROIS[:,runNum,:], sampling_interval=TR) fixTS.metadata['roi'] = roi_names # Get plot and correlations C=CorrelationAnalyzer(fixTS) fig01 = drawmatrix_channels(C.corrcoef, roi_names, size=[10., 10.], color_anchor=0, title='Correlation Results Run %i' % runNum) plt.show() # Save correlation corr_all[subject][runNum]=C.corrcoef # Get coherence Coh = CoherenceAnalyzer(fixTS) Coh.method['NFFT'] = NFFT Coh.method['n_overlap']=n_overlap # Get the index for the frequencies inside the ub and lb freq_idx = np.where((Coh.frequencies > f_lb) * (Coh.frequencies < f_ub))[0] # Extract coherence # Coher[0]= correlations for first ROI in list with others coher = np.mean(Coh.coherence[:, :, freq_idx], -1) # Averaging on the last dimension fig03 = drawmatrix_channels(coher, roi_names, size=[10., 10.], color_anchor=0, title='Coherence Results Run %i' % runNum) # Save coherence (coher is the average of the coherence over the specified frequency) coh_all[subject][runNum]=coher file=open(saveFile, 'w') # write mode
color_anchor=0, title='MTCoherenceAnalyzer') """ .. image:: fig/multi_taper_coh_02.png For comparison, we also perform the analysis using the standard CoherenceAnalyzer object, which does the analysis using Welch's windowed periodogram, instead of the multi-taper spectral estimation method (see :ref:`resting_state` for a more thorough analysis of this data using this method): """ C3 = CoherenceAnalyzer(T) freq_idx = np.where((C3.frequencies > f_lb) * (C3.frequencies < f_ub))[0] #Extract the coherence and average across these frequency bands: coh = np.mean(C3.coherence[:, :, freq_idx], -1) # Averaging on the last dimension fig03 = drawmatrix_channels(coh, roi_names, size=[10., 10.], color_anchor=0, title='CoherenceAnalyzer') """ .. image:: fig/multi_taper_coh_03.png
fixTS.metadata['roi'] = roi_names # Get plot and correlations C = CorrelationAnalyzer(fixTS) fig01 = drawmatrix_channels(C.corrcoef, roi_names, size=[10., 10.], color_anchor=0, title='Correlation Results Run %i' % runNum) plt.show() # Save correlation corr_all[subject][runNum] = C.corrcoef # Get coherence Coh = CoherenceAnalyzer(fixTS) Coh.method['NFFT'] = NFFT Coh.method['n_overlap'] = n_overlap # Get the index for the frequencies inside the ub and lb freq_idx = np.where( (Coh.frequencies > f_lb) * (Coh.frequencies < f_ub))[0] # Extract coherence # Coher[0]= correlations for first ROI in list with others coher = np.mean(Coh.coherence[:, :, freq_idx], -1) # Averaging on the last dimension fig03 = drawmatrix_channels(coher, roi_names, size=[10., 10.],