def test_parse_pajet_mat(self): data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n""" G = nx.parse_pajek(data) assert set(G.nodes()) == {'one', 'two', 'three'} assert G.nodes['two'] == {'id': '2'} assert_edges_equal(set(G.edges()), {('one', 'one'), ('two', 'one'), ('two', 'two'), ('two', 'three')})
def test_noname(self): # Make sure we can parse a line such as: *network # Issue #952 line = "*network\n" other_lines = self.data.split('\n')[1:] data = line + '\n'.join(other_lines) G = nx.parse_pajek(data)
def pajek_to_files(name, url, pajek_lines, dir_name): if pajek_lines: try: G = nx.parse_pajek(pajek_lines) if not nx.is_empty(G): old_attributes = list(G.nodes) G = nx.convert_node_labels_to_integers(G) id_mapping = [] node_list = list(G.nodes) for i in range(len(node_list)): id_mapping.append([old_attributes[i], str(node_list[i])]) mapping_file = open('..' + dir_name + '/node_id_mappings/mapping_' + url.split('/')[-1] + '.csv', 'w', newline='') mapping_file_writer = csv.writer(mapping_file) mapping_file_writer.writerow(['id', 'name']) for tup in id_mapping: mapping_file_writer.writerow(list(tup)) nx.write_edgelist(G, '..' + dir_name + '/edge_lists/' + url.split('/')[-1] + '.csv', delimiter=',') insert_into_db( name, url, dir_name + '/edge_lists/' + url.split('/')[-1] + '.csv', dir_name + '/node_id_mappings/mapping_' + url.split('/')[-1] + '.csv', G.is_directed(), G.is_multigraph(), int(G.number_of_nodes()), int(nx.number_of_selfloops(G))) except Exception as e: traceback.print_exc() print(e) print("Couldn't parse " + url)
def test_read_pajek(self): G = nx.parse_pajek(self.data) Gin = nx.read_pajek(self.fname) assert sorted(G.nodes()) == sorted(Gin.nodes()) assert_edges_equal(G.edges(), Gin.edges()) assert self.G.graph == Gin.graph for n in G: assert G.nodes[n] == Gin.nodes[n]
def test_write_pajek(self): import io G = nx.parse_pajek(self.data) fh = io.BytesIO() nx.write_pajek(G, fh) fh.seek(0) H = nx.read_pajek(fh) assert_nodes_equal(list(G), list(H)) assert_edges_equal(list(G.edges()), list(H.edges()))
def test_parse_pajet_mat(self): data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n""" G = nx.parse_pajek(data) assert set(G.nodes()) == {"one", "two", "three"} assert G.nodes["two"] == {"id": "2"} assert_edges_equal( set(G.edges()), {("one", "one"), ("two", "one"), ("two", "two"), ("two", "three")}, )
def test_parse_pajek(self): G = nx.parse_pajek(self.data) assert sorted(G.nodes()) == ["A1", "Bb", "C", "D2"] assert_edges_equal( G.edges(), [ ("A1", "A1"), ("A1", "Bb"), ("A1", "C"), ("Bb", "A1"), ("C", "C"), ("C", "D2"), ("D2", "Bb"), ], )
def graphs_visualize_visjs(request, input_dict, output_dict, widget): import networkx as nx def prepare_visjs_data(g): nodes = [] for node in g.nodes_iter(): new = {'id': str(node), 'label': str(g.node[node]['label']) if 'label' in g.node[node] else str(node), 'shape': 'box' } nodes.append(new) edges = [] for fromnode, tonode, etype in g.edges_iter(keys=True): if 'label' in g[fromnode][tonode][etype]: label = str(g[fromnode][tonode][etype]['label']) elif 'weight' in g[fromnode][tonode][etype]: label = str(g[fromnode][tonode][etype]['weight']) else: #label = str(etype) label = '' new = {'from': str(fromnode), 'to': str(tonode), 'label': label, 'color': {'color': 'black', 'highlight': 'blue', 'hover': 'blue'}, } if nx.is_directed(g): new['arrows'] = 'to' edges.append(new) return {'nodes': nodes, 'edges': edges} # end g = input_dict['graph'] gtext = input_dict['gtext'] if not g: g = nx.parse_pajek(gtext) data = prepare_visjs_data(g) return render(request, 'visualizations/graphs_visualize_visjs.html', {'widget': widget, 'nodes': data['nodes'], 'edges': data['edges'] })
def pajek_to_files(name, url, pajek_lines): if pajek_lines: try: check_matrix = pajek_lines.find('*matrix') if check_matrix != -1: pajek_lines = pajek_lines[check_matrix + 6:].strip(' ').strip('\r').strip('\n') matrix_lines = pajek_lines.split('\r') numbers_exp = re.compile(r'[0-9]') append = ";" for i in range(len(matrix_lines)): if numbers_exp.search(matrix_lines[i]): matrix_lines[i] = matrix_lines[i].strip('\n') + append else: matrix_lines[i] = '' matrix_lines = list(filter(lambda x: x is not '', matrix_lines)) adj_matrix = " ".join(matrix_lines) adj_matrix = adj_matrix[:len(adj_matrix) - 1] # print(np.matrix(adj_matrix)) G = nx.from_numpy_array(np.matrix(adj_matrix)) else: G = nx.parse_pajek(pajek_lines) if not nx.is_empty(G): old_attributes = list(G.nodes) G = nx.convert_node_labels_to_integers(G) id_mapping = [] node_list = list(G.nodes) for i in range(len(node_list)): id_mapping.append([old_attributes[i], str(node_list[i])]) mapping_file = open('../pajek_networks/node_id_mappings/mapping_' + url.split('/')[-1] + '.csv', 'w', newline='') mapping_file_writer = csv.writer(mapping_file) mapping_file_writer.writerow(['id', 'name']) for tup in id_mapping: mapping_file_writer.writerow(list(tup)) nx.write_edgelist(G, '../pajek_networks/edge_lists/' + url.split('/')[-1] + '.csv', delimiter=',') utils.insert_into_db(name, url, '/pajek_networks/edge_lists/' + url.split('/')[-1] + '.csv', '/pajek_networks/node_id_mappings/mapping_' + url.split('/')[-1] + '.csv', G.is_directed(), G.is_multigraph(), int(G.number_of_nodes()), int(nx.number_of_selfloops(G))) except Exception as e: traceback.print_exc() print(e) print("Couldn't parse " + url)
def test_parse_pajek(self): G = nx.parse_pajek(self.data) assert sorted(G.nodes()) == ['A1', 'Bb', 'C', 'D2'] assert_edges_equal(G.edges(), [('A1', 'A1'), ('A1', 'Bb'), ('A1', 'C'), ('Bb', 'A1'), ('C', 'C'), ('C', 'D2'), ('D2', 'Bb')])
def test_parse_pajek_simple(self): # Example without node positions or shape data = """*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1""" G = nx.parse_pajek(data) assert sorted(G.nodes()) == ['1', '2'] assert_edges_equal(G.edges(), [('1', '2'), ('1', '2')])