#!/usr/bin/env python from nose.tools import * from nose import SkipTest import networkx as nx import io class TestGEXF(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('0',label='Hello') self.simple_directed_graph.add_node('1',label='World') self.simple_directed_graph.add_edge('0','1',id='0') self.simple_directed_fh = \ io.BytesIO(self.simple_directed_data.encode('UTF-8')) self.attribute_data=""" Gephi.org A Web network true """ self.attribute_graph=nx.DiGraph() self.attribute_graph.graph['node_default']={'frog':True} self.attribute_graph.add_node('0', label='Gephi', url='http://gephi.org', indegree=1, frog=False) self.attribute_graph.add_node('1', label='Webatlas', url='http://webatlas.fr', indegree=2, frog=False) self.attribute_graph.add_node('2', label='RTGI', url='http://rtgi.fr', indegree=1, frog=True) self.attribute_graph.add_node('3', label='BarabasiLab', url='http://barabasilab.com', indegree=1, frog=True) self.attribute_graph.add_edge('0','1',id='0') self.attribute_graph.add_edge('0','2',id='1') self.attribute_graph.add_edge('1','0',id='2') self.attribute_graph.add_edge('2','1',id='3') self.attribute_graph.add_edge('0','3',id='4') 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('0',label='Hello') self.simple_undirected_graph.add_node('1',label='World') self.simple_undirected_graph.add_edge('0','1',id='0') 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_gexf(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) def test_write_read_simple_directed_graphml(self): G=self.simple_directed_graph fh=io.BytesIO() nx.write_gexf(G,fh) fh.seek(0) H=nx.read_gexf(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_gexf(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) def test_read_attribute_graphml(self): G=self.attribute_graph H=nx.read_gexf(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) def test_directed_edge_in_undirected(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_gexf,fh) def test_undirected_edge_in_directed(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_gexf,fh) def test_key_error(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError,nx.read_gexf,fh) def test_relabel(self): s=""" """ fh = io.BytesIO(s.encode('UTF-8')) G=nx.read_gexf(fh,relabel=True) assert_equal(sorted(G.nodes()),["Hello","Word"]) 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,foo=3) G.graph['node_default']={'color':'yellow'} G.graph['edge_default']={'foo':7} fh = io.BytesIO() nx.write_gexf(G,fh) fh.seek(0) H=nx.read_gexf(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())) # Reading a gexf graph always sets mode attribute to either # 'static' or 'dynamic'. Remove the mode attribute from the # read graph for the sake of comparing remaining attributes. del H.graph['mode'] assert_equal(G.graph,H.graph) def test_serialize_ints_to_strings(self): G=nx.Graph() G.add_node(1,id=7,label=77) fh = io.BytesIO() nx.write_gexf(G,fh) fh.seek(0) H=nx.read_gexf(fh,node_type=int) assert_equal(H.nodes(),[7]) assert_equal(H.node[7]['label'],'77') def test_write_with_node_attributes(self): # Addresses #673. G = nx.path_graph(4) for i in range(4): G.node[i]['id'] = i G.node[i]['label'] = i G.node[i]['pid'] = i expected = """ """ obtained = '\n'.join(nx.generate_gexf(G)) assert_equal( expected, obtained ) def test_bool(self): G=nx.Graph() G.add_node(1, testattr=True) fh = io.BytesIO() nx.write_gexf(G,fh) fh.seek(0) H=nx.read_gexf(fh,node_type=int) assert_equal(H.node[1]['testattr'], True)