Esempio n. 1
0
def individual_group_clustered_maps(indiv_stability_list, clusters_G, roi_mask_file):
    """
    Calculate the individual stability maps of each subject based on the group stability clustering solution.
    
    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
    clusters_G : array_like
        Length `V` array of cluster assignments for each voxel
        
    Returns
    -------
    individual_cluster_voxel_scores : list of strings
        A length `N` list of nifti files of the individual group clustered stability maps for each cluster.  Temporal
        dimension of each file corresponds to each subject.
    
    """
    import os
    import numpy as np
    from CPAC.basc import cluster_matrix_average, ndarray_to_vol
    
    indiv_stability_set = np.asarray([np.load(ism_file) for ism_file in indiv_stability_list])

    nSubjects = indiv_stability_set.shape[0]
    nVoxels = indiv_stability_set.shape[1]

    cluster_ids = np.unique(clusters_G)
    nClusters = cluster_ids.shape[0]
    
    cluster_voxel_scores = np.zeros((nClusters, nSubjects, nVoxels))
    for i in range(nSubjects):
        cluster_voxel_scores[:,i] = cluster_matrix_average(indiv_stability_set[i], clusters_G)
    
    icvs = []
    icvs_idx = 0
    for k in cluster_ids:
        icvs.append(ndarray_to_vol(cluster_voxel_scores[icvs_idx], roi_mask_file, roi_mask_file, 'individual_group_cluster%i_stability.nii.gz' % k))
        icvs_idx += 1
 
    return icvs
Esempio n. 2
0
def individual_group_clustered_maps(indiv_stability_list, clusters_G, roi_mask_file):
    """
    Calculate the individual stability maps of each subject based on the group stability clustering solution.
    
    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
    clusters_G : array_like
        Length `V` array of cluster assignments for each voxel
        
    Returns
    -------
    individual_cluster_voxel_scores : list of strings
        A length `N` list of nifti files of the individual group clustered stability maps for each cluster.  Temporal
        dimension of each file corresponds to each subject.
    
    """
    import os
    import numpy as np
    from CPAC.basc import cluster_matrix_average, ndarray_to_vol
    
    indiv_stability_set = np.asarray([np.load(ism_file) for ism_file in indiv_stability_list])

    nSubjects = indiv_stability_set.shape[0]
    nVoxels = indiv_stability_set.shape[1]

    cluster_ids = np.unique(clusters_G)
    nClusters = cluster_ids.shape[0]
    
    cluster_voxel_scores = np.zeros((nClusters, nSubjects, nVoxels))
    for i in range(nSubjects):
        cluster_voxel_scores[:,i] = cluster_matrix_average(indiv_stability_set[i], clusters_G)
    
    icvs = []
    icvs_idx = 0
    for k in cluster_ids:
        icvs.append(ndarray_to_vol(cluster_voxel_scores[icvs_idx], roi_mask_file, roi_mask_file, 'individual_group_cluster%i_stability.nii.gz' % k))
        icvs_idx += 1
 
    return icvs
Esempio n. 3
0
def group_stability_matrix(indiv_stability_list, n_bootstraps, k_clusters, stratification=None):
    """
    Calculate the group stability matrix of the entire dataset by bootstrapping the dataset
    
    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_bootstraps : integer
        Number of bootstrap datasets
    k_clusters : integer
        Number of clusters
    stratification : array_like, optional
        List of integer entries denoting stratums for indiv_stability_list
    
    
    Returns
    -------
    G : array_like
        Group stability matrix of shape (`V`, `V`), `V` voxels
    clusters_G : array_like
        Length `V` array of cluster assignments for each voxel
    cluster_voxel_scores : array_like
        `K` by `V` matrix of within-cluster average values for each cluster of each voxel
    """
    print(('Calculating group stability matrix for', len(indiv_stability_list), 'subjects.'))

    if stratification is not None:
        print('Applying stratification to group dataset')
                
    from CPAC.basc import standard_bootstrap, adjacency_matrix, cluster_timeseries, cluster_matrix_average
    import numpy as np

    indiv_stability_set = np.asarray([np.load(ism_file) for ism_file in indiv_stability_list])
#    indiv_stability_set = indiv_stability_list
    print(('Individual stability list dimensions:', indiv_stability_set.shape))
    
    V = indiv_stability_set.shape[2]
    
    G = np.zeros((V,V))
    for bootstrap_i in range(n_bootstraps):
        if stratification is not None:
            strata = np.unique(stratification)
            J = np.zeros((V,V))
            for stratum in strata:
                J += standard_bootstrap(indiv_stability_set[np.where(stratification == stratum)]).sum(0)
            J /= indiv_stability_set.shape[0]
        else:
            J = standard_bootstrap(indiv_stability_set).mean(0)
        G += adjacency_matrix(cluster_timeseries(J, k_clusters, similarity_metric = 'data')[:,np.newaxis])
    G /= n_bootstraps


    clusters_G = cluster_timeseries(G, k_clusters, similarity_metric = 'data')
    
    cluster_voxel_scores = cluster_matrix_average(G, clusters_G)
    
    # Cluster labels normally start from 0, start from 1 to provide contrast when viewing between 0 voxels
    clusters_G += 1

    return G, clusters_G, cluster_voxel_scores
Esempio n. 4
0
def group_stability_matrix(indiv_stability_list, n_bootstraps, k_clusters, stratification=None):
    """
    Calculate the group stability matrix of the entire dataset by bootstrapping the dataset
    
    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_bootstraps : integer
        Number of bootstrap datasets
    k_clusters : integer
        Number of clusters
    stratification : array_like, optional
        List of integer entries denoting stratums for indiv_stability_list
    
    
    Returns
    -------
    G : array_like
        Group stability matrix of shape (`V`, `V`), `V` voxels
    clusters_G : array_like
        Length `V` array of cluster assignments for each voxel
    cluster_voxel_scores : array_like
        `K` by `V` matrix of within-cluster average values for each cluster of each voxel
    """
    print 'Calculating group stability matrix for', len(indiv_stability_list), 'subjects.'

    if stratification is not None:
        print 'Applying stratification to group dataset'
                
    from CPAC.basc import standard_bootstrap, adjacency_matrix, cluster_timeseries, cluster_matrix_average
    import numpy as np

    indiv_stability_set = np.asarray([np.load(ism_file) for ism_file in indiv_stability_list])
#    indiv_stability_set = indiv_stability_list
    print 'Individual stability list dimensions:', indiv_stability_set.shape
    
    V = indiv_stability_set.shape[2]
    
    G = np.zeros((V,V))
    for bootstrap_i in range(n_bootstraps):
        if stratification is not None:
            strata = np.unique(stratification)
            J = np.zeros((V,V))
            for stratum in strata:
                J += standard_bootstrap(indiv_stability_set[np.where(stratification == stratum)]).sum(0)
            J /= indiv_stability_set.shape[0]
        else:
            J = standard_bootstrap(indiv_stability_set).mean(0)
        G += adjacency_matrix(cluster_timeseries(J, k_clusters, similarity_metric = 'data')[:,np.newaxis])
    G /= n_bootstraps


    clusters_G = cluster_timeseries(G, k_clusters, similarity_metric = 'data')
    
    cluster_voxel_scores = cluster_matrix_average(G, clusters_G)
    
    # Cluster labels normally start from 0, start from 1 to provide contrast when viewing between 0 voxels
    clusters_G += 1

    return G, clusters_G, cluster_voxel_scores