Skip to content

Commit 177422b

Browse files
committed
add examples to docs
1 parent 41b9d2e commit 177422b

25 files changed

Lines changed: 734 additions & 88 deletions

docs/algorithms/assortativity.rst

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
Assortativity
22
=============
33

4-
.. note::
5-
The methods in this section rely on calculating the Pearson correlation
6-
coefficient between pairs of value sequences. If the network is regular (i.e.
7-
all vertices have equal degree or label) or if the network has fewer than two
8-
pairs of interacting vertices, the result of calculating assortativity will
9-
be :cpp:`NaN`.
4+
Assortativity is a measure of preference of nodes to attach to other nodes
5+
similar to themselves. In many cases, researchers are interested in degree
6+
assortaivity, which measures the correlation between the degrees of connected
7+
vertices. However, assortativity can also be calculated for other node attributes :cite:p:`newman2003mixing`.
8+
9+
Reticula provides a set of functions to calculate assortativity in directed
10+
and undirected networks, either in terms of degree or other numerical node
11+
attributes.
12+
13+
.. note ::
14+
15+
The methods in this section rely on calculating the Pearson correlation
16+
coefficient between pairs of value sequences. If all connected vertices
17+
have equal degree or attribute, or if the network has fewer than two pairs
18+
of interacting vertices, the result of calculating assortativity will be
19+
:cpp:`NaN`.
1020
1121
1222
Degree assortativity
@@ -32,6 +42,13 @@ Calculates degree assortativity for undirected dyadic and hypergraph static
3242
networks. Assortativity is calculated through Pearson correlation coefficient
3343
over degrees of all pairs of interacting vertices.
3444

45+
.. code-block:: pycon
46+
47+
>>> import reticula as ret
48+
>>> gen = ret.mersenne_twister(42)
49+
>>> net = ret.random_gnp_graph[ret.int64](n=128, p=0.05, random_state=gen)
50+
>>> ret.degree_assortativity(net)
51+
0.012174626999704952
3552
3653
Directed networks
3754
^^^^^^^^^^^^^^^^^
@@ -62,6 +79,17 @@ Calculates in-/out-degree assortativity for directed dyadic and hypergraph
6279
static networks. Assortativity is calculated through Pearson correlation
6380
coefficient over degrees of all pairs of interacting vertices.
6481

82+
.. code-block:: pycon
83+
84+
>>> import reticula as ret
85+
>>> gen = ret.mersenne_twister(42)
86+
>>> net = ret.random_directed_gnp_graph[ret.int64](
87+
... n=128, p=0.05, random_state=gen)
88+
>>> ret.in_in_degree_assortativity(net)
89+
-0.008986050522667814
90+
>>> ret.in_out_degree_assortativity(net)
91+
-0.05295305364798128
92+
6593
Numerical attribute assortativity
6694
---------------------------------
6795

@@ -95,13 +123,25 @@ correlation coefficient over the value of the given function over all pairs of
95123
interacting vertices.
96124

97125

126+
.. code-block:: pycon
127+
128+
>>> import reticula as ret
129+
>>> import math
130+
>>> gen = ret.mersenne_twister(42)
131+
>>> net = ret.random_gnp_graph[ret.int64](
132+
... n=128, p=0.05, random_state=gen)
133+
>>> ret.attribute_assortativity(net,
134+
... lambda v: math.log(ret.degree(net, v)))
135+
0.018211216997337246
136+
137+
98138
.. tab-set::
99139

100140
.. tab-item:: Python
101141
:sync: python
102142

103143
.. py:function:: attribute_assortativity(undirected_network, \
104-
attribute_map: dict[network.edge_type(), float], \
144+
attribute_map: dict[network.vertex_type(), float], \
105145
default_value : float) -> float
106146
:noindex:
107147

@@ -110,17 +150,32 @@ interacting vertices.
110150

111151
.. cpp:function:: template <\
112152
network_edge EdgeT, \
113-
mapping<EdgeT, double> MapT> \
153+
mapping<VertT, double> MapT> \
114154
double attribute_assortativity(\
115155
const network<EdgeT>& net, \
116156
const MapT& attribute_map, \
117157
double default_value)
118158

119159
Calculates assortativity of the given mapping of vertices to floating-point
120-
(dictionary) for undirected dyadic and hypergraph static networks. Assortativity
121-
is calculated through Pearson correlation coefficient over the value of the
122-
given mapping over all pairs of interacting vertices. If a vertex is not present
123-
in the mapping, the value of the argument :cpp:`default_value` is used.
160+
(dictionary) for undirected dyadic and hypergraph static networks.
161+
Assortativity is calculated through Pearson correlation coefficient over the
162+
value of the given mapping over all pairs of interacting vertices. If a vertex
163+
is not present in the mapping, the value of the argument :cpp:`default_value`
164+
is used.
165+
166+
167+
.. code-block:: pycon
168+
169+
>>> import reticula as ret
170+
>>> gen = ret.mersenne_twister(42)
171+
>>> net = ret.random_gnp_graph[ret.int64](
172+
... n=128, p=0.05, random_state=gen)
173+
>>> attribute_map = {
174+
... v: math.log(ret.degree(net, v))
175+
... for v in net.vertices()}
176+
>>> ret.attribute_assortativity(net, attribute_map, default_value=0.0)
177+
0.018211216997337246
178+
124179
125180
126181
Directed networks
@@ -132,8 +187,8 @@ Directed networks
132187
:sync: python
133188

134189
.. py:function:: attribute_assortativity(directed_network, \
135-
mutator_attribute_fun: Callable[[network.edge_type()], float], \
136-
mutated_attribute_fun: Callable[[network.edge_type()], float]) -> float
190+
mutator_attribute_fun: Callable[[network.vertex_type()], float], \
191+
mutated_attribute_fun: Callable[[network.vertex_type()], float]) -> float
137192
138193
.. tab-item:: C++
139194
:sync: cpp
@@ -162,6 +217,17 @@ representation of a directed link) is calculated from the function
162217
:cpp:`mutator_attribute_fun`, and the mutated vertex (head of the arrow) from
163218
:cpp:`mutated_attribute_fun`.
164219

220+
.. code-block:: pycon
221+
222+
>>> import reticula as ret
223+
>>> import math
224+
>>> gen = ret.mersenne_twister(42)
225+
>>> net = ret.random_directed_gnp_graph[ret.int64](
226+
... n=128, p=0.05, random_state=gen)
227+
>>> ret.attribute_assortativity(net,
228+
... lambda tail: math.log(ret.in_degree(net, tail) + 1),
229+
... lambda head: math.log(ret.out_degree(net, head) + 1))
230+
-0.05753797100756569
165231
166232
.. tab-set::
167233

@@ -198,3 +264,19 @@ argument :cpp:`mutator_default_value` or :cpp:`mutated_attribute_fun` is used.
198264
The associated value of the mutator (tail of the arrow representation of a
199265
directed link) is calculated from the function :cpp:`mutator_attribute_fun`, and
200266
the mutated vertex (head of the arrow) from :cpp:`mutated_attribute_fun`.
267+
268+
.. code-block:: pycon
269+
270+
>>> import reticula as ret
271+
>>> gen = ret.mersenne_twister(42)
272+
>>> net = ret.random_directed_gnp_graph[ret.int64](
273+
... n=128, p=0.05, random_state=gen)
274+
>>> mutator_map = {
275+
... tail: math.log(ret.in_degree(net, tail) + 1)
276+
... for tail in net.vertices()}
277+
>>> mutated_map = {
278+
... head: math.log(ret.out_degree(net, head) + 1)
279+
... for head in net.vertices()}
280+
>>> ret.attribute_assortativity(net, mutator_map, mutated_map,
281+
... mutator_default_value=0.0, mutated_default_value=0.0)
282+
-0.05753797100756569

docs/algorithms/edge_degrees.rst

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Edge degrees (orders)
44
Degree functions
55
----------------
66

7-
There are multiple ways of defining the degrees for (hyper-)edges depending on
8-
the type of edge. All edge types have the following degree functions defined:
7+
There are multiple ways of defining the degrees (orders) for (hyper-)edges
8+
depending on the type of edge. All edge types have the following degree
9+
functions defined:
910

1011
* **Edge in-degree** referes to the number of vertices that are in-incident to
1112
the edge, i.e., the cardinality of the set of mutator vertices of the edge.
@@ -17,7 +18,7 @@ the type of edge. All edge types have the following degree functions defined:
1718
.. note::
1819
Unlike :ref:`vertex degrees <algorithms/vertex_degrees:Vertex degrees>`, edge degree
1920
functions do not require a network to be passed as the first argument. This
20-
is because the degree of an edge can be determined by the edge itself.
21+
is because the degree of an edge can be determined from the edge itself.
2122

2223
.. tab-set::
2324

@@ -43,9 +44,29 @@ the type of edge. All edge types have the following degree functions defined:
4344
std::size_t edge_incident_degree(const EdgeT& edge)
4445

4546

46-
For undirected edges, **degree** of a edge referes to the incident-degree of
47-
that edge. For directed edges, the word degree on its own is vaguely-defined.
48-
In hypergraphs, this is commonly refered to as the *order* of the edge.
47+
.. code-block:: pycon
48+
49+
>>> import reticula as ret
50+
>>> edge = ret.undirected_edge[ret.int64](0, 1)
51+
>>> ret.edge_in_degree(edge)
52+
2
53+
>>> ret.edge_out_degree(edge)
54+
2
55+
>>> ret.edge_incident_degree(edge)
56+
2
57+
>>> hyperedge = ret.directed_hyperedge[ret.int64]([0, 1, 2], [2, 3])
58+
>>> ret.edge_in_degree(hyperedge)
59+
3
60+
>>> ret.edge_out_degree(hyperedge)
61+
2
62+
>>> ret.edge_incident_degree(hyperedge)
63+
4
64+
65+
66+
For undirected edges, **degree** of a (hyper)edge referes to the
67+
incident-degree of that edge. For directed edges, the word degree on its own is
68+
vaguely-defined. In hypergraphs, this is commonly refered to as the *order* of
69+
the edge.
4970

5071

5172
.. tab-set::
@@ -61,7 +82,15 @@ In hypergraphs, this is commonly refered to as the *order* of the edge.
6182
.. cpp:function:: template <undirected_network_edge EdgeT> \
6283
std::size_t edge_degree(const EdgeT& edge)
6384

85+
.. code-block::
6486
87+
>>> import reticula as ret
88+
>>> edge = ret.undirected_edge[ret.int64](0, 1)
89+
>>> ret.edge_degree(edge)
90+
2
91+
>>> edge = ret.undirected_hyperedge[ret.int64]([0, 1, 2, 3])
92+
>>> ret.edge_degree(edge)
93+
4
6594
6695
Edge degree sequences
6796
---------------------
@@ -104,6 +133,24 @@ pair sequence is a sequence of tuples of in- and out-degrees of edges.
104133
edge_in_out_degree_pair_sequence(\
105134
const network<EdgeT>& net)
106135

136+
.. code-block:: pycon
137+
138+
>>> import reticula as ret
139+
>>> gen = ret.mersenne_twister(42)
140+
>>> g = ret.random_directed_expected_degree_sequence_hypergraph[ret.int64](
141+
... vertex_in_out_weight_sequence=[(2, 2), (2, 2), (1, 1), (1, 1)],
142+
... edge_in_out_weight_sequence=[(2, 2), (2, 2), (1, 1), (1, 1)],
143+
... random_state=gen)
144+
>>> ret.edge_in_degree_sequence(g)
145+
[0, 3, 4, 2]
146+
>>> ret.edge_out_degree_sequence(g)
147+
[2, 3, 2, 0]
148+
>>> ret.edge_incident_degree_sequence(g)
149+
[2, 4, 4, 2]
150+
>>> ret.edge_in_out_degree_pair_sequence(g)
151+
[(0, 2), (3, 3), (4, 2), (2, 0)]
152+
153+
107154
Similar to :cpp:func:`edge_degree`, edge degree sequence without a prefix is only defined
108155
for undirected networks.
109156

@@ -119,3 +166,16 @@ for undirected networks.
119166

120167
.. cpp:function:: template <undirected_network_edge EdgeT> \
121168
std::vector<std::size_t> edge_degree_sequence(const network<EdgeT>& net)
169+
170+
.. code-block:: pycon
171+
172+
>>> import reticula as ret
173+
>>> gen = ret.mersenne_twister(42)
174+
>>> g = ret.random_expected_degree_sequence_hypergraph[ret.int64](
175+
... vertex_weight_sequence=[2, 2, 1, 1],
176+
... edge_weight_sequence=[2, 2, 1, 1],
177+
... random_state=gen)
178+
>>> g.edges()
179+
[undirected_hyperedge[int64]([]), undirected_hyperedge[int64]([0, 1]), undirected_hyperedge[int64]([0, 1, 3])]
180+
>>> ret.edge_degree_sequence(g)
181+
[0, 2, 3]

docs/algorithms/graph_properties.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ possible ordered pairs of distinct vertices.
3131

3232
Note that in the presense of self-links, density might be higher than 1.
3333

34+
.. code-block:: pycon
35+
36+
>>> import reticula as ret
37+
>>> g = ret.cycle_graph[ret.int64](size=128)
38+
>>> ret.density(g)
39+
0.015748031496062992
40+
41+
3442
Temporal network observation window
3543
-----------------------------------
3644

docs/algorithms/graphicallity.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Checks if the sequence can be the degree sequence of a valid undirected graph,
2222
containing no multi-edges or loops, based on the Erdős--Gallai algorithm
2323
:cite:p:`erdos1960graphs,choudum1986simple`.
2424

25+
.. code-block:: pycon
26+
27+
>>> import reticula as ret
28+
>>> ret.is_graphic([2, 2, 1, 1])
29+
True
30+
>>> ret.is_graphic([2, 2, 1, 2])
31+
False
32+
2533
2634
Directed degree-pair sequence
2735
-----------------------------
@@ -49,3 +57,11 @@ and Wang :cite:p:`kleitman1973algorithms`.
4957
A degree pair sequence is a range (list) of pairs (2-tuples) of integers,
5058
where the first element of each item represents in-degree and the second item
5159
out-degree of one vertex.
60+
61+
.. code-block:: pycon
62+
63+
>>> import reticula as ret
64+
>>> ret.is_digraphic([(0, 1), (2, 0), (0, 1), (1, 1)])
65+
True
66+
>>> ret.is_digraphic([(0, 1), (2, 0), (0, 1), (1, 2)])
67+
False

docs/algorithms/temporal_network_reachability.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ one of those points, can spread to our target. The contrast between this and
2626
forward and backwards process is similar to the contrast between calculating in-
2727
and out-components on a directed acyclic graph.
2828

29+
.. hint::
30+
31+
Check out a more in-depth :doc:`temporal network reachability example
32+
</examples/temporal_network>` to learn more!
33+
2934
From/to a single point
3035
^^^^^^^^^^^^^^^^^^^^^^
3136

0 commit comments

Comments
 (0)