def test_fast_on_real_data(): from pandas import read_table from os import path as op k = 200 subdir = "/home2/data/Projects/CWAS/share/nki/subinfo/40_Set1_N104" ffile = op.join(subdir, "short_compcor_rois_random_k%04i.txt" % k) fpaths = read_table(ffile, header=None) fpath = fpaths.ix[0, 0] import nibabel as nib img = nib.load(fpath) dat = img.get_data() import numpy as np from CPAC.cwas.subdist import norm_cols, ncor norm_dat = norm_cols(dat) corr_dat = norm_dat.T.dot(norm_dat) ref = eigenvector_centrality(corr_dat) comp = fast_eigenvector_centrality(norm_dat) diff = np.abs(ref - comp).mean() # mean diff print(diff) ok_(diff < np.spacing(1e10)) # allow some differences
def test_fast_on_real_data(): from pandas import read_table from os import path as op k = 200 subdir = "/home2/data/Projects/CWAS/share/nki/subinfo/40_Set1_N104" ffile = op.join(subdir, "short_compcor_rois_random_k%04i.txt" % k) fpaths = read_table(ffile, header=None) fpath = fpaths.ix[0,0] import nibabel as nib img = nib.load(fpath) dat = img.get_data() import numpy as np from CPAC.cwas.subdist import norm_cols, ncor norm_dat = norm_cols(dat) corr_dat = norm_dat.T.dot(norm_dat) ref = eigenvector_centrality(corr_dat) comp = fast_eigenvector_centrality(norm_dat) diff = np.abs(ref-comp).mean() # mean diff print(diff) ok_(diff < np.spacing(1e10)) # allow some differences
def test_fast_eigenvector_centrality(ntpts=100, nvoxs=1000): print "testing fast_eigenvector_centrality" # Simulate Data import numpy as np from CPAC.cwas.subdist import norm_cols # Normalize Random Time-Series Data m = np.random.random((ntpts,nvoxs)) m = norm_cols(m) # Correlation Data with Range 0-1 mm = m.T.dot(m) # note that need to generate connectivity matrix here # Execute #from CPAC.network_centrality.core import fast_eigenvector_centrality,slow_eigenvector_centrality ref = eigenvector_centrality(mm, verbose=False) # we need to transform mm to be a distance comp = fast_eigenvector_centrality(m, verbose=False) diff = np.abs(ref-comp).mean() # mean diff print(diff) ok_(diff < np.spacing(1e2)) # allow minimal difference
def test_fast_eigenvector_centrality(ntpts=100, nvoxs=1000): print "testing fast_eigenvector_centrality" # Simulate Data import numpy as np from CPAC.cwas.subdist import norm_cols # Normalize Random Time-Series Data m = np.random.random((ntpts, nvoxs)) m = norm_cols(m) # Correlation Data with Range 0-1 mm = m.T.dot(m) # note that need to generate connectivity matrix here # Execute #from CPAC.network_centrality.core import fast_eigenvector_centrality,slow_eigenvector_centrality ref = eigenvector_centrality( mm, verbose=False) # we need to transform mm to be a distance comp = fast_eigenvector_centrality(m, verbose=False) diff = np.abs(ref - comp).mean() # mean diff print(diff) ok_(diff < np.spacing(1e2)) # allow minimal difference
def get_centrality_fast(timeseries, method_options): ''' Method to calculate degree and eigen vector centrality. Relative to `get_centrality_opt`, it runs fast by not directly computing the correlation matrix. As a consequence, there are several differences/ limitations from the standard approach: 1. Cannot specify a correlation threshold 2. As a consequence, the weighted dense matrix centrality is computed 3. No memory limit is specified since it is assumed to be ok Note that the approach doesn't directly calculate the complete correlation matrix. Parameters ---------- timeseries_data : numpy array timeseries of the input subject method_options : string (list of boolean) list of two booleans for binarize and weighted options respectively Returns ------- out_list : string (list of tuples) list of tuple containing output name to be used to store nifti image for centrality and centrality matrix. this will only be weighted since the fast approaches are limited to this type of output. Raises ------ Exception ''' # Import packages from CPAC.network_centrality import fast_degree_centrality,\ fast_eigenvector_centrality from CPAC.cwas.subdist import norm_cols try: out_list = [] nvoxs = timeseries.shape[0] ntpts = timeseries.shape[1] # It's assumed that there is enough memory # So a block size isn't set here calc_degree = method_options[0] calc_eigen = method_options[1] print "Normalize Time-series" timeseries = norm_cols(timeseries.T) print "Computing centrality across %i voxels" % nvoxs if calc_degree: print "...calculating degree" degree_weighted = fast_degree_centrality(timeseries) out_list.append(('degree_centrality_weighted', degree_weighted)) if calc_eigen: print "...calculating eigen" eigen_weighted = fast_eigenvector_centrality(timeseries) out_list.append(('eigenvector_centrality_weighted', eigen_weighted)) return out_list except Exception: print "Error in calcuating centrality" raise