visibgraph 1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- visibgraph/__init__.py +136 -0
- visibgraph/accessor.py +64 -0
- visibgraph/construct/__init__.py +43 -0
- visibgraph/construct/horizontal.py +110 -0
- visibgraph/construct/multivariate.py +180 -0
- visibgraph/construct/natural.py +162 -0
- visibgraph/construct/penetrable.py +172 -0
- visibgraph/construct/spatial.py +145 -0
- visibgraph/construct/variants.py +152 -0
- visibgraph/construct/windows.py +128 -0
- visibgraph/core.py +441 -0
- visibgraph/datasets/__init__.py +49 -0
- visibgraph/datasets/_bundled.py +78 -0
- visibgraph/datasets/benchmark.py +229 -0
- visibgraph/datasets/data/README.md +35 -0
- visibgraph/datasets/data/lynx.csv +115 -0
- visibgraph/datasets/data/nile.csv +101 -0
- visibgraph/datasets/data/sunspots.csv +290 -0
- visibgraph/datasets/data/tep_d00.csv.gz +0 -0
- visibgraph/datasets/data/tep_d00_te.csv.gz +0 -0
- visibgraph/datasets/data/tep_d01_te.csv.gz +0 -0
- visibgraph/datasets/data/tep_d04_te.csv.gz +0 -0
- visibgraph/datasets/data/tep_d05_te.csv.gz +0 -0
- visibgraph/datasets/data/tep_d06_te.csv.gz +0 -0
- visibgraph/datasets/data/tep_d11_te.csv.gz +0 -0
- visibgraph/datasets/generators.py +216 -0
- visibgraph/interactive.py +84 -0
- visibgraph/io.py +153 -0
- visibgraph/labelled.py +263 -0
- visibgraph/measures.py +586 -0
- visibgraph/ml.py +179 -0
- visibgraph/networks.py +142 -0
- visibgraph/segment.py +642 -0
- visibgraph/spc.py +267 -0
- visibgraph/surrogates.py +147 -0
- visibgraph/symbolic.py +226 -0
- visibgraph/viz/__init__.py +62 -0
- visibgraph/viz/_style.py +92 -0
- visibgraph/viz/arcs.py +175 -0
- visibgraph/viz/distributions.py +202 -0
- visibgraph/viz/graphs.py +175 -0
- visibgraph/viz/multi.py +84 -0
- visibgraph/viz/series.py +223 -0
- visibgraph-1.0.dist-info/METADATA +181 -0
- visibgraph-1.0.dist-info/RECORD +47 -0
- visibgraph-1.0.dist-info/WHEEL +4 -0
- visibgraph-1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
year,sunspots
|
|
2
|
+
1700,5.0
|
|
3
|
+
1701,11.0
|
|
4
|
+
1702,16.0
|
|
5
|
+
1703,23.0
|
|
6
|
+
1704,36.0
|
|
7
|
+
1705,58.0
|
|
8
|
+
1706,29.0
|
|
9
|
+
1707,20.0
|
|
10
|
+
1708,10.0
|
|
11
|
+
1709,8.0
|
|
12
|
+
1710,3.0
|
|
13
|
+
1711,0.0
|
|
14
|
+
1712,0.0
|
|
15
|
+
1713,2.0
|
|
16
|
+
1714,11.0
|
|
17
|
+
1715,27.0
|
|
18
|
+
1716,47.0
|
|
19
|
+
1717,63.0
|
|
20
|
+
1718,60.0
|
|
21
|
+
1719,39.0
|
|
22
|
+
1720,28.0
|
|
23
|
+
1721,26.0
|
|
24
|
+
1722,22.0
|
|
25
|
+
1723,11.0
|
|
26
|
+
1724,21.0
|
|
27
|
+
1725,40.0
|
|
28
|
+
1726,78.0
|
|
29
|
+
1727,122.0
|
|
30
|
+
1728,103.0
|
|
31
|
+
1729,73.0
|
|
32
|
+
1730,47.0
|
|
33
|
+
1731,35.0
|
|
34
|
+
1732,11.0
|
|
35
|
+
1733,5.0
|
|
36
|
+
1734,16.0
|
|
37
|
+
1735,34.0
|
|
38
|
+
1736,70.0
|
|
39
|
+
1737,81.0
|
|
40
|
+
1738,111.0
|
|
41
|
+
1739,101.0
|
|
42
|
+
1740,73.0
|
|
43
|
+
1741,40.0
|
|
44
|
+
1742,20.0
|
|
45
|
+
1743,16.0
|
|
46
|
+
1744,5.0
|
|
47
|
+
1745,11.0
|
|
48
|
+
1746,22.0
|
|
49
|
+
1747,40.0
|
|
50
|
+
1748,60.0
|
|
51
|
+
1749,80.9
|
|
52
|
+
1750,83.4
|
|
53
|
+
1751,47.7
|
|
54
|
+
1752,47.8
|
|
55
|
+
1753,30.7
|
|
56
|
+
1754,12.2
|
|
57
|
+
1755,9.6
|
|
58
|
+
1756,10.2
|
|
59
|
+
1757,32.4
|
|
60
|
+
1758,47.6
|
|
61
|
+
1759,54.0
|
|
62
|
+
1760,62.9
|
|
63
|
+
1761,85.9
|
|
64
|
+
1762,61.2
|
|
65
|
+
1763,45.1
|
|
66
|
+
1764,36.4
|
|
67
|
+
1765,20.9
|
|
68
|
+
1766,11.4
|
|
69
|
+
1767,37.8
|
|
70
|
+
1768,69.8
|
|
71
|
+
1769,106.1
|
|
72
|
+
1770,100.8
|
|
73
|
+
1771,81.6
|
|
74
|
+
1772,66.5
|
|
75
|
+
1773,34.8
|
|
76
|
+
1774,30.6
|
|
77
|
+
1775,7.0
|
|
78
|
+
1776,19.8
|
|
79
|
+
1777,92.5
|
|
80
|
+
1778,154.4
|
|
81
|
+
1779,125.9
|
|
82
|
+
1780,84.8
|
|
83
|
+
1781,68.1
|
|
84
|
+
1782,38.5
|
|
85
|
+
1783,22.8
|
|
86
|
+
1784,10.2
|
|
87
|
+
1785,24.1
|
|
88
|
+
1786,82.9
|
|
89
|
+
1787,132.0
|
|
90
|
+
1788,130.9
|
|
91
|
+
1789,118.1
|
|
92
|
+
1790,89.9
|
|
93
|
+
1791,66.6
|
|
94
|
+
1792,60.0
|
|
95
|
+
1793,46.9
|
|
96
|
+
1794,41.0
|
|
97
|
+
1795,21.3
|
|
98
|
+
1796,16.0
|
|
99
|
+
1797,6.4
|
|
100
|
+
1798,4.1
|
|
101
|
+
1799,6.8
|
|
102
|
+
1800,14.5
|
|
103
|
+
1801,34.0
|
|
104
|
+
1802,45.0
|
|
105
|
+
1803,43.1
|
|
106
|
+
1804,47.5
|
|
107
|
+
1805,42.2
|
|
108
|
+
1806,28.1
|
|
109
|
+
1807,10.1
|
|
110
|
+
1808,8.1
|
|
111
|
+
1809,2.5
|
|
112
|
+
1810,0.0
|
|
113
|
+
1811,1.4
|
|
114
|
+
1812,5.0
|
|
115
|
+
1813,12.2
|
|
116
|
+
1814,13.9
|
|
117
|
+
1815,35.4
|
|
118
|
+
1816,45.8
|
|
119
|
+
1817,41.1
|
|
120
|
+
1818,30.1
|
|
121
|
+
1819,23.9
|
|
122
|
+
1820,15.6
|
|
123
|
+
1821,6.6
|
|
124
|
+
1822,4.0
|
|
125
|
+
1823,1.8
|
|
126
|
+
1824,8.5
|
|
127
|
+
1825,16.6
|
|
128
|
+
1826,36.3
|
|
129
|
+
1827,49.6
|
|
130
|
+
1828,64.2
|
|
131
|
+
1829,67.0
|
|
132
|
+
1830,70.9
|
|
133
|
+
1831,47.8
|
|
134
|
+
1832,27.5
|
|
135
|
+
1833,8.5
|
|
136
|
+
1834,13.2
|
|
137
|
+
1835,56.9
|
|
138
|
+
1836,121.5
|
|
139
|
+
1837,138.3
|
|
140
|
+
1838,103.2
|
|
141
|
+
1839,85.7
|
|
142
|
+
1840,64.6
|
|
143
|
+
1841,36.7
|
|
144
|
+
1842,24.2
|
|
145
|
+
1843,10.7
|
|
146
|
+
1844,15.0
|
|
147
|
+
1845,40.1
|
|
148
|
+
1846,61.5
|
|
149
|
+
1847,98.5
|
|
150
|
+
1848,124.7
|
|
151
|
+
1849,96.3
|
|
152
|
+
1850,66.6
|
|
153
|
+
1851,64.5
|
|
154
|
+
1852,54.1
|
|
155
|
+
1853,39.0
|
|
156
|
+
1854,20.6
|
|
157
|
+
1855,6.7
|
|
158
|
+
1856,4.3
|
|
159
|
+
1857,22.7
|
|
160
|
+
1858,54.8
|
|
161
|
+
1859,93.8
|
|
162
|
+
1860,95.8
|
|
163
|
+
1861,77.2
|
|
164
|
+
1862,59.1
|
|
165
|
+
1863,44.0
|
|
166
|
+
1864,47.0
|
|
167
|
+
1865,30.5
|
|
168
|
+
1866,16.3
|
|
169
|
+
1867,7.3
|
|
170
|
+
1868,37.6
|
|
171
|
+
1869,74.0
|
|
172
|
+
1870,139.0
|
|
173
|
+
1871,111.2
|
|
174
|
+
1872,101.6
|
|
175
|
+
1873,66.2
|
|
176
|
+
1874,44.7
|
|
177
|
+
1875,17.0
|
|
178
|
+
1876,11.3
|
|
179
|
+
1877,12.4
|
|
180
|
+
1878,3.4
|
|
181
|
+
1879,6.0
|
|
182
|
+
1880,32.3
|
|
183
|
+
1881,54.3
|
|
184
|
+
1882,59.7
|
|
185
|
+
1883,63.7
|
|
186
|
+
1884,63.5
|
|
187
|
+
1885,52.2
|
|
188
|
+
1886,25.4
|
|
189
|
+
1887,13.1
|
|
190
|
+
1888,6.8
|
|
191
|
+
1889,6.3
|
|
192
|
+
1890,7.1
|
|
193
|
+
1891,35.6
|
|
194
|
+
1892,73.0
|
|
195
|
+
1893,85.1
|
|
196
|
+
1894,78.0
|
|
197
|
+
1895,64.0
|
|
198
|
+
1896,41.8
|
|
199
|
+
1897,26.2
|
|
200
|
+
1898,26.7
|
|
201
|
+
1899,12.1
|
|
202
|
+
1900,9.5
|
|
203
|
+
1901,2.7
|
|
204
|
+
1902,5.0
|
|
205
|
+
1903,24.4
|
|
206
|
+
1904,42.0
|
|
207
|
+
1905,63.5
|
|
208
|
+
1906,53.8
|
|
209
|
+
1907,62.0
|
|
210
|
+
1908,48.5
|
|
211
|
+
1909,43.9
|
|
212
|
+
1910,18.6
|
|
213
|
+
1911,5.7
|
|
214
|
+
1912,3.6
|
|
215
|
+
1913,1.4
|
|
216
|
+
1914,9.6
|
|
217
|
+
1915,47.4
|
|
218
|
+
1916,57.1
|
|
219
|
+
1917,103.9
|
|
220
|
+
1918,80.6
|
|
221
|
+
1919,63.6
|
|
222
|
+
1920,37.6
|
|
223
|
+
1921,26.1
|
|
224
|
+
1922,14.2
|
|
225
|
+
1923,5.8
|
|
226
|
+
1924,16.7
|
|
227
|
+
1925,44.3
|
|
228
|
+
1926,63.9
|
|
229
|
+
1927,69.0
|
|
230
|
+
1928,77.8
|
|
231
|
+
1929,64.9
|
|
232
|
+
1930,35.7
|
|
233
|
+
1931,21.2
|
|
234
|
+
1932,11.1
|
|
235
|
+
1933,5.7
|
|
236
|
+
1934,8.7
|
|
237
|
+
1935,36.1
|
|
238
|
+
1936,79.7
|
|
239
|
+
1937,114.4
|
|
240
|
+
1938,109.6
|
|
241
|
+
1939,88.8
|
|
242
|
+
1940,67.8
|
|
243
|
+
1941,47.5
|
|
244
|
+
1942,30.6
|
|
245
|
+
1943,16.3
|
|
246
|
+
1944,9.6
|
|
247
|
+
1945,33.2
|
|
248
|
+
1946,92.6
|
|
249
|
+
1947,151.6
|
|
250
|
+
1948,136.3
|
|
251
|
+
1949,134.7
|
|
252
|
+
1950,83.9
|
|
253
|
+
1951,69.4
|
|
254
|
+
1952,31.5
|
|
255
|
+
1953,13.9
|
|
256
|
+
1954,4.4
|
|
257
|
+
1955,38.0
|
|
258
|
+
1956,141.7
|
|
259
|
+
1957,190.2
|
|
260
|
+
1958,184.8
|
|
261
|
+
1959,159.0
|
|
262
|
+
1960,112.3
|
|
263
|
+
1961,53.9
|
|
264
|
+
1962,37.5
|
|
265
|
+
1963,27.9
|
|
266
|
+
1964,10.2
|
|
267
|
+
1965,15.1
|
|
268
|
+
1966,47.0
|
|
269
|
+
1967,93.8
|
|
270
|
+
1968,105.9
|
|
271
|
+
1969,105.5
|
|
272
|
+
1970,104.5
|
|
273
|
+
1971,66.6
|
|
274
|
+
1972,68.9
|
|
275
|
+
1973,38.0
|
|
276
|
+
1974,34.5
|
|
277
|
+
1975,15.5
|
|
278
|
+
1976,12.6
|
|
279
|
+
1977,27.5
|
|
280
|
+
1978,92.5
|
|
281
|
+
1979,155.4
|
|
282
|
+
1980,154.7
|
|
283
|
+
1981,140.5
|
|
284
|
+
1982,115.9
|
|
285
|
+
1983,66.6
|
|
286
|
+
1984,45.9
|
|
287
|
+
1985,17.9
|
|
288
|
+
1986,13.4
|
|
289
|
+
1987,29.2
|
|
290
|
+
1988,100.2
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"""Synthetic series with known structure.
|
|
2
|
+
|
|
3
|
+
Every generator takes a ``random_state`` and returns a NumPy array (or a
|
|
4
|
+
series and its ground truth), so that each example of the book is exactly
|
|
5
|
+
reproducible.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Sequence
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"white_noise",
|
|
16
|
+
"ar1",
|
|
17
|
+
"logistic_map",
|
|
18
|
+
"henon_map",
|
|
19
|
+
"lorenz",
|
|
20
|
+
"fgn",
|
|
21
|
+
"fbm",
|
|
22
|
+
"periodic",
|
|
23
|
+
"regime_switching",
|
|
24
|
+
"control_chart_patterns",
|
|
25
|
+
"inject_shift",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def white_noise(n: int, dist: str = "normal", random_state=None) -> np.ndarray:
|
|
30
|
+
"""Independent, identically distributed values: ``"normal"``, ``"uniform"``, ``"exponential"`` or ``"cauchy"``."""
|
|
31
|
+
rng = np.random.default_rng(random_state)
|
|
32
|
+
if dist == "normal":
|
|
33
|
+
return rng.normal(size=n)
|
|
34
|
+
if dist == "uniform":
|
|
35
|
+
return rng.random(n)
|
|
36
|
+
if dist == "exponential":
|
|
37
|
+
return rng.exponential(size=n)
|
|
38
|
+
if dist == "cauchy":
|
|
39
|
+
return rng.standard_cauchy(n)
|
|
40
|
+
raise ValueError(f"unknown distribution {dist!r}")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def ar1(n: int, phi: float = 0.8, sigma: float = 1.0, random_state=None, burn: int = 200) -> np.ndarray:
|
|
44
|
+
"""First-order autoregressive process :math:`y_t=\\phi y_{t-1}+\\varepsilon_t`."""
|
|
45
|
+
rng = np.random.default_rng(random_state)
|
|
46
|
+
e = rng.normal(0, sigma, n + burn)
|
|
47
|
+
y = np.empty(n + burn)
|
|
48
|
+
y[0] = e[0]
|
|
49
|
+
for k in range(1, n + burn):
|
|
50
|
+
y[k] = phi * y[k - 1] + e[k]
|
|
51
|
+
return y[burn:]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def logistic_map(n: int, r: float = 4.0, x0: float | None = None, discard: int = 1000, random_state=None) -> np.ndarray:
|
|
55
|
+
"""Logistic map :math:`x_{t+1}=rx_t(1-x_t)` (fully chaotic at :math:`r=4`)."""
|
|
56
|
+
rng = np.random.default_rng(random_state)
|
|
57
|
+
x = rng.uniform(0.1, 0.9) if x0 is None else float(x0)
|
|
58
|
+
out = np.empty(n)
|
|
59
|
+
for k in range(discard + n):
|
|
60
|
+
x = r * x * (1 - x)
|
|
61
|
+
if k >= discard:
|
|
62
|
+
out[k - discard] = x
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def henon_map(n: int, a: float = 1.4, b: float = 0.3, discard: int = 1000, random_state=None) -> np.ndarray:
|
|
67
|
+
""":math:`x` component of the Hénon map :math:`x_{t+1}=1-ax_t^2+y_t`, :math:`y_{t+1}=bx_t`."""
|
|
68
|
+
rng = np.random.default_rng(random_state)
|
|
69
|
+
x, y = rng.uniform(-0.1, 0.1), rng.uniform(-0.1, 0.1)
|
|
70
|
+
out = np.empty(n)
|
|
71
|
+
for k in range(discard + n):
|
|
72
|
+
x, y = 1 - a * x * x + y, b * x
|
|
73
|
+
if k >= discard:
|
|
74
|
+
out[k - discard] = x
|
|
75
|
+
return out
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def lorenz(n: int, dt: float = 0.02, sigma: float = 10.0, rho: float = 28.0, beta: float = 8 / 3, component: int = 0,
|
|
79
|
+
discard: int = 2000, random_state=None) -> np.ndarray:
|
|
80
|
+
"""One component of the Lorenz system integrated by fourth-order Runge–Kutta with step ``dt``.
|
|
81
|
+
|
|
82
|
+
``component=None`` returns all three components, shape (n, 3).
|
|
83
|
+
"""
|
|
84
|
+
rng = np.random.default_rng(random_state)
|
|
85
|
+
s = np.array([1.0, 1.0, 1.0]) + rng.normal(0, 0.1, 3)
|
|
86
|
+
|
|
87
|
+
def f(v):
|
|
88
|
+
x, y, z = v
|
|
89
|
+
return np.array([sigma * (y - x), x * (rho - z) - y, x * y - beta * z])
|
|
90
|
+
|
|
91
|
+
out = np.empty((n, 3))
|
|
92
|
+
for k in range(discard + n):
|
|
93
|
+
k1 = f(s)
|
|
94
|
+
k2 = f(s + dt / 2 * k1)
|
|
95
|
+
k3 = f(s + dt / 2 * k2)
|
|
96
|
+
k4 = f(s + dt * k3)
|
|
97
|
+
s = s + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
|
|
98
|
+
if k >= discard:
|
|
99
|
+
out[k - discard] = s
|
|
100
|
+
return out if component is None else out[:, component]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def fgn(n: int, H: float = 0.7, random_state=None) -> np.ndarray:
|
|
104
|
+
"""Fractional Gaussian noise with Hurst exponent :math:`H`, exact by circulant embedding (Davies–Harte).
|
|
105
|
+
|
|
106
|
+
References
|
|
107
|
+
----------
|
|
108
|
+
Davies, R. B., Harte, D. S. (1987). Tests for Hurst effect. Biometrika 74, 95–101.
|
|
109
|
+
"""
|
|
110
|
+
rng = np.random.default_rng(random_state)
|
|
111
|
+
k = np.arange(n + 1)
|
|
112
|
+
gamma = 0.5 * ((k + 1.0) ** (2 * H) - 2 * k ** (2.0 * H) + np.abs(k - 1.0) ** (2 * H))
|
|
113
|
+
row = np.concatenate([gamma, gamma[-2:0:-1]])
|
|
114
|
+
lam = np.fft.fft(row).real
|
|
115
|
+
lam = np.maximum(lam, 0.0)
|
|
116
|
+
m = row.size
|
|
117
|
+
W = rng.normal(size=m) + 1j * rng.normal(size=m)
|
|
118
|
+
Z = np.fft.fft(np.sqrt(lam / m) * W)
|
|
119
|
+
return Z.real[:n]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def fbm(n: int, H: float = 0.7, random_state=None) -> np.ndarray:
|
|
123
|
+
"""Fractional Brownian motion: cumulative sum of :func:`fgn` (starting at 0)."""
|
|
124
|
+
return np.concatenate([[0.0], np.cumsum(fgn(n - 1, H, random_state))])
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def periodic(n: int, period: int, noise: float = 0.0, random_state=None) -> np.ndarray:
|
|
128
|
+
"""A random pattern of ``period`` distinct values repeated, plus optional Gaussian noise."""
|
|
129
|
+
rng = np.random.default_rng(random_state)
|
|
130
|
+
pat = rng.permutation(period).astype(float) + rng.random(period) * 0.1
|
|
131
|
+
y = np.tile(pat, n // period + 1)[:n]
|
|
132
|
+
return y + noise * rng.normal(size=n)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def regime_switching(lengths: Sequence[int] = (200, 200, 200, 200),
|
|
136
|
+
regimes: Sequence[str] = ("noise", "ar", "chaos", "periodic"), *, scale: bool = True,
|
|
137
|
+
random_state=None) -> tuple[np.ndarray, list[int]]:
|
|
138
|
+
"""Concatenation of segments with different dynamics; returns the series and the true change points.
|
|
139
|
+
|
|
140
|
+
Regimes: ``"noise"`` (Gaussian), ``"ar"`` (AR(1), :math:`\\phi=0.9`), ``"antiar"``
|
|
141
|
+
(:math:`\\phi=-0.7`), ``"chaos"`` (logistic map), ``"henon"``, ``"periodic"``
|
|
142
|
+
(period 7 plus noise), ``"fgn"`` (:math:`H=0.85`), ``"walk"`` (random walk).
|
|
143
|
+
With ``scale=True`` every segment is standardized, so that the regimes differ
|
|
144
|
+
in temporal organization only, not in mean or variance — the hard case for
|
|
145
|
+
level-based detectors.
|
|
146
|
+
"""
|
|
147
|
+
rng = np.random.default_rng(random_state)
|
|
148
|
+
seeds = rng.integers(0, 2**31, size=len(lengths))
|
|
149
|
+
parts = []
|
|
150
|
+
for L, r, sd in zip(lengths, regimes, seeds):
|
|
151
|
+
if r == "noise":
|
|
152
|
+
x = white_noise(L, random_state=sd)
|
|
153
|
+
elif r == "ar":
|
|
154
|
+
x = ar1(L, 0.9, random_state=sd)
|
|
155
|
+
elif r == "antiar":
|
|
156
|
+
x = ar1(L, -0.7, random_state=sd)
|
|
157
|
+
elif r == "chaos":
|
|
158
|
+
x = logistic_map(L, random_state=sd)
|
|
159
|
+
elif r == "henon":
|
|
160
|
+
x = henon_map(L, random_state=sd)
|
|
161
|
+
elif r == "periodic":
|
|
162
|
+
x = periodic(L, 7, noise=0.3, random_state=sd)
|
|
163
|
+
elif r == "fgn":
|
|
164
|
+
x = fgn(L, 0.85, random_state=sd)
|
|
165
|
+
elif r == "walk":
|
|
166
|
+
x = np.cumsum(np.random.default_rng(sd).normal(size=L))
|
|
167
|
+
else:
|
|
168
|
+
raise ValueError(f"unknown regime {r!r}")
|
|
169
|
+
if scale:
|
|
170
|
+
x = (x - x.mean()) / (x.std() or 1.0)
|
|
171
|
+
parts.append(x)
|
|
172
|
+
bps = np.cumsum(lengths)[:-1].tolist()
|
|
173
|
+
return np.concatenate(parts), [int(b) for b in bps]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def control_chart_patterns(n_per_class: int = 100, length: int = 60, *, mean: float = 30.0, sd: float = 2.0,
|
|
177
|
+
random_state=None) -> tuple[np.ndarray, np.ndarray]:
|
|
178
|
+
"""Six classic control chart patterns: normal, cyclic, increasing and decreasing trend, upward and downward shift.
|
|
179
|
+
|
|
180
|
+
The six classes are those of the synthetic control chart data set of the
|
|
181
|
+
UCI Machine Learning Repository; the parameters are ours, stated in units
|
|
182
|
+
of :math:`\\sigma`: Gaussian noise around ``mean``; plus a sine of
|
|
183
|
+
random amplitude (:math:`[1.5,2.5]\\,\\sigma`) and period (10–15 samples);
|
|
184
|
+
plus a linear trend of slope :math:`\\pm[0.1,0.25]\\,\\sigma` per sample;
|
|
185
|
+
plus a step of :math:`\\pm[1.5,3]\\,\\sigma` at a random time in the middle third.
|
|
186
|
+
|
|
187
|
+
Returns
|
|
188
|
+
-------
|
|
189
|
+
X : ndarray of shape (6 * n_per_class, length)
|
|
190
|
+
y : ndarray of str
|
|
191
|
+
``"normal"``, ``"cyclic"``, ``"up-trend"``, ``"down-trend"``, ``"up-shift"``, ``"down-shift"``.
|
|
192
|
+
"""
|
|
193
|
+
rng = np.random.default_rng(random_state)
|
|
194
|
+
t = np.arange(length)
|
|
195
|
+
X, y = [], []
|
|
196
|
+
for cls in ("normal", "cyclic", "up-trend", "down-trend", "up-shift", "down-shift"):
|
|
197
|
+
for _ in range(n_per_class):
|
|
198
|
+
x = mean + sd * rng.normal(size=length)
|
|
199
|
+
if cls == "cyclic":
|
|
200
|
+
x += sd * rng.uniform(1.5, 2.5) * np.sin(2 * np.pi * t / rng.uniform(10, 15) + rng.uniform(0, 2 * np.pi))
|
|
201
|
+
elif cls.endswith("trend"):
|
|
202
|
+
x += (1 if cls.startswith("up") else -1) * sd * rng.uniform(0.1, 0.25) * t
|
|
203
|
+
elif cls.endswith("shift"):
|
|
204
|
+
k = rng.integers(length // 3, 2 * length // 3)
|
|
205
|
+
x += (1 if cls.startswith("up") else -1) * sd * rng.uniform(1.5, 3.0) * (t >= k)
|
|
206
|
+
X.append(x)
|
|
207
|
+
y.append(cls)
|
|
208
|
+
return np.asarray(X), np.asarray(y)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def inject_shift(y, start: int, size: float, *, length: int | None = None) -> np.ndarray:
|
|
212
|
+
"""Add a step of ``size`` from ``start`` (for ``length`` samples, or to the end)."""
|
|
213
|
+
out = np.asarray(y, dtype=float).copy()
|
|
214
|
+
stop = out.size if length is None else start + length
|
|
215
|
+
out[start:stop] += size
|
|
216
|
+
return out
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Interactive figures (plotly, optional dependency).
|
|
2
|
+
|
|
3
|
+
``plotly_visibility`` draws the series with its lines of sight and hover
|
|
4
|
+
information on every observation (time, value, degree, label, horizons);
|
|
5
|
+
``plotly_arcs`` draws the arc diagram. ``write_html`` stores a figure as a
|
|
6
|
+
self-contained HTML page.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
from .core import VisibilityGraph
|
|
14
|
+
from .measures import horizons
|
|
15
|
+
|
|
16
|
+
__all__ = ["plotly_visibility", "plotly_arcs", "write_html"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _go():
|
|
20
|
+
try:
|
|
21
|
+
import plotly.graph_objects as go
|
|
22
|
+
except ImportError as exc: # pragma: no cover
|
|
23
|
+
raise ImportError("interactive figures need plotly: pip install 'visibgraph[interactive]'") from exc
|
|
24
|
+
return go
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def plotly_visibility(G: VisibilityGraph, *, max_edges: int = 5000, title: str | None = None):
|
|
28
|
+
"""Series with lines of sight; hovering a point shows its degree, label and horizons."""
|
|
29
|
+
go = _go()
|
|
30
|
+
e = G.edges
|
|
31
|
+
if e.shape[0] > max_edges:
|
|
32
|
+
e = e[np.sort(np.argsort(G.spans("index"))[-max_edges:])]
|
|
33
|
+
xs, ys = [], []
|
|
34
|
+
hv = "hvg" in G.kind
|
|
35
|
+
for i, j in e.tolist():
|
|
36
|
+
if hv:
|
|
37
|
+
h = min(G.y[i], G.y[j])
|
|
38
|
+
xs += [G.t[i], G.t[j], None]
|
|
39
|
+
ys += [h, h, None]
|
|
40
|
+
else:
|
|
41
|
+
xs += [G.t[i], G.t[j], None]
|
|
42
|
+
ys += [G.y[i], G.y[j], None]
|
|
43
|
+
k = G.degree()
|
|
44
|
+
left, right = horizons(G)
|
|
45
|
+
lab = [str(G.label_of(i)) for i in range(G.n_nodes)] if G.labels is not None else [""] * G.n_nodes
|
|
46
|
+
fig = go.Figure()
|
|
47
|
+
fig.add_trace(go.Scatter(x=xs, y=ys, mode="lines", line={"color": "rgba(40,87,128,0.35)", "width": 1}, hoverinfo="skip",
|
|
48
|
+
name="lines of sight"))
|
|
49
|
+
fig.add_trace(go.Scatter(
|
|
50
|
+
x=G.t, y=G.y, mode="lines+markers", line={"color": "#1F2A36", "width": 1},
|
|
51
|
+
marker={"size": 4 + 10 * (k - k.min()) / max(1, np.ptp(k)), "color": k, "colorscale": "Viridis", "showscale": True,
|
|
52
|
+
"colorbar": {"title": "degree"}},
|
|
53
|
+
customdata=np.column_stack([k, left, right, lab]),
|
|
54
|
+
hovertemplate="t=%{x}<br>y=%{y:.4g}<br>degree=%{customdata[0]}<br>horizon past=%{customdata[1]}<br>"
|
|
55
|
+
"horizon future=%{customdata[2]}<br>label=%{customdata[3]}<extra></extra>",
|
|
56
|
+
name="series"))
|
|
57
|
+
fig.update_layout(title=title or f"{G.kind.upper()} — {G.n_nodes} nodes, {G.n_edges} edges", template="simple_white",
|
|
58
|
+
showlegend=False, height=420)
|
|
59
|
+
return fig
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def plotly_arcs(G: VisibilityGraph, *, max_edges: int = 5000, title: str | None = None):
|
|
63
|
+
"""Interactive arc diagram (arcs above the time axis, height ~ square root of the span)."""
|
|
64
|
+
go = _go()
|
|
65
|
+
e = G.edges
|
|
66
|
+
if e.shape[0] > max_edges:
|
|
67
|
+
e = e[np.sort(np.argsort(G.spans("index"))[-max_edges:])]
|
|
68
|
+
T = (G.t[-1] - G.t[0]) or 1.0
|
|
69
|
+
th = np.linspace(0, np.pi, 20)
|
|
70
|
+
xs, ys = [], []
|
|
71
|
+
for i, j in e.tolist():
|
|
72
|
+
xm, r = (G.t[i] + G.t[j]) / 2, (G.t[j] - G.t[i]) / 2
|
|
73
|
+
h = np.sqrt((G.t[j] - G.t[i]) / T)
|
|
74
|
+
xs += list(xm - r * np.cos(th)) + [None]
|
|
75
|
+
ys += list(h * np.sin(th)) + [None]
|
|
76
|
+
fig = go.Figure(go.Scatter(x=xs, y=ys, mode="lines", line={"color": "rgba(40,87,128,0.45)", "width": 1}, hoverinfo="skip"))
|
|
77
|
+
fig.update_layout(title=title or f"Arc diagram ({G.kind.upper()})", template="simple_white", yaxis={"visible": False}, height=360)
|
|
78
|
+
return fig
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def write_html(fig, path) -> str:
|
|
82
|
+
"""Write a plotly figure as a self-contained HTML file; returns the path."""
|
|
83
|
+
fig.write_html(str(path), include_plotlyjs="cdn", full_html=True)
|
|
84
|
+
return str(path)
|