python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Class EdgeSeq
[hide private]

Class EdgeSeq

source code

     object --+    
              |    
_igraph.EdgeSeq --+
                  |
                 EdgeSeq

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
Instance Methods [hide private]
 
attributes(self)
Returns the list of all the edge attributes in the graph associated to this edge sequence.
source code
Edge
find(self, *args, **kwds)
Returns the first edge of the edge sequence that matches some criteria.
source code
VertexSeq
select(self, *args, **kwds)
Selects a subset of the edge sequence based on some criteria
source code
 
__call__(self, *args, **kwds)
Shorthand notation to select()
source code
 
count_multiple(*args, **kwds)
Proxy method to Graph.count_multiple()
source code
 
delete(*args, **kwds)
Proxy method to Graph.delete_edges()
source code
 
edge_betweenness(*args, **kwds)
Proxy method to Graph.edge_betweenness()
source code
 
is_loop(*args, **kwds)
Proxy method to Graph.is_loop()
source code
 
is_multiple(*args, **kwds)
Proxy method to Graph.is_multiple()
source code
 
is_mutual(*args, **kwds)
Proxy method to Graph.is_mutual()
source code
 
subgraph(*args, **kwds)
Proxy method to Graph.subgraph_edges()
source code

Inherited from _igraph.EdgeSeq: __delitem__, __getitem__, __init__, __len__, __new__, __setitem__, attribute_names, get_attribute_values, is_all, set_attribute_values

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

Properties [hide private]

Inherited from _igraph.EdgeSeq: graph, indices

Inherited from object: __class__

Method Details [hide private]

find(self, *args, **kwds)

source code 

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
Returns: Edge
Overrides: _igraph.EdgeSeq.find

select(self, *args, **kwds)

source code 

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.
  2. _target or {_to} means the target vertex of an edge.
  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. 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)
Returns: VertexSeq
the new, filtered edge sequence
Overrides: _igraph.EdgeSeq.select

__call__(self, *args, **kwds)
(Call operator)

source code 

Shorthand notation to select()

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

count_multiple(*args, **kwds)

source code 

Proxy method to Graph.count_multiple()

This method calls the count_multiple() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.count_multiple() for details.

delete(*args, **kwds)

source code 

Proxy method to Graph.delete_edges()

This method calls the delete_edges() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.delete_edges() for details.

edge_betweenness(*args, **kwds)

source code 

Proxy method to Graph.edge_betweenness()

This method calls the edge_betweenness() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.edge_betweenness() for details.

is_loop(*args, **kwds)

source code 

Proxy method to Graph.is_loop()

This method calls the is_loop() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.is_loop() for details.

is_multiple(*args, **kwds)

source code 

Proxy method to Graph.is_multiple()

This method calls the is_multiple() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.is_multiple() for details.

is_mutual(*args, **kwds)

source code 

Proxy method to Graph.is_mutual()

This method calls the is_mutual() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.is_mutual() for details.

subgraph(*args, **kwds)

source code 

Proxy method to Graph.subgraph_edges()

This method calls the subgraph_edges() method of the Graph class restricted to this sequence, and returns the result.

See Also: Graph.subgraph_edges() for details.


   Home       Trees       Indices       Help