Esempio n. 1
0
def gsm_nifti(roi_mask_file, n_clusters, out_dir):
    import CPAC.basc.utils as utils
    import basc
    import numpy as np
    import os
#Individual subject ISM to NIFTI and individual

#Inputs Subject ISM, ROIFile, 


    
    
    #for i in range(nSubjects):
    gsmdir=out_dir + '/workflow_output/basc_workflow_runner/basc/join_group_stability/'
    os.chdir(gsmdir)

    gsm=np.load(gsmdir + '/group_stability_matrix.npy')
    clusters_gsm = utils.cluster_timeseries(gsm, n_clusters, similarity_metric = 'correlation', affinity_threshold=0.0)
    clusters_gsm = clusters_gsm+1
    #niftifilename = gsmdir  +'/gsm_clust.nii.gz'
    #clusters_gsm_file = gsmdir +'/clusters_gsm.npy'
    #Saving Individual Level Cluster Solution
#    ndarray_to_vol(clusters_gsm, roi_mask_file, roi_mask_file, niftifilename)
#    np.save(clusters_gsm_file, clusters_gsm)
    
    
    cluster_ids = np.unique(clusters_gsm)
    nClusters = cluster_ids.shape[0]
    nVoxels = clusters_gsm.shape[0]
    gsm_cluster_voxel_scores = np.zeros((nClusters, nVoxels))
    k_mask=np.zeros((nVoxels, nVoxels))
    gsm_cluster_voxel_scores[:,:], k_mask[:,:] = utils.cluster_matrix_average(gsm, clusters_gsm)
    gsm_cluster_voxel_scores=gsm_cluster_voxel_scores.astype("uint8")
    
    grp_cluster_stability=[]
    grp_cluster_INSTABILITY=[]
    grp_cluster_stability_Diff=[]
    
    grp_cluster_stability_file = os.path.join(os.getcwd(), 'grp_cluster_stability.npy')
    grp_cluster_INSTABILITY_file = os.path.join(os.getcwd(), 'grp_cluster_INSTABILITY.npy')
    grp_cluster_stability_Diff_file = os.path.join(os.getcwd(), 'grp_cluster_stability_Diff.npy')
    gsm_cluster_voxel_scores_file = os.path.join(os.getcwd(), 'gsm_cluster_voxel_scores.npy')
    
    for k in cluster_ids:
        grp_cluster_stability.append(gsm_cluster_voxel_scores[(k-1),clusters_gsm==k].mean())
        grp_cluster_INSTABILITY.append(gsm_cluster_voxel_scores[(k-1),clusters_gsm!=k].mean())
        A, B = basc.ndarray_to_vol(gsm_cluster_voxel_scores[k-1,:], roi_mask_file, roi_mask_file, 'gsm_single_cluster%i_stability.nii.gz' % k)
    grp_cluster_stability=np.asarray(grp_cluster_stability)
    grp_cluster_INSTABILITY=np.asarray(grp_cluster_INSTABILITY)
    grp_cluster_stability_Diff=grp_cluster_stability-grp_cluster_INSTABILITY
    
    np.save(grp_cluster_stability_file, grp_cluster_stability)
    np.save(grp_cluster_INSTABILITY_file, grp_cluster_INSTABILITY)
    np.save(grp_cluster_stability_Diff_file, grp_cluster_stability_Diff)
    np.save(gsm_cluster_voxel_scores_file, gsm_cluster_voxel_scores)


    return
Esempio n. 2
0
def create_group_cluster_maps(gsm_file, clusters_G_file, roi_mask_file):
    """
    Loops through every row of cluster_voxel_scores and creates nifti files
    
    Parameters
    ----------
        gsm_file- a cluster number by voxel measure of the stability
            of group level solutions.
        clusters_G_file- taken from igcm output
        roi_mask_file- file you want to transform the data back into.

    Returns
    -------
    Creates NIFTI files for all the gsm file for the group across all clusters 
    
    """

    import numpy as np
    import basc
    import utils

    group_stability_mat = np.asarray([np.load(gsm_file)])
    group_stability_set = group_stability_mat[0]
    #nSubjects = indiv_stability_set.shape[0]
    nVoxels = group_stability_set.shape[1]
    clusters_G = np.load(clusters_G_file)
    group_cluster_stability = []
    cluster_ids = np.unique(clusters_G)
    nClusters = cluster_ids.shape[0]
    print("igcm debug 2")

    #    cluster_voxel_scores = np.zeros((nSubjects,nClusters, nVoxels))
    #    k_mask=np.zeros((nSubjects,nVoxels, nVoxels))
    group_cluster_voxel_scores = np.zeros((nClusters, nVoxels))
    k_mask = np.zeros((nVoxels, nVoxels))
    #for i in range(nSubjects):

    group_cluster_voxel_scores[:, :], k_mask[:, :] = utils.cluster_matrix_average(
        group_stability_set, clusters_G)

    for i in cluster_ids:
        group_cluster_stability.append(
            group_cluster_voxel_scores[(i - 1), clusters_G == i].mean())

    for k in cluster_ids:
        print(
            'k equals \n\n', k, '\n\n'
        )  #Loops through every row of cluster_voxel_scores and creates nifti files
        print('clustervoxelscores equals \n\n',
              group_cluster_voxel_scores[k - 1, :], '\n\n')
        A, B = basc.ndarray_to_vol(
            group_cluster_voxel_scores[k - 1, :], roi_mask_file, roi_mask_file,
            'group_level_cluster%i_stability.nii.gz' % k)
        print('Output A equals', A, '\n\n')
    return
Esempio n. 3
0
def test_ndarray_to_vol():
    import basc
    import nibabel as nb

    subject_file = home + '/git_repo/PyBASC/sample_data/sub1/Func_Quarter_Res.nii.gz'
    subject_file = home + '/git_repo/PyBASC/sample_data/test.nii.gz'
    data = nb.load(subject_file).get_data().astype('float32')
    roi_mask_file = home + '/git_repo/PyBASC/masks/LC_Quarter_Res.nii.gz'
    print('Data Loaded')

    roi_mask_file_nb = nb.load(roi_mask_file)

    roi_mask_nparray = nb.load(roi_mask_file).get_data().astype(
        'float32').astype('bool')
    roi1data = data[roi_mask_nparray]

    data_array = roi1data
    sample_file = subject_file
    filename = home + '/git_repo/PyBASC/sample_data/ndarray_to_vol_test.nii.gz'

    basc.ndarray_to_vol(data_array, roi_mask_file, roi_mask_file, filename)
Esempio n. 4
0
def save_igcm_nifti(cluster_voxel_scores_file, clusters_G_file, roi_mask_file):
    """
    Loops through every row of cluster_voxel_scores and creates nifti files
    
    Parameters
    ----------
        cluster_voxel_scores_file- a cluster number by voxel measure of the stability
    of group level solutions at the individual level.
        clusters_G_file- taken from igcm output
        roi_mask_file- file you want to transform the data back into.

    Returns
    -------
    Creates NIFTI files for all the igcm files for each participant across all clusters 
    """

    import numpy as np
    import basc
    cluster_voxel_scores = np.load(cluster_voxel_scores_file)
    clusters_G = np.load(clusters_G_file)
    cluster_ids = np.unique(clusters_G)
    icvs_idx = 0
    A = []
    B = []
    #import pdb; pdb.set_trace()

    for k in cluster_ids:
        print(
            'k equals \n\n', k, '\n\n'
        )  #Loops through every row of cluster_voxel_scores and creates nifti files
        print('clustervoxelscores equals \n\n', cluster_voxel_scores[k - 1, :],
              '\n\n')
        A, B = basc.ndarray_to_vol(
            cluster_voxel_scores[k - 1, :], roi_mask_file, roi_mask_file,
            'individual_group_cluster%i_stability.nii.gz' % k)
        print('Output A equals', A, '\n\n')
Esempio n. 5
0
def ism_nifti(roi_mask_file, n_clusters, out_dir):
    import utils
    import basc
    import numpy as np
    import os
    #Individual subject ISM to NIFTI and individual

    #Inputs Subject ISM, ROIFile,

    #for i in range(nSubjects):
    ismdir = out_dir + '/workflow_output/basc_workflow_runner/basc/individual_stability_matrices/mapflow/'
    os.chdir(ismdir)
    os.chdir(
        out_dir +
        '/workflow_output/basc_workflow_runner/basc/individual_stability_matrices/mapflow/'
    )
    subdirs_all = [x[1] for x in os.walk(ismdir)]
    subdirs = subdirs_all[0]

    for subdir in subdirs:
        os.chdir(ismdir + subdir)
        ism = np.load(ismdir + subdir + '/individual_stability_matrix.npy')
        clusters_ism = utils.cluster_timeseries(
            ism,
            n_clusters,
            similarity_metric='correlation',
            affinity_threshold=0.0)
        clusters_ism = clusters_ism + 1
        niftifilename = ismdir + subdir + '/ism_clust.nii.gz'
        clusters_ism_file = ismdir + subdir + '/clusters_ism.npy'
        #Saving Individual Level Cluster Solution
        ndarray_to_vol(clusters_ism, roi_mask_file, roi_mask_file,
                       niftifilename)
        np.save(clusters_ism_file, clusters_ism)

        cluster_ids = np.unique(clusters_ism)
        nClusters = cluster_ids.shape[0]
        nVoxels = clusters_ism.shape[0]
        ism_cluster_voxel_scores = np.zeros((nClusters, nVoxels))
        k_mask = np.zeros((nVoxels, nVoxels))
        ism_cluster_voxel_scores[:, :], k_mask[:, :] = utils.cluster_matrix_average(
            ism, clusters_ism)
        ism_cluster_voxel_scores = ism_cluster_voxel_scores.astype("uint8")

        ind_cluster_stability = []
        ind_cluster_INSTABILITY = []
        ind_cluster_stability_Diff = []

        ind_cluster_stability_file = os.path.join(os.getcwd(),
                                                  'ind_cluster_stability.npy')
        ind_cluster_INSTABILITY_file = os.path.join(
            os.getcwd(), 'ind_cluster_INSTABILITY.npy')
        ind_cluster_stability_Diff_file = os.path.join(
            os.getcwd(), 'ind_cluster_stability_Diff.npy')
        ism_cluster_voxel_scores_file = os.path.join(
            os.getcwd(), 'ism_cluster_voxel_scores.npy')

        os.chdir(ismdir + '/' + subdir)

        for k in cluster_ids:
            ind_cluster_stability.append(
                ism_cluster_voxel_scores[(k - 1), clusters_ism == k].mean())
            ind_cluster_INSTABILITY.append(
                ism_cluster_voxel_scores[(k - 1), clusters_ism != k].mean())
            A, B = basc.ndarray_to_vol(
                ism_cluster_voxel_scores[k - 1, :], roi_mask_file,
                roi_mask_file, 'ism_single_cluster%i_stability.nii.gz' % k)
        ind_cluster_stability = np.asarray(ind_cluster_stability)
        ind_cluster_INSTABILITY = np.asarray(ind_cluster_INSTABILITY)
        ind_cluster_stability_Diff = ind_cluster_stability - ind_cluster_INSTABILITY

        np.save(ind_cluster_stability_file, ind_cluster_stability)
        np.save(ind_cluster_INSTABILITY_file, ind_cluster_INSTABILITY)
        np.save(ind_cluster_stability_Diff_file, ind_cluster_stability_Diff)
        np.save(ism_cluster_voxel_scores_file, ism_cluster_voxel_scores)

    return
Esempio n. 6
0
    
    
    
    grp_cluster_stability=[]
    grp_cluster_INSTABILITY=[]
    grp_cluster_stability_Diff=[]
    
    grp_cluster_stability_file = os.path.join(os.getcwd(), 'grp_cluster_stability.npy')
    grp_cluster_INSTABILITY_file = os.path.join(os.getcwd(), 'grp_cluster_INSTABILITY.npy')
    grp_cluster_stability_Diff_file = os.path.join(os.getcwd(), 'grp_cluster_stability_Diff.npy')
    gsm_cluster_voxel_scores_file = os.path.join(os.getcwd(), 'gsm_cluster_voxel_scores.npy')
    
    for k in cluster_ids:
        grp_cluster_stability.append(gsm_cluster_voxel_scores[(k-1),clusters_gsm==k].mean())
        grp_cluster_INSTABILITY.append(gsm_cluster_voxel_scores[(k-1),clusters_gsm!=k].mean())
        A, B = basc.ndarray_to_vol(gsm_cluster_voxel_scores[k-1,:], roi_mask_file, roi_mask_file, 'gsm_single_cluster%i_stability.nii.gz' % k)
    grp_cluster_stability=np.asarray(grp_cluster_stability)
    grp_cluster_INSTABILITY=np.asarray(grp_cluster_INSTABILITY)
    grp_cluster_stability_Diff=grp_cluster_stability-grp_cluster_INSTABILITY
    
    np.save(grp_cluster_stability_file, grp_cluster_stability)
    np.save(grp_cluster_INSTABILITY_file, grp_cluster_INSTABILITY)
    np.save(grp_cluster_stability_Diff_file, grp_cluster_stability_Diff)
    np.save(gsm_cluster_voxel_scores_file, gsm_cluster_voxel_scores)

#%%
def set_style():
    # This sets reasonable defaults for font size for
    # a figure that will go in a paper
    sns.set_context("paper")
    
Esempio n. 7
0
def gsm_nifti(roi_mask_file, n_clusters, out_dir):
    """
    Calculate the group level stability and instability maps for each of the group clusters.
    Create Nifti files for each individual cluster's stability map
    
    
    Parameters
    ----------
        roi_mask_file: the mask of the region to calculate stability for.
        n_clusters: the number of clusters calculated
        out_dir: the directory to output the saved nifti images
    

    Returns
    -------
    Creates NIFTI files for all the gsm cluster stability maps
    
    """

    import utils
    import basc
    import numpy as np
    import os
    #*ACTION - FIGURE OUT IF CAN BE ADDED TO BASC WORKFLOW, OR DIFFERENT WORKFLOW?
    #Individual subject ISM to NIFTI and individual

    #Inputs Subject ISM, ROIFile,

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

    #for i in range(nSubjects):
    gsmdir = out_dir + '/workflow_output/basc_workflow_runner/basc/join_group_stability/'
    os.chdir(gsmdir)

    gsm = np.load(gsmdir + '/group_stability_matrix.npy')
    clusters_gsm = utils.cluster_timeseries(gsm,
                                            roi_mask_nparray,
                                            n_clusters,
                                            similarity_metric='correlation',
                                            affinity_threshold=0.0,
                                            cluster_method='ward')
    clusters_gsm = clusters_gsm + 1
    #niftifilename = gsmdir  +'/gsm_clust.nii.gz'
    #clusters_gsm_file = gsmdir +'/clusters_gsm.npy'
    #Saving Individual Level Cluster Solution
    #    ndarray_to_vol(clusters_gsm, roi_mask_file, roi_mask_file, niftifilename)
    #    np.save(clusters_gsm_file, clusters_gsm)

    cluster_ids = np.unique(clusters_gsm)
    nClusters = cluster_ids.shape[0]
    nVoxels = clusters_gsm.shape[0]
    gsm_cluster_voxel_scores = np.zeros((nClusters, nVoxels))
    k_mask = np.zeros((nVoxels, nVoxels))
    gsm_cluster_voxel_scores[:, :], k_mask[:, :] = utils.cluster_matrix_average(
        gsm, clusters_gsm)
    gsm_cluster_voxel_scores = gsm_cluster_voxel_scores.astype("uint8")

    grp_cluster_stability = []
    grp_cluster_INSTABILITY = []
    grp_cluster_stability_Diff = []

    grp_cluster_stability_file = os.path.join(os.getcwd(),
                                              'grp_cluster_stability.npy')
    grp_cluster_INSTABILITY_file = os.path.join(os.getcwd(),
                                                'grp_cluster_INSTABILITY.npy')
    grp_cluster_stability_Diff_file = os.path.join(
        os.getcwd(), 'grp_cluster_stability_Diff.npy')
    gsm_cluster_voxel_scores_file = os.path.join(
        os.getcwd(), 'gsm_cluster_voxel_scores.npy')

    for k in cluster_ids:
        grp_cluster_stability.append(
            gsm_cluster_voxel_scores[(k - 1), clusters_gsm == k].mean())
        grp_cluster_INSTABILITY.append(
            gsm_cluster_voxel_scores[(k - 1), clusters_gsm != k].mean())
        A, B = basc.ndarray_to_vol(gsm_cluster_voxel_scores[k - 1, :],
                                   roi_mask_file, roi_mask_file,
                                   'gsm_single_cluster%i_stability.nii.gz' % k)
    grp_cluster_stability = np.asarray(grp_cluster_stability)
    grp_cluster_INSTABILITY = np.asarray(grp_cluster_INSTABILITY)
    grp_cluster_stability_Diff = grp_cluster_stability - grp_cluster_INSTABILITY

    np.save(grp_cluster_stability_file, grp_cluster_stability)
    np.save(grp_cluster_INSTABILITY_file, grp_cluster_INSTABILITY)
    np.save(grp_cluster_stability_Diff_file, grp_cluster_stability_Diff)
    np.save(gsm_cluster_voxel_scores_file, gsm_cluster_voxel_scores)

    return
Esempio n. 8
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.

    """
    ##*ACTION - CLEAN UP COMMENTED OUT CODE
    import os
    import numpy as np
    import utils
    import basc
    print('starting igcm')
    #import pdb; pdb.set_trace()
    print("igcm debug 1")
    # *REMOVE LINE* indiv_stability_set = np.asarray([np.load(ism_file) for ism_file in indiv_stability_list])
    indiv_stability_mat = np.asarray([np.load(indiv_stability_list)])
    indiv_stability_set = indiv_stability_mat[0]
    #*REMOVE LINE*nSubjects = indiv_stability_set.shape[0]
    nVoxels = indiv_stability_set.shape[1]

    cluster_ids = np.unique(clusters_G)
    nClusters = cluster_ids.shape[0]
    print("igcm debug 2")

    #    *REMOVE LINE*cluster_voxel_scores = np.zeros((nSubjects,nClusters, nVoxels))
    #    *REMOVE LINE*k_mask=np.zeros((nSubjects,nVoxels, nVoxels))
    cluster_voxel_scores = np.zeros((nClusters, nVoxels))
    k_mask = np.zeros((nVoxels, nVoxels))
    #*REMOVE LINE*for i in range(nSubjects):

    cluster_voxel_scores[:, :], k_mask[:, :] = utils.cluster_matrix_average(
        indiv_stability_set, clusters_G)
    #*REMOVE LINE*clust5[0,clusters_g==1].mean()
    print("igcm debug 3")
    #*REMOVE LINE*import pdb; pdb.set_trace()

    #*REMOVE LINE*cluster_voxel_scores[0,clusters_g==1].mean()

    ind_group_cluster_stability = []
    icvs = []
    icvs = np.asarray(1)
    icvs_idx = 0
    print("igcm debug 4")

    #*REMOVE LINE*ind_group_cluster_stability.append(cluster_voxel_scores[(k-1),clusters_G==k].mean())
    #*REMOVE LINE*icvs_idx += 1

    for i in cluster_ids:
        ind_group_cluster_stability.append(
            cluster_voxel_scores[(i - 1), clusters_G == i].mean())
    print("igcm debug 5")

    ind_group_cluster_stability = np.array(ind_group_cluster_stability)
    ind_group_cluster_stability = np.array([1, 2, 3, 4, 5])
    #print( 'saving files: icvs')
    icvs_file = os.path.join(os.getcwd(), 'icvs.npy')
    #*REMOVE LINE*np.save(icvs_file, icvs)

    print('saving files: cluster_voxel_scores')
    cluster_voxel_scores = cluster_voxel_scores.astype("uint8")
    cluster_voxel_scores_file = os.path.join(os.getcwd(),
                                             'cluster_voxel_scores.npy')
    #*REMOVE LINE*np.save(cluster_voxel_scores_file, cluster_voxel_scores)
    #REMOVE
    #import pdb; pdb.set_trace()

    #np.save(individualized_group_clusters_img_file, img)
    #return img_file, img
    #For each column, which row has the largest number?
    #    Creates a single row with voxel labels.

    #print( 'saving files: k_mask')
    k_mask = k_mask.astype("bool_")
    k_mask_file = os.path.join(os.getcwd(), 'k_mask.npy')
    #*REMOVE LINE*np.save(k_mask_file, k_mask)
    #REMOVE

    #print( 'saving files: ind_group_cluster_stability')
    #ind_group_cluster_stability=ind_group_cluster_stability.astype("uint8")
    ind_group_cluster_stability_file = os.path.join(
        os.getcwd(), 'ind_group_cluster_stability.npy')
    np.save(ind_group_cluster_stability_file, ind_group_cluster_stability)

    #import pdb; pdb.set_trace()
    Individualized_Group_Cluster = np.argmax(cluster_voxel_scores, axis=0) + 1
    individualized_group_clusters_img_file, img = basc.ndarray_to_vol(
        Individualized_Group_Cluster, roi_mask_file, roi_mask_file,
        os.path.join(os.getcwd(), 'IndividualizedGroupClusters.nii.gz'))
    img.to_filename(individualized_group_clusters_img_file)

    return icvs_file, cluster_voxel_scores_file, k_mask_file, ind_group_cluster_stability_file, individualized_group_clusters_img_file  #icvs, cluster_voxel_scores, k_mask
Esempio n. 9
0
                data_array2 = SimVisual
                roi_mask_file2 = '/Users/aki.nikolaidis/git_repo/PyBASC/masks/Yeo7_3mmMasks/Yeo_1_3mm.nii.gz'
                sample_file2 = '/Users/aki.nikolaidis/git_repo/PyBASC/masks/Yeo7_3mmMasks/Yeo_1_3mm.nii.gz'
                filename2 = '/Users/aki.nikolaidis/git_repo/PyBASC/SimData4/SimVisual_' + 'sub_' + str(
                    subs) + 'corrstrength_' + str(
                        corrstrength) + 'noise_' + str(noiselevel) + '.nii.gz'

                data_array3 = SimMotor
                roi_mask_file3 = '/Users/aki.nikolaidis/git_repo/PyBASC/masks/Yeo7_3mmMasks/Yeo_2_3mm.nii.gz'
                sample_file3 = '/Users/aki.nikolaidis/git_repo/PyBASC/masks/Yeo7_3mmMasks/Yeo_2_3mm.nii.gz'
                filename3 = '/Users/aki.nikolaidis/git_repo/PyBASC/SimData4/SimMotor_' + 'sub_' + str(
                    subs) + 'corrstrength_' + str(
                        corrstrength) + 'noise_' + str(noiselevel) + '.nii.gz'

                #write Regions to nifti file
                basc.ndarray_to_vol(data_array_right, roi_mask_file_right,
                                    sample_file_right, filename_right)
                basc.ndarray_to_vol(data_array_thal, roi_mask_file_thal,
                                    sample_file_thal, filename_thal)
                basc.ndarray_to_vol(data_array_left, roi_mask_file_left,
                                    sample_file_left, filename_left)
                #write Region One to nifti file
                basc.ndarray_to_vol(data_array2, roi_mask_file2, sample_file2,
                                    filename2)
                #
                basc.ndarray_to_vol(data_array3, roi_mask_file3, sample_file3,
                                    filename3)

                niftiadditionfile = 'fslmaths ' + filename_right + ' -add ' + filename_thal + ' -add ' + filename_left + ' -add ' + filename2 + ' -add ' + filename3 + ' /Users/aki.nikolaidis/git_repo/PyBASC/SimData4/sub_' + str(
                    subs) + 'corr_' + str(corrstrength) + '_noise_' + str(
                        noiselevel) + '_TRs_' + str(n) + '.nii.gz'