python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Module clustering :: Class VertexClustering
[hide private]

Class VertexClustering

source code

object --+    
         |    
Clustering --+
             |
            VertexClustering
Known Subclasses:

The clustering of the vertex set of a graph.

This class extends Clustering by linking it to a specific Graph object and by optionally storing the modularity score of the clustering. It also provides some handy methods like getting the subgraph corresponding to a cluster and such.


Note: since this class is linked to a Graph, destroying the graph by the del operator does not free the memory occupied by the graph if there exists a VertexClustering that references the Graph.

Instance Methods [hide private]
 
__init__(self, graph, membership=None, modularity=None, params=None, modularity_params=None)
Creates a clustering object for a given graph.
source code
 
as_cover(self)
Returns a VertexCover that contains the same clusters as this clustering.
source code
 
cluster_graph(self, combine_vertices=None, combine_edges=None)
Returns a graph where each cluster is contracted into a single vertex.
source code
 
crossing(self)
Returns a boolean vector where element i is True iff edge i lies between clusters, False otherwise.
source code
 
recalculate_modularity(self)
Recalculates the stored modularity value.
source code
 
_recalculate_modularity_safe(self)
Recalculates the stored modularity value and swallows all exceptions raised by the modularity function (if any).
source code
 
subgraph(self, idx)
Get the subgraph belonging to a given cluster.
source code
 
subgraphs(self)
Gets all the subgraphs belonging to each of the clusters.
source code
 
giant(self)
Returns the giant community of the clustered graph.
source code
 
__plot__(self, context, bbox, palette, *args, **kwds)
Plots the clustering to the given Cairo context in the given bounding box.
source code

Inherited from Clustering: __getitem__, __iter__, __len__, __str__, compare_to, size, size_histogram, sizes, summary

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Methods [hide private]
 
FromAttribute(cls, graph, attribute, intervals=None, params=None)
Creates a vertex clustering based on the value of a vertex attribute.
Class Variables [hide private]
  _default_palette = None
hash(x)
Properties [hide private]
  modularity
Returns the modularity score
  q
Returns the modularity score
  graph
Returns the graph belonging to this object

Inherited from Clustering: membership, n

Inherited from object: __class__

Method Details [hide private]

__init__(self, graph, membership=None, modularity=None, params=None, modularity_params=None)
(Constructor)

source code 

Creates a clustering object for a given graph.

Parameters:
  • graph - the graph that will be associated to the clustering
  • membership - the membership list. The length of the list must be equal to the number of vertices in the graph. If None, every vertex is assumed to belong to the same cluster.
  • modularity - the modularity score of the clustering. If None, it will be calculated when needed.
  • params - additional parameters to be stored in this object.
  • modularity_params - arguments that should be passed to Graph.modularity when the modularity is (re)calculated. If the original graph was weighted, you should pass a dictionary containing a weight key with the appropriate value here.
Overrides: object.__init__

FromAttribute(cls, graph, attribute, intervals=None, params=None)
Class Method

 

Creates a vertex clustering based on the value of a vertex attribute.

Vertices having the same attribute will correspond to the same cluster.

Parameters:
  • graph - the graph on which we are working
  • attribute - name of the attribute on which the clustering is based.
  • intervals - for numeric attributes, you can either pass a single number or a list of numbers here. A single number means that the vertices will be put in bins of that width and vertices ending up in the same bin will be in the same cluster. A list of numbers specify the bin positions explicitly; e.g., [10, 20, 30] means that there will be four categories: vertices with the attribute value less than 10, between 10 and 20, between 20 and 30 and over 30. Intervals are closed from the left and open from the right.
  • params - additional parameters to be stored in this object.
Returns:
a new VertexClustering object

as_cover(self)

source code 

Returns a VertexCover that contains the same clusters as this clustering.

Overrides: Clustering.as_cover

cluster_graph(self, combine_vertices=None, combine_edges=None)

source code 

Returns a graph where each cluster is contracted into a single vertex.

In the resulting graph, vertex i represents cluster i in this clustering. Vertex i and j will be connected if there was at least one connected vertex pair (a, b) in the original graph such that vertex a was in cluster i and vertex b was in cluster j.

Parameters:
  • combine_vertices - specifies how to derive the attributes of the vertices in the new graph from the attributes of the old ones. See Graph.contract_vertices() for more details.
  • combine_edges - specifies how to derive the attributes of the edges in the new graph from the attributes of the old ones. See Graph.simplify() for more details. If you specify False here, edges will not be combined, and the number of edges between the vertices representing the original clusters will be equal to the number of edges between the members of those clusters in the original graph.
Returns:
the new graph.

recalculate_modularity(self)

source code 

Recalculates the stored modularity value.

This method must be called before querying the modularity score of the clustering through the class member modularity or q if the graph has been modified (edges have been added or removed) since the creation of the VertexClustering object.

Returns:
the new modularity score

_recalculate_modularity_safe(self)

source code 

Recalculates the stored modularity value and swallows all exceptions raised by the modularity function (if any).

Returns:
the new modularity score or None if the modularity function could not be calculated.

subgraph(self, idx)

source code 

Get the subgraph belonging to a given cluster.

Parameters:
  • idx - the cluster index
Returns:
a copy of the subgraph

Precondition: the vertex set of the graph hasn't been modified since the moment the clustering was constructed.

subgraphs(self)

source code 

Gets all the subgraphs belonging to each of the clusters.

Returns:
a list containing copies of the subgraphs

Precondition: the vertex set of the graph hasn't been modified since the moment the clustering was constructed.

giant(self)

source code 

Returns the giant community of the clustered graph.

The giant component a community for which no larger community exists.

Returns:
a copy of the giant community.

Note: there can be multiple giant communities, this method will return the copy of an arbitrary one if there are multiple giant communities.

Precondition: the vertex set of the graph hasn't been modified since the moment the clustering was constructed.

__plot__(self, context, bbox, palette, *args, **kwds)

source code 

Plots the clustering to the given Cairo context in the given bounding box.

This is done by calling Graph.__plot__() with the same arguments, but coloring the graph vertices according to the current clustering (unless overridden by the vertex_color argument explicitly).

This method understands all the positional and keyword arguments that are understood by Graph.__plot__(), only the differences will be highlighted here:

  • mark_groups: whether to highlight some of the vertex groups by colored polygons. Besides the values accepted by Graph.__plot__ (i.e., a dict mapping colors to vertex indices, a list containing lists of vertex indices, or False), the following are also accepted:
    • True: all the groups will be highlighted, the colors matching the corresponding color indices from the current palette (see the palette keyword argument of Graph.__plot__.
    • A dict mapping cluster indices or tuples of vertex indices to color names. The given clusters or vertex groups will be highlighted by the given colors.
    • A list of cluster indices. This is equivalent to passing a dict mapping numeric color indices from the current palette to cluster indices; therefore, the cluster referred to by element i of the list will be highlighted by color i from the palette.

    The value of the plotting.mark_groups configuration key is also taken into account here; if that configuration key is True and mark_groups is not given explicitly, it will automatically be set to True.

    In place of lists of vertex indices, you may also use VertexSeq instances.

    In place of color names, you may also use color indices into the current palette. None as a color name will mean that the corresponding group is ignored.

  • palette: the palette used to resolve numeric color indices to RGBA values. By default, this is an instance of ClusterColoringPalette.

See Also: Graph.__plot__() for more supported keyword arguments.


Property Details [hide private]

modularity

Returns the modularity score

Get Method:
unreachable.modularity(self) - Returns the modularity score

q

Returns the modularity score

Get Method:
unreachable.modularity(self) - Returns the modularity score

graph

Returns the graph belonging to this object

Get Method:
unreachable.graph(self) - Returns the graph belonging to this object

   Home       Trees       Indices       Help