def test_cluster_matrix_average(): import utils import basc import matplotlib.pyplot as plt roi_mask_nparray = 'empty' blobs = generate_blobs() n_clusters = 3 similarity_metric = 'correlation' ism = utils.individual_stability_matrix(blobs, 100, n_clusters, similarity_metric) y_predict = utils.cluster_timeseries(blobs, roi_mask_nparray, n_clusters, similarity_metric, affinity_threshold=0.0, neighbors=10) cluster_voxel_scores, K_mask = utils.cluster_matrix_average(ism, y_predict) plt.imshow(K_mask)
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
def ism_nifti(roi_mask_file, n_clusters, out_dir): #NEED TO CHANGE THIS SCRIPT TO: #APPLY GROUP LEVEL CLUSTER LABELS TO INDIVIDUAL LEVELS # #EXTRACT VOXELWISE STABILITY INFO FOR THAT CLUSTER ACROSS ALL PARTICIPANTS #USE KMASK TO CREATE THAT CLUSTER INFORMATION # #Loop over all ISMs, # load ISM, # loop over all clusters, # perform calculation, # add calculation to running tabs. # #Calculate mean at each voxel and CV- # output a voxelwise mean stability and CV Nifti map for each cluster. """ Calculate the individual 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 ism cluster stability maps """ import utils import basc import numpy as np import os from pathlib import Path #*ACTION - FIGURE OUT IF CAN BE ADDED TO BASC WORKFLOW, OR DIFFERENT WORKFLOW? #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) subdirs_all = [x[1] for x in os.walk(ismdir)] subdirs=subdirs_all[0] roi_mask_nparray = nb.load(roi_mask_file).get_data().astype('float32').astype('bool') for subdir in subdirs: os.chdir(ismdir + subdir) 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') end_file = Path(ism_cluster_voxel_scores_file) if end_file.exists(): return else: ism=np.load(ismdir + subdir + '/individual_stability_matrix.npy') clusters_ism = utils.cluster_timeseries(ism, roi_mask_nparray, n_clusters, similarity_metric = 'correlation', affinity_threshold=0.0, cluster_method='ward') 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)
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
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
def ism_nifti(roi_mask_file, n_clusters, out_dir): """ Calculate the individual 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 ism cluster stability maps """ import utils import basc import numpy as np import os from pathlib import Path #*ACTION - FIGURE OUT IF CAN BE ADDED TO BASC WORKFLOW, OR DIFFERENT WORKFLOW? #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) subdirs_all = [x[1] for x in os.walk(ismdir)] subdirs = subdirs_all[0] roi_mask_nparray = nb.load(roi_mask_file).get_data().astype( 'float32').astype('bool') for subdir in subdirs: os.chdir(ismdir + subdir) 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') end_file = Path(ism_cluster_voxel_scores_file) if end_file.exists(): return else: ism = np.load(ismdir + subdir + '/individual_stability_matrix.npy') clusters_ism = utils.cluster_timeseries( ism, roi_mask_nparray, n_clusters, similarity_metric='correlation', affinity_threshold=0.0, cluster_method='ward') 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
def join_group_stability(indiv_stability_list, group_stability_list, n_bootstraps, n_clusters, roi_mask_file): """ Merges the group stability maps for all and compares to all individual stability maps 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 group_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 : array_like Number of bootstraps to join and average. 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("starting join group stability") group_stability_set = np.asarray( [np.load(G_file) for G_file in group_stability_list]) gsm = group_stability_set.sum(axis=0) G = gsm / int(n_bootstraps) G = G * 100 G = G.astype("uint8") #SPATIAL CONSTRAINT EXPERIMENT# #roi_mask_nparray='empty' #SPATIAL CONSTRAINT EXPERIMENT# print('calculating clusters_G') #import pdb; pdb.set_trace() roi_mask_nparray = nb.load(roi_mask_file).get_data().astype( 'float32').astype('bool') clusters_G = utils.cluster_timeseries(G, roi_mask_nparray, n_clusters, similarity_metric='correlation', affinity_threshold=0.0, cluster_method='ward') #APPLY THIS METHOD TO THE INDIVIDUAL LEVEL CLUSTER print('calculating cluster_voxel scores') #print( '1') # Cluster labels normally start from 0, start from 1 to provide contrast when viewing between 0 voxels clusters_G += 1 clusters_G = clusters_G.astype("uint8") indiv_stability_set = np.asarray( [np.load(ism_file) for ism_file in indiv_stability_list]) #print( '2') ism_gsm_corr = np.zeros(len(indiv_stability_list)) for i in range(0, len(indiv_stability_list)): #import pdb;pdb.set_trace() ism_gsm_corr[i] = utils.compare_stability_matrices( indiv_stability_set[i], G) #print( '3') print('saving files: G') print(G) gsm_file = os.path.join(os.getcwd(), 'group_stability_matrix.npy') np.save(gsm_file, G) print('saving files: clusters_G') clusters_G_file = os.path.join(os.getcwd(), 'clusters_G.npy') np.save(clusters_G_file, clusters_G) print('saving files: ism_gsm_corr') ism_gsm_corr_file = os.path.join(os.getcwd(), 'ism_gsm_corr.npy') np.save(ism_gsm_corr_file, ism_gsm_corr) return G, clusters_G, ism_gsm_corr, gsm_file, clusters_G_file, ism_gsm_corr_file
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
def gsm_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): 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