python-igraph API reference

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

class documentation

class EdgeSeq(_EdgeSeq):

View In Hierarchy

Class representing a sequence of edges in the graph.

This class is most easily accessed by the es field of the Graph object, which returns an ordered sequence of all edges in the graph. The edge sequence can be refined by invoking the EdgeSeq.select() method. EdgeSeq.select() can also be accessed by simply calling the EdgeSeq object.

An alternative way to create an edge sequence referring to a given graph is to use the constructor directly:

>>> g = Graph.Full(3)
>>> es = EdgeSeq(g)
>>> restricted_es = EdgeSeq(g, [0, 1])

The individual edges can be accessed by indexing the edge sequence object. It can be used as an iterable as well, or even in a list comprehension:

>>> g=Graph.Full(3)
>>> for e in g.es:
...   print(e.tuple)
...
(0, 1)
(0, 2)
(1, 2)
>>> [max(e.tuple) for e in g.es]
[1, 2, 2]

The edge sequence can also be used as a dictionary where the keys are the attribute names. The values corresponding to the keys are the values of the given attribute of every edge in the graph:

>>> g=Graph.Full(3)
>>> for idx, e in enumerate(g.es):
...   e["weight"] = idx*(idx+1)
...
>>> g.es["weight"]
[0, 2, 6]
>>> g.es["weight"] = range(3)
>>> g.es["weight"]
[0, 1, 2]

If you specify a sequence that is shorter than the number of edges in the EdgeSeq, the sequence is reused:

>>> g = Graph.Tree(7, 2)
>>> g.es["color"] = ["red", "green"]
>>> g.es["color"]
['red', 'green', 'red', 'green', 'red', 'green']

You can even pass a single string or integer, it will be considered as a sequence of length 1:

>>> g.es["color"] = "red"
>>> g.es["color"]
['red', 'red', 'red', 'red', 'red', 'red']

Some methods of the edge sequences are simply proxy methods to the corresponding methods in the Graph object. One such example is EdgeSeq.is_multiple():

>>> g=Graph(3, [(0,1), (1,0), (1,2)])
>>> g.es.is_multiple()
[False, True, False]
>>> g.es.is_multiple() == g.is_multiple()
True
Method attributes Returns the list of all the edge attributes in the graph associated to this edge sequence.
Method find Returns the first edge of the edge sequence that matches some criteria.
Method select Selects a subset of the edge sequence based on some criteria
Method __call__ Shorthand notation to select()

Inherited from EdgeSeq:

Method __new__ Create and return a new object. See help(type) for accurate signature.
Method attribute_names Returns the attribute name list of the graph's edges
Method get_attribute_values Returns the value of a given edge attribute for all edges.
Method is_all Returns whether the edge sequence contains all the edges exactly once, in the order of their edge IDs.
Method set_attribute_values Sets the value of a given edge attribute for all vertices @param attrname: the name of the attribute @param values: the new attribute values in a list
def attributes(self):

Returns the list of all the edge attributes in the graph associated to this edge sequence.

def find(self, *args, **kwds):

Returns the first edge of the edge sequence that matches some criteria.

The selection criteria are equal to the ones allowed by VertexSeq.select. See VertexSeq.select for more details.

For instance, to find the first edge with weight larger than 5 in graph g:

>>> g.es.find(weight_gt=5)           #doctest:+SKIP
def select(self, *args, **kwds):

Selects a subset of the edge sequence based on some criteria

The selection criteria can be specified by the positional and the keyword arguments. Positional arguments are always processed before keyword arguments.

  • If the first positional argument is None, an empty sequence is returned.
  • If the first positional argument is a callable object, the object will be called for every edge in the sequence. If it returns True, the edge will be included, otherwise it will be excluded.
  • If the first positional argument is an iterable, it must return integers and they will be considered as indices of the current edge set (NOT the whole edge set of the graph -- the difference matters when one filters an edge set that has already been filtered by a previous invocation of EdgeSeq.select(). In this case, the indices do not refer directly to the edges of the graph but to the elements of the filtered edge sequence.
  • If the first positional argument is an integer, all remaining arguments are expected to be integers. They are considered as indices of the current edge set again.

Keyword arguments can be used to filter the edges based on their attributes and properties. The name of the keyword specifies the name of the attribute and the filtering operator, they should be concatenated by an underscore (_) character. Attribute names can also contain underscores, but operator names don't, so the operator is always the largest trailing substring of the keyword name that does not contain an underscore. Possible operators are:

  • eq: equal to
  • ne: not equal to
  • lt: less than
  • gt: greater than
  • le: less than or equal to
  • ge: greater than or equal to
  • in: checks if the value of an attribute is in a given list
  • notin: checks if the value of an attribute is not in a given list

For instance, if you want to filter edges with a numeric weight property larger than 50, you have to write:

>>> g.es.select(weight_gt=50)            #doctest: +SKIP

Similarly, to filter edges whose type is in a list of predefined types:

>>> list_of_types = ["inhibitory", "excitatory"]
>>> g.es.select(type_in=list_of_types)   #doctest: +SKIP

If the operator is omitted, it defaults to eq. For instance, the following selector selects edges whose type property is intracluster:

>>> g.es.select(type="intracluster")     #doctest: +SKIP

In the case of an unknown operator, it is assumed that the recognized operator is part of the attribute name and the actual operator is eq.

Keyword arguments are treated specially if they start with an underscore (_). These are not real attributes but refer to specific properties of the edges, e.g., their centrality. The rules are as follows:

  1. _source or {_from} means the source vertex of an edge. For undirected graphs, only the eq operator is supported and it is treated as {_incident} (since undirected graphs have no notion of edge directionality).
  2. _target or {_to} means the target vertex of an edge. For undirected graphs, only the eq operator is supported and it is treated as {_incident} (since undirected graphs have no notion of edge directionality).
  3. _within ignores the operator and checks whether both endpoints of the edge lie within a specified set.
  4. _between ignores the operator and checks whether one endpoint of the edge lies within a specified set and the other endpoint lies within another specified set. The two sets must be given as a tuple.
  5. _incident ignores the operator and checks whether the edge is incident on a specific vertex or a set of vertices.
  6. Otherwise, the rest of the name is interpreted as a method of the Graph object. This method is called with the edge sequence as its first argument (all others left at default values) and edges are filtered according to the value returned by the method.

For instance, if you want to exclude edges with a betweenness centrality less than 2:

>>> g = Graph.Famous("zachary")
>>> excl = g.es.select(_edge_betweenness_ge = 2)

To select edges originating from vertices 2 and 4:

>>> edges = g.es.select(_source_in = [2, 4])

To select edges lying entirely within the subgraph spanned by vertices 2, 3, 4 and 7:

>>> edges = g.es.select(_within = [2, 3, 4, 7])

To select edges with one endpoint in the vertex set containing vertices 2, 3, 4 and 7 and the other endpoint in the vertex set containing vertices 8 and 9:

>>> edges = g.es.select(_between = ([2, 3, 4, 7], [8, 9]))

For properties that take a long time to be computed (e.g., betweenness centrality for large graphs), it is advised to calculate the values in advance and store it in a graph attribute. The same applies when you are selecting based on the same property more than once in the same select() call to avoid calculating it twice unnecessarily. For instance, the following would calculate betweenness centralities twice:

>>> edges = g.es.select(_edge_betweenness_gt=10,       # doctest:+SKIP
...                     _edge_betweenness_lt=30)

It is advised to use this instead:

>>> g.es["bs"] = g.edge_betweenness()
>>> edges = g.es.select(bs_gt=10, bs_lt=30)
Returnsthe new, filtered edge sequence
def __call__(self, *args, **kwds):

Shorthand notation to select()

This method simply passes all its arguments to EdgeSeq.select().

API Documentation for python-igraph, generated by pydoctor 21.2.2.