def _run_interface(self, runtime): rada_lol_file = self.inputs.rada_lol_file Pajek_net_file = self.inputs.Pajek_net_file conmat_file = self.inputs.conmat_file export_excel = self.inputs.export_excel community_vect = read_lol_file(rada_lol_file) corres_nodes, sparse_mat = \ read_Pajek_corres_nodes_and_sparse_matrix(Pajek_net_file) # density conmat = np.load(conmat_file) corres_mat = conmat[:, corres_nodes][corres_nodes, :] # intermodule if not is_symetrical(corres_mat): corres_mat = corres_mat + np.transpose(corres_mat) df_avgmat = _inter_module_avgmat(corres_mat, community_vect) df_avgmat_file = os.path.abspath("res_avgmat.csv") df_avgmat.to_csv(df_avgmat_file) if export_excel: try: import xlwt # noqa df_avgmat_excel_file = os.path.abspath("res_avgmat.xls") df_avgmat.to_excel(df_avgmat_excel_file) except ImportError: print("Error, xlwt not installed, cannot export Excel file") return runtime
def test_read_lol_file(): """ Test reading modular partition as radatools representation lol_file """ # TODO explicit assert test community_vect = read_lol_file(lol_file) print(community_vect)
def test_compute_modular_matrix(): """ Test computing modular matrix where edges between nodes belonging to the same module are given the same value """ # TODO explicit assert test corres, sp = read_Pajek_corres_nodes_and_sparse_matrix(Pajek_net_file) community_vect = read_lol_file(lol_file) mod_mat = compute_modular_matrix(sp, community_vect) print(mod_mat)
def test_compute_roles(): """ test_node_roles """ # TODO explicit assert test community_vect = read_lol_file(lol_file) node_corres, sparse_matrix = read_Pajek_corres_nodes_and_sparse_matrix( Pajek_net_file) val = compute_roles(community_vect, sparse_matrix, role_type='4roles') print(val)
def _run_interface(self, runtime): rada_lol_file = self.inputs.rada_lol_file Pajek_net_file = self.inputs.Pajek_net_file print('Loading Pajek_net_file for reading node_corres') node_corres, sparse_mat = read_Pajek_corres_nodes_and_sparse_matrix( Pajek_net_file) print(sparse_mat.todense()) print(node_corres.shape, sparse_mat.todense().shape) print("Loading community belonging file " + rada_lol_file) community_vect = read_lol_file(rada_lol_file) print(community_vect) print("Computing node roles") node_roles, all_Z_com_degree, all_participation_coeff = compute_roles( community_vect, sparse_mat, role_type=self.inputs.role_type) node_roles_file = os.path.abspath('node_roles.txt') np.savetxt(node_roles_file, node_roles, fmt='%d') all_Z_com_degree_file = os.path.abspath('all_Z_com_degree.txt') np.savetxt(all_Z_com_degree_file, all_Z_com_degree, fmt='%f') all_participation_coeff_file = os.path.abspath( 'all_participation_coeff.txt') np.savetxt(all_participation_coeff_file, all_participation_coeff, fmt='%f') if self.inputs.compute_ndi: ndi_values = compute_node_dissociation_index( community_vect, sparse_mat) ndi_values_file = os.path.abspath('ndi_values.txt') np.savetxt(ndi_values_file, ndi_values, fmt='%f') return runtime
def _run_interface(self, runtime): rada_lol_file = self.inputs.rada_lol_file Pajek_net_file = self.inputs.Pajek_net_file group_conmat_file = self.inputs.group_conmat_file export_excel = self.inputs.export_excel community_vect = read_lol_file(rada_lol_file) corres_nodes, sparse_mat = \ read_Pajek_corres_nodes_and_sparse_matrix(Pajek_net_file) # density group_conmats = np.load(group_conmat_file) if len(group_conmats.shape) == 2: group_conmats = group_conmats.reshape(group_conmats.shape[0], group_conmats.shape[1], 1) print(group_conmats.shape) for i in range(group_conmats.shape[2]): conmat = group_conmats[:, :, i] corres_mat = conmat[:, corres_nodes][corres_nodes, :] # intermodule if not is_symetrical(corres_mat): corres_mat = corres_mat + np.transpose(corres_mat) df_avgmat = _inter_module_avgmat(corres_mat, community_vect) df_avgmat_file = os.path.abspath("res_avgmat_" + str(i) + ".csv") df_avgmat.to_csv(df_avgmat_file) if export_excel: try: import xlwt # noqa df_avgmat.to_excel( os.path.abspath("res_avgmat_" + str(i) + ".xls")) except ImportError: print("Error xlwt not installed, cannot export Excel file") return runtime
def visu_graph_modules_roles(net_file, lol_file, roles_file, coords_file, inter_modules=True, modality_type="", s_textcolor="white", c_colval=c_colval_modules, umin=0, umax=50, x_offset=0, y_offset=0, z_offset=0, default_size=10, hub_to_non_hub=3): # coords coords = np.loadtxt(coords_file) if modality_type == "MEG": coords = 1000 * coords temp = np.copy(coords) coords[:, 1] = coords[:, 0] coords[:, 0] = temp[:, 1] coords[:, 2] += z_offset coords[:, 1] += y_offset coords[:, 0] += x_offset # net file node_corres, sparse_matrix = read_Pajek_corres_nodes_and_sparse_matrix( net_file) corres_coords = coords[node_corres, :] corres_coords = coords[node_corres, :] # lol file community_vect = read_lol_file(lol_file) max_col = np.array([val for val in c_colval.keys()]).max() community_vect[community_vect > max_col] = max_col """Create the connectivity object :""" c_connect = np.array(compute_modular_matrix(sparse_matrix, community_vect), dtype='float64') c_connect = np.ma.masked_array(c_connect, mask=True) c_connect.mask[-1.0 <= c_connect] = False if inter_modules: c_colval[-1] = "grey" else: c_connect.mask[-1.0 == c_connect] = True c_obj = ConnectObj('ConnectObj1', corres_coords, c_connect, color_by='strength', custom_colors=c_colval) """Create the source object :""" node_roles = np.array(np.loadtxt(roles_file), dtype='int64') # prov_hubs prov_hubs = (node_roles[:, 0] == 2) & (node_roles[:, 1] == 1) coords_prov_hubs = corres_coords[prov_hubs] colors_prov_hubs = [c_colval[i] for i in community_vect[prov_hubs]] # prov_no_hubs prov_no_hubs = (node_roles[:, 0] == 1) & (node_roles[:, 1] == 1) coords_prov_no_hubs = corres_coords[prov_no_hubs] colors_prov_no_hubs = [c_colval[i] for i in community_vect[prov_no_hubs]] # connec_hubs connec_hubs = (node_roles[:, 0] == 2) & (node_roles[:, 1] == 2) coords_connec_hubs = corres_coords[connec_hubs] colors_connec_hubs = [c_colval[i] for i in community_vect[connec_hubs]] # connec_no_hubs connec_no_hubs = (node_roles[:, 0] == 1) & (node_roles[:, 1] == 2) coords_connec_no_hubs = corres_coords[connec_no_hubs] colors_connec_no_hubs = [ c_colval[i] for i in community_vect[connec_no_hubs] ] list_sources = [] if len(coords_prov_no_hubs != 0): s_obj1 = SourceObj('prov_no_hubs', coords_prov_no_hubs, color=colors_prov_no_hubs, alpha=.5, edge_width=2., radius_min=default_size, radius_max=default_size) list_sources.append(s_obj1) if len(coords_connec_no_hubs != 0): s_obj2 = SourceObj('connec_no_hubs', coords_connec_no_hubs, color=colors_connec_no_hubs, alpha=.5, edge_width=2., radius_min=default_size, radius_max=default_size, symbol='square') list_sources.append(s_obj2) if len(coords_prov_hubs != 0): s_obj3 = SourceObj('prov_hubs', coords_prov_hubs, color=colors_prov_hubs, alpha=.5, edge_width=2., radius_min=default_size * hub_to_non_hub, radius_max=default_size * hub_to_non_hub) list_sources.append(s_obj3) if len(coords_connec_hubs != 0): s_obj4 = SourceObj('connec_hubs', coords_connec_hubs, color=colors_connec_hubs, alpha=.5, edge_width=2., radius_min=default_size * hub_to_non_hub, radius_max=default_size * hub_to_non_hub, symbol='square') list_sources.append(s_obj4) return c_obj, list_sources
def visu_graph_modules(net_file, lol_file, coords_file, labels_file=0, inter_modules=True, modality_type="", s_textcolor="white", c_colval=c_colval_modules, umin=0, umax=50, x_offset=0, y_offset=0, z_offset=0): # coords coords = np.loadtxt(coords_file) if modality_type == "MEG": coords = 1000 * coords temp = np.copy(coords) coords[:, 1] = coords[:, 0] coords[:, 0] = temp[:, 1] coords[:, 2] += z_offset coords[:, 1] += y_offset coords[:, 0] += x_offset # labels if labels_file: labels = [line.strip() for line in open(labels_file)] np_labels = np.array(labels) # net file node_corres, sparse_matrix = read_Pajek_corres_nodes_and_sparse_matrix( net_file) # lol file community_vect = read_lol_file(lol_file) c_connect = np.array(compute_modular_matrix(sparse_matrix, community_vect), dtype='float64') c_connect = np.ma.masked_array(c_connect, mask=True) c_connect.mask[c_connect > -1.0] = False corres_coords = coords[node_corres, :] if inter_modules: c_colval[-1] = "grey" """Create the connectivity object :""" c_obj = ConnectObj('ConnectObj1', corres_coords, c_connect, color_by='strength', custom_colors=c_colval) # source object colors_nodes = [] for i in community_vect: if i in c_colval.keys(): colors_nodes.append(c_colval[i]) else: colors_nodes.append("black") if labels_file: corres_labels = np_labels[node_corres] s_obj = SourceObj('SourceObj1', corres_coords, text=corres_labels, text_color=s_textcolor, text_size=10, color=colors_nodes, alpha=.5, edge_width=2., radius_min=10., radius_max=10.) else: s_obj = SourceObj('SourceObj1', corres_coords, text_color=s_textcolor, text_size=10, color=colors_nodes, alpha=.5, edge_width=2., radius_min=10., radius_max=10.) return c_obj, s_obj
def compute_nodes_rada_df(local_dir, gm_coords=[], coords_file="", gm_labels=[], labels_file="", radatools_version="3.2", mapflow=[], mapflow_name=""): """node properties df""" if radatools_version == "3.2": net_prop_dir = "net_prop" elif radatools_version == "4.0": net_prop_dir = "prep_rada" elif radatools_version == "5.0": net_prop_dir = "net_prop" elif radatools_version == "run": net_prop_dir = "" else: print("Warning, could not find radatools_version {}".format( radatools_version)) return list_df = [] if len(mapflow) == 0: Pajek_file = os.path.join(local_dir, net_prop_dir, "Z_List.net") if os.path.exists(Pajek_file): columns = [] columns_names = [] # nodes in the connected graph node_corres = read_Pajek_corres_nodes(Pajek_file) print(os.path.exists(coords_file)) if os.path.exists(coords_file) and len(gm_coords): # MNI coordinates coords = np.array(np.loadtxt(coords_file), dtype=int) # node_coords node_coords = coords[node_corres, :] # where_in_gm_mask where_in_gm_mask = where_in_coords(node_coords, gm_coords) where_in_gm_mask = where_in_gm_mask.reshape( where_in_gm_mask.shape[0], 1) columns.append(where_in_gm_mask) columns_names.append('Where_in_GM_mask') if os.path.exists(labels_file): labels = np.array( [line.strip() for line in open(labels_file)], dtype=str) node_labels = labels[node_corres].reshape(-1, 1) columns.append(node_coords) columns_names.append('labels') columns.append(node_coords) columns_names.expend(['MNI_x', 'MNI_y', 'MNI_z']) elif os.path.exists(labels_file) and len(gm_labels): # TODO labels = np.array([line.strip() for line in open(labels_file)], dtype=str) node_labels = labels[node_corres].reshape(-1, 1) where_in_gm_mask = where_in_labels(node_labels, labels) columns.append(where_in_gm_mask) columns_names.append('Where_in_GM_mask') columns.append(node_labels) columns_names.append('labels') 0 / 0 elif len(gm_labels): node_labels = np.array(gm_labels)[node_corres].reshape(-1, 1) where_in_gm_mask = where_in_labels(node_labels, gm_labels).reshape(-1, 1) print(node_labels) print(where_in_gm_mask) columns.append(where_in_gm_mask) columns_names.append('Where_in_GM_mask') columns.append(node_labels) columns_names.append('labels') else: print("No labels, no coords") columns.append(node_corres) columns_names.append('node_corres') print(columns) print(columns_names) list_df.append( pd.DataFrame(np.concatenate(tuple(columns), axis=1), columns=columns_names)) else: print("Missing {}".format(Pajek_file)) info_nodes_file = os.path.join(local_dir, net_prop_dir, "Z_List-info_nodes.txt") print(info_nodes_file) if os.path.exists(info_nodes_file): # loading info_nodes df_node_info = pd.read_table(info_nodes_file) list_df.append(df_node_info) # modules /community_vect partition_file = os.path.join(local_dir, "community_rada", "Z_List.lol") if os.path.exists(partition_file): # loading partition_file community_vect = read_lol_file(partition_file) list_df.append(pd.DataFrame(community_vect, columns=['Module'])) # node roles roles_file = os.path.join(local_dir, "node_roles", "node_roles.txt") part_coeff_file = os.path.join(local_dir, "node_roles", "all_participation_coeff.txt") Z_com_degree_file = os.path.join(local_dir, "node_roles", "all_Z_com_degree.txt") if os.path.exists(roles_file) and os.path.exists(part_coeff_file) and \ os.path.exists(Z_com_degree_file): # loding node roles node_roles = np.array(np.loadtxt(roles_file), dtype=int) part_coeff = np.loadtxt(part_coeff_file) part_coeff = part_coeff.reshape(part_coeff.shape[0], 1) Z_com_degree = np.loadtxt(Z_com_degree_file) Z_com_degree = Z_com_degree.reshape(Z_com_degree.shape[0], 1) list_df.append( pd.DataFrame(np.concatenate( (node_roles, part_coeff, Z_com_degree), axis=1), columns=[ 'Role_quality', 'Role_quantity', 'Participation_coefficient', 'Z_community_degree' ])) # ndi values ndi_values_file = os.path.join(local_dir, "node_roles", "ndi_values.txt") if os.path.exists(ndi_values_file): # loding node roles ndi_values = np.array(np.loadtxt(ndi_values_file)) list_df.append( pd.DataFrame(ndi_values, columns=['Node_Dissociation_Index'])) else: # Multiple files (mapflow) for i, cond in enumerate(mapflow): list_strip_df = [] Pajek_file = os.path.join(local_dir, "prep_rada", "mapflow", "_prep_rada" + str(i), "Z_List.net") if os.path.exists(coords_file) and os.path.exists(Pajek_file) and \ os.path.exists(labels_file): # labels labels = np.array([line.strip() for line in open(labels_file)], dtype=str) # MNI coordinates coords = np.array(np.loadtxt(coords_file), dtype=int) # nodes in the connected graph node_corres = read_Pajek_corres_nodes(Pajek_file) # node_coords node_coords = coords[node_corres, :] node_labels = labels[node_corres].reshape(-1, 1) # where_in_gm_mask where_in_gm_mask = where_in_coords(node_coords, gm_coords) where_in_gm_mask = where_in_gm_mask.reshape( where_in_gm_mask.shape[0], 1) # print where_in_gm_mask print(where_in_gm_mask.shape) list_strip_df.append( pd.DataFrame(np.concatenate( (where_in_gm_mask, node_labels, node_coords), axis=1), columns=[ 'Where_in_GM_mask', 'labels', 'MNI_x', 'MNI_y', 'MNI_z' ])) else: if not os.path.exists(coords_file): print("Missing {}".format(coords_file)) if not os.path.exists(Pajek_file): print("Missing {}".format(Pajek_file)) if not os.path.exists(labels_file): print("Missing {}".format(labels_file)) info_nodes_file = os.path.join(local_dir, net_prop_dir, "Z_List-info_nodes.txt") print(info_nodes_file) if os.path.exists(info_nodes_file): # loading info_nodes df_node_info = pd.read_table(info_nodes_file) list_strip_df.append(df_node_info) # modules /community_vect partition_file = os.path.join(local_dir, "community_rada", "mapflow", "_community_rada" + str(i), "Z_List.lol") if os.path.exists(partition_file): # loading partition_file community_vect = read_lol_file(partition_file) list_strip_df.append( pd.DataFrame(community_vect, columns=['Module'])) # node roles roles_file = os.path.join(local_dir, "node_roles", "mapflow", "_node_roles" + str(i), "node_roles.txt") part_coeff_file = os.path.join(local_dir, "node_roles", "mapflow", "_node_roles" + str(i), "all_participation_coeff.txt") Z_com_degree_file = os.path.join(local_dir, "node_roles", "mapflow", "_node_roles" + str(i), "all_Z_com_degree.txt") if os.path.exists(roles_file) and os.path.exists(part_coeff_file) \ and os.path.exists(Z_com_degree_file): node_roles = np.array(np.loadtxt(roles_file), dtype=int) part_coeff = np.loadtxt(part_coeff_file) part_coeff = part_coeff.reshape(part_coeff.shape[0], 1) Z_com_degree = np.loadtxt(Z_com_degree_file) Z_com_degree = Z_com_degree.reshape(Z_com_degree.shape[0], 1) list_strip_df.append( pd.DataFrame(np.concatenate( (node_roles, part_coeff, Z_com_degree), axis=1), columns=[ 'Role_quality', 'Role_quantity', 'Participation_coefficient', 'Z_community_degree' ])) # ndi values ndi_values_file = os.path.join(local_dir, "node_roles", "mapflow", "_node_roles" + str(i), "ndi_values.txt") if os.path.exists(ndi_values_file): ndi_values = np.array(np.loadtxt(ndi_values_file)) list_strip_df.append( pd.DataFrame(ndi_values, columns=['Node_Dissociation_Index'])) # Converting list_strip_df to df, and adding it to list_df if len(list_strip_df): nb_nodes = len(list_strip_df[0].index) list_strip_df.append( pd.DataFrame([i] * nb_nodes, columns=[mapflow_name])) strip_df = pd.concat(list_strip_df, axis=1) list_df.append(strip_df) return list_df
def compute_nodes_rada_df(local_dir, gm_coords, coords_file, labels_file, radatools_version="3.2"): """node properties df""" if radatools_version == "3.2": net_prop_dir = "net_prop" elif radatools_version == "4.0": net_prop_dir = "prep_rada" elif radatools_version == "5.0": net_prop_dir = "net_prop" else: print("Warning, could not find radatools_version {}".format( radatools_version)) return list_df = [] Pajek_file = os.path.join(local_dir, "prep_rada", "Z_List.net") if os.path.exists(coords_file) and os.path.exists(Pajek_file) and \ os.path.exists(labels_file): # labels labels = np.array([line.strip() for line in open(labels_file)], dtype=str) # MNI coordinates coords = np.array(np.loadtxt(coords_file), dtype=int) # nodes in the connected graph node_corres = read_Pajek_corres_nodes(Pajek_file) # node_coords node_coords = coords[node_corres, :] node_labels = labels[node_corres].reshape(-1, 1) # where_in_gm_mask where_in_gm_mask = where_in_coords(node_coords, gm_coords) where_in_gm_mask = where_in_gm_mask.reshape(where_in_gm_mask.shape[0], 1) # print where_in_gm_mask print(where_in_gm_mask.shape) list_df.append( pd.DataFrame(np.concatenate( (where_in_gm_mask, node_labels, node_coords), axis=1), columns=[ 'Where_in_GM_mask', 'labels', 'MNI_x', 'MNI_y', 'MNI_z' ])) else: if not os.path.exists(coords_file): print("Missing {}".format(coords_file)) if not os.path.exists(Pajek_file): print("Missing {}".format(Pajek_file)) if not os.path.exists(labels_file): print("Missing {}".format(labels_file)) info_nodes_file = os.path.join(local_dir, net_prop_dir, "Z_List-info_nodes.txt") print(info_nodes_file) if os.path.exists(info_nodes_file): # loading info_nodes df_node_info = pd.read_table(info_nodes_file) list_df.append(df_node_info) # modules /community_vect partition_file = os.path.join(local_dir, "community_rada", "Z_List.lol") if os.path.exists(partition_file): # loading partition_file community_vect = read_lol_file(partition_file) list_df.append(pd.DataFrame(community_vect, columns=['Module'])) # node roles roles_file = os.path.join(local_dir, "node_roles", "node_roles.txt") part_coeff_file = os.path.join(local_dir, "node_roles", "all_participation_coeff.txt") Z_com_degree_file = os.path.join(local_dir, "node_roles", "all_Z_com_degree.txt") if os.path.exists(roles_file) and os.path.exists(part_coeff_file) and \ os.path.exists(Z_com_degree_file): # loding node roles node_roles = np.array(np.loadtxt(roles_file), dtype=int) part_coeff = np.loadtxt(part_coeff_file) part_coeff = part_coeff.reshape(part_coeff.shape[0], 1) Z_com_degree = np.loadtxt(Z_com_degree_file) Z_com_degree = Z_com_degree.reshape(Z_com_degree.shape[0], 1) list_df.append( pd.DataFrame(np.concatenate((node_roles, part_coeff, Z_com_degree), axis=1), columns=[ 'Role_quality', 'Role_quantity', 'Participation_coefficient', 'Z_community_degree' ])) # ndi values ndi_values_file = os.path.join(local_dir, "node_roles", "ndi_values.txt") if os.path.exists(ndi_values_file): # loding node roles ndi_values = np.array(np.loadtxt(ndi_values_file)) list_df.append( pd.DataFrame(ndi_values, columns=['Node_Dissociation_Index'])) return list_df