def calc_centrality(method_options, weight_options, option, threshold, timeseries_data, scans, template_type, template_data, affine, allocated_memory): """ Method to calculate centrality and map them to a nifti file Parameters ---------- method_options : list (boolean) list of two booleans for binarize and weighted options respectively weight_options : list (boolean) list of two booleans for binarize and weighted options respectively option : an integer 0 for probability p_value, 1 for sparsity threshold, any other for threshold value threshold : a float pvalue/sparsity_threshold/threshold value timeseries_data : string (numpy filepath) timeseries of the input subject scans : an integer number of scans in the subject template_type : an integer 0 for mask, 1 for roi template_data : string (numpy filepath) path to file containing mask/parcellation unit matrix affine : string (filepath) path to file containing affine matrix of the input data allocated_memory : string amount of memory allocated to degree centrality Returns ------- out_list : list list containing out mapped centrality images """ from CPAC.network_centrality import map_centrality_matrix,\ get_centrality, \ get_centrality_opt,\ calc_threshold out_list = [] if method_options.count(True) == 0: raise Exception("Invalid values in method_options " \ "Atleast one True value is required") if weight_options.count(True) == 0: raise Exception("Invalid values in weight options" \ "Atleast one True value is required") #for sparsity threshold if option == 1 and allocated_memory == None: centrality_matrix = get_centrality(timeseries_data, method_options, weight_options, threshold, option, scans, allocated_memory) #optimized centrality else: if option == 1: r_value = None else: r_value = calc_threshold(option, threshold, scans) print "inside optimized_centrality, r_value -> ", r_value centrality_matrix = get_centrality_opt(timeseries_data, method_options, weight_options, allocated_memory, threshold, scans, r_value) def get_image(matrix, template_type): centrality_image = map_centrality_matrix(matrix, affine, template_data, template_type) out_list.append(centrality_image) for mat in centrality_matrix: get_image(mat, template_type) return out_list
def get_centrality_opt(timeseries_data, method_options, weight_options, memory_allocated, threshold, scans, r_value=None): """ Method to calculate degree and eigen vector centrality. This method takes into consideration the amount of memory allocated by the user to calculate degree centrality. Parameters ---------- timeseries_data : numpy array timeseries of the input subject weight_options : string (list of boolean) list of two booleans for binarize and weighted options respectively method_options : string (list of boolean) list of two booleans for binarize and weighted options respectively memory_allocated : a string amount of memory allocated to degree centrality scans : an integer number of scans in the subject r_value :a float threshold value 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 Raises ------ Exception """ import numpy as np from CPAC.network_centrality import load_mat,\ calc_corrcoef,\ calc_blocksize,\ calc_eigenV,\ calc_threshold #from scipy.sparse import dok_matrix try: out_list = [] timeseries = load_mat(timeseries_data) shape = timeseries.shape try: block_size = calc_blocksize(shape, memory_allocated) except: raise Exception("Error in calculating block size") r_matrix = None if method_options[0]: if weight_options[0]: degree_mat_binarize = np.zeros(shape[0], dtype=np.float32) out_list.append( ('degree_centrality_binarize', degree_mat_binarize)) if weight_options[1]: degree_mat_weighted = np.zeros(shape[0], dtype=np.float32) out_list.append( ('degree_centrality_weighted', degree_mat_weighted)) if method_options[1]: r_matrix = np.zeros((shape[0], shape[0]), dtype=np.float32) j = 0 i = block_size while i <= timeseries.shape[0]: print "running block -> ", i + j try: corr_matrix = np.nan_to_num( calc_corrcoef(timeseries[j:i].T, timeseries.T)) except: raise Exception( "Error in calcuating block wise correlation for the block %,%" % (j, i)) if r_value == None: r_value = calc_threshold(1, threshold, scans, corr_matrix, full_matrix=False) if method_options[1]: r_matrix[j:i] = corr_matrix if method_options[0]: if weight_options[0]: degree_mat_binarize[j:i] = np.sum( (corr_matrix > r_value).astype(np.float32), axis=1) - 1 if weight_options[1]: degree_mat_weighted[j:i] = np.sum( corr_matrix * (corr_matrix > r_value).astype(np.float32), axis=1) - 1 j = i if i == timeseries.shape[0]: break elif (i + block_size) > timeseries.shape[0]: i = timeseries.shape[0] else: i += block_size try: if method_options[1]: out_list.extend(calc_eigenV(r_matrix, r_value, weight_options)) except Exception: print "Error in calcuating eigen vector centrality" raise return out_list except Exception: print "Error in calcuating Centrality" raise
def get_centrality(timeseries_data, method_options, weight_options, threshold, option, scans, memory_allocated): """ Method to calculate degree and eigen vector centrality Parameters ---------- weight_options : string (list of boolean) list of two booleans for binarize and weighted options respectively method_options : string (list of boolean) list of two booleans for binarize and weighted options respectively threshold_matrix : string (numpy npy file) path to file containing thresholded correlation matrix correlation_matrix : string (numpy npy file) path to file containing correlation matrix template_data : string (numpy npy file) path to file containing mask or parcellation unit data 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 Raises ------ Exception """ import numpy as np from CPAC.network_centrality import load_mat,\ calc_corrcoef,\ calc_blocksize,\ calc_threshold,\ calc_eigenV out_list = [] try: timeseries = load_mat(timeseries_data) shape = timeseries.shape block_size = calc_blocksize(shape, memory_allocated) corr_matrix = np.zeros((shape[0], shape[0]), dtype=np.float16) j = 0 i = block_size while i <= timeseries.shape[0]: print "block -> ", i + j temp_matrix = np.nan_to_num( calc_corrcoef(timeseries[j:i].T, timeseries.T)) corr_matrix[j:i] = temp_matrix j = i if i == timeseries.shape[0]: break elif (i + block_size) > timeseries.shape[0]: i = timeseries.shape[0] else: i += block_size r_value = calc_threshold(option, threshold, scans, corr_matrix, full_matrix=True) print "r_value -> ", r_value if method_options[0]: print "calculating binarize degree centrality matrix..." degree_matrix = np.sum(corr_matrix > r_value, axis=1) - 1 out_list.append(('degree_centrality_binarize', degree_matrix)) print "calculating weighted degree centrality matrix..." degree_matrix = np.sum(corr_matrix * (corr_matrix > r_value), axis=1) - 1 out_list.append(('degree_centrality_weighted', degree_matrix)) if method_options[1]: out_list.extend(calc_eigenV(corr_matrix, r_value, weight_options)) except Exception: print "Error while calculating centrality" raise return out_list
def calc_centrality(method_options, weight_options, option, threshold, timeseries_data, scans, template_type, template_data, affine, allocated_memory): """ Method to calculate centrality and map them to a nifti file Parameters ---------- method_options : list (boolean) list of two booleans for binarize and weighted options respectively weight_options : list (boolean) list of two booleans for binarize and weighted options respectively option : an integer 0 for probability p_value, 1 for sparsity threshold, any other for threshold value threshold : a float pvalue/sparsity_threshold/threshold value timeseries_data : string (numpy filepath) timeseries of the input subject scans : an integer number of scans in the subject template_type : an integer 0 for mask, 1 for roi template_data : string (numpy filepath) path to file containing mask/parcellation unit matrix affine : string (filepath) path to file containing affine matrix of the input data allocated_memory : string amount of memory allocated to degree centrality Returns ------- out_list : list list containing out mapped centrality images """ from CPAC.network_centrality import map_centrality_matrix,\ get_centrality, \ get_centrality_opt,\ calc_threshold out_list = [] if method_options.count(True) == 0: raise Exception("Invalid values in method_options " \ "Atleast one True value is required") if weight_options.count(True) == 0: raise Exception("Invalid values in weight options" \ "Atleast one True value is required") #for sparsity threshold if option == 1 and allocated_memory == None: centrality_matrix = get_centrality(timeseries_data, method_options, weight_options, threshold, option, scans, allocated_memory) #optimized centrality else: if option ==1 : r_value = None else: r_value = calc_threshold(option, threshold, scans) print "inside optimized_centrality, r_value -> ", r_value centrality_matrix = get_centrality_opt(timeseries_data, method_options, weight_options, allocated_memory, threshold, scans, r_value) def get_image(matrix, template_type): centrality_image = map_centrality_matrix(matrix, affine, template_data, template_type) out_list.append(centrality_image) for mat in centrality_matrix: get_image(mat, template_type) return out_list
def get_centrality_opt(timeseries_data, method_options, weight_options, memory_allocated, threshold, scans, r_value = None): """ Method to calculate degree and eigen vector centrality. This method takes into consideration the amount of memory allocated by the user to calculate degree centrality. Parameters ---------- timeseries_data : numpy array timeseries of the input subject weight_options : string (list of boolean) list of two booleans for binarize and weighted options respectively method_options : string (list of boolean) list of two booleans for binarize and weighted options respectively memory_allocated : a string amount of memory allocated to degree centrality scans : an integer number of scans in the subject r_value :a float threshold value 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 Raises ------ Exception """ import numpy as np from CPAC.network_centrality import load_mat,\ calc_corrcoef,\ calc_blocksize,\ calc_eigenV,\ calc_threshold #from scipy.sparse import dok_matrix try: out_list =[] timeseries = load_mat(timeseries_data) shape = timeseries.shape try: block_size = calc_blocksize(shape, memory_allocated) except: raise Exception("Error in calculating block size") r_matrix = None if method_options[0]: if weight_options[0]: degree_mat_binarize = np.zeros(shape[0], dtype= np.float32) out_list.append(('degree_centrality_binarize', degree_mat_binarize)) if weight_options[1]: degree_mat_weighted = np.zeros(shape[0], dtype = np.float32) out_list.append(('degree_centrality_weighted', degree_mat_weighted)) if method_options[1]: r_matrix = np.zeros((shape[0], shape[0]), dtype = np.float32) j=0 i = block_size while i <= timeseries.shape[0]: print "running block -> ", i + j try: corr_matrix = np.nan_to_num(calc_corrcoef(timeseries[j:i].T, timeseries.T)) except: raise Exception("Error in calcuating block wise correlation for the block %,%"%(j,i)) if r_value == None: r_value = calc_threshold(1, threshold, scans, corr_matrix, full_matrix = False) if method_options[1]: r_matrix[j:i] = corr_matrix if method_options[0]: if weight_options[0]: degree_mat_binarize[j:i] = np.sum((corr_matrix > r_value).astype(np.float32), axis = 1) -1 if weight_options[1]: degree_mat_weighted[j:i] = np.sum(corr_matrix*(corr_matrix > r_value).astype(np.float32), axis = 1) -1 j = i if i == timeseries.shape[0]: break elif (i+block_size) > timeseries.shape[0]: i = timeseries.shape[0] else: i += block_size try: if method_options[1]: out_list.extend(calc_eigenV(r_matrix, r_value, weight_options)) except Exception: print "Error in calcuating eigen vector centrality" raise return out_list except Exception: print "Error in calcuating Centrality" raise
def get_centrality(timeseries_data, method_options, weight_options, threshold, option, scans, memory_allocated): """ Method to calculate degree and eigen vector centrality Parameters ---------- weight_options : string (list of boolean) list of two booleans for binarize and weighted options respectively method_options : string (list of boolean) list of two booleans for binarize and weighted options respectively threshold_matrix : string (numpy npy file) path to file containing thresholded correlation matrix correlation_matrix : string (numpy npy file) path to file containing correlation matrix template_data : string (numpy npy file) path to file containing mask or parcellation unit data 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 Raises ------ Exception """ import numpy as np from CPAC.network_centrality import load_mat,\ calc_corrcoef,\ calc_blocksize,\ calc_threshold,\ calc_eigenV out_list=[] try: timeseries = load_mat(timeseries_data) shape = timeseries.shape block_size = calc_blocksize(shape, memory_allocated) corr_matrix = np.zeros((shape[0], shape[0]), dtype = np.float16) j=0 i = block_size while i <= timeseries.shape[0]: print "block -> ", i + j temp_matrix = np.nan_to_num(calc_corrcoef(timeseries[j:i].T, timeseries.T)) corr_matrix[j:i] = temp_matrix j = i if i == timeseries.shape[0]: break elif (i+block_size) > timeseries.shape[0]: i = timeseries.shape[0] else: i += block_size r_value = calc_threshold(option, threshold, scans, corr_matrix, full_matrix = True) print "r_value -> ", r_value if method_options[0]: print "calculating binarize degree centrality matrix..." degree_matrix = np.sum( corr_matrix > r_value , axis = 1) -1 out_list.append(('degree_centrality_binarize', degree_matrix)) print "calculating weighted degree centrality matrix..." degree_matrix = np.sum( corr_matrix*(corr_matrix > r_value), axis= 1) -1 out_list.append(('degree_centrality_weighted', degree_matrix)) if method_options[1]: out_list.extend(calc_eigenV(corr_matrix, r_value, weight_options)) except Exception: print "Error while calculating centrality" raise return out_list