在networkx.write_gexf中分配默认名称空间 [英] Assigning default namespace in networkx.write_gexf
问题描述
考虑到python networkx库生成的这段代码作为有效的GEXF文件,我无法在文档中找到将xmlns:ns0更改为xmlns:viz ...符合GEXF的命名空间的任何地方.
Give this bit of code that python's networkx library generates as a valid GEXF file, I cannot find anywhere in the docs where I change the xmlns:ns0 to be instead xmlns:viz... the GEXF compliant namespace.
<?xml version="1.0" encoding="utf-8"?><gexf xmlns:ns0="http://www.gexf.net/1.1draft/viz"
version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="directed" mode="static">
<attributes class="node" mode="static">
<attribute id="0" title="origin" type="double" />
<attribute id="1" title="size" type="integer" />
</attributes>
<nodes>
<node id="0" label="Vijana Amani Pamoja (VAP)">
<ns0:color b="70" g="11" r="160" />
<ns0:size value="10" />
<attvalues>
<attvalue for="0" value="1.25" />
<attvalue for="1" value="10" />
</attvalues>
</node>
在某些地方,我可能已经覆盖了networkx的write_gexf函数的默认名称空间的VIZ部分,但是我也不知道我在哪里做了-所以我在这里问.
Somewhere I might have overridden the VIZ part of the default namespace for networkx's write_gexf function, but I don't know where I did that either - so I am asking here.
networkx.write_gexf(G,f) # G is the graph and f is the file to write.
(已编辑): 节点说的是ns0:...,而不是viz:...,如GEXF文档所示.这会导致与其他使用viz参数(但找不到它们)的GEXF库的兼容性问题.
(EDITED): The nodes say ns0:... and not viz:... as shown in the GEXF documents. This causes compatability problems with other GEXF libraries that use the viz parameters (and cannot find them).
推荐答案
我遇到了同样的问题,并在gexf.py中的GEXFWriter类中更新了"add_viz"函数,如下所示:
I had the same problem and I updated "add_viz" function in class GEXFWriter in gexf.py as follows:
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("viz:color")
e.attrib['r']=str(color.get('r'))
e.attrib['g']=str(color.get('g'))
e.attrib['b']=str(color.get('b'))
if self.VERSION!='1.1':
e.attrib['a']=str(color.get('a'))
e.text=" "
element.append(e)
size=viz.get('size')
if size is not None:
e=Element("viz:size")
e.attrib['value']=str(size)
e.text=" "
element.append(e)
thickness=viz.get('thickness')
if thickness is not None:
e=Element("viz:thickness")
e.attrib['value']=str(thickness)
e.text=" "
element.append(e)
shape=viz.get('shape')
if shape is not None:
if shape.startswith('http'):
e=Element("viz:shape")
e.attrib['value']= 'image'
e.attrib['uri']= str(shape)
else:
e=Element("viz:shape")
e.attrib['value']= str(shape)
e.text=" "
element.append(e)
position=viz.get('position')
if position is not None:
e=Element("viz:position")
e.attrib['x']= str(position.get('x'))
e.attrib['y']= str(position.get('y'))
e.attrib['z']= str(position.get('z'))
e.text=" "
element.append(e)
return node_data
这为我使用的GEXF库/工具解决了兼容性问题.
This solved the compatibility problem for me with the GEXF libraries/tools that I use.
最佳, ZP
这篇关于在networkx.write_gexf中分配默认名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!