trophic_levels#
- graph_tool.centrality.trophic_levels(g, weight=None, method='levine', vprop=None)[git][source]#
Return the trophic levels of the vertices of a directed graph.
- Parameters:
- g
Graph Directed graph to be used, with edges pointing from prey to predator (i.e. in the direction of the flow).
- weight
EdgePropertyMap(optional, default:None) Edge weights. If not given, every edge has unit weight.
- method
str(optional, default:"levine") Definition of the levels:
"levine"or"mackay"(see below).- vprop
VertexPropertyMap(optional, default:None) If provided, the levels will be stored in this property map.
- g
- Returns:
- levels
VertexPropertyMap The trophic level of every vertex.
- levels
See also
trophic_incoherenceTrophic incoherence parameter
global_reaching_centralityGlobal reaching centrality
graph_tool.structure.flow_hierarchyFlow hierarchy of a directed graph
Notes
With
method == "levine"[levine-several-1980], the vertices without in-edges (basal vertices) have level one, and every other vertex has a level one unit above the weighted average of the levels of its in-neighbors,\[s_i = 1 + \frac{1}{k^{\text{in}}_i}\sum_j w_{ji} s_j,\]where \(w_{ji}\) is the total weight of the edges \(j\to i\) (their number, if
weightis not given) and \(k^{\text{in}}_i = \sum_jw_{ji}\) the weighted in-degree, with \(s_i = 1\) for the basal vertices. This requires every vertex to be reachable from a basal one.With
method == "mackay"[mackay-how-2020], the levels are defined for any directed graph, as the solution of\[(k^{\text{in}}_i + k^{\text{out}}_i)h_i - \sum_j (w_{ij} + w_{ji})h_j = k^{\text{in}}_i - k^{\text{out}}_i,\]where \(w_{ij}\) is the total weight of the edges \(i\to j\), and \(k^{\text{in}}_i = \sum_jw_{ji}\) and \(k^{\text{out}}_i = \sum_jw_{ij}\) are the weighted in- and out-degrees. This solution minimizes \(\sum_{ij} w_{ij}(h_j - h_i - 1)^2\); the levels are defined up to a constant in every weakly connected component, which is chosen so that the smallest level of the component is zero.
Both are obtained by solving a sparse linear system. The trophic incoherence is given by
trophic_incoherence().References
[levine-several-1980]Stephen Levine, “Several measures of trophic structure applicable to complex food webs”, Journal of Theoretical Biology 83, 195-207 (1980), DOI: 10.1016/0022-5193(80)90288-X [sci-hub, @tor]
[mackay-how-2020]R. S. MacKay, S. Johnson and B. Sansom, “How directed is a directed network?”, Royal Society Open Science 7, 201138 (2020), DOI: 10.1098/rsos.201138 [sci-hub, @tor], arXiv: 2001.05173
Examples
>>> g = gt.Graph([(0, 2), (1, 2), (2, 3), (0, 3)]) >>> print(gt.trophic_levels(g).a) [1. 1. 2. 2.5] >>> print(gt.trophic_levels(g, method="mackay").a) [0.333... 0. ... 1. ... 1.666...]
The levels of MacKay et al. are also defined for networks with cycles, and they can be shown as the vertical coordinates of the vertices. We use a directed network generated with
price_network(), to which we add three edges that close cycles:>>> g = gt.price_network(40, m=2) >>> g.add_edge_list([(0, 30), (2, 25), (5, 35)]) >>> h = gt.trophic_levels(g, method="mackay") >>> x = gt.sfdp_layout(g).get_2d_array([0])[0] >>> y = -h.a * (x.max() - x.min()) / (h.a.max() - h.a.min()) >>> pos = g.new_vp("vector<double>", vals=np.column_stack((x, y))) >>> gt.graph_draw(g, pos, vertex_fill_color="#9bbedc", vertex_size=10, ... edge_color="#999999", output="trophic_levels-price.pdf") <...>
Trophic levels of MacKay et al. of the vertices of a directed network generated with the Price model, with three additional edges that close cycles. The levels are given by the vertical coordinates, increasing from bottom to top, and the horizontal coordinates are those of a force- directed layout.#