def test_copy_of_view(self): G = nx.OrderedMultiGraph(self.MGv) assert G.__class__.__name__ == 'OrderedMultiGraph' G = G.copy(as_view=True) assert G.__class__.__name__ == 'OrderedMultiGraph'
def test_multigraph(self): G = nx.OrderedMultiGraph()
def matching( paths, atlas, namer_dir, ): import glob import networkx as nx import numpy as np from pynets.core import thresholding from pynets.statistics.utils import parse_closest_ixs from graspologic.utils import remove_loops, symmetrize, \ multigraph_lcc_intersection [dwi_graph_path, func_graph_path] = paths dwi_mat = np.load(dwi_graph_path) func_mat = np.load(func_graph_path) dwi_mat = thresholding.autofix(symmetrize(remove_loops(dwi_mat))) func_mat = thresholding.autofix(symmetrize(remove_loops(func_mat))) dwi_mat = thresholding.standardize(dwi_mat) func_mat = thresholding.standardize(func_mat) node_dict_dwi = parse_closest_ixs( glob.glob(f"{str(Path(dwi_graph_path).parent.parent)}" f"/nodes/*.json"), dwi_mat.shape[0])[1] node_dict_func = parse_closest_ixs( glob.glob(f"{str(Path(func_graph_path).parent.parent)}" f"/nodes/*.json"), func_mat.shape[0])[1] G_dwi = nx.from_numpy_array(dwi_mat) nx.set_edge_attributes(G_dwi, 'structural', nx.get_edge_attributes(G_dwi, 'weight').values()) nx.set_node_attributes(G_dwi, dict(node_dict_dwi), name='dwi') #G_dwi.nodes(data=True) G_func = nx.from_numpy_array(func_mat) nx.set_edge_attributes(G_func, 'functional', nx.get_edge_attributes(G_func, 'weight').values()) nx.set_node_attributes(G_func, dict(node_dict_func), name='func') #G_func.nodes(data=True) R = G_dwi.copy() R.remove_nodes_from(n for n in G_dwi if n not in G_func) R.remove_edges_from(e for e in G_dwi.edges if e not in G_func.edges) G_dwi = R.copy() R = G_func.copy() R.remove_nodes_from(n for n in G_func if n not in G_dwi) R.remove_edges_from(e for e in G_func.edges if e not in G_dwi.edges) G_func = R.copy() [G_dwi, G_func] = multigraph_lcc_intersection([G_dwi, G_func]) def writeJSON(metadata_str, outputdir): import json import uuid modality = metadata_str.split('modality-')[1].split('_')[0] metadata_list = [ i for i in metadata_str.split('modality-')[1].split('_') if '-' in i ] hash = str(uuid.uuid4()) filename = f"{outputdir}/sidecar_modality-{modality}_{hash}.json" metadata_dict = {} for meta in metadata_list: k, v = meta.split('-') metadata_dict[k] = v with open(filename, 'w+') as jsonfile: json.dump(metadata_dict, jsonfile, indent=4) jsonfile.close() return hash dwi_name = dwi_graph_path.split("/")[-1].split(".npy")[0] func_name = func_graph_path.split("/")[-1].split(".npy")[0] dwi_hash = writeJSON(dwi_name, namer_dir) func_hash = writeJSON(func_name, namer_dir) name = f"{atlas}_mplx_layer1-dwi_ensemble-{dwi_hash}_" \ f"layer2-func_ensemble-{func_hash}" dwi_opt, func_opt, best_mi = optimize_mutual_info( nx.to_numpy_array(G_dwi), nx.to_numpy_array(G_func), bins=50) func_mat_final = list(func_opt.values())[0] dwi_mat_final = list(dwi_opt.values())[0] G_dwi_final = nx.from_numpy_array(dwi_mat_final) G_func_final = nx.from_numpy_array(func_mat_final) G_multi = nx.OrderedMultiGraph(nx.compose(G_dwi_final, G_func_final)) out_name = f"{name}_matchthr-{list(dwi_opt.keys())[0]}_" \ f"{list(func_opt.keys())[0]}" mG = build_mx_multigraph(nx.to_numpy_array(G_func_final), nx.to_numpy_array(G_dwi_final), out_name, namer_dir) mG_nx = f"{namer_dir}/{out_name}.gpickle" nx.write_gpickle(G_multi, mG_nx) dwi_file_out = f"{namer_dir}/{dwi_name}.npy" func_file_out = f"{namer_dir}/{func_name}.npy" np.save(dwi_file_out, dwi_mat_final) np.save(func_file_out, func_mat_final) return mG_nx, mG, dwi_file_out, func_file_out