Exemplo n.º 1
0
                                        #                                    spectral1.fit(Sim_mat_of_ABC_One)
                                        #                                    y_pred1 = spectral1.labels_.astype(np.int)

                                        print(
                                            'Clustering ABC according to connectivity to region 2'
                                        )
                                        ward2 = FeatureAgglomeration(
                                            n_clusters=n_clusters,
                                            affinity='euclidean',
                                            linkage='ward')
                                        ward2.fit(Sim_mat_of_ABC_Two)
                                        y_pred2 = ward2.labels_.astype(np.int)
                                        #                                    spectral2.fit(Sim_mat_of_ABC_Two)
                                        #                                    y_pred2 = spectral2.labels_.astype(np.int)

                                        y1_adj = adjacency_matrix(y_pred1)
                                        y2_adj = adjacency_matrix(y_pred2)

                                        reg1_ism += y1_adj
                                        reg2_ism += y2_adj
                                    #reg1_ism=np.asarray(reg1_ism)
                                    #reg2_ism=np.asarray(reg2_ism)

                                    #import pdb; pdb.set_trace()

                                    if bootstraps == 0:
                                        #print('No Bootstraps!')
                                        reg1_final = reg1_ism
                                        reg2_final = reg2_ism
                                        reg1_pred = y_pred1
                                        reg2_pred = y_pred2
Exemplo n.º 2
0
def individual_stability_matrix(Y1, roi_mask_nparray, n_bootstraps, n_clusters, similarity_metric, Y2=None, cross_cluster=False, cbb_block_size = None, blocklength=1, affinity_threshold = 0.5):
    """
    Calculate the individual stability matrix of a single subject by bootstrapping their time-series

    Parameters
    ----------
    Y1 : array_like
        A matrix of shape (`V`, `N`) with `V` voxels `N` timepoints
    Y2 : array_like
        A matrix of shape (`V`, `N`) with `V` voxels `N` timepoints
        For Cross-cluster solutions- this will be the matrix by which Y1 is clustered
    n_bootstraps : integer
        Number of bootstrap samples
    k_clusters : integer
        Number of clusters
    cbb_block_size : integer, optional
        Block size to use for the Circular Block Bootstrap algorithm
    affinity_threshold : float, optional
        Minimum threshold for similarity matrix based on correlation to create an edge

    Returns
    -------
    S : array_like
        A matrix of shape (`V1`, `V1`), each element v1_{ij} representing the stability of the adjacency of voxel i with voxel j
    """
    
    import utils 
    import time
    import numpy as np
    #print("Calculating Individual Stability Matrix")
    ismtime=time.time()
   
    
    if affinity_threshold < 0.0:
        raise ValueError('affinity_threshold %d must be non-negative value' % affinity_threshold)

    
    N1 = Y1.shape[0]
    V1 = Y1.shape[1]
    #import pdb; pdb.set_trace()
    print('N1',N1)
    print('V1',V1)
    print(int(np.sqrt(N1)))
    print('block size is- ', cbb_block_size)
    temp_block_size = int(np.sqrt(N1))
    cbb_block_size = int(temp_block_size * blocklength)
#    if(cbb_block_size is None):
#        cbb_block_size = int(np.sqrt(N1))
    print('block size now is- ', cbb_block_size)
    S = np.zeros((V1, V1))
    #import pdb;pdb.set_trace()
    if (cross_cluster is True):
        for bootstrap_i in range(n_bootstraps):
        
            N2 = Y2.shape[1]
            temp_block_size2 = int(np.sqrt(N2))
            cbb_block_size2 = int(temp_block_size2 * blocklength)
            
            if (bootstrap_i==1):
                Y_b1=Y1
                Y_b2=Y2
            else:
                Y_b1, block_mask = utils.timeseries_bootstrap(Y1, cbb_block_size)
                Y_b2 = Y2[block_mask.astype('int'), :]
            #import pdb;pdb.set_trace()
            #tseries[block_mask.astype('int'), :]
            #import pdb; pdb.set_trace()
            
            #SPATIAL CONSTRAINT EXPERIMENT#
            roi_mask_nparray='empty'
            #SPATIAL CONSTRAINT EXPERIMENT#
            
#            if spatial_constraint==true:
#                roi_mask_nparray='empty'
#            else:
#                roi_mask_nparray=roi_mask_nparray
            #import pdb; pdb.set_trace()
            S += utils.adjacency_matrix(utils.cross_cluster_timeseries(Y_b1, Y_b2, roi_mask_nparray, n_clusters, similarity_metric = similarity_metric, affinity_threshold= affinity_threshold, cluster_method='ward'))

            
        S /= n_bootstraps
        
        S=S*100
        S=S.astype("uint8")
        #print('ISM calculation took', (time.time() - ismtime), ' seconds')
    else:
        for bootstrap_i in range(n_bootstraps):
            print('ismcalc1')
            print('block size', cbb_block_size)
            #import pdb; pdb.set_trace()
            
            if (bootstrap_i==1):
                Y_b1=Y1
                Y_b2=Y2
            else:
                Y_b1, block_mask = utils.timeseries_bootstrap(Y1, cbb_block_size)
            
            print('ismcalc2')
            #import pdb;pdb.set_trace()
            
            #SPATIAL CONSTRAINT EXPERIMENT#
            roi_mask_nparray='empty'
            #SPATIAL CONSTRAINT EXPERIMENT#
            
            S += utils.adjacency_matrix(utils.cluster_timeseries(Y_b1, roi_mask_nparray, n_clusters, similarity_metric = similarity_metric, affinity_threshold = affinity_threshold, cluster_method='ward')[:,np.newaxis])
            
            print('S shape0', S.shape[0])
            print('S shape1', S.shape[1])
            print('ismcalc3')

        S /= n_bootstraps
        print('ismcalc4')

        S=S*100
        S=S.astype("uint8")
            #print('ISM calculation took', (time.time() - ismtime), ' seconds')
    return S
Exemplo n.º 3
0
######## 1.1

fs = 160 # Frequency of sampling, given by data
resolution = 100 # Resolution of model (s.t. each bin has 1Hz of width)
freq = 10 # Frequency of interest
density = 0.2 # Density of the graph desired


###PDC

# Fitting PDC models
eo_pdc = ut.fit_model(eo, fs, resolution, "pdc", freq)
ec_pdc = ut.fit_model(ec, fs, resolution, "pdc", freq)

# Adjacency Matrices for 20% density networks
ut.adjacency_matrix(eo_pdc, ut.find_threshold(eo_pdc,density), "eo_pdc_20")
ut.adjacency_matrix(ec_pdc, ut.find_threshold(ec_pdc,density), "ec_pdc_20")



######## 1.2


# Fitting DTF models
eo_dtf = ut.fit_model(eo, fs, resolution, "dtf", freq)
ec_dtf = ut.fit_model(ec, fs, resolution, "dtf", freq)

# Adjacency Matrices for 20% density networks
ut.adjacency_matrix(eo_dtf, ut.find_threshold(eo_dtf,density), "eo_dtf_20")
ut.adjacency_matrix(ec_dtf, ut.find_threshold(ec_dtf,density), "ec_dtf_20")
Exemplo n.º 4
0
def map_group_stability(indiv_stability_list, n_clusters, bootstrap_list,
                        roi_mask_file):
    """
    Calculate the group stability maps for each group-level bootstrap

    Parameters
    ----------
    indiv_stability_list : list of strings
        A length `N` list of file paths to numpy matrices of shape (`V`, `V`), `N` subjects, `V` voxels
    n_clusters : array_like
        number of clusters extrated from adjacency matrx

    Returns
    -------
    G_file : numpy array
        The group stability matrix for a single bootstrap repitition

    """

    import os
    import numpy as np
    import nibabel as nb
    import utils

    print('Calculating group stability matrix for', len(indiv_stability_list),
          'subjects.')

    #import pdb; pdb.set_trace()
    indiv_stability_set = np.asarray(
        [np.load(ism_file) for ism_file in indiv_stability_list])
    #print( 'Group stability list dimensions:', indiv_stability_set.shape )

    V = indiv_stability_set.shape[2]

    G = np.zeros((V, V))
    if (bootstrap_list == 1):
        J = indiv_stability_set.mean(0)
    else:
        J = utils.standard_bootstrap(indiv_stability_set).mean(0)

    roi_mask_nparray = nb.load(roi_mask_file).get_data().astype(
        'float32').astype('bool')
    J = J.astype("uint8")

    #SPATIAL CONSTRAINT EXPERIMENT#
    #roi_mask_nparray='empty'
    #SPATIAL CONSTRAINT EXPERIMENT#

    #print( 'calculating adjacency matrix')
    G = utils.adjacency_matrix(
        utils.cluster_timeseries(J,
                                 roi_mask_nparray,
                                 n_clusters,
                                 similarity_metric='correlation',
                                 affinity_threshold=0.0,
                                 cluster_method='ward')[:, np.newaxis])
    #print("finished calculating group stability matrix")

    G = G.astype("uint8")

    G_file = os.path.join(os.getcwd(), 'group_stability_matrix.npy')
    np.save(G_file, G)
    print('Saving group stability matrix %s' % (G_file))

    return G_file