def _prep_coords_cormat(self): cor_mat_files = self.inputs.cor_mat_files nb_cormats = len(cor_mat_files) coords_files = self.inputs.coords_files gm_mask_coords_file = self.inputs.gm_mask_coords_file # loading gm mask corres gm_mask_coords = np.array(np.loadtxt(gm_mask_coords_file), dtype='int') nb_nodes = gm_mask_coords.shape[0] # defining return matrices sum_cormat = np.zeros((nb_nodes, nb_nodes), dtype=float) group_cormat = np.zeros((nb_nodes, nb_nodes, nb_cormats), dtype=float) group_vect = np.zeros((nb_nodes, nb_cormats), dtype=float) assert nb_cormats == len(coords_files), \ ("Error, nb_cormats and coords_files are imcompatible {} \ {}".format(nb_cormats, len(coords_files))) for index_file in range(nb_cormats): assert os.path.exists(cor_mat_files[index_file]) assert os.path.exists(coords_files[index_file]) Z_cor_mat = np.load(cor_mat_files[index_file]) print(Z_cor_mat.shape) coords = np.array(np.loadtxt( coords_files[index_file]), dtype='int') print(coords.shape) corres_cor_mat, possible_edge_mat = \ return_corres_correl_mat(Z_cor_mat, coords, gm_mask_coords) corres_cor_mat = corres_cor_mat + np.transpose(corres_cor_mat) sum_cormat += corres_cor_mat group_cormat[:, :, index_file] = corres_cor_mat group_vect[:, index_file] = np.sum(corres_cor_mat, axis=0) return sum_cormat, group_cormat, group_vect
def _run_interface(self, runtime): print('in prepare_cormat') cor_mat_files = self.inputs.cor_mat_files if isdefined(self.inputs.gm_mask_coords_file) and isdefined(self.inputs.coords_files): coords_files = self.inputs.coords_files gm_mask_coords_file = self.inputs.gm_mask_coords_file print('loading gm mask corres') gm_mask_coords = np.array(np.loadtxt(gm_mask_coords_file), dtype = 'int') print(gm_mask_coords.shape) #### read matrix from the first group #print Z_cor_mat_files sum_cormat = np.zeros((gm_mask_coords.shape[0],gm_mask_coords.shape[0]),dtype = float) print(sum_cormat.shape) group_cormat = np.zeros((gm_mask_coords.shape[0],gm_mask_coords.shape[0],len(cor_mat_files)),dtype = float) print(group_cormat.shape) group_vect = np.zeros((gm_mask_coords.shape[0],len(cor_mat_files)),dtype = float) print(group_vect.shape) if len(cor_mat_files) != len(coords_files): print("warning, length of cor_mat_files, coords_files are imcompatible {} {} {}".format(len(cor_mat_files),len(coords_files))) for index_file in range(len(cor_mat_files)): print(cor_mat_files[index_file]) if os.path.exists(cor_mat_files[index_file]) and os.path.exists(coords_files[index_file]): Z_cor_mat = np.load(cor_mat_files[index_file]) print(Z_cor_mat.shape) coords = np.array(np.loadtxt(coords_files[index_file]),dtype = 'int') print(coords.shape) corres_cor_mat,possible_edge_mat = return_corres_correl_mat(Z_cor_mat,coords,gm_mask_coords) corres_cor_mat = corres_cor_mat + np.transpose(corres_cor_mat) print(corres_cor_mat.shape) print(group_cormat.shape) sum_cormat += corres_cor_mat group_cormat[:,:,index_file] = corres_cor_mat group_vect[:,index_file] = np.sum(corres_cor_mat,axis = 0) else: print("Warning, one or more files between " + cor_mat_files[index_file] + ', ' + coords_files[index_file] + " do not exists") elif isdefined(self.inputs.gm_mask_labels_file) and isdefined(self.inputs.labels_files): labels_files = self.inputs.labels_files gm_mask_labels_file = self.inputs.gm_mask_labels_file print('loading gm mask labels') #gm_mask_labels = [line.strip() for line in open(gm_mask_labels_file)] #print len(gm_mask_labels) gm_mask_labels = np.array([line.strip() for line in open(gm_mask_labels_file)],dtype = 'str') print(gm_mask_labels.shape) #### read matrix from the first group #print Z_cor_mat_files sum_cormat = np.zeros((gm_mask_labels.shape[0],gm_mask_labels.shape[0]),dtype = float) print(sum_cormat.shape) group_cormat = np.zeros((gm_mask_labels.shape[0],gm_mask_labels.shape[0],len(cor_mat_files)),dtype = float) print(group_cormat.shape) group_vect = np.zeros((gm_mask_labels.shape[0],len(cor_mat_files)),dtype = float) print(group_vect.shape) if len(cor_mat_files) != len(labels_files): print("warning, length of cor_mat_files, labels_files are imcompatible {} {} {}".format(len(cor_mat_files),len(labels_files))) print(cor_mat_files) for index_file in range(len(cor_mat_files)): print(cor_mat_files[index_file]) if os.path.exists(cor_mat_files[index_file]) and os.path.exists(labels_files[index_file]): Z_cor_mat = np.load(cor_mat_files[index_file]) print(Z_cor_mat) print(Z_cor_mat.shape) labels = np.array([line.strip() for line in open(labels_files[index_file])],dtype = 'str') print("labels_subj:") print(labels.shape) corres_cor_mat,possible_edge_mat = return_corres_correl_mat_labels(Z_cor_mat,labels,gm_mask_labels) corres_cor_mat = corres_cor_mat + np.transpose(corres_cor_mat) print(corres_cor_mat) print(group_cormat.shape) sum_cormat += corres_cor_mat group_cormat[:,:,index_file] = corres_cor_mat group_vect[:,index_file] = np.sum(corres_cor_mat,axis = 0) else: print("Warning, one or more files between " + cor_mat_files[index_file] + ', ' + coords_files[index_file] + " do not exists") print(group_cormat) else: print("Error, neither coords nor labels are defined properly") print(self.inputs.gm_mask_coords_file) print(self.inputs.gm_mask_labels_file) print(self.inputs.coords_files) 0/0 group_cormat_file= os.path.abspath('group_cormat.npy') np.save(group_cormat_file,group_cormat) group_vect_file= os.path.abspath('group_vect.npy') np.save(group_vect_file,group_vect) print('saving cor_mat matrix') avg_cormat_file = os.path.abspath('avg_cormat.npy') if (len(cor_mat_files) != 0): avg_cormat = sum_cormat /len(cor_mat_files) np.save(avg_cormat_file,avg_cormat) return runtime
def return_all_iter_cormats(cormat_path, iterables, iternames, gm_mask_coords_file=0, gm_mask_labels_file=0, mapflow_iterables=0, mapflow_iternames=0, export_df=False): """ gm_mask_coords_file is the coords commun to all analyses """ print(product(*iterables)) all_iter_cormats = [] all_descriptors = [] assert isInAlphabeticalOrder(iternames), \ ("Warning, iternames are not in alphabetical oroder, check the \ iterables order as well" ) if gm_mask_coords_file != 0: gm_mask_coords = np.loadtxt(gm_mask_coords_file) print(gm_mask_coords) if export_df: writer = pd.ExcelWriter(os.path.join(cormat_path, "all_cormats.xls")) for iter_obj in product(*iterables): print(iter_obj) assert len(iter_obj) == len( iternames), "Error, different number of iternames and iterables" iter_dir = "".join([ "_" + zip_iter[0].strip() + "_" + zip_iter[1].strip() for zip_iter in zip(iternames, iter_obj) ]) print(iter_dir) if mapflow_iterables == 0: cormat_file = os.path.join(cormat_path, iter_dir, "compute_conf_cor_mat", "Z_cor_mat_resid_ts.npy") assert os.path.exists(cormat_file), \ ("Warning, file {} could not be found".format(cormat_file)) cormat = np.load(cormat_file) print(cormat.shape) if gm_mask_coords_file != 0: coords_file = os.path.join(cormat_path, iter_dir, "extract_mean_ROI_ts", "subj_coord_rois.txt") coords = np.loadtxt(coords_file) cormat, _ = return_corres_correl_mat(cormat, coords, gm_mask_coords) if export_df: if gm_mask_labels_file: labels = [ line.strip() for line in open(gm_mask_labels_file) ] else: labels = list(range(cormat.shape[0])) df = pd.DataFrame(cormat, columns=labels, index=labels) df.to_excel(writer, "_".join(iter_obj)) all_iter_cormats.append(cormat) all_descriptors.append(iter_obj) else: for i, map_iter in enumerate(mapflow_iterables): cormat_file = os.path.join(cormat_path, iter_dir, "compute_conf_cor_mat", "mapflow", "_compute_conf_cor_mat" + str(i), "Z_cor_mat_resid_ts.npy") new_iter_obj = list(iter_obj) new_iter_obj.append(str(map_iter)) assert os.path.exists(cormat_file), \ ("Warning, file {} could not be found".format(cormat_file)) cormat = np.load(cormat_file) if gm_mask_coords_file: coords_file = os.path.join(cormat_path, iter_dir, "extract_mean_ROI_ts", "subj_coord_rois.txt") coords = np.loadtxt(coords_file) cormat, _ = return_corres_correl_mat( cormat, coords, gm_mask_coords) if export_df: if gm_mask_labels_file: labels = [ line.strip() for line in open(gm_mask_labels_file) ] else: labels = list(range(cormat.shape[0])) df = pd.DataFrame(cormat, columns=labels, index=labels) df.to_excel(writer, "_".join(new_iter_obj)) all_iter_cormats.append(cormat) all_descriptors.append(new_iter_obj) if mapflow_iternames != 0: iternames.append(mapflow_iternames) pd_all_descriptors = pd.DataFrame(all_descriptors, columns=iternames) if export_df: pd_all_descriptors.to_excel( os.path.join(cormat_path, "all_descriptors.xls")) return np.array(all_iter_cormats), pd_all_descriptors
def test_return_corres_correl_mat(): """test corres matrix based on coords""" # TODO find a real assert test... ref_mat = return_corres_correl_mat(mat, coords, ref_coords) print(ref_mat)