python-igraph union with current graph issue
python-igraph union with current graph issue
我有一个图子class。我的尝试是附加一个子间隙(也是 igraph.Graph
的一个实例)作为联合
import igraph
class Graph(igraph.Graph):
def __init__(self, param_1, param_2):
self.param_1 = param_1
self.param_2 = param_2
super(Graph, self).__init__()
def append_subgraph(self, subgraph):
self = igraph.union([self, subgraph])
def execute():
graph = Graph(None, None)
graph2 = Graph(None, None)
graph.append_subgraph(graph2)
print(graph)
if __name__ == '__main__':
execute()
当我在此 class 的实例上执行此方法时,出现错误
File "/usr/local/lib/python3.9/site-packages/igraph/operators.py", line 180, in union
res = _union(newgraphs, edgemaps)
TypeError: __init__() got an unexpected keyword argument '__ptr'
不过,当我合并 2 个单独的图表而不参考 self
时,这个问题不适用。
有没有人遇到过这个问题?
我将创建一个最小示例来重现此问题,然后向您展示如何修复它,但首先让我们检查图并集是否没有错误(一切都将在 ipython 中为我完成方便):
In [36]: import igraph as ig
...: ig.union((ig.Graph(), ig.Graph()))
Out[36]: <igraph.Graph at 0x24343faab80>
好的,我们知道联合有效,让我们尝试最小继承:
In [37]: class Graph(ig.Graph):
...: ...
...: ig.union((Graph(), Graph()))
Out[37]: <__main__.Graph at 0x24343faac70>
到目前为止,还不错。让我们尝试添加新的 __init__
:
In [38]: class Graph(ig.Graph):
...: def __init__(self, p, q):
...: self.p = p
...: self.q = q
...: super().__init__()
...: ig.union((Graph(1, 2), Graph(1, 2)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-68cf6a0e1e15> in <module>
4 self.q = q
5 super().__init__()
----> 6 ig.union((Graph(1, 2), Graph(1, 2)))
c:\python38\lib\site-packages\igraph\operators.py in union(graphs, byname)
178 # If any graph has any edge attributes, we need edgemaps
179 edgemaps = any(len(g.edge_attributes()) for g in graphs)
--> 180 res = _union(newgraphs, edgemaps)
181 if edgemaps:
182 graph_union = res["graph"]
TypeError: __init__() got an unexpected keyword argument '__ptr'
这是我们的错误。 igraph 期望 Graph
接受与 ig.Graph
相同的参数和关键字参数。这就是我们解决它的方法:
In [39]: class Graph(ig.Graph):
...: def __init__(self, *args, p=None, q=None, **kwargs):
...: self.p = p
...: self.q = q
...: super().__init__(*args, **kwargs)
...: ig.union((Graph(p=1, q=2), Graph(p=1, q=2)))
Out[39]: <__main__.Graph at 0x24343faa400>
我们已经更改了 __init__
的签名以接受任意位置参数,我们的特定参数作为关键字参数,然后是任意关键字参数。现在,igraph 可以传递它传递给 ig.Graph
s 的相同参数。希望这对您有所帮助!
我有一个图子class。我的尝试是附加一个子间隙(也是 igraph.Graph
的一个实例)作为联合
import igraph
class Graph(igraph.Graph):
def __init__(self, param_1, param_2):
self.param_1 = param_1
self.param_2 = param_2
super(Graph, self).__init__()
def append_subgraph(self, subgraph):
self = igraph.union([self, subgraph])
def execute():
graph = Graph(None, None)
graph2 = Graph(None, None)
graph.append_subgraph(graph2)
print(graph)
if __name__ == '__main__':
execute()
当我在此 class 的实例上执行此方法时,出现错误
File "/usr/local/lib/python3.9/site-packages/igraph/operators.py", line 180, in union
res = _union(newgraphs, edgemaps)
TypeError: __init__() got an unexpected keyword argument '__ptr'
不过,当我合并 2 个单独的图表而不参考 self
时,这个问题不适用。
有没有人遇到过这个问题?
我将创建一个最小示例来重现此问题,然后向您展示如何修复它,但首先让我们检查图并集是否没有错误(一切都将在 ipython 中为我完成方便):
In [36]: import igraph as ig
...: ig.union((ig.Graph(), ig.Graph()))
Out[36]: <igraph.Graph at 0x24343faab80>
好的,我们知道联合有效,让我们尝试最小继承:
In [37]: class Graph(ig.Graph):
...: ...
...: ig.union((Graph(), Graph()))
Out[37]: <__main__.Graph at 0x24343faac70>
到目前为止,还不错。让我们尝试添加新的 __init__
:
In [38]: class Graph(ig.Graph):
...: def __init__(self, p, q):
...: self.p = p
...: self.q = q
...: super().__init__()
...: ig.union((Graph(1, 2), Graph(1, 2)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-68cf6a0e1e15> in <module>
4 self.q = q
5 super().__init__()
----> 6 ig.union((Graph(1, 2), Graph(1, 2)))
c:\python38\lib\site-packages\igraph\operators.py in union(graphs, byname)
178 # If any graph has any edge attributes, we need edgemaps
179 edgemaps = any(len(g.edge_attributes()) for g in graphs)
--> 180 res = _union(newgraphs, edgemaps)
181 if edgemaps:
182 graph_union = res["graph"]
TypeError: __init__() got an unexpected keyword argument '__ptr'
这是我们的错误。 igraph 期望 Graph
接受与 ig.Graph
相同的参数和关键字参数。这就是我们解决它的方法:
In [39]: class Graph(ig.Graph):
...: def __init__(self, *args, p=None, q=None, **kwargs):
...: self.p = p
...: self.q = q
...: super().__init__(*args, **kwargs)
...: ig.union((Graph(p=1, q=2), Graph(p=1, q=2)))
Out[39]: <__main__.Graph at 0x24343faa400>
我们已经更改了 __init__
的签名以接受任意位置参数,我们的特定参数作为关键字参数,然后是任意关键字参数。现在,igraph 可以传递它传递给 ig.Graph
s 的相同参数。希望这对您有所帮助!