【问题标题】:Assigning default namespace in networkx.write_gexf在 networkx.write_gexf 中分配默认命名空间
【发布时间】:2015-02-12 19:08:32
【问题描述】:

将 python 的 networkx 库生成的这段代码作为有效的 GEXF 文件提供,我在文档中找不到任何地方我将 xmlns:ns0 更改为 xmlns:viz... GEXF 兼容的命名空间。

<?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 部分,但我也不知道我在哪里做的 - 所以我在这里问。

networkx.write_gexf(G,f) # G is the graph and f is the file to write.

(已编辑): 节点说 ns0:... 而不是 viz:... 如 GEXF 文档中所示。这会导致与使用 viz 参数的其他 GEXF 库的兼容性问题(并且找不到它们)。

【问题讨论】:

  • xmlns:ns0="http://www.gexf.net/1.1draft/viz" - xmlns:ns0xmlns:viz 都在那里并指向gexf/.../viz。我错过了什么吗?
  • 我编辑了这个例子——正如你在节点部分看到的那样,它使用 ns0 作为默认命名空间,而不是 viz。
  • @MarcMaxson:你知道如何改变命名空间了吗?我遇到了同样的问题...
  • 我认为这是 networkx 中的一个错误
  • networkx 2.1 似乎发生了很大变化,包括这个错误。标题现在有 xmlns:viz 而不是 xmlns:ns0 但控制边缘厚度和颜色仍然没有很好的记录。我确实发现如果你给一个节点一个 viz:color 它将继承到节点的边缘。

标签: python networkx


【解决方案1】:

我遇到了同样的问题,我在 gexf.py 的 GEXFWriter 类中更新了“add_viz”函数,如下所示:

 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 库/工具的兼容性问题。

最好的, ZP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2020-10-02
    • 2020-12-26
    • 2014-04-16
    • 2010-09-05
    • 2011-07-18
    相关资源
    最近更新 更多