-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
delete_vertices
does not work on subclassed graph
#496
Comments
Thanks, I'll look into this before the upcoming 0.9.9 release, due in a few days. |
On further inspection, it appears that no graph manipulation is needed. I have added test cases. |
Thanks! I'm not sure that it matters, but in the rare cases when I ever needed to override the Another thing that looks weird to me is to call May I ask what your original goal is with the subclassing? Maybe there's an easier way to solve your original goal while I sort out the underlying problem. |
Okay, I figured it out. There's nothing wrong with the code; the problem is that
I'll close this, but it would be great if you could explain your specific use-case to see if there is a simpler or more idiomatic solution. Is your goal to extend the |
Hi, thanks for the investigation and sorry for my late comment. You're right, my original intention was to allow the subclass constructor to "enhance" an existing graph, adding properties and methods to the graph instance.
This is precisely what put me off track here and led to the solution of calling the superclass' As it turns out, achieving my original goal only by overriding class IgraphSub(Graph):
instance_attr: bool
def __init__(self, *args, vertex_property_list: List[bool] = None, **kwargs) -> None:
super().__init__(*args, **kwargs)
# set properties of vertices
self.vs['property'] = vertex_property_list or [False] * len(self.vs)
# set instance variables
self.instance_attr = False and using an existing graph instance is as easy as: class SubclassTest(unittest.TestCase):
def test_subclass(self) -> None:
lattice = Graph.Lattice(dim=[4, 4])
graph = IgraphSub(n=len(lattice.vs), edges=lattice.get_edgelist(), vertex_property_list=[True] * 16)
self.assertEqual([True] * 16, graph.vs['property'])
self.assertEqual(False, graph.instance_attr) For some reason, though, this didn't work when I tried half a year ago (or maybe it did, and I was chasing ghosts back then). So far, the documentation is silent on subclassing - I suppose adding this bit of code as a recipe to get people started might be a good idea? |
Thanks for the feedback and I'm glad that you managed to make it work by overriding I need to think about it a bit before deciding on how to include this in the docs. In general, I usually favour composition over inheritance so I wasn't thinking too much about subclassing the
I think that in 99.99% of the cases you should be able to achieve what you want by overriding |
The nuclear option would be to autodecorate all methods of |
Nah, I've made it so that an empty graph is created unconditionally in |
hehe cleaner solution for sure |
Describe the bug
I have subclassed Graph and added a custom
__init__
method by:igraph.Graph.__init__
but this apparently fails when trying to delete vertices. The edge set is not updated properly. In the test, the graph is:
0 -> 0
1 -> 1
and the first edge has
prop
set toa
while the second edge hasprop
set tob
. Now, if I delete the first vertex, it would be expected thatgraph.es['prop']
returnsb
, but it returnsa
in the subclass case.Fwiw,
delete_vertices
deals with vertex properties correctly anddelete_edges
seems to work fine as well.To reproduce
Test case:
Version information
igraph 0.9.8 on Python 3.9, also tested on igraph 0.9.1
The text was updated successfully, but these errors were encountered: