def scale_free_tau(corrmat, skew_thresh, proportional=True):
    ''''
    Calculates threshold at which network becomes scale-free, estimated from the skewness of the networks degree distribution.
    Parameters
    ----------
    corrmat : numpy.array
        Correlation or other connectivity matrix from which tau_connected will be estimated.
        Should be values between 0 and 1.
    proportional : bool
        Determines whether connectivity matrix is thresholded proportionally or absolutely.
        Default is proportional as maintaining network density across participants is a priority
    Returns
    -------
    tau : float
        Lowest vaue of tau (threshold) at which network is scale-free.
    '''
    tau = 0.01
    skewness = 1
    while abs(skewness) > 0.3:
        if proportional:
            w = bct.threshold_proportional(corrmat, tau)
        else:
            w = bct.threshold_absolute(corrmat, tau)
        skewness = skew(bct.degrees_und(w))
        tau += 0.01
    return tau
def connected_tau(corrmat, proportional=True):
    '''
    Calculates threshold at network becomes node connected, using NetworkX's `is_connected` function.
    Parameters
    ----------
    corrmat : numpy.array
        Correlation or other connectivity matrix from which tau_connected will be estimated.
        Should be values between 0 and 1.
    proportional : bool
        Determines whether connectivity matrix is thresholded proportionally or absolutely.
        Default is proportional as maintaining network density across participants is a priority
    Returns
    -------
    tau : float
        Highest vaue of tau (threshold) at which network becomes node-connected.
    '''
    tau = 1
    connected = False
    while connected == False:
        if proportional:
            w = bct.threshold_proportional(corrmat, tau)
        else:
            w = bct.threshold_absolute(corrmat, tau)
        w_nx = nx.convert_matrix.from_numpy_array(w)
        connected = nx.algorithms.components.is_connected(w_nx)
        tau -= 0.01
    return tau
Exemple #3
0
def plot_spring_layout(results, communities, threshold):
    """
    #================================
    Plot community affiliation in a spring layout
    #================================

    inputs:
    results: pandas dataframe with cognitive variables
    communities: numpy array of community affiliation
    threshold: Pearson corrletion threshold
    """

    import bct
    import matplotlib.pyplot as plt
    import networkx as nx
    import numpy as np
    from scipy.stats import zscore

    results[results.columns] = results.apply(zscore)
    correlation_matrix = results.transpose().corr().values

    plt.figure(figsize=(one_half_column / 2, one_half_column / 2), dpi=300)
    G = nx.from_numpy_matrix(
        bct.threshold_absolute(correlation_matrix, threshold))
    colours = ['#FF8C00', '#FFD700', '#D3D3D3']

    pos = nx.spring_layout(G)

    for community in np.unique(communities):

        nx.draw_networkx_nodes(
            G,
            pos,
            nodelist=np.where(communities == community)[0].tolist(),
            node_color=colours[community - 1],
            node_size=40,
            alpha=0.8)

    nx.draw_networkx_edges(G, pos, width=0.5, alpha=0.5)
    plt.axis('off')
    plt.legend(['C' + str(community) for community in np.unique(communities)])
    #plt.xlim([0,min((W>0).sum(axis=0).max(),pij.sum(axis=0).max())])
    #plt.ylim([0,min((W>0).sum(axis=0).max(),pij.sum(axis=0).max())])

    plt.subplot(2,3,5)
    plt.plot(W.sum(axis=0),wij.sum(axis=0), 'b.')
    plt.plot(np.linspace(0,wij.sum(axis=0).max()),np.linspace(0,wij.sum(axis=0).max()),'r-')
    plt.title('$ s_i - <s_i>$')
    plt.axis('equal')
    #plt.xlim([0,wij.sum(axis=0).max()])
    #plt.ylim([0,wij.sum(axis=0).max()])
    plt.grid(True)
    plt.ylabel('model')
    plt.xlabel('empirical')

    plt.tight_layout()
    plt.show()

if __name__=='__main__':

    thresh = 0.2 # threshold
    T = 200 # number of time points to sample
    eta = 3.0 # localnoise
    mu = 1.0 # globalnoise
    C = np.arctanh(factor_model([1]*40 + [2]*40 + [3]*30, T, eta, mu, True))
    At = bct.threshold_absolute(C, thresh)
    n=len(At)
    k = (At>0).sum(axis=0)
    s = At.sum(axis=0)

    x,y = inference_cEWRGt(At, thresh)
    plot_results(At, x, y, thresh)
Exemple #5
0
def test_threshold_absolute():
    x = load_sample()
    x = bct.threshold_absolute(x, 2.1)
    assert np.allclose(np.sum(x), 13280.17768104)
    #  print '   e.g. average_lh_inflated  U050E1IO_adjacency_matrix_pcc.txt 0.0'
    print '   e.g. average_lh_inflated  0.0'
    sys.exit()

root_fname = sys.argv[1]
#nw_fname = sys.argv[2]
nw_fname = "1X4I9DF0_adjacency_matrix_pcc.txt"
threshold = float(sys.argv[2])
print 'threshold=', threshold

adjMtx = np.genfromtxt(nw_fname, delimiter='\t')
"""Rubinov,Sporns 2010:  all self-connections or negative connections
(such as functional anticorrelations) must currently be removed
from the networks prior to analysis"""

bct.threshold_absolute(adjMtx, threshold)

# NB! be sure to put before masking the matrix
[Ci, Q] = bct.modularity_und(adjMtx)

roi_legend = 'roi_legend.txt'
try:
    fp_roi = open(roi_legend, 'r')
except:
    print("Error opening " + roi_legend)

# Create a dictionary mapping ID --> RGB
vc = {}  # vertex color dictionary
count = 0
# colors (rgb) for modules (Q: what's the max # of modules we'll have??)
# rf. http://www.rapidtables.com/web/color/RGB_Color.htm
Exemple #7
0
#mc=[['255 0 0'],['0 255 0'],['0 0 255'],['255 255 0'],['0 255 255']]

threshVal = 0.3
threshVal = 0.0
threshVal = input("Enter threshold: ")

#testDir = '/Users/heiland/Documents/Heiland/BioVis14/contest_subject_networks/'
#fname = 'U050E1IO_adjacency_matrix_pcc.txt'
fname = '1X4I9DF0_adjacency_matrix_pcc.txt'
#d = np.genfromtxt(testDir + fname, delimiter='\t')
try:
    A = np.genfromtxt(fname, delimiter='\t')
except:
    print "Error opening " + fname

bct.threshold_absolute(A, threshVal)

# Calculate the network's modularity
[Ci, Q] = bct.modularity_und(A)

print Ci
for i in range(len(Ci)):
    for j in range(i + 1, len(Ci)):
        #    print i,j
        if Ci[i] > Ci[j]:
            temp = Ci[i]
            Ci[i] = Ci[j]
            Ci[j] = temp
            print str(i) + ' <-> ' + str(j)
            A[[i, j]] = A[[j, i]]  # swap rows
            A[:, [i, j]] = A[:, [j, i]]  # swap cols
  print '   e.g. average_lh_inflated  0.0'
  sys.exit()

root_fname = sys.argv[1]
#nw_fname = sys.argv[2]
nw_fname = "1X4I9DF0_adjacency_matrix_pcc.txt"
threshold = float(sys.argv[2])
print 'threshold=',threshold

adjMtx = np.genfromtxt(nw_fname, delimiter='\t')

"""Rubinov,Sporns 2010:  all self-connections or negative connections
(such as functional anticorrelations) must currently be removed
from the networks prior to analysis"""

bct.threshold_absolute(adjMtx,threshold)

# NB! be sure to put before masking the matrix
[Ci,Q]=bct.modularity_und(adjMtx)

roi_legend = 'roi_legend.txt'
try:
  fp_roi =open(roi_legend, 'r')
except:
  print("Error opening " + roi_legend)

# Create a dictionary mapping ID --> RGB
vc = {}  # vertex color dictionary
count = 0
# colors (rgb) for modules (Q: what's the max # of modules we'll have??)
# rf. http://www.rapidtables.com/web/color/RGB_Color.htm
def graph_estimates(cm, th):

    #dictionary for storing our results
    d = OrderedDict()

    #thresholding moved here for other matrices than MatLab matrices
    #removes negative weights
    cm = bct.threshold_absolute(cm, 0.0)

    cm = threshold_connected(cm, th)

    
    #for binarizing the connectivity matrices, 
    #we work with weighted so this is turned off
    #bin_cm = bct.binarize(cm)
    
    #invert the connectivity for computing shortest paths
    cm_inv = bct.invert(cm)

    #modularity_und is found in modularity.py
    modularity_und = bct.modularity_und(cm)

    #the community_affiliation vector that gets input to some of the functions
    community_affiliation = modularity_und[0]
    
    #distance_wei and charpath is found in distance.py
    distance_wei = bct.distance_wei(cm_inv)
    charpath = bct.charpath(distance_wei[0], False, False)

    #clustering_coef_wu is found in clustering.py
    clustering_coef_wu = bct.clustering_coef_wu(cm)
    avg_clustering_coef_wu = np.mean(clustering_coef_wu)


    #assortativity_wei is found in core.py
    d['assortativity_wei-r'] = bct.assortativity_wei(cm, flag=0)

    #just taking the average of clustering_coef_wu
    d['avg_clustering_coef_wu:C'] = avg_clustering_coef_wu

    d['charpath-lambda'] = charpath[0]
    #d['charpath-efficiency'] = charpath[1]   
    #d['charpath-ecc'] = charpath[2]           
    #d['charpath-radius'] = charpath[3]
    #d['charpath-diameter'] = charpath[4]

    d['clustering_coef_wu-C'] = clustering_coef_wu


    d['efficiency_wei-Eglob'] = bct.efficiency_wei(cm)
    #d['efficiency_wei-Eloc'] = bct.efficiency_wei(cm, True)

    #d['modularity_und-ci'] = modularity_und[0]
    d['modularity_und-Q'] = modularity_und[1]

    d['small_worldness:S'] = compute_small_worldness(cm,
                                                     avg_clustering_coef_wu,
                                                     charpath[0])

   
   #transitivity_wu can be found in clustering.py
    d['transitivity_wu-T'] = bct.transitivity_wu(cm)


    #EXAMPLES for local measures and binary measures. Comment in to use. 

    #VECTOR MEASURES
    #d['betweenness_wei-BC'] = bct.betweenness_wei(cm_inv)
    # d['module_degree_zscore-Z'] = bct.module_degree_zscore(cm, community_affiliation)
    #d['degrees_und-deg'] = bct.degrees_und(cm)
    #d['charpath-ecc'] = charpath[2]


    #BINARIES
    # d['clustering_coef_bu-C'] = bct.clustering_coef_bu(bin_cm)
    # d['efficiency_bin-Eglob'] = bct.efficiency_bin(bin_cm)
    # d['efficiency_bin-Eloc'] = bct.efficiency_bin(bin_cm, True)
    #  d['modularity_und_bin-ci'] = modularity_und_bin[0]
    #  d['modularity_und_bin-Q'] = modularity_und_bin[1]
    # d['transitivity_bu-T'] = bct.transitivity_bu(bin_cm)
    #  d['betweenness_bin-BC'] = bct.betweenness_bin(bin_cm)
    #  modularity_und_bin = bct.modularity_und(bin_cm)
    #d['participation_coef'] = bct.participation_coef(cm, community_affiliation)


    ######## charpath giving problems with ecc, radius and diameter
    # np.seterr(invalid='ignore')


    return d
Exemple #10
0
                     2)
gamma_EEG = np.delete((io.loadmat(gamma_EEG_path)).get('connEEGgamma'),
                      np.s_[:3], 2)

# create static arrays (no time series)
static_fMRI = np.average(fMRI, 2)
static_broad_EEG = np.average(broad_EEG, 2)
static_delta_EEG = np.average(delta_EEG, 2)
static_theta_EEG = np.average(theta_EEG, 2)
static_alpha_EEG = np.average(alpha_EEG, 2)
static_beta_EEG = np.average(beta_EEG, 2)
static_gamma_EEG = np.average(gamma_EEG, 2)

# threshold fMRI
#plots.plot_connectivity_matrix(static_fMRI, "Connectivity", "No threshold", "static_fMRI", False)
bct.threshold_absolute(static_fMRI, thr=0, copy=False)
#plots.plot_connectivity_matrix_thresholded(static_fMRI, "Connectivity", "Threshold > 0", "static_fMRI_threshold0", False)
static_fMRI_unweighted = bct.weight_conversion(static_fMRI,
                                               'binarize',
                                               copy=True)
#plots.plot_connectivity_matrix_binarized(static_fMRI_unweighted, "Unweighted (binarized)", "static_fMRI_unweighted", False)

# threshold EEGs
names = ("static_broad_EEG", "static_delta_EEG", "static_theta_EEG",
         "static_alpha_EEG", "static_beta_EEG", "static_gamma_EEG")
matrices = [
    static_broad_EEG, static_delta_EEG, static_theta_EEG, static_alpha_EEG,
    static_beta_EEG, static_gamma_EEG
]
matrices_unweighted = []
Exemple #11
0
def cal_thalamus_and_cortical_ROIs_nodal_properties(Thalamocortical_corrmat, Cortical_adj, \
 Cortical_plus_thalamus_CI, Thalamus_CIs, Cortical_CI, Cortical_ROIs_positions, Thalamus_voxel_positions, cost_thresholds):
    '''Function to calculate voxel-wise nodal properties of the thalamus, and nodal properties of cortical ROIs. 
	Metrics to be calculated include:
	
	Participation Coefficient (PC)
	Between network connectivity weiight (BNWR)
		Ratio of connection weight devoted to between network interactions
	Number of network/modules/components connected (NNC)
	Within module degree zscore (WMD)
		For WMD, matrices will be binarzied, and normalized to corticocortical connections' mean and SD

	usage: PCs, BNWRs, NNCs, WMDs, bPCs, mean_NNC, mean_BNWR, mean_PC, mean_bPC, mean_WMD = cal_thalamus_and_cortical_ROIs_nodal_properties(Thalamocor_adj,
                Cortical_adj,
                Cortical_plus_thalamus_CI,
                Thalamus_CIs,
                Cortical_CI,
                Cortical_ROIs_positions,
                Thalamus_voxel_positions,
                cost_thresholds)
    
    ----
    Parameters
    ----
    Thalamocor_adj: Thalamocortical adj matrix
    Cortical_adj: corticocortical adj matrix
    Cortical_plus_thalamus_CI: A vector of community/module/network assignment of all nodes, cortical ROIs + thalamic voxels
    Thalamus_CIs: A vector of network assignements for thalamic voxels
    Cortical_CI: A vector of network assignments for cortical ROIs 
    Cortical_ROIs_positions: a position vector indicating in the thalamocortical adj matrix which rows/columns are cortical ROIs
    Thalamus_voxel_posistions: a position vector indicating in the thalamocortical adj matrix which rows/columns are thalamic voxels
    cost_thresholds: the thoresholds that can threshold the thalamocortical edges at density .01 to .15. 

	return variables are graph metrics across thresholds (with "s"), or averaged across thresholds "mean"

    '''

    ##Thalamus nodal roles
    Thalamocortical_corrmat[np.isnan(Thalamocortical_corrmat)] = 0

    #PC
    PCs = []  #np.zeros(Cortical_plus_thalamus_CI.size)
    bPCs = []  #np.zeros(Cortical_plus_thalamus_CI.size)
    #BNWR between network connectivity weight
    BNWRs = []  #np.zeros(Cortical_plus_thalamus_CI.size)
    #get number of networks/communities connected
    NNCs = []  #np.zeros(Cortical_plus_thalamus_CI.size)

    #loop through costs
    for c in cost_thresholds:
        #copy adj matrix and then threshold
        Par_adj = Thalamocortical_corrmat.copy()
        #remove weights connected to low SNR communities (CI==0, orbital frontal, inferior temporal)
        Par_adj[Cortical_ROIs_positions[Cortical_CI == 0], :] = 0
        Par_adj[:, Cortical_ROIs_positions[Cortical_CI == 0]] = 0
        Par_adj[Par_adj < c] = 0

        #binary
        bPar_adj = Par_adj.copy()
        bPar_adj = bPar_adj > c

        #PC
        PCs += [bct.participation_coef(Par_adj, Cortical_plus_thalamus_CI)]
        bPCs += [bct.participation_coef(bPar_adj, Cortical_plus_thalamus_CI)]
        #aPCs += [bct.participation_coef(Par_adj, Cortical_plus_thalamus_CI)]

        #BNWR and NNCs
        Tha_BNWR = np.zeros(Cortical_plus_thalamus_CI.size)
        Tha_NNCs = np.zeros(Cortical_plus_thalamus_CI.size)
        for ix, i in enumerate(Thalamus_voxel_positions):
            sum_between_weight = np.nansum(
                Par_adj[i, Cortical_plus_thalamus_CI != Thalamus_CIs[ix]])
            sum_total = np.nansum(Par_adj[i, :])
            Tha_BNWR[i] = sum_between_weight / sum_total
            Tha_BNWR[i] = np.nan_to_num(Tha_BNWR[i])

            Tha_NNCs[i] = len(
                np.unique(Cortical_plus_thalamus_CI[Par_adj[i, ] != 0]))
        BNWRs += [Tha_BNWR]
        NNCs += [Tha_NNCs]

    ##Cortical nodal roles
    Cortical_adj[np.isnan(Cortical_adj)] = 0

    Cortical_PCs = []  #np.zeros(Cortical_CI.size)
    Cortical_bPCs = []  #np.zeros(Cortical_CI.size)
    Cortical_BNWR = []  #np.zeros(Cortical_CI.size)
    Cortical_NNCs = []  #np.zeros(Cortical_plus_thalamus_CI.size)

    for ix, c in enumerate(np.arange(0.01, 0.16, 0.01)):
        M = bct.threshold_proportional(Cortical_adj, c, copy=True)
        bM = bct.weight_conversion(M, 'binarize', copy=True)

        #PC
        Cortical_PCs += [bct.participation_coef(M, Cortical_CI)]
        Cortical_bPCs += [bct.participation_coef(bM, Cortical_CI)]

        #BNWR and NNC
        BNWR = np.zeros(Cortical_CI.size)
        Cor_NNCs = np.zeros(Cortical_plus_thalamus_CI.size)
        for i in range(len(Cortical_CI)):
            sum_between_weight = np.nansum(M[i, Cortical_CI != Cortical_CI[i]])
            sum_total = np.nansum(M[i, :])
            BNWR[i] = sum_between_weight / sum_total
            BNWR[i] = np.nan_to_num(BNWR[i])

            Cor_NNCs[i] = len(np.unique(Cortical_CI[M[i, ] != 0]))
        Cortical_BNWR += [BNWR]
        Cortical_NNCs += [Cor_NNCs]

    #do WMD, first convert matrices to binary, then calcuate z score using mean and std of "corticocortical degrees"
    Cortical_wm_mean = {}
    Cortical_wm_std = {}
    Cortical_WMDs = []  #np.zeros(Cortical_CI.size)
    WMDs = []  #np.zeros(Cortical_plus_thalamus_CI.size)
    for ix, c in enumerate(np.arange(0.01, 0.16, 0.01)):

        #threshold by density
        bM = bct.weight_conversion(
            bct.threshold_proportional(Cortical_adj, c, copy=True), 'binarize')
        Cortical_WMDs += [bct.module_degree_zscore(bM, Cortical_CI)]

        #return mean and degree
        for CI in np.unique(Cortical_CI):
            Cortical_wm_mean[ix + 1, CI] = np.nanmean(
                np.sum(bM[Cortical_CI == CI, :][:, Cortical_CI == CI], 1))
            Cortical_wm_std[ix + 1, CI] = np.nanstd(
                np.sum(bM[Cortical_CI == CI, :][:, Cortical_CI == CI], 1))

        #thalamic WMD, threshold by density
        M = bct.weight_conversion(
            bct.threshold_absolute(Thalamocortical_corrmat,
                                   cost_thresholds[ix],
                                   copy=True), 'binarize')

        tha_wmd = np.zeros(Cortical_plus_thalamus_CI.size)
        for i in np.unique(Cortical_CI):
            tha_wmd[Cortical_plus_thalamus_CI==i] = (np.sum(M[Cortical_plus_thalamus_CI==i][:, Cortical_plus_thalamus_CI==i],1)\
            - Cortical_wm_mean[ix+1,i])/Cortical_wm_std[ix+1,i]
        tha_wmd = np.nan_to_num(tha_wmd)
        WMDs += [tha_wmd]

    # organize output
    NNCs = np.array(NNCs)
    BNWRs = np.array(BNWRs)
    PCs = np.array(PCs)
    bPCs = np.array(bPCs)
    WMDs = np.array(WMDs)

    NNCs[:, Cortical_ROIs_positions] = np.array(
        Cortical_NNCs)[:, Cortical_ROIs_positions]
    BNWRs[:, Cortical_ROIs_positions] = np.array(
        Cortical_BNWR)[:, Cortical_ROIs_positions]
    PCs[:, Cortical_ROIs_positions] = np.array(
        Cortical_PCs)[:, Cortical_ROIs_positions]
    bPCs[:, Cortical_ROIs_positions] = np.array(
        Cortical_bPCs)[:, Cortical_ROIs_positions]
    WMDs[:, Cortical_ROIs_positions] = np.array(
        Cortical_WMDs)[:, Cortical_ROIs_positions]

    # average across thresholds, convert into percentage
    mean_NNC = (np.sum(NNCs, axis=0) / 15.0) * 100
    mean_BNWR = (np.sum(BNWRs, axis=0) / 15.0) * 100
    mean_PC = (np.sum(PCs, axis=0) /
               13.5) * 100  #this is the thoretical upperbound
    mean_bPC = (np.sum(bPCs, axis=0) /
                13.5) * 100  #this is the thoretical upperbound
    mean_WMD = (np.sum(WMDs, axis=0) / 15.0) * 100

    return PCs, BNWRs, NNCs, WMDs, bPCs, mean_NNC, mean_BNWR, mean_PC, mean_bPC, mean_WMD
Exemple #12
0
#mc=[['255 0 0'],['0 255 0'],['0 0 255'],['255 255 0'],['0 255 255']]

threshVal=0.3
threshVal=0.0
threshVal=input("Enter threshold: ")

#testDir = '/Users/heiland/Documents/Heiland/BioVis14/contest_subject_networks/'
#fname = 'U050E1IO_adjacency_matrix_pcc.txt'
fname = '1X4I9DF0_adjacency_matrix_pcc.txt'
#d = np.genfromtxt(testDir + fname, delimiter='\t')
try:
  A = np.genfromtxt(fname, delimiter='\t')
except:
  print "Error opening " + fname

bct.threshold_absolute(A,threshVal)

# Calculate the network's modularity
[Ci,Q]=bct.modularity_und(A)

print Ci
for i in range(len(Ci)):
  for j in range(i+1,len(Ci)):
#    print i,j
    if Ci[i] > Ci[j]:
      temp = Ci[i]
      Ci[i] = Ci[j]
      Ci[j] = temp
      print str(i) + ' <-> ' + str(j)
      A[[i,j]] = A[[j,i]]  # swap rows
      A[:,[i,j]] = A[:,[j,i]]  # swap cols