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
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 from CPAC.basc.basc import ndarray_to_vol import CPAC.basc.utils as 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 = 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
def ism_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): 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 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 """ from CPAC.basc.basc import ndarray_to_vol import CPAC.basc.utils as utils 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 = 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 """ from CPAC.basc.basc import ndarray_to_vol import CPAC.basc.utils as utils 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 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 from CPAC.basc.basc import ndarray_to_vol import CPAC.basc.utils as utils 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 = 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