class documentation

表示图中一系列边的类。

这个类最容易通过es字段访问,该字段返回图中所有边的有序序列。可以通过调用EdgeSeq.select()方法来细化边序列。EdgeSeq.select()也可以通过简单地调用EdgeSeq对象来访问。

创建引用给定图的边序列的另一种方法是直接使用构造函数:

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

可以通过索引边序列对象来访问各个边。它也可以用作可迭代对象,甚至可以在列表推导式中使用:

>>> 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]

边序列也可以用作字典,其中键是属性名称。键对应的值是图中每条边的给定属性的值:

>>> 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]

如果您指定的序列比EdgeSeq中的边数短,则序列将被重复使用:

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

你甚至可以传递一个单独的字符串或整数,它将被视为长度为1的序列:

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

边序列的一些方法只是Graph对象中相应方法的代理方法。一个这样的例子是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
方法 __call__ select() 的简写表示法
方法 attributes 返回与此边序列关联的图中所有边属性的列表。
方法 find 返回符合某些条件的边序列中的第一条边。
方法 select 根据某些条件选择边序列的子集

继承自 EdgeSeq:

方法 attribute_names 返回图的边的属性名称列表
方法 get_attribute_values 返回所有边的给定边属性的值。
方法 is_all 返回边序列是否包含所有边且每条边仅出现一次,按照边的ID顺序排列。
方法 set_attribute_values 为所有顶点设置给定边属性的值
def __call__(self, *args, **kwds): (source)

选择()的简写符号

此方法简单地将所有参数传递给EdgeSeq.select()

def attributes(self): (source)

返回与此边序列关联的图中所有边属性的列表。

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

返回符合某些条件的边序列中的第一条边。

选择标准与VertexSeq.select允许的标准相同。有关更多详细信息,请参阅VertexSeq.select

例如,要在图 g 中找到第一个权重大于5的边:

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

根据某些标准选择边序列的子集

选择标准可以通过位置参数和关键字参数来指定。位置参数总是在关键字参数之前处理。

  • 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.

关键字参数可用于根据边的属性和属性进行过滤。关键字的名称指定了属性的名称和过滤操作符,它们应该通过下划线(_)字符连接。属性名称也可以包含下划线,但操作符名称不包含,因此操作符始终是关键字名称中不包含下划线的最大尾随子字符串。可能的操作符有:

  • 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

例如,如果你想过滤具有数值weight属性大于50的边,你必须编写:

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

同样地,要过滤type在预定义类型列表中的边:

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

如果省略操作符,则默认为eq。例如,以下选择器选择type属性为intracluster的边:

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

在未知运算符的情况下,假定识别的运算符是属性名称的一部分,实际运算符为 eq

关键字参数如果以下划线开头(_),则会被特殊处理。这些不是真正的属性,而是指代边的特定属性,例如它们的中心性。规则如下:

  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.

例如,如果你想排除中介中心性小于2的边:

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

选择从顶点2和4出发的边:

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

选择完全位于由顶点2、3、4和7跨越的子图中的边:

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

要选择一端在包含顶点2、3、4和7的顶点集中,另一端在包含顶点8和9的顶点集中的边:

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

对于需要长时间计算的属性(例如,大型图的介数中心性),建议预先计算这些值并将其存储在图的属性中。当你在同一个select()调用中多次基于同一属性进行选择时,同样适用,以避免不必要地计算两次。例如,以下代码会计算两次介数中心性:

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

建议使用以下替代方案:

>>> g.es["bs"] = g.edge_betweenness()
>>> edges = g.es.select(bs_gt=10, bs_lt=30)
返回
新的、过滤后的边序列