从graph_tool包中继承Graph [英] Subclassing Graph from the graph_tool package
问题描述
我正在尝试从graph_tool
包中为Graph
子类化,以便在Python中进行某些图形分析(以便我可以生成自己的某些函数,但仍仍使用Graph_Tool的函数),但似乎无法使用graph_tool
的图形生成器方法.
I am attempting to sub-class Graph
from the graph_tool
package for some graph analysis in Python (so that I can generate some of my own functions, but still use Graph_Tool's functions as well), and I cannot seem to use graph_tool
's graph generator methods.
我首先导入我的课程:
import graph_tool.all as gt
import numpy.random as np
np.seed(42)
我尝试了__init__
方法的各种版本:
I've tried various versions of the __init__
method:
-
从头开始构建图形.这行得通,但我不想 使用它,因为
graph_tool
有一些不错的预填充方法 您的图表(请参见下面的2.和3.).
Build a graph from scratch. This works, but I'd prefer not to use this, because
graph_tool
has some nice ways to pre-populate your graphs (see 2. and 3. below).
class myGraph(gt.Graph):
def __init__(self):
super(myGraph, self).__init__()
self.add_vertex(4)
使用graph_tool
图生成器方法.这将在函数内部生成一个gt.Graph
对象.但是,当我尝试在函数外部打印对象时,出现错误.
Use graph_tool
graph generator methods. This generates a gt.Graph
object inside the function. But when I try to print the object outside the function, I get an error.
class myGraph(gt.Graph):
def __init__(self):
self = gt.collection.data['celegansneural']
print self
g = myGraph()
print g
上面的代码返回(注意第一行是我的 init 方法中print self
的结果):
The above code returns (note the first line is the result of print self
in my `init method):
<Graph object, directed, with 297 vertices and 2359 edges at 0x1049d2a50>
Traceback (most recent call last): <br>
File "Graph_Tool.py", line 54, in <module> <br>
print g <br>
File "/usr/local/lib/python2.7/site-packages/graph_tool/__init__.py", line 1683, in __repr__ <br>
d = "directed" if self.is_directed() else "undirected" <br>
File "/usr/local/lib/python2.7/site-packages/graph_tool/__init__.py", line 2412, in is_directed <br>
return self.__graph.get_directed() <br>
AttributeError: 'myGraph' object has no attribute '_Graph__graph'
-
我的另一种方法是调用父级的
__init__
,然后用新数据覆盖该对象.当我这样做时,只要停留在__init__
方法中,一切看起来就很好,但是一旦离开它,我的图形就会被擦除.
My other approach is to call the parent's
__init__
but then override the object with new data. When I do this, everything looks fine as long as I stay in my__init__
method, but once I leave it, my graph is wiped.
class myGraph(gt.Graph):
def __init__(self):
super(myGraph, self).__init__()
self = gt.collection.data['celegansneural']
print self
g = myGraph()
print g
哪个返回以下内容.请注意,第一个print self
返回一个填充的Graph
对象,而第二个print g
返回一个空的myGraph
对象
Which returns the following. Note the first print self
returns a populated Graph
object, whereas the second print g
returns an empty myGraph
object
<Graph object, directed, with 297 vertices and 2359 edges at 0x11df610d0>
<myGraph object, directed, with 0 vertices and 0 edges at 0x109d14190>
如果这是对graph_tool
库的挑剔问题,我事先表示歉意,但我认为与我的代码错误相比,我的编码错误更有可能发生.
I apologize in advance if this is some picky problem of graph_tool
, library, but I figured that it is more likely my coding error than theirs.
推荐答案
您似乎对python中赋值的工作方式有些困惑.但是我想实现所需目标的正确方法是使用适当的参数调用父级的构造函数:
You seem a bit confused about how assignment works in python. But I suppose the correct way to achieve what you want is to call the parent's constructors with the appropriate arguments:
class myGraph(gt.Graph):
def __init__(self):
super(myGraph, self).__init__(gt.collection.data['celegansneural'])
g = myGraph()
print(g)
这篇关于从graph_tool包中继承Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!