def test_SeedCorrelationAnalyzer(): targ = ts.TimeSeries(np.random.rand(10, 10), sampling_interval=1) # Test single source case seed = ts.TimeSeries(np.random.rand(10), sampling_interval=1) corr = nta.SeedCorrelationAnalyzer(seed, targ) our_coef_array = corr.corrcoef np_coef_array = np.array( [np.corrcoef(seed.data, a)[0, 1] for a in targ.data]) npt.assert_array_almost_equal(our_coef_array, np_coef_array) # Test multiple sources seed = ts.TimeSeries(np.random.rand(2, 10), sampling_interval=1) corr = nta.SeedCorrelationAnalyzer(seed, targ) our_coef_array = corr.corrcoef for source in [0, 1]: np_coef_array = np.array( [np.corrcoef(seed.data[source], a)[0, 1] for a in targ.data]) npt.assert_array_almost_equal(our_coef_array[source], np_coef_array)
def _fit(ts_set1, ts_set2): import nitime.analysis as nta analyzer = nta.SeedCorrelationAnalyzer(ts_set1, ts_set2) n_seeds = ts_set1.data.shape[0] if ts_set1.data.ndim > 1 else 1 if n_seeds == 1: cor = analyzer.corrcoef else: cor = [] for seed in range(n_seeds): cor.append(analyzer.corrcoef[seed]) return cor
coords = list(np.ndindex(volume_shape)) n_seeds = 3 seeds = np.random.randint(0, len(coords), n_seeds) coords_seeds = np.array(coords)[seeds].T coords_target = np.array(coords).T time_series_seed = io.time_series_from_file(fmri_file, coords_seeds, TR=TR, normalize='percent', filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) time_series_target = io.time_series_from_file(fmri_file, coords_target, TR=TR, normalize='percent',filter=dict(lb=f_lb, ub=f_ub, method='boxcar')) A = nta.SeedCoherenceAnalyzer(time_series_seed, time_series_target, method=dict(NFFT=20)) B = nta.SeedCorrelationAnalyzer(time_series_seed, time_series_target) freq_idx = np.where((A.frequencies > f_lb) * (A.frequencies < f_ub))[0] cor = [] coh = [] for this_seed in range(n_seeds): coh.append(np.mean(A.coherence[this_seed][:, freq_idx], -1)) cor.append(B.corrcoef[this_seed]) coords_indices = list(coords_target) vol_coh = [] vol_cor = [] for this_vol in range(n_seeds): vol_coh.append(np.empty(volume_shape))
# normalize='percent' nan_targets = np.isnan(np.mean(target_data,1)) print '\n', nan_targets.sum(), 'voxels with nan values ... removing' target_ts[i_target] = np.mean(target_data[~nan_targets,:],0) # take average across voxels target_T = ntts.TimeSeries(target_ts, sampling_interval=TR) fig_target_tseries = viz.plot_tseries(target_T) plt.title('target tseries, {}'.format(fmri_file)) target_Cor = nta.CorrelationAnalyzer(target_T) fig_target_cor = viz.drawmatrix_channels(target_Cor.corrcoef, target_rois, color_anchor=0) plt.title('correlation, {}'.format(fmri_file)) # correlation analyzer seed_target_Cor = nta.SeedCorrelationAnalyzer(seed_T, target_T) # coherence analyzer seed_target_Coh = nta.SeedCoherenceAnalyzer(seed_T, target_T, method=dict(NFFT=nfft)) # select frequency band freq_idx = np.where((seed_target_Coh.frequencies > f_lb) * (seed_target_Coh.frequencies < f_ub))[0] # extract correlation and coherence values print 'Calculating correlation and coherence' cor = seed_target_Cor.corrcoef coh = np.mean(seed_target_Coh.coherence[:, :, freq_idx], -1) # show correlation matrix visualize.display_matrix(cor,