def faster_correlation(fname, tr): # Load the ds114_sub009_t2r1.nii image img = nib.load(fname + ".nii") # Get the number of volumes in ds114_sub009_t2r1.nii n_trs = img.shape[-1] # Time between 3D volumes in seconds TR = tr # Get on-off timecourse time_course = events2neural(fname + "_cond.txt", TR, n_trs) # Drop the first 4 volumes, and the first 4 on-off values data = img.get_data() data = data[..., 4:] time_course = time_course[4:] # Calculate the number of voxels (number of elements in one volume) n_voxels = np.prod(data.shape[:-1]) # Reshape 4D array to 2D array n_voxels by n_volumes data_2d = np.reshape(data, (n_voxels, data.shape[-1])) # Transpose 2D array to give n_volumes, n_voxels array data_2d_T = data_2d.T # Calculate 1D vector length n_voxels of correlation coefficients correlation_1d = pearson.pearson_2d(time_course, data_2d_T) # Reshape the correlations array back to 3D correlation_3d = correlation_1d.reshape(data.shape[:-1]) return correlation_3d
def test_pearson_2d(): # Test pearson_2d routine x = np.random.rand(22) Y = np.random.normal(size=(22, 12)) # Does routine give same answers as np.corrcoef? expected = np.corrcoef(x, Y.T)[0, 1:] actual = pearson.pearson_2d(x, Y) # Did you, gentle user, forget to return the value? if actual is None: raise RuntimeError("function returned None") assert_almost_equal(expected, actual)