Graph-Tool:Edge属性无法将其设置为type'对象'

发布于 2025-01-31 21:21:53 字数 1467 浏览 3 评论 0原文

我已经使用networkx,并且正在尝试Graph-tool,以获得更好的性能。我希望通过文档来创建具有属性的边缘。在networkx中,我可以为某些图形G

G.add_edge(vertex_1, vertex_2, attr_dict={python dict of 'property: value'})

我想在Graph-tool中执行相同的操作。来自我尝试了一个示例,并进行了略有更改,该示例是python dict 而不是 int 中的一个属性:

edge_list = [(0, 1, .3, {'name':'hello'}), (2, 3, .1,  {'name':'good'}), (2, 0, .4,  {'name':'by'})]
g = gt.Graph()
eweight = g.new_ep("double")
elayer = g.new_ep("object")
g.add_edge_list(edge_list, eprops=[eweight, elayer])
print(eweight.a)
print(elayer.a)
g.get_edges()

OUTPUT:

[0.3 0.1 0.4]
None
array([[0, 1],
       [2, 3],
       [2, 0]])

我可以看到正确输入了边缘,为什么是'没有人返回“ Elayer”属性?

更新

我已经查看了属性映射 docs ,那里的示例看起来像这样:

eprop_dict = g.new_edge_property("object")                # Arbitrary Python object.
e = g.edges().next()
eprop_dict[e] = {"foo": "bar", "gnu": 42}                 # In this case, a dict.

还可以查看源代码

I have worked with networkx and am trying graph-tool for potentially better performance. Working through the documentation I wish to create edges with properties. In networkx I can do something like this for some graph G:

G.add_edge(vertex_1, vertex_2, attr_dict={python dict of 'property: value'})

I wanted to do the same in graph-tool. From the docs I tried the example with a slight change in that one of the properties in a python dict instead of an int:

edge_list = [(0, 1, .3, {'name':'hello'}), (2, 3, .1,  {'name':'good'}), (2, 0, .4,  {'name':'by'})]
g = gt.Graph()
eweight = g.new_ep("double")
elayer = g.new_ep("object")
g.add_edge_list(edge_list, eprops=[eweight, elayer])
print(eweight.a)
print(elayer.a)
g.get_edges()

OUTPUT:

[0.3 0.1 0.4]
None
array([[0, 1],
       [2, 3],
       [2, 0]])

I can see that the edges were entered correctly why is 'None' returned for the 'elayer' property?

UPDATE:

I have looked at the Property Map docs and the example there looks like this:

eprop_dict = g.new_edge_property("object")                # Arbitrary Python object.
e = g.edges().next()
eprop_dict[e] = {"foo": "bar", "gnu": 42}                 # In this case, a dict.

Also had a look at the source code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-07 21:21:53

让我们看一下文档告诉我们的内容:

help(elayer)

这给了我们:

a
    Shortcut to the :meth:`~PropertyMap.get_array` method
    as an attribute. This makes assignments more convenient, e.g.:
        
    >>> g = gt.Graph()
    >>> g.add_vertex(10)
    <...>
    >>> prop = g.new_vertex_property("double")
    >>> prop.a = np.random.random(10)           # Assignment from array

现在让我们查看propertymap.get_array的文档

help(elayer.get_array)


get_array() method of graph_tool.EdgePropertyMap instance
    Get a :class:`numpy.ndarray` subclass (:class:`~graph_tool.PropertyArray`)
    with the property values.
    
    .. note::
    
       An array is returned *only if* the value type of the property map is
       a scalar. For vector, string or object types, ``None`` is returned
       instead. For vector and string objects, indirect array access is
       provided via the :func:`~graph_tool.PropertyMap.get_2d_array()` and
       :func:`~graph_tool.PropertyMap.set_2d_array()` member functions.

上面的注释回答您的问题:数组接口仅适用于标量类型,而不是标量任意python对象。

对于任意类型,使用基于边缘描述符的标准接口仍然可以进行属性的分配和查找,例如:

for e in g.edges():
    print(elayer[e])

它为您提供

{'name': 'hello'}
{'name': 'good'}
{'name': 'by'}

Let's look at what the documentation tells us:

help(elayer)

This gives us:

a
    Shortcut to the :meth:`~PropertyMap.get_array` method
    as an attribute. This makes assignments more convenient, e.g.:
        
    >>> g = gt.Graph()
    >>> g.add_vertex(10)
    <...>
    >>> prop = g.new_vertex_property("double")
    >>> prop.a = np.random.random(10)           # Assignment from array

Now let us look at the documentation of PropertyMap.get_array:

help(elayer.get_array)


get_array() method of graph_tool.EdgePropertyMap instance
    Get a :class:`numpy.ndarray` subclass (:class:`~graph_tool.PropertyArray`)
    with the property values.
    
    .. note::
    
       An array is returned *only if* the value type of the property map is
       a scalar. For vector, string or object types, ``None`` is returned
       instead. For vector and string objects, indirect array access is
       provided via the :func:`~graph_tool.PropertyMap.get_2d_array()` and
       :func:`~graph_tool.PropertyMap.set_2d_array()` member functions.

The note above answers your question: the array interface is provided only for scalar types, not arbitrary python objects.

For arbitrary types, assignment and lookup of properties is still possible using the standard interface based on edge descriptors, e.g.:

for e in g.edges():
    print(elayer[e])

which gives you

{'name': 'hello'}
{'name': 'good'}
{'name': 'by'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文