python-igraph API reference

List of all classes, functions and methods in python-igraph

class documentation

class VertexClustering(Clustering):

Known subclasses: igraph.Cut

View In Hierarchy

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.
Class Method ​From​Attribute Creates a vertex clustering based on the value of a vertex attribute.
Method __init__ Creates a clustering object for a given graph.
Method __plot__ Plots the clustering to the given Cairo context or matplotlib Axes.
Method as​_cover Returns a VertexCover that contains the same clusters as this clustering.
Method cluster​_graph Returns a graph where each cluster is contracted into a single vertex.
Method crossing Returns a boolean vector where element i is True iff edge i lies between clusters, False otherwise.
Method giant Returns the largest cluster of the clustered graph.
Method recalculate​_modularity Recalculates the stored modularity value.
Method subgraph Get the subgraph belonging to a given cluster.
Method subgraphs Gets all the subgraphs belonging to each of the clusters.
Property graph Returns the graph belonging to this object
Property modularity Returns the modularity score
Method _formatted​_cluster​_iterator Iterates over the clusters and formats them into a string to be presented in the summary.
Method _recalculate​_modularity​_safe Recalculates the stored modularity value and swallows all exceptions raised by the modularity function (if any).
Class Variable _default​_palette Undocumented
Instance Variable _graph Undocumented
Instance Variable _modularity Undocumented
Instance Variable _modularity​_dirty Undocumented
Instance Variable _modularity​_params Undocumented

Inherited from Clustering:

Method __getitem__ Returns the members of the specified cluster.
Method __iter__ Iterates over the clusters in this clustering.
Method __len__ Returns the number of clusters.
Method __str__ Undocumented
Method compare​_to Compares this clustering to another one using some similarity or distance metric.
Method size Returns the size of a given cluster.
Method size​_histogram Returns the histogram of cluster sizes.
Method sizes Returns the size of given clusters.
Method summary Returns the summary of the clustering.
Property membership Returns the membership vector.
Property n Returns the number of elements covered by this clustering.
Instance Variable _len Undocumented
Instance Variable _membership Undocumented
@classmethod
def FromAttribute(cls, graph, attribute, intervals=None, params=None):

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

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

Parameters
graphthe graph on which we are working
attributename of the attribute on which the clustering is based.
intervalsfor 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.
paramsadditional parameters to be stored in this object.
Returns
a new VertexClustering object
def __init__(self, graph, membership=None, modularity=None, params=None, modularity_params=None):
overridden in igraph.Cut
Creates a clustering object for a given graph.
Parameters
graphthe graph that will be associated to the clustering
membershipthe 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.
modularitythe modularity score of the clustering. If None, it will be calculated when needed.
paramsadditional parameters to be stored in this object.
modularity​_paramsarguments 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.
def __plot__(self, backend, context, *args, **kwds):

Plots the clustering to the given Cairo context or matplotlib Axes.

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.
def as_cover(self):
Returns a VertexCover that contains the same clusters as this clustering.
def cluster_graph(self, combine_vertices=None, combine_edges=None):

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​_verticesspecifies 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​_edgesspecifies 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.
def crossing(self):
Returns a boolean vector where element i is True iff edge i lies between clusters, False otherwise.
def giant(self):

Returns the largest cluster of the clustered graph.

The largest cluster is a cluster for which no larger cluster exists in the clustering. It may also be known as the giant community if the clustering represents the result of a community detection function.

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

Returns
a copy of the largest cluster.
Note
there can be multiple largest clusters, this method will return the copy of an arbitrary one if there are multiple largest clusters.
def recalculate_modularity(self):

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
def subgraph(self, idx):

Get the subgraph belonging to a given cluster.

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

Parameters
idxthe cluster index
Returns
a copy of the subgraph
def subgraphs(self):

Gets all the subgraphs belonging to each of the clusters.

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

Returns
a list containing copies of the subgraphs
@property
graph =
Returns the graph belonging to this object
@property
modularity =
Returns the modularity score
def _formatted_cluster_iterator(self):
Iterates over the clusters and formats them into a string to be presented in the summary.
def _recalculate_modularity_safe(self):
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.
_default_palette =

Undocumented

_graph =

Undocumented

_modularity =

Undocumented

_modularity_dirty: bool =

Undocumented

_modularity_params: dict =

Undocumented