如何使用用户定义的类对象作为networkx节点? [英] How to use user-defined class object as a networkx node?

查看:171
本文介绍了如何使用用户定义的类对象作为networkx节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类点的定义为(其中也包含一些方法,属性和东西,但这只是最小的一部分):

Class point is defined as (there are also some methods, atributes, and stuff in it, but this is minimal part):

class point():
    def ___init___(self, x, y):
        self.x = x
        self.y = y

因此,我看到

So, I saw this question, but when I tried applying it, it returns an error:

G = nx.Graph()
p = point(0,0)
G.add_node(0, p)

NetworkXError:attr_dict参数必须是字典.

NetworkXError: The attr_dict argument must be a dictionary.

如果我使用

G = nx.Graph()
p = point(0,0)
G.add_node(0, data = p)

我没有收到错误,但是当我尝试访问x坐标时,事实证明它没有将其保存为点.

I don't get an error, but when i try to access the x-coordinate, it turns out it didn't save it as a point.

G[0].x

返回:AttributeError:'dict'对象没有属性'x'

returns: AttributeError: 'dict' object has no attribute 'x'

G = nx.Graph()
G.add_node(0, data = point(0,0))
G[0]

返回: {}

这意味着它仍将其另存为字典.

which means it still saves it as a dictionary.

我看到我可以使点成为可散列的,并将这些对象用作节点,所以我添加了属性ID,因为点将移动.我将其添加到类中,并__repr__来绘制漂亮的图形:

I saw I can make my points hashable, and use these objects as nodes, so i added atribute id, since points are going to move. I added this to the class, and __repr__ for nice drawing of the graphs:

def __hash__(self):
    return self.id_n
def __cmp__(self, p):
    if self.id_n < p.id_n: return -1
    elif self.id_n == p.id_n: return 0
    else: return 1
def __eq__(self, p):
    if p.id_n == self.id_n: return True
    else: return False
def __repr__(self):
    return str(self.id_n) 

但这有点奇怪,因为我当时不知道如何通过

but that is a bit wierd, since I don't understand how to select a node then, by

G[<what should i put here?>]

所以,问题是,什么是正确的方法?

So, question is, what is a proper way to do this?

我希望能够使用类似的东西

I hoped to be able to use something like

G[node_id].some_method(some_args)

推荐答案

您正在查看G[0].但这不是您想要的. G[0]包含有关节点0的邻居和边缘属性的信息,但不包含有关节点0的属性的信息.

You're looking at G[0]. But that's not what you want. G[0] contains the information about neighbors of node 0 and the attributes of the edges, but nothing about the attributes of node 0.

class point():
    def __init__(self, x, y):
        self.x = x
        self.y = y

import networkx as nx
G = nx.Graph()
p0 = point(0,0)
p1 = point(1,1)

G.add_node(0, data=p0)
G.add_node(1, data=p1)
G.add_edge(0,1, weight=4)
G[0]
> AtlasView({1: {'weight': 4}})  #in networkx 1.x this is actually a dict. In 2.x it is an "AtlasView"

对于networkx,期望一个节点可能有很多与其关联的数据.在您的情况下,您只有一条数据,即该点.但是您也可能已经指定了颜色,权重,时间,年龄等.因此,networkx将所有属性存储在另一个字典中,但是该字典是通过G.node[0]而不是G[0]访问的.

For networkx there is an expectation that a node may have a lot of data associated with it. In your case, you only have a single piece of data, namely the point. But you could have also assigned a color, a weight, a time, an age, etc. So networkx is going to store all the attributes in another dictionary, but that dictionary is accessed through G.node[0] rather than G[0].

G.node[0]
> {'data': <__main__.point at 0x11056da20>}
G.node[0]['data'].x
> 0

请注意,输入中的data变为字符串'data'.

Notice that data in your input becomes the string 'data'.

输入诸如G.add_node(0, x=0, y=0)之类的节点可能会更好,然后您可以将其输入为G.node[0]['x'].

It might be better to enter the nodes like G.add_node(0, x=0, y=0) and then you can access the entries as G.node[0]['x'].

这篇关于如何使用用户定义的类对象作为networkx节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆