GraphBuilder
index
../../../../piana/code/Graph/GraphBuilder.py

 File       : GraphBuilder.py
 Author     : R. Aragues & D. Jaeggi
 Creation   : 31.07.2003
 Contents   : class for building a graph from a database
 Called from: Graph.py and piana.py
 
=======================================================================================================
 
A class that defines how a graph should be built and that provides functions to
allow a graph to be automatically be built from a database by calling Graph.build_graph()
 
 
Common to all these methods and, importantly, problem independent, are methods to
recursively expand a graph and update a Graph object with the appropriate data. This
methods are contained in this class.
 
Making a GraphBuilder instance problem specific relies only on making one method
problem specific: GraphBuilder.get_links(node). This method gets all the links for a node and
should be overridden by the user to be adapted to the data source and type being used.
 
The concept is that, once a specific GraphBuilder class has been written (and possibly
GraphNodeAttribute and GraphEdgeAttribute) the following calls would be sufficient to generate
a complete graph (containing, in this case, all actors linked to Jack Nicholson by a
maximum hop of 2 co-actors):
 
my_graph = Graph("My Graph")
my_builder = MyGraphBuilder(depth=2, node1_id="Jack Nicholson")
my_graph.build_graph(my_builder) 
 
In this instance MyGraphBuilder may look something like:
 
class MyGraphBuilder(GraphBuilder):
    def get_links(self, actor):
        ....SQL statements....
        links = []
        for linked_actor in linked_actors:
            new_link = LinkDetail(actor, linked_actor)
            links.append(new_link)
        return links
 
That's all it needs to do, on a basic level. Easy peasy, non?!
Of course, more complex problems may need more complexity here, but
this is fundamentally all that needs to be done.

 
Modules
       
sys
time

 
Classes
       
GraphBuilder

 
class GraphBuilder
    The main GraphBuilder class.
 
A subclass must be created to suit the problem type, overriding at least methods get_link_details() and create_node_attribute()
 
  Methods defined here:
__init__(self, list_node_id=None, depth=0, hub_threshold=0)
list_node_id is the list of node_id that will be used to build the graph
 
depth represents "how many nodes away from each node1_id" will the builder go
 
depth can be:
        ---> 1-N : depth
        ---> 0: no depth taken into account (ie. build complete graph around nodes in list_node_id)
 
"hub_threshold" is used to set a threshold to the number of links that a node can have in order to be added to the network
        - if 0, no thresholds will be used: all nodes will be added to network
        - if >0, only those nodes having less than "hub_threshold" number of links will be added to the network
build_graph(self, complete_taxID=0)
The main graph building method. This is called by Graph.build_graph()
build_graph_by_levels(self)
The main graph building method, building the graph by levels. This is called by Graph.build_graph()
 
Other difference with build_graph is that it will always go until depth+1, in order to check for interactions inner the last level, but
new nodes found in depth+1 will not be added to the graph (hub threshold will not be taken into account)
create_node_attribute(self, node_id)
To be overridden by a method specific to the subclass of GraphBuilder
This should return an attribute of the class
empty_edges_buffer(self)
To be overridden by a method specific to the database
This should delete from memory all buffers used
get_link_details(self, node_id)
To be overridden by a method specific to the database
This should return an array of LinkDetail objects defining links to "node_id"
get_link_details_two_nodes(self, node_id1, node_id2)
To be overridden by a method specific to the database
This should return a LinkDetail object defining links between "node_id1" and "node_id2"
set_graph(self, graph_object)
Sets the graph to be built - this is called by Graph.build_graph()

Data and other attributes defined here:
LinkDetail = <class GraphBuilder.LinkDetail>
Internal class for storing and transmitting link details.
A LinkDetail must be composed of 2 nodes as a minimum.
Each node can have one attribute.
The link can also have any number of attributes.

 
Data
        verbose = 0
verbose_build = 0
verbose_build_shallow = 0
verbose_build_very_shallow = 0
verbose_get_all_links = 0
verbose_has_edge = 0