Esempio n. 1
0
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_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
Esempio n. 3
0
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 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