Subgraph returns the same graph class as the original class. So it
should give you an instance of your derived class.
> self.a = a
> self.b = b
Something like:
from networkx import Graph
class FooGraph(Graph):
def __init__(self,**kwds):
Graph.__init__(self,kwds)
self.a=10
then,
In [1]: import foograph
In [2]: F=foograph.FooGraph()
In [3]: FS=F.subgraph(None)
In [4]: FS.a
Out[4]: 10
In [5]: FS.get_a()
Out[5]: 10
Aric
That isn't quite the same code as in the current subgraph()
method, but the current version also uses H=self.__class__() in
the same way to initialize a subgraph.
Do I understand correctly you want to be able to pass arguments to the
__class__() method there in order to initialize your (subclassed) Graph
object?
I don't think there would be any harm in passing keywords from
the subgraph() method through to the __class__() call.
Would that solve your problem?
def subgraph(self, nbunch, copy=True, **kwds):
...
H = self.__class__(**kwds)
...
Then you could use e.g. G.subgraph(nodes,foo='bar').
Can you give a simple concrete example?
Aric
Maybe the simplest solution is to provide a subgraph method in
your subclass that does the same as the base class subgraph (could
just call it) and then afterwards performs whatever assignments you
need.
Here is one way:
from networkx import Graph
class FooGraph(Graph):
def __init__(self,**kwds):
Graph.__init__(self,kwds)
self.a=None
def set_a(self,A):
self.a=A
def subgraph(self, nbunch, copy=True):
H=Graph.subgraph(self,nbunch,copy)
H.a=self.a
return H
Then,
In [2]: G=FooGraph()
In [3]: G.set_a([1,2,3])
In [4]: H=G.subgraph(None)
In [5]: H.a
Out[5]: [1, 2, 3]
Aric
I'm not sure what you mean by "filter". For your subclass you control
the __init__() method and can add whatever arguments you like.
If they are not optional keywords some of the NetworkX functions
or methods may not work since they expect to be able to init
the graph with __class__() [see create_empty_copy()]. We might
be able to fix that as Dan suggested in an earlier message.
Aric