def compute_small_worldness(cm, cc, cpl):

    #randmio_und_connected can be found in reference.py
    #second argument is number of iterations
    #construct a random network for comparison with our real network
    rand_network = bct.randmio_und_connected(cm,5)[0]

    #clustering_coef_wu is found in clustering.py
    #make sure that C_rand is non-zero, so we avoid division with zero
    #could probably be made more correct by taking the average of
    #some number of random networks. 
    #we did not do this to keep run time at a minimum
    C_rand = np.mean(bct.clustering_coef_wu(rand_network))
    while C_rand == 0.0:
        rand_network = bct.randmio_und_connected(cm,5)[0]
        C_rand = np.mean(bct.clustering_coef_wu(rand_network))

    #invert can be found in other.py
    rand_inv = bct.invert(rand_network)
    
    #distance_wei and charpath can be found in distance.py
    distance_rand = bct.distance_wei(rand_inv)
    charpath_rand = bct.charpath(distance_rand[0])
 
    #compute the small worldness index according to Rubinov
    C = cc
    L = cpl
    L_rand = charpath_rand[0]

    Ctemp = C/C_rand
    Ltemp = L/L_rand

    S_W = Ctemp / Ltemp

    return S_W
    def create_feature_matrix(self):
       # Feature matrix with each element containing an NxN array
       feature_matrix = []

       # EDGE WEIGHT (Depth 0)
       structural_connectivity_array = self.get_structure_and_function()
       feature_matrix.append(structural_connectivity_array)

       # DEGREE (Depth 1 & 2)
       deg = bct.degrees_und(structural_connectivity_array)
       self.fill_array_2D(feature_matrix, deg)

       # Conversion of connection weights to connection lengths
       connection_length_matrix = bct.weight_conversion(structural_connectivity_array, 'lengths')

       # SHORTEST PATH LENGTH (Depth 3 & 4)
       shortest_path = bct.distance_wei(connection_length_matrix)
       feature_matrix.append(shortest_path[0])  # distance (shortest weighted path) matrix
       feature_matrix.append(shortest_path[1])  # matrix of number of edges in shortest weighted path

       # BETWEENNESS CENTRALITY (Depth 5 & 6)
       bc = bct.betweenness_wei(connection_length_matrix)
       from python_files.create_feature_matrix import fill_array_2D
       self.fill_array_2D(feature_matrix, bc)

       # CLUSTERING COEFFICIENTS (Depth 7 & 8)
       cl = bct.clustering_coef_wu(connection_length_matrix)
       self.fill_array_2D(feature_matrix, cl)

       return feature_matrix
def local_clustering(data):
    cluster_coeffs = []
    for i, x in enumerate(data):
        matrix = mp.preprocess_matrix(x)
        coeffs = bct.clustering_coef_wu(matrix)

        cluster_coeffs.append(coeffs.tolist())

    return cluster_coeffs
Exemple #4
0
    def compute(self):
        # supposed to be the binarized matrix
        wcc = bct.clustering_coef_wu(self.g)

        self.stats['WeightedCluseringCoefficient'] = [v for v in wcc]
        average = statistics.mean(self.stats['WeightedCluseringCoefficient'])

        print("Average Weighted Clustering Coefficient: " + str(average) +
              "\n")
        return self.stats
def small_world_wu(W):
    '''
    An implementation of small worldness. Returned is the coefficient cc/lambda,
    the ratio of the clustering coefficient to the characteristic path length.
    This ratio is >>1 for small world networks.

    inputs: W		weighted undirected connectivity matrix

    output: s		small world coefficient
    '''
    cc = clustering_coef_wu(W)
    _, dists = breadthdist(W)
    _lambda, _, _, _, _ = charpath(dists)
    return np.mean(cc) / _lambda
Exemple #6
0
def small_world_wu(W):
    '''
    An implementation of small worldness. Returned is the coefficient cc/lambda,
    the ratio of the clustering coefficient to the characteristic path length.
    This ratio is >>1 for small world networks.

    inputs: W		weighted undirected connectivity matrix

    output: s		small world coefficient
    '''
    cc = clustering_coef_wu(W)
    _, dists = breadthdist(W)
    _lambda, _, _, _, _ = charpath(dists)
    return np.mean(cc) / _lambda
Exemple #7
0
def extract_epoch_graph_features(W):
    import bct

    L = bct.weight_conversion(W, "lengths")
    L[W == 0] = np.inf
    D, _ = bct.distance_wei(L)

    l, eff, ecc, radius, diameter = bct.charpath(D, include_infinite=False)

    return [
        bct.clustering_coef_wu(W),
        bct.efficiency_wei(W, local=True),
        bct.betweenness_wei(L),
        ecc,
        [l, eff, radius, diameter],
    ]
def get_outlier_nodes(data, feat="deg", threshold=1e-3):
    T = len(data)
    n = len(data[0])
    feature = np.empty((T, n))
    for t in range(0, T):
        if feat == "deg":
            feature[t] = data[t].sum(axis=0)
        elif feat == "cent":
            feature[t] = bct.betweenness_wei(data[t])
        elif feat == "cc":
            feature[t] = bct.clustering_coef_wu(data[t])
        elif feat == "assort":
            feature[t] = bct.assortativity_wei(data[t])

    diff = forward_diff(forward_diff(feature))
    ind_vec = diff > threshold
    print(ind_vec.sum(axis=None))
    return ind_vec
Exemple #9
0
def do_opt(adj,mods,option):
    if option=='global efficiency':
        return bct.efficiency_wei(adj)
    elif option=='local efficiency':
        return bct.efficiency_wei(adj,local=True)
    elif option=='average strength':
        return bct.strengths_und(adj)
    elif option=='clustering coefficient':
        return bct.clustering_coef_wu(adj)
    elif option=='eigenvector centrality':
        return bct.eigenvector_centrality_und(adj)
    elif option=='binary kcore':
        return bct.kcoreness_centrality_bu(adj)[0]

    elif option=='modularity':
        return bct.modularity_und(adj,mods)[1]
    elif option=='participation coefficient':
        return bct.participation_coef(adj,mods)
    elif option=='within-module degree':
        return bct.module_degree_zscore(adj,mods)
Exemple #10
0
def do_opt(adj, mods, option):
    if option == 'global efficiency':
        return bct.efficiency_wei(adj)
    elif option == 'local efficiency':
        return bct.efficiency_wei(adj, local=True)
    elif option == 'average strength':
        return bct.strengths_und(adj)
    elif option == 'clustering coefficient':
        return bct.clustering_coef_wu(adj)
    elif option == 'eigenvector centrality':
        return bct.eigenvector_centrality_und(adj)
    elif option == 'binary kcore':
        return bct.kcoreness_centrality_bu(adj)[0]

    elif option == 'modularity':
        return bct.modularity_und(adj, mods)[1]
    elif option == 'participation coefficient':
        return bct.participation_coef(adj, mods)
    elif option == 'within-module degree':
        return bct.module_degree_zscore(adj, mods)
        participants = ['z1','z2','z3','z4','z5','z6','z8']
    elif group == 'control':
        participants = ['c1','c2','c3','c5','c6','c7','c8']
        
    all_measures = np.empty(shape=[68,len(participants),5])
    adjmats =  np.empty(shape=[68,68,len(participants)])
    counter = 0

    for participant in participants:
        adjmat = sio.loadmat(participant + '_FA.mat')
        adjmat = adjmat['adjacency_matrix']
        labels = get_parcellation_labels(generate_ROI_file(FreeSurfer_ROI_file)).values
        labels,adjmat = remove_non_cortical_ROIs(labels,adjmat)
        all_measures[:,counter,0] = bct.degrees_und(adjmat)
        all_measures[:,counter,1] = bct.strengths_und(adjmat)
        all_measures[:,counter,2] = bct.clustering_coef_wu(adjmat)
        all_measures[:,counter,3] = bct.betweenness_wei(adjmat)
        all_measures[:,counter,4] = bct.efficiency_wei(adjmat,local=True)
        adjmats[:,:,counter] = adjmat
        counter += 1
        
        
    mean_measures = np.mean(all_measures,axis=1)
    if group == 'patient':
        patient = pd.DataFrame(mean_measures, index=labels,columns=['patient.NodeDegree','patient.Strength','patient.ClustCoeff','patient.BetweenCent','patient.LocEff'])
        patient_measures = all_measures
        patient_adjmats = adjmats
    elif group == 'control':
        control = pd.DataFrame(mean_measures, index=labels,columns=['control.NodeDegree','control.Strength','control.ClustCoeff','control.BetweenCent','control.LocEff'])
        control_measures = all_measures
        control_adjmats = adjmats
Exemple #12
0
        #the more metrics you use, the more you have to correct for multiple comparisons
        #make sure this is hypothesis-driven and not fishing

        for p in thresh_range:
            ge = []
            cc = []
            ntwk_corrmat_thresh = bct.threshold_proportional(
                network_correlation_matrix, p, copy=True)
            #np.savetxt(join(sink_dir, sessions[i], s, '{0}_corrmat_Laird2011_thresh_{1}.csv'.format(s, p)), ntwk_corrmat_thresh, delimiter=',')
            #measures of interest here
            #global efficiency
            le = bct.efficiency_wei(ntwk_corrmat_thresh)
            ge.append(le)

            #clustering coefficient
            c = bct.clustering_coef_wu(ntwk_corrmat_thresh)
            cc.append(c)

            network[p] = ge
            network_wise[p] = cc

        ntwk_df = pd.Series(network).T
        #ntwk_df.columns = ['total positive', 'total negative', 'efficiency', 'path length', 'modularity']

        ntwk_wise_df = pd.Series(network_wise).T
        #ntwk_wise_df.columns = ['betweenness', 'degree', 'positive weights', 'negative weights',
        #                                                   'community index', 'clustering coefficient']
        ntwk_df.to_csv(join(sink_dir, sessions[i], s,
                            '{0}_network_metrics.csv'.format(s)),
                       sep=',')
        ntwk_wise_df.to_csv(join(sink_dir, sessions[i], s,
Exemple #13
0
def test_cluscoef_wu():
    x = load_sample(thres=.23)
    cc = bct.clustering_coef_wu(x)
    assert np.allclose(np.sum(cc), 187.95878414)
Exemple #14
0
def test_cluscoef_signed():
    x = load_signed_sample(thres=.85)
    cc = bct.clustering_coef_wu(x)
    assert np.imag(np.sum(cc)) == 0
    elif group == 'control':
        participants = ['c1', 'c2', 'c3', 'c5', 'c6', 'c7', 'c8']

    all_measures = np.empty(shape=[68, len(participants), 5])
    adjmats = np.empty(shape=[68, 68, len(participants)])
    counter = 0

    for participant in participants:
        adjmat = sio.loadmat(participant + '_FA.mat')
        adjmat = adjmat['adjacency_matrix']
        labels = get_parcellation_labels(
            generate_ROI_file(FreeSurfer_ROI_file)).values
        labels, adjmat = remove_non_cortical_ROIs(labels, adjmat)
        all_measures[:, counter, 0] = bct.degrees_und(adjmat)
        all_measures[:, counter, 1] = bct.strengths_und(adjmat)
        all_measures[:, counter, 2] = bct.clustering_coef_wu(adjmat)
        all_measures[:, counter, 3] = bct.betweenness_wei(adjmat)
        all_measures[:, counter, 4] = bct.efficiency_wei(adjmat, local=True)
        adjmats[:, :, counter] = adjmat
        counter += 1

    mean_measures = np.mean(all_measures, axis=1)
    if group == 'patient':
        patient = pd.DataFrame(mean_measures,
                               index=labels,
                               columns=[
                                   'patient.NodeDegree', 'patient.Strength',
                                   'patient.ClustCoeff', 'patient.BetweenCent',
                                   'patient.LocEff'
                               ])
        patient_measures = all_measures
def load_network(kind,
                 parcel,
                 data="lau",
                 weights='log',
                 hemi="both",
                 version=1,
                 subset="all",
                 path=None):
    '''
    Function to load a dictionary containing information about the specified
    brain network.

    Parameters
    ----------
    kind : string
        Either 'SC' or 'FC'.
    hemi : string
        Either "both", "L" or "R".
    weights " string
        The weights of the edges. The options  "normal", "log" or "binary".
        The default is "log".
    data : string
        Either "HCP" or "lau".
    parcel : string
        Either "68", "114", ... [if 'lau'] / "s400", "s800" [if "HCP"]
    version : int
        Version of the network.
    subset : string
        Either 'discov', 'valid' or 'all'
    path : string
        path to the "data" folder in which the data will be stored. If
        none, then assumes that path is current folder.

    Returns
    -------
    Network : dictionary
        Dictionary storing relevant attributes about the network
    '''

    # Initialize dictionary + store basic information about the network
    Network = {}
    Network["info"] = {}
    Network["info"]["kind"] = kind
    Network["info"]["parcel"] = parcel
    Network["info"]["data"] = data
    Network["info"]["hemi"] = hemi
    Network["info"]["weights"] = weights
    Network["info"]["version"] = version
    Network["info"]["subset"] = subset

    # Modify parameter names to what they are in file names
    version = '' if version == 1 else '_v' + str(version)
    subset = '' if subset == 'all' else subset
    hemi = '' if hemi == 'both' else hemi

    # Store important paths for loading the relevant data
    main_path = path + "/brainNetworks/" + data + "/"
    network_path = (main_path + "matrices/consensus/" + subset + kind +
                    parcel + hemi + version + "/")
    matrix_path = network_path + "/" + weights

    # Store general information about the network's parcellation
    parcel_info = get_general_parcellation_info(parcel)
    Network['order'] = parcel_info[0]
    Network['noplot'] = parcel_info[1]
    Network['lhannot'] = parcel_info[2]
    Network['rhannot'] = parcel_info[3]
    Network['atlas'] = parcel_info[4]

    # Load the cammoun_id of the parcellation, if Cammoun (i.e. 033, 060, etc.)
    if parcel[0] != 's':
        Network['cammoun_id'] = parcel_to_n(parcel)

    # masks
    masks = get_node_masks(Network, path=main_path)
    Network['node_mask'] = masks[0]
    Network['hemi_mask'] = masks[1]
    Network['subcortex_mask'] = masks[2]

    # hemisphere
    Network['hemi'] = get_hemi(Network, path=main_path)

    # coordinates
    Network['coords'] = get_coordinates(Network, path=main_path)

    # Adjacency matrix
    Network['adj'], last_modified = get_adjacency(Network,
                                                  matrix_path,
                                                  minimal_processing=True,
                                                  return_last_modified=True)

    # Test whether the network is connected. Raise a warning if not...
    if not np.all(bct.reachdist(Network['adj'])[0]):
        warnings.warn(("This brain network appears to be disconnected. This "
                       "might cause problem for the computation of the other "
                       "measures"))

    # node strength
    Network["str"] = np.sum(Network['adj'], axis=0)

    # Inverse of adjacency matrix
    inv = Network['adj'].copy()
    inv[Network['adj'] > 0] = 1 / inv[Network['adj'] > 0]
    Network["inv_adj"] = inv

    # distance
    Network["dist"] = cdist(Network["coords"], Network["coords"])

    # clustering coefficient
    Network["cc"] = bct.clustering_coef_wu(Network['adj'])

    # shortest path
    Network['sp'] = get_shortest_path(Network,
                                      matrix_path=matrix_path,
                                      last_modified=last_modified)

    # diffusion embedding
    de = compute_diffusion_map(Network['adj'],
                               n_components=10,
                               return_result=True,
                               skip_checks=True)
    Network["de"] = de[0]
    Network["de_extra"] = de[1]

    # Principal components
    Network['PCs'], Network['PCs_ev'] = getPCs(Network['adj'])

    # eigenvector centrality
    Network["ec"] = bct.eigenvector_centrality_und(Network['adj'])

    # mean first passage time
    Network["mfpt"] = bct.mean_first_passage_time(Network['adj'])

    # betweenness centrality
    Network['bc'] = get_betweenness(Network,
                                    matrix_path=matrix_path,
                                    last_modified=last_modified)

    # routing efficiency
    Network["r_eff"] = efficiency(Network)

    # diffusion efficiency
    Network["d_eff"] = efficiency_diffusion(Network)

    # subgraph centrality
    Network["subc"] = bct.subgraph_centrality(Network["adj"])

    # closeness centrality
    Network['clo'] = 1 / np.mean(Network['sp'], axis=0)

    # communities + participation coefficient
    path = matrix_path + "/communities/"
    if os.path.exists(path):
        files = []
        for i in os.listdir(path):
            if os.path.isfile(os.path.join(path, i)) and 'ci_' in i:
                files.append(i)
        if len(files) > 0:
            Network["ci"] = []
            for file in files:
                Network["ci"].append(np.load(os.path.join(path, file)))

            Network["ppc"] = []
            for i in range(len(files)):
                ppc = bct.participation_coef(Network['adj'], Network["ci"][i])
                Network["ppc"].append(ppc)

    # Edge lengths
    if (data == "HCP") and (kind == "SC"):
        path = main_path + "matrices/" + subset + kind + parcel + hemi + "_lengths.npy"
        if os.path.exists(path):
            Network["lengths"] = np.load(path)

    # streamline connection lengths
    path = network_path + "/len.npy"
    if os.path.exists(path):
        Network['len'] = np.load(path)

    # ROI names
    if parcel[0] != "s":
        Network['ROInames'] = get_ROInames(Network)

    # geodesic distances between nodes
    if parcel[0] == "s":
        n = parcel[1:]
        fname_l = n + "Parcels7Networks_lh_dist.csv"
        fname_r = n + "Parcels7Networks_rh_dist.csv"
    else:
        fname_l = "scale" + Network['cammoun_id'] + "_lh_dist.csv"
        fname_r = "scale" + Network['cammoun_id'] + "_rh_dist.csv"
    Network['geo_dist_L'] = pd.read_csv(main_path + "/geodesic/medial/" +
                                        fname_l,
                                        header=None).values
    Network['geo_dist_R'] = pd.read_csv(main_path + "/geodesic/medial/" +
                                        fname_r,
                                        header=None).values

    return Network
Exemple #17
0
def figure_4(data, specify, t_ids_b, method='laplacian', panels='all',
             show=True, save=False, save_path=None):
    '''
    Function to create Figure 4

    Parameters
    ----------
    data : dict
        Dictionary of the data to be used to generate the figures. If the
        required data is not found in this dictionary, the item of figure
        1 that requires this data will not be created and a message
        will be printed.
    specify : dict
        Dictionary containing information about the trajectories that will be
        shown in panel a (see main_script.py for an example).
    t_ids_b: List
        List of time points indices indicating the time points at which
        the centrality slope distributions will be plotted on the surface
        of the brain (panel b).
    method : str
        Method used to compute the transition probabilities. The purpose of
        this parameter is to choose whether the x_scale of our figures should
        be linear or logarithmic.
    panels : str or list
        List of the panels of Figure 1 that we want to create. If we want
        to create all of them, use 'all'. Otherwise, individual panels can
        be specified. For example, we could have panels=['a'] or
        panels=['a', 'b'].
    show : Boolean
        If True, the figures will be displayed. If not, the figures will
        bot be displayed.
    save : Boolean
        If True, the figures will be saved in the folder specified by the
        save_path parameter.
    save_path : str
        Path of the folder in which the figures will be saved.
    '''

    if show is False:
        plt.ioff()

    if panels == 'all':
        panels = ['a', 'b', 'c']

    n = len(data['sc'])
    k = len(data['t_points'])

    # Slopes of the closeness centrality trajectories
    slopes = np.gradient(data['cmulti'], axis=0)

    if 'a' in panels:

        required_entries = ['t_points', 'cmulti']
        requirements = check_requirements(data, required_entries)
        if requirements:

            node_ids = specify['ID']

            fig = plt.figure(figsize=(9, 3))
            ax = fig.add_subplot(111)
            for i in range(n):
                ax.plot(data['t_points'],
                        data['cmulti'][:, i],
                        c='lightgray')

            abs_max_color = max(-1 * np.amin(slopes), np.amax(slopes))

            for i, id in enumerate(node_ids):
                norm = plt.Normalize(-abs_max_color, abs_max_color)
                slope_colors = RdBu_11_r.mpl_colormap(norm(slopes[:, id]))
                for ii in range(k-1):
                    plt.plot([data['t_points'][ii],
                              data['t_points'][ii+1]],
                             [data['cmulti'][ii, id],
                              data['cmulti'][ii+1, id]],
                             c=slope_colors[ii, :],
                             linewidth=3)

            if method == 'laplacian':
                plt.xscale('log')

            plt.xlabel('t')
            plt.ylabel("c_multi")

            if save:
                figure_name = 'cmulti_with_gradient.png'
                fig.savefig(os.path.join(save_path, figure_name))

    if 'b' in panels:

        required_entries = ['t_points', 'lhannot', 'rhannot', 'noplot',
                            'order']
        requirements = check_requirements(data, required_entries)
        if requirements:

            fig = plt.figure(figsize=(9, 3))
            ax = fig.add_subplot(111)
            for i in range(n):
                ax.plot(data['t_points'],
                        slopes[:, i],
                        c='lightgray',
                        zorder=0)
            for t_id in t_ids_b:
                plt.scatter(np.zeros((n))+data['t_points'][t_id],
                            slopes[t_id, :],
                            marker='s', c=slopes[t_id, :],
                            cmap=RdBu_11_r.mpl_colormap, rasterized=True,
                            zorder=1)

            if method == 'laplacian':
                plt.xscale('log')

            plt.xlabel('t')
            plt.ylabel("slope")

            if save:
                figure_name = 'slopes.png'
                fig.savefig(os.path.join(save_path, figure_name))

            for t_id in t_ids_b:
                im = plot_fsaverage(slopes[t_id, :],
                                    lhannot=data['lhannot'],
                                    rhannot=data['rhannot'],
                                    noplot=data['noplot'],
                                    order=data['order'],
                                    views=['lateral', 'm'],
                                    vmin=np.amin(slopes[t_id, :]),
                                    vmax=np.amax(slopes[t_id, :]),
                                    colormap=RdBu_11_r.mpl_colormap)

                if save:
                    figure_name = ('slopes_brain_surface_' +
                                   str(int(round(data['t_points'][t_id]))) +
                                   '.png')
                    im.save_image(os.path.join(save_path, figure_name),
                                  mode='rgba')

    if 'c' in panels:

        required_entries = ['sc', 't_points', 'ci']
        requirements = check_requirements(data, required_entries)
        if requirements:

            measures = []
            labels = []

            measures.append(np.sum(data['sc'], axis=0))
            labels.append("strength")

            measures.append(-bct.clustering_coef_wu(data['sc']))
            labels.append("clustering(-)")

            for ci in data['ci']:
                measures.append(bct.participation_coef(data['sc'], ci))
                labels.append(("participation (" +
                               str(int(round(ci.max()))) +
                               ")"))

            k = len(data['t_points'])
            m = len(measures)

            corrs = np.zeros((m, k))
            for i in range(m):
                for j in range(k):
                    corrs[i, j] = pearsonr(slopes[j, :], measures[i])[0]

            corr_min = np.amin(corrs)
            corr_max = np.amax(corrs)

            for i in range(m):
                plt.figure()
                plt.imshow(corrs[i, :][np.newaxis, :],
                           cmap=Spectral_4_r.mpl_colormap,
                           vmin=corr_min,
                           vmax=corr_max,
                           aspect=0.1 * k)
                plt.axis('off')
                plt.title(labels[i])

                if save is True:
                    figure_name = "correlations_" + labels[i] + ".png"
                    plt.savefig(os.path.join(save_path, figure_name))

    if show is False:
        plt.close('all')
        plt.ion()
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
def clusteringCoefficient(network):
    return bct.clustering_coef_wu(network)
Exemple #20
0
def test_cluscoef_wu():
	x = load_sample(thres=.23)
	cc = bct.clustering_coef_wu(x)
	assert np.allclose(np.sum(cc), 187.95878414)
                    ge_s = []
                    ge_c = []

                    md_s = []
                    md_c = []
                    for p in np.arange(kappa_upper, kappa_lower, 0.01):
                        thresh = bct.threshold_proportional(corrmat, p, copy=True)

                        # network measures of interest here
                        # global efficiency
                        ge = bct.efficiency_wei(thresh, local=True)
                        ge_s.append(ge)

                        # modularity
                        md = bct.clustering_coef_wu(thresh)
                        md_s.append(md)

                    ge_s = np.asarray(ge_s)
                    md_s = np.asarray(md_s)
                    leff = np.trapz(ge_s, dx=0.01, axis=0)
                    print("local efficiency:", leff[0])
                    ccoef = np.trapz(md_s, dx=0.01, axis=0)
                    for j in np.arange(1, 270):
                        df.at[
                            (subject, session, task, mask), "lEff{0}".format(j)
                        ] = leff[j - 1]
                        df.at[
                            (subject, session, task, mask), "clustCoeff{0}".format(j)
                        ] = ccoef[j - 1]
                    # df.to_csv(join(sink_dir, 'resting-state_graphtheory_shen+craddock.csv'), sep=',')
Exemple #22
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.in_length_matrix, args.in_conn_matrix])

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)

    if not args.append_json:
        assert_outputs_exist(parser, args, args.out_json)
    else:
        logging.debug('Using --append_json, make sure to delete {} '
                      'before re-launching a group analysis.'.format(
                          args.out_json))

    if args.append_json and args.overwrite:
        parser.error('Cannot use the append option at the same time as '
                     'overwrite.\nAmbiguous behavior, consider deleting the '
                     'output json file first instead.')

    conn_matrix = load_matrix_in_any_format(args.in_conn_matrix)
    len_matrix = load_matrix_in_any_format(args.in_length_matrix)

    if args.filtering_mask:
        mask_matrix = load_matrix_in_any_format(args.filtering_mask)
        conn_matrix *= mask_matrix
        len_matrix *= mask_matrix
    N = len_matrix.shape[0]

    if args.avg_node_wise:
        func_cast = avg_cast
    else:
        func_cast = list_cast

    gtm_dict = {}
    betweenness_centrality = bct.betweenness_wei(len_matrix) / ((N - 1) *
                                                                (N - 2))
    gtm_dict['betweenness_centrality'] = func_cast(betweenness_centrality)
    ci, gtm_dict['modularity'] = bct.modularity_louvain_und(conn_matrix,
                                                            seed=0)

    gtm_dict['assortativity'] = bct.assortativity_wei(conn_matrix, flag=0)
    gtm_dict['participation'] = func_cast(
        bct.participation_coef_sign(conn_matrix, ci)[0])
    gtm_dict['clustering'] = func_cast(bct.clustering_coef_wu(conn_matrix))

    gtm_dict['nodal_strength'] = func_cast(bct.strengths_und(conn_matrix))
    gtm_dict['local_efficiency'] = func_cast(
        bct.efficiency_wei(len_matrix, local=True))
    gtm_dict['global_efficiency'] = func_cast(bct.efficiency_wei(len_matrix))
    gtm_dict['density'] = func_cast(bct.density_und(conn_matrix)[0])

    # Rich club always gives an error for the matrix rank and gives NaN
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        tmp_rich_club = bct.rich_club_wu(conn_matrix)
    gtm_dict['rich_club'] = func_cast(tmp_rich_club[~np.isnan(tmp_rich_club)])

    # Path length gives an infinite distance for unconnected nodes
    # All of this is simply to fix that
    empty_connections = np.where(np.sum(len_matrix, axis=1) < 0.001)[0]
    if len(empty_connections):
        len_matrix = np.delete(len_matrix, empty_connections, axis=0)
        len_matrix = np.delete(len_matrix, empty_connections, axis=1)

    path_length_tuple = bct.distance_wei(len_matrix)
    gtm_dict['path_length'] = func_cast(path_length_tuple[0])
    gtm_dict['edge_count'] = func_cast(path_length_tuple[1])

    if not args.avg_node_wise:
        for i in empty_connections:
            gtm_dict['path_length'].insert(i, -1)
            gtm_dict['edge_count'].insert(i, -1)

    if args.small_world:
        gtm_dict['omega'], gtm_dict['sigma'] = omega_sigma(len_matrix)

    if os.path.isfile(args.out_json) and args.append_json:
        with open(args.out_json) as json_data:
            out_dict = json.load(json_data)
        for key in gtm_dict.keys():
            if isinstance(out_dict[key], list):
                out_dict[key].append(gtm_dict[key])
            else:
                out_dict[key] = [out_dict[key], gtm_dict[key]]
    else:
        out_dict = {}
        for key in gtm_dict.keys():
            out_dict[key] = [gtm_dict[key]]

    with open(args.out_json, 'w') as outfile:
        json.dump(out_dict,
                  outfile,
                  indent=args.indent,
                  sort_keys=args.sort_keys)
Exemple #23
0
 def cc(self):
     """Clustering coefficient"""
     return bct.clustering_coef_wu(self.adj)
Exemple #24
0
def process(data):
    return bct.clustering_coef_wu(data)
Exemple #25
0
def get_networks(subject_list, variable, isDynamic, isEffective):
    """
        subject_list : list of subject IDs
        kind         : the kind of connectivity to be used, e.g. lasso, partial correlation, correlation
        atlas_name   : name of the parcellation atlas used
        variable     : variable name in the .mat file that has been used to save the precomputed networks


    return:
        matrix      : feature matrix of connectivity networks (num_subjects x network_size)
    """

    with open('name.data', 'rb') as f:
        name = pickle.load(f)

    with open('alff.data', 'rb') as f:
        alff = pickle.load(f)

    with open('reho.data', 'rb') as f:
        reho = pickle.load(f)

        dynamicset = []
        all_networks = []
        timeseries_set = []

    if isEffective == True:
        fc = sio.loadmat(
            os.path.join('./EffectiveFC/Before_Dropout/0.1.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.01.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.001.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.2.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.5.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.05.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/Before_Dropout/0.15.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.001.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.01.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.05.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.1.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.15.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.2.mat'))['sparse_f']
        # fc = sio.loadmat(os.path.join('./EffectiveFC/After_Dropout/0.5.mat'))['sparse_f']
        for i in range(len(subject_list)):
            all_networks.append(fc[:, :, i])

        # all_networks_set = np.dstack(all_networks)
        # matrix = np.transpose(all_networks_set, (2,0,1))

        vec_networks = [np.reshape(mat, [1, -1]) for mat in all_networks]
        matrix = np.vstack(vec_networks)

    else:
        for subject in subject_list:
            flname = [
                i for i in os.listdir(data_folder) if
                os.path.isfile(os.path.join(data_folder, i)) and subject in i
            ]
            fl = os.path.join(data_folder, flname[0])

            # Estimate connectivity matrix
            timeseries = sio.loadmat(fl)['ROI']

            if variable == 'correlation':
                conn_measure = connectome.ConnectivityMeasure(kind=variable)
                # conn_measure = connectome.ConnectivityMeasure(kind=variable).fit_transform([timeseries])[0]
                # conn_measure_2nd = np.matmul(conn_measure, conn_measure)
                # conn_measure_3rd = np.matmul(conn_measure, conn_measure_2nd)
                # connectivity = conn_measure + conn_measure_2nd + conn_measure_3rd
                ft = conn_measure.fit_transform([timeseries])[0]
            elif variable == 'graph_measure':
                conn_measure = connectome.ConnectivityMeasure(
                    kind='correlation')
                connectivity = conn_measure.fit_transform([timeseries])[0]
                ft = bct.clustering_coef_wu(connectivity)

            timeseries_set.append(timeseries)
            all_networks.append(ft)
            dynamicset.append(
                np.concatenate(
                    (alff[name.index(subject)], reho[name.index(subject)])))

            # all_networks=np.array(all_networks)
            if variable == 'correlation':
                idx = np.triu_indices_from(all_networks[0], 1)
                norm_networks = [np.arctanh(mat) for mat in all_networks]
                vec_networks = [mat[idx] for mat in norm_networks]
                # vec_networks = [mat[idx] for mat in all_networks]
                matrix = np.vstack(vec_networks)
            elif variable == 'graph_measure':
                matrix = np.vstack(all_networks)

            # all_networks_set = np.dstack(all_networks)
            # matrix = np.transpose(all_networks_set, (2,0,1))

    # if isDynamic == True:
    #     dynamicset = np.vstack(dynamicset)
    #     matrix = np.concatenate((matrix, dynamicset), axis=1)
    #
    #
    # with open('./train_data.pkl', 'wb') as filehandle:
    #     pickle.dump(timeseries_set, filehandle)

    return matrix
Exemple #26
0
def test_cluscoef_signed():
	x = load_signed_sample(thres=.85)
	cc = bct.clustering_coef_wu(x)
	assert np.imag(np.sum(cc))==0	
def create_feature_matrix(structure_matrix_file):
    # Feature matrix with each element containing an NxN array
    feature_matrix = []

    # EDGE WEIGHT (Depth 0)
    # weighted & undirected network
    structural_connectivity_array = np.array(
        pd.DataFrame(loadmat(structure_matrix_file)['connectivity']))
    feature_matrix.append(structural_connectivity_array)

    # DEGREE (Depth 1 & 2)
    # Node degree is the number of links connected to the node.
    deg = bct.degrees_und(structural_connectivity_array)
    fill_array_2D(feature_matrix, deg)

    # *** Conversion of connection weights to connection lengths ***
    connection_length_matrix = bct.weight_conversion(
        structural_connectivity_array, 'lengths')
    # print(connection_length_matrix)

    # SHORTEST PATH LENGTH (Depth 3 & 4)
    '''
    The distance matrix contains lengths of shortest paths between all pairs of nodes.
    An entry (u,v) represents the length of shortest path from node u to node v.
    The average shortest path length is the characteristic path length of the network.
    '''
    shortest_path = bct.distance_wei(connection_length_matrix)
    feature_matrix.append(
        shortest_path[0])  # distance (shortest weighted path) matrix
    feature_matrix.append(
        shortest_path[1]
    )  # matrix of number of edges in shortest weighted path

    # BETWEENNESS CENTRALITY (Depth 5 & 6)
    '''
    Node betweenness centrality is the fraction of all shortest paths in
    the network that contain a given node. Nodes with high values of
    betweenness centrality participate in a large number of shortest paths.
    '''
    bc = bct.betweenness_wei(connection_length_matrix)
    fill_array_2D(feature_matrix, bc)

    # CLUSTERING COEFFICIENTS (Depth 7 & 8)
    '''
    The weighted clustering coefficient is the average "intensity" of
    triangles around a node.
    '''
    cl = bct.clustering_coef_wu(connection_length_matrix)
    fill_array_2D(feature_matrix, cl)

    # Find disconnected nodes - component size set to 1
    new_array = structural_connectivity_array
    W_bin = bct.weight_conversion(structural_connectivity_array, 'binarize')
    [comps, comp_sizes] = bct.get_components(W_bin)
    print('comp: ', comps)
    print('sizes: ', comp_sizes)
    for i in range(len(comps)):
        if (comps[i] != statistics.mode(comps)):
            new_array = np.delete(new_array, new_array[i])

    return feature_matrix
def get_true_network_metrics(A):

    #control centrality
    c_c = control_centrality(A)

    cc_fake = np.zeros((100, 1))
    for i in range(0, 100):
        cc_fake[i] = np.mean(control_centrality(generate_fake_graph(A)))

    m_cc_fake = np.mean(cc_fake)
    cc_norm = c_c / m_cc_fake

    # Get identity of node with lowest control centrality
    min_cc_true = np.where(c_c == np.amin(c_c))[0]

    # get synchronizability
    sync = synchronizability(A)

    # normalized sync
    sync_fake = np.zeros((100, 1))
    for i in range(0, 100):
        sync_fake[i] = synchronizability(generate_fake_graph(A))

    m_sync_fake = np.mean(sync_fake)
    sync_norm = sync / m_sync_fake

    # get betweeness centrality
    bc = betweenness_centrality(A)
    bc_fake = np.zeros((100, 1))
    for i in range(0, 100):
        bc_fake[i] = np.mean(betweenness_centrality(generate_fake_graph(A)))

    m_bc_fake = np.mean(bc_fake)
    bc_norm = bc / m_bc_fake

    # Get identity of node with max bc
    max_bc_true = np.where(bc == np.amax(bc))[0]

    # get eigenvector centrality
    ec = bct.eigenvector_centrality_und(A)
    ec_fake = np.zeros((100, 1))
    for i in range(0, 100):
        ec_fake[i] = np.mean(
            bct.eigenvector_centrality_und(generate_fake_graph(A)))

    m_ec_fake = np.mean(ec_fake)
    ec_norm = ec / m_ec_fake

    # Get identity of node with max ec
    max_ec_true = np.where(ec == np.amax(ec))[0]

    # get edge betweeness centrality
    edge_bc, ignore = bct.edge_betweenness_wei(A)
    edge_bc_fake = np.zeros((100, 1))
    for i in range(0, 100):
        edge_bc_fake[i] = np.mean(
            bct.edge_betweenness_wei(generate_fake_graph(A))[0])
    m_edge_bc_fake = np.mean(edge_bc_fake)
    edge_bc_norm = edge_bc / m_edge_bc_fake

    # get clustering coeff
    clust = bct.clustering_coef_wu(A)
    clust_fake = np.zeros((100, 1))
    for i in range(0, 100):
        clust_fake[i] = np.mean(bct.clustering_coef_wu(generate_fake_graph(A)))

    m_clust_fake = np.mean(clust_fake)
    clust_norm = clust / m_clust_fake

    # Get identity of node with max clust
    max_clust_true = np.where(clust == np.amax(clust))[0]

    # get node strength
    ns = node_strength(A)
    ns_fake = np.zeros((100, 1))
    for i in range(0, 100):
        ns_fake[i] = np.mean(node_strength(generate_fake_graph(A)))

    m_ns_fake = np.mean(ns_fake)
    ns_norm = ns / m_ns_fake

    # Get identity of node with max clust
    max_ns_true = np.where(ns == np.amax(ns))[0]

    #Get true efficiency
    Ci, ignore = bct.modularity_und(A)
    par = bct.participation_coef(A, Ci)

    eff = bct.efficiency_wei(A, 0)
    eff_fake = np.zeros((100, 1))
    for i in range(0, 100):
        eff_fake[i] = (bct.efficiency_wei(generate_fake_graph(A)))

    m_eff_fake = np.mean(eff_fake)
    eff_norm = eff / m_eff_fake

    # Get true transistivity
    trans = bct.transitivity_wu(A)
    trans_fake = np.zeros((100, 1))
    for i in range(0, 100):
        trans_fake[i] = (bct.transitivity_wu(generate_fake_graph(A)))

    m_trans_fake = np.mean(trans_fake)
    trans_norm = trans / m_trans_fake

    # store output results in a dictionary
    #nodal
    results = {}
    results['control_centrality'] = c_c
    results['control_centrality_norm'] = cc_norm
    results['min_cc_node'] = min_cc_true

    # global measure
    results['sync'] = sync
    results['sync_norm'] = sync_norm

    # nodal
    results['bc'] = bc
    results['bc_norm'] = bc_norm
    results['max_bc_node'] = max_bc_true

    # nodal
    results['ec'] = ec
    results['ec_norm'] = ec_norm
    results['max_ec_node'] = max_ec_true

    # nodal
    results['clust'] = clust
    results['clust_norm'] = clust_norm
    results['max_clust_node'] = max_clust_true

    # nodal
    results['ns'] = ns
    results['ns_norm'] = ns_norm
    results['max_ns_node'] = max_ns_true

    # global
    results['eff'] = eff
    results['eff_norm'] = eff_norm

    # global
    results['trans'] = trans
    results['trans_norm'] = trans_norm

    # nodal
    results['par'] = par

    # edge
    results['edge_bc'] = edge_bc
    results['edge_bc_norm'] = edge_bc_norm

    return (results)
Exemple #29
0
def calc_graph_vector(filename, thresholds) :
    '''
    This function calculates graph measures for connectivity matrix loaded from textfile
    and save results under the same name with additional superscript +'_GV' (in same dir
    filename is located)
    
    Input arguments:                                               
        filename(str):     name of file containing connectivity matrix (txt extension)
        thresholds(list):  list containing thresholds of interest        #
    
    Kamil Bonna, 14.08.2018 
    '''
    #--- check inputs
    import os
    if not os.path.exists(filename):
        raise Exception('{} does not exist'.format(filename))
    if type(thresholds) != list: 
        raise Exception('thresholds should be a list!')
        
    import numpy as np
    import bct

    #=== inner variables
    N_rep_louvain = 10   # number of Louvain algorithm repetitions
    N_measures = 10      # number of graph measures
    gamma = 1            # Louvain resolution parameter
    
    #--- load matrix 
    A_raw = np.loadtxt(filename)
    N = A_raw.shape[0]   # number of nodes
    M_sat = N*(N-1)/2    # max number of connections 

    #=== calculate output
    graph_measures = np.zeros([ len(thresholds), N_measures ])  # create empty output matrix
    for thr in range(len(thresholds)) : 
        #--- thresholding 
        A = bct.threshold_proportional( A_raw, p=thresholds[thr], copy=True );
        A[np.nonzero(A<0)] = 0                                  # ensure only positive weights
        M_act = A[np.nonzero(A>0)].shape[0] / 2                 # actual number of nonzero connections
        #--- calculate measures
        #-- mean connection strenght 
        S = np.sum(A)/M_act
        #-- connection strenght std
        Svar = np.std(A[np.nonzero(A)])
        #-- modularity
        [M,Q] = bct.modularity_louvain_und(A, gamma)
        for i in range(N_rep_louvain) :
            [Mt,Qt] = bct.modularity_louvain_und(A, gamma)
            if Qt > Q :
                Q = Qt
                M = Mt
        #-- participation coefficient
        P = np.mean(bct.participation_coef_sign(A, M))
        #-- clustering 
        C = np.mean(bct.clustering_coef_wu(A))
        #-- transitivity 
        T = bct.transitivity_wu(A)
        #-- assortativity
        Asso = bct.assortativity_wei(A)
        #-- global & local efficiency 
        Eglo = bct.efficiency_wei(A)
        Eloc = np.mean(bct.efficiency_wei(A, local=True))
        #-- mean eigenvector centralit
        Eig = np.mean(bct.eigenvector_centrality_und(A))
        #--- write vector to matrix
        graph_measures[thr] = [ S, Svar, Q, P, C, T, Asso, Eglo, Eloc, Eig ]

    #=== save results to file
    np.savetxt( filename[:-4]+'_GV.txt', graph_measures )