-
Notifications
You must be signed in to change notification settings - Fork 1
/
gexf.py
801 lines (684 loc) · 27.6 KB
/
gexf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
"""
****
GEXF
****
Read and write graphs in GEXF format.
GEXF (Graph Exchange XML Format) is a language for describing complex
network structures, their associated data and dynamics.
This implementation does not support mixed graphs (directed and
unidirected edges together).
Format
------
GEXF is an XML format. See http://gexf.net/format/schema.html for the
specification and http://gexf.net/format/basic.html for examples.
"""
#from lxml import etree
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
# Based on GraphML NetworkX GraphML reader
__author__ = """\n""".join(['Aric Hagberg (hagberg@lanl.gov)'])
__all__ = ['write_gexf', 'read_gexf', 'relabel_gexf_graph']
import itertools
import networkx as nx
#from networkx.utils import _get_fh, make_str
#import utils
from utils import _get_fh, make_str
#import xml.etree.ElementTree as ET
from xml.etree.cElementTree import ElementTree, Element
def write_gexf(G, path, encoding='utf-8',prettyprint=True):
"""Write G in GEXF format to path.
"GEXF (Graph Exchange XML Format) is a language for describing
complex networks structures, their associated data and dynamics" [1]_.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
encoding : string (optional)
Encoding for text data.
prettyprint : bool (optional)
If True use line breaks and indenting in output XML.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_gexf(G, "test.gexf")
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together).
The node id attribute is set to be the string of the node label.
If you want to specify an id use set it as node data, e.g.
node['a']['id']=1 to set the id of node 'a' to 1.
References
----------
.. [1] GEXF graph format, http://gexf.net/format/
"""
fh = _get_fh(path, mode='wb')
writer = GEXFWriter(encoding=encoding,prettyprint=prettyprint)
writer.add_graph(G)
writer.write(fh)
def read_gexf(path,node_type=str,relabel=False):
"""Read graph in GEXF format from path.
"GEXF (Graph Exchange XML Format) is a language for describing
complex networks structures, their associated data and dynamics" [1]_.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
node_type: Python type (default: str)
Convert node ids to this type
relabel : bool (default: False)
If True relabel the nodes to use the GEXF node "label" attribute
instead of the node "id" attribute as the NetworkX node label.
Returns
-------
graph: NetworkX graph
If no parallel edges are found a Graph or DiGraph is returned.
Otherwise a MultiGraph or MultiDiGraph is returned.
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together).
References
----------
.. [1] GEXF graph format, http://gexf.net/format/
"""
fh=_get_fh(path,mode='rb')
reader = GEXFReader(node_type=node_type)
if relabel:
G=relabel_gexf_graph(reader(fh))
else:
G=reader(fh)
return G
class GEXF(object):
NS_GEXF = "http://www.gexf.net/1.1draft"
NS_VIZ = "http://www.gexf.net/1.1draft/viz"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
SCHEMALOCATION = \
' '.join(['http://www.gexf.net/1.1draft',
'http://www.gexf.net/1.1draft/gexf.xsd'
])
VERSION='1.1'
ET=ElementTree
#try:
# register_namespace = etree.register_namespace
#except AttributeError:
#def register_namespace(self, prefix, uri):
# etree._namespace_map[uri] = prefix
#etree.register_namespace('viz', NS_VIZ)
types=[(int,"integer"),
(float,"float"),
(float,"double"),
(bool,"boolean"),
(list,"string"),
(dict,"string"),
]
try: # Python 3.x
blurb = chr(1245) # just to trigger the exception
types.extend([
(str,"liststring"),
(str,"anyURI"),
(str,"string")])
except ValueError: # Python 2.6+
types.extend([
(str,"liststring"),
(str,"anyURI"),
(str,"string"),
(unicode,"liststring"),
(unicode,"anyURI"),
(unicode,"string")])
xml_type = dict(types)
python_type = dict(reversed(a) for a in types)
convert_bool={'true':True,'false':False}
class GEXFWriter(GEXF):
# class for writing GEXF format files
# use write_gexf() function
def __init__(self, encoding="utf-8",mode='static',prettyprint=True):
try:
import xml.etree.cElementTree
except ImportError:
raise ImportError('GEXF writer requires',
'xml.elementtree.ElementTree')
self.prettyprint=prettyprint
self.mode=mode
self.encoding = encoding
self.xml = Element("gexf",
{'xmlns':self.NS_GEXF,
'xmlns:xsi':self.NS_XSI,
'xmlns:viz':self.NS_VIZ,
'xsi:schemaLocation':self.SCHEMALOCATION,
'version':self.VERSION}
)
# counters for edge and attribute identifiers
self.edge_id=itertools.count()
self.attr_id=itertools.count()
# default attributes are stored in dictionaries
self.attr={}
self.attr['node']={}
self.attr['edge']={}
self.attr['node']['dynamic']={}
self.attr['node']['static']={}
self.attr['edge']['dynamic']={}
self.attr['edge']['static']={}
def add_graph(self, G):
# Add a graph element to the XML
if G.is_directed():
default='directed'
else:
default='undirected'
graph_element = Element("graph",defaultedgetype=default,mode=self.mode)
self.graph_element=graph_element
self.add_nodes(G,graph_element)
self.add_edges(G,graph_element)
self.xml.append(graph_element)
def add_nodes(self, G, graph_element):
nodes_element = Element('nodes')
for node,data in G.nodes_iter(data=True):
node_data=data.copy()
#
node_id=node_data.pop('id',make_str(node))
kw={'id':node_id}
label=node_data.pop('label',make_str(node))
kw['label']=label
pid=node_data.pop('pid',False)
if pid:
kw['pid']=pid
# add node element with attributes
node_element = Element("node", **kw)
# add node element and attr subelements
default=G.graph.get('node_default',{})
node_data=self.add_parents(node_element, node_data)
node_data=self.add_slices(node_element, node_data)
node_data=self.add_viz(node_element,node_data)
node_data=self.add_attributes("node", node_element,
node_data, default)
nodes_element.append(node_element)
graph_element.append(nodes_element)
def add_edges(self, G, graph_element):
def edge_key_data(G):
# helper function to unify multigraph and graph edge iterator
if G.is_multigraph():
for u,v,key,data in G.edges_iter(data=True,keys=True):
edge_data=data.copy()
edge_data.update(key=key)
edge_id=edge_data.pop('id',None)
if edge_id is None:
edge_id=self.edge_id.next()
yield u,v,edge_id,edge_data
else:
for u,v,data in G.edges_iter(data=True):
edge_data=data.copy()
edge_id=edge_data.pop('id',None)
if edge_id is None:
edge_id=next(self.edge_id)
yield u,v,edge_id,edge_data
edges_element = Element('edges')
for u,v,key,edge_data in edge_key_data(G):
kw={'id':make_str(key)}
edge_weight=edge_data.pop('weight',False)
if edge_weight:
kw['weight']=make_str(edge_weight)
edge_type=edge_data.pop('type',False)
if edge_type:
kw['type']=make_str(edge_type)
edge_element = Element("edge",
source=make_str(u),target=make_str(v),
**kw)
default=G.graph.get('edge_default',{})
edge_data=self.add_viz(edge_element,edge_data)
edge_data=self.add_attributes("edge", edge_element,
edge_data, default)
edges_element.append(edge_element)
graph_element.append(edges_element)
def add_attributes(self, node_or_edge, xml_obj, data, default):
# Add attrvalues to node or edge
attvalues=Element('attvalues')
if len(data)==0:
return data
if 'start' in data or 'end' in data:
mode='dynamic'
else:
mode='static'
for k,v in list(data.items()):
attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)],
node_or_edge, default, mode)
if type(v)==list:
# dynamic data
for val,start,end in v:
e=Element("attvalue")
e.attrib['for']=attr_id
e.attrib['value']=make_str(val)
e.attrib['start']=make_str(start)
e.attrib['end']=make_str(end)
attvalues.append(e)
else:
# static data
e=Element("attvalue")
e.attrib['for']=attr_id
e.attrib['value']=make_str(v)
attvalues.append(e)
xml_obj.append(attvalues)
return data
def get_attr_id(self, title, attr_type, edge_or_node, default, mode):
# find the id of the attribute or generate a new id
try:
return self.attr[edge_or_node][mode][title]
except KeyError:
# generate new id
self.attr_id.next()
new_id=str( self.attr_id )
self.attr[edge_or_node][mode][title] = new_id
attr_kwargs = {"id":new_id, "title":title, "type":attr_type}
attribute=Element("attribute",**attr_kwargs)
# add subelement for data default value if present
default_title=default.get(title)
if default_title is not None:
default_element=Element("default")
default_element.text=make_str(default_title)
attribute.append(default_element)
# new insert it into the XML
attributes_element=None
for a in self.graph_element.findall("attributes"):
# find existing attributes element by class and mode
a_class=a.get('class')
a_mode=a.get('mode','static') # default mode is static
if a_class==edge_or_node and a_mode==mode:
attributes_element=a
if attributes_element is None:
# create new attributes element
attr_kwargs = {"mode":mode,"class":edge_or_node}
attributes_element=Element('attributes', **attr_kwargs)
self.graph_element.insert(0,attributes_element)
attributes_element.append(attribute)
return new_id
def add_viz(self,element,node_data):
viz=node_data.pop('viz',False)
if viz:
color=viz.get('color')
if color is not None:
e=Element("{%s}color"%self.NS_VIZ,
r=str(color.get('r')),
g=str(color.get('g')),
b=str(color.get('b')),
)
element.append(e)
size=viz.get('size')
if size is not None:
e=Element("{%s}size"%self.NS_VIZ,value=str(size))
element.append(e)
thickness=viz.get('thickness')
if thickness is not None:
e=Element("{%s}thickness"%self.NS_VIZ,value=str(thickness))
element.append(e)
shape=viz.get('shape')
if shape is not None:
if shape.startswith('http'):
e=Element("{%s}shape"%self.NS_VIZ,
value='image',uri=str(shape))
else:
e=Element("{%s}shape"%self.NS_VIZ,value=str(shape.get))
element.append(e)
position=viz.get('position')
if position is not None:
e=Element("{%s}position"%self.NS_VIZ,
x=str(position.get('x')),
y=str(position.get('y')),
z=str(position.get('z')),
)
element.append(e)
return node_data
def add_parents(self,node_element,node_data):
parents=node_data.pop('parents',False)
if parents:
parents_element=Element('parents')
for p in parents:
e=Element('parent')
e.attrib['for']=str(p)
parents_element.append(e)
node_element.append(parents_element)
return node_data
def add_slices(self,node_element,node_data):
slices=node_data.pop('slices',False)
if slices:
slices_element=Element('slices')
for start,end in slices:
e=Element('slice',start=str(start),end=str(end))
slices_element.append(e)
node_element.append(slices_element)
return node_data
def write(self, fh):
# Serialize graph G in GEXF to the open fh
if self.prettyprint:
self.indent(self.xml)
document = ElementTree(self.xml)
header='<?xml version="1.0" encoding="%s"?>'%self.encoding
fh.write(header.encode(self.encoding))
document.write(fh, encoding=self.encoding)
def indent(self, elem, level=0):
# in-place prettyprint formatter
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
self.indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
class GEXFReader(GEXF):
# Class to read GEXF format files
# use read_gexf() function
def __init__(self, node_type=None):
try:
import xml.etree.cElementTree
except ImportError:
raise ImportError('GEXF reader requires',
'xml.elementtree.ElementTree')
self.node_type=node_type
# assume simple graph and test for multigraph on read
self.simple_graph=True
def __call__(self, stream):
self.xml = ElementTree(file=stream)
g=self.xml.find("{%s}graph" % self.NS_GEXF)
if g is None:
raise nx.NetworkXError("No <graph> element in GEXF file")
return self.make_graph(g)
def make_graph(self, graph_xml):
# mode is "static" or "dynamic"
graph_mode = graph_xml.get("mode", "")
self.dynamic=(graph_mode=='dynamic')
# start with empty DiGraph or MultiDiGraph
edgedefault = graph_xml.get("defaultedgetype", None)
if edgedefault=='directed':
G=nx.MultiDiGraph()
else:
G=nx.MultiGraph()
# graph attributes
graph_start=graph_xml.get('start')
if graph_start is not None:
G.graph['start']=graph_start
graph_end=graph_xml.get('end')
if graph_end is not None:
G.graph['end']=graph_end
# node and edge attributes
attributes_elements=graph_xml.findall("{%s}attributes"%self.NS_GEXF)
# dictionaries to hold attributes and attribute defaults
node_attr={}
node_default={}
edge_attr={}
edge_default={}
for a in attributes_elements:
attr_class = a.get("class")
if attr_class=='node':
na,nd = self.find_gexf_attributes(a)
node_attr.update(na)
node_default.update(nd)
G.graph['node_default']=node_default
elif attr_class=='edge':
ea,ed = self.find_gexf_attributes(a)
edge_attr.update(ea)
edge_default.update(ed)
G.graph['edge_default']=edge_default
else:
raise # unknown attribute class
# add nodes
nodes_element=graph_xml.find("{%s}nodes" % self.NS_GEXF)
if nodes_element is not None:
for node_xml in nodes_element.findall("{%s}node" % self.NS_GEXF):
self.add_node(G, node_xml, node_attr)
# add edges
edges_element=graph_xml.find("{%s}edges" % self.NS_GEXF)
if edges_element is not None:
for edge_xml in edges_element.findall("{%s}edge" % self.NS_GEXF):
self.add_edge(G, edge_xml, edge_attr)
# switch to Graph or DiGraph if no parallel edges were found.
if self.simple_graph:
if G.is_directed():
G=nx.DiGraph(G)
else:
G=nx.Graph(G)
return G
def add_node(self, G, node_xml, node_attr, node_pid=None):
# add a single node with attributes to the graph
# get attributes and subattributues for node
data = self.decode_attr_elements(node_attr, node_xml)
data = self.add_parents(data, node_xml) # add any parents
data = self.add_slices(data, node_xml) # add slices
data = self.add_viz(data, node_xml) # add viz
data = self.add_start_end(data, node_xml) # add start/end
# find the node id and cast it to the appropriate type
node_id = node_xml.get("id")
if self.node_type is not None:
node_id=self.node_type(node_id)
# every node should have a label
node_label = node_xml.get("label")
data['label']=node_label
# parent node id
node_pid = node_xml.get("pid", node_pid)
if node_pid is not None:
data['pid']=node_pid
# check for subnodes, recursive
subnodes=node_xml.find("{%s}nodes" % self.NS_GEXF)
if subnodes is not None:
for node_xml in subnodes.findall("{%s}node" % self.NS_GEXF):
self.add_node(G, node_xml, node_attr, node_pid=node_id)
G.add_node(node_id, data)
def add_start_end(self, data, xml):
# start and end times
node_start = xml.get("start")
if node_start is not None:
data['start']=node_start
node_end = xml.get("end")
if node_end is not None:
data['end']=node_end
return data
def add_viz(self, data, node_xml):
# add viz element for node
viz={}
color=node_xml.find("{%s}color"%self.NS_VIZ)
if color is not None:
viz['color']={'r':int(color.get('r')),
'g':int(color.get('g')),
'b':int(color.get('b'))}
size=node_xml.find("{%s}size"%self.NS_VIZ)
if size is not None:
viz['size']=float(size.get('value'))
thickness=node_xml.find("{%s}thickness"%self.NS_VIZ)
if thickness is not None:
viz['thickness']=float(thickness.get('value'))
shape=node_xml.find("{%s}shape"%self.NS_VIZ)
if shape is not None:
viz['shape']=shape.get('shape')
if viz['shape']=='image':
viz['shape']=shape.get('uri')
position=node_xml.find("{%s}position"%self.NS_VIZ)
if position is not None:
viz['position']={'x':float(position.get('x',0)),
'y':float(position.get('y',0)),
'z':float(position.get('z',0))}
if len(viz)>0:
data['viz']=viz
return data
def add_parents(self, data, node_xml):
parents_element=node_xml.find("{%s}parents"%self.NS_GEXF)
if parents_element is not None:
data['parents']=[]
for p in parents_element.findall("{%s}parent"%self.NS_GEXF):
parent=p.get('for')
data['parents'].append(parent)
return data
def add_slices(self, data, node_xml):
slices_element=node_xml.find("{%s}slices"%self.NS_GEXF)
if slices_element is not None:
data['slices']=[]
for s in slices_element.findall("{%s}slice"%self.NS_GEXF):
start=s.get('start')
end=s.get('end')
data['slices'].append((start,end))
return data
def add_edge(self, G, edge_element, edge_attr):
# add an edge to the graph
# raise error if we find mixed directed and undirected edges
edge_direction = edge_element.get("type")
if G.is_directed() and edge_direction=='undirected':
raise nx.NetworkXError(\
"Undirected edge found in directed graph.")
if (not G.is_directed()) and edge_direction=='directed':
raise nx.NetworkXError(\
"Directed edge found in undirected graph.")
# Get source and target and recast type if required
source = edge_element.get("source")
target = edge_element.get("target")
if self.node_type is not None:
source=self.node_type(source)
target=self.node_type(target)
data = self.decode_attr_elements(edge_attr, edge_element)
data = self.add_start_end(data,edge_element)
# GEXF stores edge ids as an attribute
# NetworkX uses them as keys in multigraphs too
edge_id = edge_element.get("id")
if edge_id is not None:
data["id"] = edge_id
weight = edge_element.get('weight')
if weight is not None:
data['weight']=float(weight)
edge_label = edge_element.get("label")
if edge_label is not None:
data['label']=edge_label
if G.has_edge(source,target):
# seen this edge before - this is a multigraph
self.simple_graph=False
G.add_edge(source, target, key=edge_id, **data)
if edge_direction=='mutual':
G.add_edge(target, source, key=edge_id, **data)
def decode_attr_elements(self, gexf_keys, obj_xml):
# Use the key information to decode the attr XML
attr = {}
# look for outer "<attvalues>" element
attr_element=obj_xml.find("{%s}attvalues" % self.NS_GEXF)
if attr_element is not None:
# loop over <attvalue> elements
for a in attr_element.findall("{%s}attvalue" % self.NS_GEXF):
key = a.get('for') # for is required
try: # should be in our gexf_keys dictionary
title=gexf_keys[key]['title']
except KeyError:
raise nx.NetworkXError("No attribute defined for=%s"%key)
atype=gexf_keys[key]['type']
value=a.get('value')
if atype=='boolean':
value=self.convert_bool[value]
else:
value=self.python_type[atype](value)
if gexf_keys[key]['mode']=='dynamic':
# for dynamic graphs use list of three-tuples
# [(value1,start1,end1), (value2,start2,end2), etc]
start=a.get('start')
end=a.get('end')
if title in attr:
attr[title].append((value,start,end))
else:
attr[title]=[(value,start,end)]
else:
# for static graphs just assign the value
attr[title] = value
return attr
def find_gexf_attributes(self, attributes_element):
# Extract all the attributes and defaults
attrs = {}
defaults = {}
mode=attributes_element.get('mode')
for k in attributes_element.findall("{%s}attribute" % self.NS_GEXF):
attr_id = k.get("id")
title=k.get('title')
atype=k.get('type')
attrs[attr_id]={'title':title,'type':atype,'mode':mode}
# check for the "default" subelement of key element and add
default=k.find("{%s}default" % self.NS_GEXF)
if default is not None:
if atype=='boolean':
value=self.convert_bool[default.text]
else:
value=self.python_type[atype](default.text)
defaults[title]=value
return attrs,defaults
def relabel_gexf_graph(G):
"""Relabel graph using "label" node keyword for node label.
Parameters
----------
G : graph
A NetworkX graph read from GEXF data
Returns
-------
H : graph
A NetworkX graph with relabed nodes
Notes
-----
This function relabels the nodes in a NetworkX graph with the
"label" attribute. It also handles relabeling the specific GEXF
node attributes "parents", and "pid".
"""
# build mapping of node labels, do some error checking
try:
mapping=[(u,G.node[u]['label']) for u in G]
except KeyError:
raise nx.NetworkXError('Failed to relabel nodes: '
'missing node labels found. '
'Use relabel=False.')
x,y=zip(*mapping)
if len(set(y))!=len(G):
raise nx.NetworkXError('Failed to relabel nodes: '
'duplicate node labels found. '
'Use relabel=False.')
mapping=dict(mapping)
H=nx.relabel_nodes(G,mapping)
# relabel attributes
for n in G:
m=mapping[n]
H.node[m]['id']=n
if 'pid' in H.node[m]:
H.node[m]['pid']=mapping[G.node[n]['pid']]
if 'parents' in H.node[m]:
H.node[m]['parents']=[mapping[p] for p in G.node[n]['parents']]
return H
# fixture for nose tests
def setup_module(module):
from nose import SkipTest
try:
import xml.etree.cElementTree
except:
raise SkipTest("xml.etree.cElementTree not available")
# fixture for nose tests
def teardown_module(module):
import os
os.unlink('test.gexf')