#!/usr/bin/env python from nose.tools import * from nose import SkipTest import networkx as nx import io import tempfile import os class TestGraph(object): @classmethod def setupClass(cls): try: import xml.etree.ElementTree except ImportError: raise SkipTest('xml.etree.ElementTree not available.') def setUp(self): self.simple_directed_data=""" """ self.simple_directed_graph=nx.DiGraph() self.simple_directed_graph.add_node('n10') self.simple_directed_graph.add_edge('n0','n2',id='foo') self.simple_directed_graph.add_edges_from([('n1','n2'), ('n2','n3'), ('n3','n5'), ('n3','n4'), ('n4','n6'), ('n6','n5'), ('n5','n7'), ('n6','n8'), ('n8','n7'), ('n8','n9'), ]) self.simple_directed_fh = \ io.BytesIO(self.simple_directed_data.encode('UTF-8')) self.attribute_data=""" yellow green blue red turquoise 1.0 1.0 2.0 1.1 """ self.attribute_graph=nx.DiGraph(id='G') self.attribute_graph.graph['node_default']={'color':'yellow'} self.attribute_graph.add_node('n0',color='green') self.attribute_graph.add_node('n2',color='blue') self.attribute_graph.add_node('n3',color='red') self.attribute_graph.add_node('n4') self.attribute_graph.add_node('n5',color='turquoise') self.attribute_graph.add_edge('n0','n2',id='e0',weight=1.0) self.attribute_graph.add_edge('n0','n1',id='e1',weight=1.0) self.attribute_graph.add_edge('n1','n3',id='e2',weight=2.0) self.attribute_graph.add_edge('n3','n2',id='e3') self.attribute_graph.add_edge('n2','n4',id='e4') self.attribute_graph.add_edge('n3','n5',id='e5') self.attribute_graph.add_edge('n5','n4',id='e6',weight=1.1) self.attribute_fh = io.BytesIO(self.attribute_data.encode('UTF-8')) self.simple_undirected_data=""" """ # self.simple_undirected_graph=nx.Graph() self.simple_undirected_graph.add_node('n10') self.simple_undirected_graph.add_edge('n0','n2',id='foo') self.simple_undirected_graph.add_edges_from([('n1','n2'), ('n2','n3'), ]) self.simple_undirected_fh = io.BytesIO(self.simple_undirected_data.encode('UTF-8')) def test_read_simple_directed_graphml(self): G=self.simple_directed_graph H=nx.read_graphml(self.simple_directed_fh) assert_equal(sorted(G.nodes()),sorted(H.nodes())) assert_equal(sorted(G.edges()),sorted(H.edges())) assert_equal(sorted(G.edges(data=True)), sorted(H.edges(data=True))) self.simple_directed_fh.seek(0) I=nx.parse_graphml(self.simple_directed_data) assert_equal(sorted(G.nodes()),sorted(I.nodes())) assert_equal(sorted(G.edges()),sorted(I.edges())) assert_equal(sorted(G.edges(data=True)), sorted(I.edges(data=True))) def test_write_read_simple_directed_graphml(self): G=self.simple_directed_graph fh=io.BytesIO() nx.write_graphml(G,fh) fh.seek(0) H=nx.read_graphml(fh) assert_equal(sorted(G.nodes()),sorted(H.nodes())) assert_equal(sorted(G.edges()),sorted(H.edges())) assert_equal(sorted(G.edges(data=True)), sorted(H.edges(data=True))) self.simple_directed_fh.seek(0) def test_read_simple_undirected_graphml(self): G=self.simple_undirected_graph H=nx.read_graphml(self.simple_undirected_fh) assert_equal(sorted(G.nodes()),sorted(H.nodes())) assert_equal( sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in H.edges())) self.simple_undirected_fh.seek(0) I=nx.parse_graphml(self.simple_undirected_data) assert_equal(sorted(G.nodes()),sorted(I.nodes())) assert_equal( sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in I.edges())) def test_read_attribute_graphml(self): G=self.attribute_graph H=nx.read_graphml(self.attribute_fh) assert_equal(sorted(G.nodes(True)),sorted(H.nodes(data=True))) ge=sorted(G.edges(data=True)) he=sorted(H.edges(data=True)) for a,b in zip(ge,he): assert_equal(a,b) self.attribute_fh.seek(0) I=nx.parse_graphml(self.attribute_data) assert_equal(sorted(G.nodes(True)),sorted(I.nodes(data=True))) ge=sorted(G.edges(data=True)) he=sorted(I.edges(data=True)) for a,b in zip(ge,he): assert_equal(a,b) def test_directed_edge_in_undirected(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_graphml,fh) assert_raises(nx.NetworkXError,nx.parse_graphml,s) def test_undirected_edge_in_directed(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_graphml,fh) assert_raises(nx.NetworkXError,nx.parse_graphml,s) def test_key_error(self): s=""" yellow green blue 1.0 """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_graphml,fh) assert_raises(nx.NetworkXError,nx.parse_graphml,s) def test_hyperedge_error(self): s=""" yellow green blue """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_graphml,fh) assert_raises(nx.NetworkXError,nx.parse_graphml,s) # remove test until we get the "name" issue sorted # https://networkx.lanl.gov/trac/ticket/544 def test_default_attribute(self): G=nx.Graph() G.add_node(1,label=1,color='green') G.add_path([0,1,2,3]) G.add_edge(1,2,weight=3) G.graph['node_default']={'color':'yellow'} G.graph['edge_default']={'weight':7} fh = io.BytesIO() nx.write_graphml(G,fh) fh.seek(0) H=nx.read_graphml(fh,node_type=int) assert_equal(sorted(G.nodes()),sorted(H.nodes())) assert_equal( sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in H.edges())) assert_equal(G.graph,H.graph) def test_multigraph_keys(self): # test that multigraphs use edge id attributes as key pass def test_multigraph_to_graph(self): # test converting multigraph to graph if no parallel edges are found pass def test_yfiles_extension(self): data=""" 1 2 """ fh = io.BytesIO(data.encode('UTF-8')) G=nx.read_graphml(fh) assert_equal(G.edges(),[('n0','n1')]) assert_equal(G['n0']['n1']['id'],'e0') assert_equal(G.node['n0']['label'],'1') assert_equal(G.node['n1']['label'],'2') H=nx.parse_graphml(data) assert_equal(H.edges(),[('n0','n1')]) assert_equal(H['n0']['n1']['id'],'e0') assert_equal(H.node['n0']['label'],'1') assert_equal(H.node['n1']['label'],'2') def test_unicode(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) node_type=str except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) node_type=unicode G.add_edge(name1, 'Radiohead', attr_dict={'foo': name2}) fd, fname = tempfile.mkstemp() nx.write_graphml(G, fname) H = nx.read_graphml(fname,node_type=node_type) assert_equal(G.adj, H.adj) os.close(fd) os.unlink(fname) def test_bool(self): s=""" false True False true false """ fh = io.BytesIO(s.encode('UTF-8')) G=nx.read_graphml(fh) assert_equal(G.node['n0']['test'],True) assert_equal(G.node['n2']['test'],False) H=nx.parse_graphml(s) assert_equal(H.node['n0']['test'],True) assert_equal(H.node['n2']['test'],False)