Search code examples
pythongraph-tool

Sub-classing Graph, Vertex and Edge in python graph-tool at the same time


Is it feasible to sub-class, at the same time, the Graph, Vertex and Edge classes in graph-tool in order to implement the behaviour of a certain type of graphs, such as social networks?
More specifically, I would like to have a class CollaborationNetwork(Graph) that comprises instances of class Person(Vertex), class Publication(Vertex), class Authored(Edge), etc.
I imagine that the Person and Publication vertices are identified by a property_map, while the Graph methods are wrapped into likes of add_authorship(Person, Publication) that creates an instance of Authored.

What I am not sure is if implementing such abstraction based on graph-tool with makes sense and, if yes, how to connect CollaborationNetwork to the sub-classes of Vertex and Edge. (I know how only Subclassing Graph from the graph_tool package can work).

About the motivation:
I will be simulating the growth of a social network and will be comparing it with actual data, which is stored as a Neo4j graph. I like the py2neo OGM approach, which represents these Person and Publication data as python objects.


Solution

  • It is possible, but I don't see any real advantage of doing so.

    Subclassing Vertex is easy, but there are several Edge classes, depending on wether the graph is directed or filtered. In order to make it work, you would have to wrap the Graph methods in your derived class that return Vertex or Edge objects (or iterators thereof) so that they convert them to your derived Edge and Vertex objects.

    Doing all this only so that the classes have different names seems to me like a waste of effort. But it is not impossible.

    A simpler strategy would be to create a separate class (say CitationGraph), that does not subclass Graph, but instead contains an instance of one. You can then implement convenience functions like add_person and add_publication that perform your basic operations, while allowing you to access the underlying Graph at any point. Doing so will prevent lots of headaches, and even gives you things like pickle support basically for free.