【问题标题】:Is there a way to cast a gremlinpython Graph to a networkx Graph有没有办法将 gremlinpython Graph 转换为 networkx Graph
【发布时间】:2021-02-19 16:51:38
【问题描述】:

我想将 gremlinpython 图表 (gremlin_python.structure.graph.Graph) 转换为 networkx 图表。

我发现使用包 ipython-gremlin (https://github.com/davebshow/ipython-gremlin) 可以将顶点转换为 pandas 数据帧,将边转换为networkx.classes.multidigraph.MultiDiGraph,但该包已过时,我找不到代码位置他们在其上进行转换。有没有办法进行转换?

谢谢!

【问题讨论】:

    标签: python networkx gremlin janusgraph gremlinpython


    【解决方案1】:

    您可能需要编写一些代码来执行此操作。在使用subgraph 步骤返回我感兴趣的图形部分之前,我已经完成了它,然后迭代创建 NetworkX 顶点和边。这很简单。唯一需要注意的问题是当前的 Gremlin Python 客户端使用默认的 Tornado 最大结果帧大小,我相信它是 10mb,因此如果您的子图结果超过该值,您将遇到异常。这是一个简单的示例,它在航线数据集中找到来自 SAF 机场的航线星图,并从中创建 NetworkX DiGraph

    import networkx as nx
    G = nx.DiGraph()
    sg = g.V().has('code','SAF').outE().subgraph('sg').cap('sg').next()
    for e in sg['@value']['edges']:
        G.add_edge(e.outV.id,e.inV.id,elabel=e.label)
    
    print(G.nodes())
    print(G.edges())
    

    运行时的结果是

    ['44', '13', '20', '31', '8']
    [('44', '13'), ('44', '20'), ('44', '31'), ('44', '8')]
    

    【讨论】:

    • 非常感谢!这就像一个魅力!
    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2014-11-06
    • 2017-03-25
    • 2021-09-01
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多