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.
Files changed (47) hide show
  1. visibgraph/__init__.py +136 -0
  2. visibgraph/accessor.py +64 -0
  3. visibgraph/construct/__init__.py +43 -0
  4. visibgraph/construct/horizontal.py +110 -0
  5. visibgraph/construct/multivariate.py +180 -0
  6. visibgraph/construct/natural.py +162 -0
  7. visibgraph/construct/penetrable.py +172 -0
  8. visibgraph/construct/spatial.py +145 -0
  9. visibgraph/construct/variants.py +152 -0
  10. visibgraph/construct/windows.py +128 -0
  11. visibgraph/core.py +441 -0
  12. visibgraph/datasets/__init__.py +49 -0
  13. visibgraph/datasets/_bundled.py +78 -0
  14. visibgraph/datasets/benchmark.py +229 -0
  15. visibgraph/datasets/data/README.md +35 -0
  16. visibgraph/datasets/data/lynx.csv +115 -0
  17. visibgraph/datasets/data/nile.csv +101 -0
  18. visibgraph/datasets/data/sunspots.csv +290 -0
  19. visibgraph/datasets/data/tep_d00.csv.gz +0 -0
  20. visibgraph/datasets/data/tep_d00_te.csv.gz +0 -0
  21. visibgraph/datasets/data/tep_d01_te.csv.gz +0 -0
  22. visibgraph/datasets/data/tep_d04_te.csv.gz +0 -0
  23. visibgraph/datasets/data/tep_d05_te.csv.gz +0 -0
  24. visibgraph/datasets/data/tep_d06_te.csv.gz +0 -0
  25. visibgraph/datasets/data/tep_d11_te.csv.gz +0 -0
  26. visibgraph/datasets/generators.py +216 -0
  27. visibgraph/interactive.py +84 -0
  28. visibgraph/io.py +153 -0
  29. visibgraph/labelled.py +263 -0
  30. visibgraph/measures.py +586 -0
  31. visibgraph/ml.py +179 -0
  32. visibgraph/networks.py +142 -0
  33. visibgraph/segment.py +642 -0
  34. visibgraph/spc.py +267 -0
  35. visibgraph/surrogates.py +147 -0
  36. visibgraph/symbolic.py +226 -0
  37. visibgraph/viz/__init__.py +62 -0
  38. visibgraph/viz/_style.py +92 -0
  39. visibgraph/viz/arcs.py +175 -0
  40. visibgraph/viz/distributions.py +202 -0
  41. visibgraph/viz/graphs.py +175 -0
  42. visibgraph/viz/multi.py +84 -0
  43. visibgraph/viz/series.py +223 -0
  44. visibgraph-1.0.dist-info/METADATA +181 -0
  45. visibgraph-1.0.dist-info/RECORD +47 -0
  46. visibgraph-1.0.dist-info/WHEEL +4 -0
  47. visibgraph-1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,229 @@
1
+ """The six-sigma visibility graph benchmark of Ferenczi, Kummer and Abonyi (2026).
2
+
3
+ Each series is drawn from :math:`N(0,1)` (length 100), its Phase-I statistics
4
+ are frozen, and exactly one Western Electric rule violation is injected
5
+ (classes 1–4, upward or downward), or none (class 0). Injected values of rules
6
+ 2–4 are kept below :math:`3\\sigma` so that they do not trigger rule 1. The
7
+ ground truth comes from the injector, not from a detector.
8
+
9
+ :func:`load_six_sigma_benchmark` is a line-by-line port of the published
10
+ generator (``Data_Generation_Westrules.py``,
11
+ github.com/DataCentricSE/Six-sigma-approach-based-visibility-graph) that draws
12
+ from private random number generators in the same order as the original, so
13
+ that with the default arguments it reproduces the published file
14
+ ``generated_timeseries_WE.csv`` (5000 series) value for value.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import random
20
+ from dataclasses import dataclass, field
21
+
22
+ import numpy as np
23
+ import pandas as pd
24
+
25
+ __all__ = ["SixSigmaBenchmark", "load_six_sigma_benchmark", "inject_we_violation"]
26
+
27
+
28
+ @dataclass
29
+ class SixSigmaBenchmark:
30
+ """Container of the benchmark.
31
+
32
+ Attributes
33
+ ----------
34
+ X : ndarray of shape (n_series, length)
35
+ rule : ndarray of int
36
+ Injected rule (0 = control).
37
+ direction : ndarray of str
38
+ ``"up"``, ``"down"`` or ``"none"``.
39
+ start, length : ndarray of int
40
+ Position and length of the injected pattern (-1, 0 for control).
41
+ mean, std : ndarray of float
42
+ Frozen Phase-I statistics of every series (before the injection).
43
+ """
44
+
45
+ X: np.ndarray
46
+ rule: np.ndarray
47
+ direction: np.ndarray
48
+ start: np.ndarray
49
+ length: np.ndarray
50
+ mean: np.ndarray
51
+ std: np.ndarray
52
+ params: dict = field(default_factory=dict)
53
+
54
+ def __len__(self) -> int:
55
+ return self.X.shape[0]
56
+
57
+ def __repr__(self) -> str:
58
+ counts = pd.Series(self.labels).value_counts().sort_index().to_dict()
59
+ return f"SixSigmaBenchmark({len(self)} series of length {self.X.shape[1]}, classes {counts})"
60
+
61
+ @property
62
+ def labels(self) -> np.ndarray:
63
+ """Class labels ``"0"``, ``"1up"``, ``"1down"``, ..., ``"4down"``."""
64
+ return np.array(["0" if r == 0 else f"{r}{d}" for r, d in zip(self.rule, self.direction)], dtype=object)
65
+
66
+ def baseline(self, i: int):
67
+ """Frozen :class:`~visibgraph.symbolic.Baseline` of series ``i``."""
68
+ from ..symbolic import Baseline
69
+
70
+ return Baseline(float(self.mean[i]), float(self.std[i]), "frozen")
71
+
72
+ def zones(self) -> np.ndarray:
73
+ """SPC zone codes of all series with their frozen baselines, shape like ``X``."""
74
+ z = (self.X - self.mean[:, None]) / self.std[:, None]
75
+ codes = np.empty(z.shape, dtype=np.int64)
76
+ lower = z <= 0
77
+ codes[lower] = np.select([z[lower] < -3, z[lower] < -2, z[lower] < -1], [0, 1, 2], 3)
78
+ codes[~lower] = np.select([z[~lower] < 1, z[~lower] < 2, z[~lower] < 3], [4, 5, 6], 7)
79
+ return codes
80
+
81
+ def windows(self, width: int = 10, random_state=0) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
82
+ """One window of ``width`` points per series, containing the injected pattern.
83
+
84
+ For a series with an injection the window start is drawn uniformly
85
+ among the positions for which the whole pattern lies inside the window;
86
+ for a control series it is drawn uniformly over the series. This is the
87
+ event-centred protocol of the case study of Ferenczi et al. (2026).
88
+
89
+ Returns
90
+ -------
91
+ Xw : ndarray of shape (n_series, width)
92
+ The windows.
93
+ Zw : ndarray of shape (n_series, width)
94
+ Their SPC zones (frozen baselines of the series).
95
+ starts : ndarray of int
96
+ Window starts.
97
+ """
98
+ rng = np.random.default_rng(random_state)
99
+ L = self.X.shape[1]
100
+ Z = self.zones()
101
+ starts = np.empty(len(self), dtype=np.int64)
102
+ for i in range(len(self)):
103
+ if self.rule[i] == 0:
104
+ starts[i] = rng.integers(0, L - width + 1)
105
+ else:
106
+ lo = max(0, self.start[i] + self.length[i] - width)
107
+ hi = min(self.start[i], L - width)
108
+ starts[i] = rng.integers(lo, hi + 1)
109
+ idx = starts[:, None] + np.arange(width)[None, :]
110
+ rows = np.arange(len(self))[:, None]
111
+ return self.X[rows, idx], Z[rows, idx], starts
112
+
113
+ def to_frame(self) -> pd.DataFrame:
114
+ """The benchmark in the column layout of the published CSV file."""
115
+ L = self.X.shape[1]
116
+ df = pd.DataFrame(self.X, columns=[f"V{i + 1}" for i in range(L)])
117
+ df["y_rule"] = self.rule
118
+ df["y_dir"] = self.direction
119
+ df["inj_start"] = self.start
120
+ df["inj_len"] = self.length
121
+ df["mean"] = self.mean
122
+ df["std_dev"] = self.std
123
+ df["UCL"] = self.mean + 3 * self.std
124
+ df["LCL"] = self.mean - 3 * self.std
125
+ return df
126
+
127
+
128
+ def _band(nr: np.random.RandomState, mean, std, lo_s, hi_s, side):
129
+ if side == "up":
130
+ return float(nr.uniform(mean + lo_s * std, mean + hi_s * std))
131
+ return float(nr.uniform(mean - hi_s * std, mean - lo_s * std))
132
+
133
+
134
+ def _clamp(x, mean, std, side, max_sigma=2.9):
135
+ return min(x, mean + max_sigma * std) if side == "up" else max(x, mean - max_sigma * std)
136
+
137
+
138
+ def inject_we_violation(ts: np.ndarray, rule: int, mean: float, std: float, nr: np.random.RandomState, pr: random.Random):
139
+ """Inject one Western Electric violation as in the published generator.
140
+
141
+ Returns ``(new_series, direction, start, length)``. ``nr`` and ``pr`` are the
142
+ NumPy and Python generators (both are needed to reproduce the original
143
+ stream of random numbers).
144
+ """
145
+ new = ts.copy()
146
+ L = len(ts)
147
+ if rule == 0:
148
+ return new, "none", -1, 0
149
+ if rule == 1:
150
+ idx = pr.randint(0, L - 1)
151
+ side = pr.choice(["up", "down"])
152
+ margin = nr.uniform(0.05, 0.8)
153
+ new[idx] = mean + (3 + margin) * std if side == "up" else mean - (3 + margin) * std
154
+ return new, side, idx, 1
155
+ if rule == 2:
156
+ start = pr.randint(0, L - 3)
157
+ side = pr.choice(["up", "down"])
158
+ idxs = [start, start + 1, start + 2]
159
+ two = pr.sample(idxs, 2)
160
+ third = [i for i in idxs if i not in two][0]
161
+ for i in two:
162
+ new[i] = _band(nr, mean, std, 2.0, 2.95, side)
163
+ new[third] = _band(nr, mean, std, 0.25, 1.75, side)
164
+ return new, side, start, 3
165
+ if rule == 3:
166
+ start = pr.randint(0, L - 5)
167
+ side = pr.choice(["up", "down"])
168
+ idxs = [start + k for k in range(5)]
169
+ four = pr.sample(idxs, 4)
170
+ one = [i for i in idxs if i not in four][0]
171
+ for i in four:
172
+ new[i] = _clamp(_band(nr, mean, std, 1.05, 2.8, side), mean, std, side)
173
+ new[one] = _band(nr, mean, std, 0.1, 0.95, side)
174
+ return new, side, start, 5
175
+ if rule == 4:
176
+ start = pr.randint(0, L - 8)
177
+ side = pr.choice(["up", "down"])
178
+ for k in range(8):
179
+ r = nr.rand()
180
+ lo, hi = (0.1, 0.9) if r < 0.6 else ((1.0, 1.9) if r < 0.9 else (2.0, 2.6))
181
+ new[start + k] = _clamp(_band(nr, mean, std, lo, hi, side), mean, std, side)
182
+ return new, side, start, 8
183
+ raise ValueError("rule must be 0..4")
184
+
185
+
186
+ def load_six_sigma_benchmark(n_per_class: int = 1000, length: int = 100, *, classes=(0, 1, 2, 3, 4), seed: int = 42) -> SixSigmaBenchmark:
187
+ """Regenerate the benchmark of Ferenczi, Kummer and Abonyi (2026).
188
+
189
+ Parameters
190
+ ----------
191
+ n_per_class : int
192
+ Series per class (1000 in the paper).
193
+ length : int
194
+ Length of a series (100 in the paper).
195
+ classes : tuple of int
196
+ Rules to include (0 = control).
197
+ seed : int
198
+ Seed of both generators (42 in the paper).
199
+
200
+ Returns
201
+ -------
202
+ SixSigmaBenchmark
203
+
204
+ Examples
205
+ --------
206
+ >>> from visibgraph.datasets import load_six_sigma_benchmark
207
+ >>> b = load_six_sigma_benchmark(n_per_class=2)
208
+ >>> b.X.shape, b.labels[:4].tolist()
209
+ ((10, 100), ['0', '0', '1up', '1down'])
210
+ """
211
+ nr = np.random.RandomState(seed)
212
+ pr = random.Random(seed)
213
+ X, R, D, S, Ln, M, SD = [], [], [], [], [], [], []
214
+ for r in classes:
215
+ for _ in range(n_per_class):
216
+ ts = nr.normal(0, 1, length)
217
+ mean, std = float(np.mean(ts)), float(np.std(ts, ddof=1))
218
+ new, side, start, ln = inject_we_violation(ts, r, mean, std, nr, pr)
219
+ X.append(new)
220
+ R.append(r)
221
+ D.append(side)
222
+ S.append(start)
223
+ Ln.append(ln)
224
+ M.append(mean)
225
+ SD.append(std)
226
+ return SixSigmaBenchmark(
227
+ np.asarray(X), np.asarray(R), np.asarray(D, dtype=object), np.asarray(S), np.asarray(Ln), np.asarray(M), np.asarray(SD),
228
+ params={"n_per_class": n_per_class, "length": length, "classes": tuple(classes), "seed": seed},
229
+ )
@@ -0,0 +1,35 @@
1
+ # Bundled data sets of visibgraph
2
+
3
+ | File | Content | Source and licence |
4
+ |---|---|---|
5
+ | `nile.csv` | Annual flow of the river Nile at Aswan, 1871–1970, in 10^8 m^3 (100 values), with an apparent change point near 1898 | Cobb, G. W. (1978), *Biometrika* 65, 243–251; Durbin, J. and Koopman, S. J. (2001), *Time Series Analysis by State Space Methods*, Oxford University Press. Taken from the R `datasets` package (`Nile`) via the Rdatasets collection (github.com/vincentarelbundock/Rdatasets). |
6
+ | `lynx.csv` | Annual numbers of lynx trappings in Canada, 1821–1934 (114 values) | Brockwell, P. J. and Davis, R. A. (1991), *Time Series: Theory and Methods*, Springer, Series G; Campbell, M. J. and Walker, A. M. (1977), *J. R. Stat. Soc. A* 140, 411–431. R `datasets` package (`lynx`). |
7
+ | `sunspots.csv` | Yearly sunspot numbers, 1700–1988 (289 values) | Tong, H. (1996), *Non-Linear Time Series*, Clarendon Press, p. 471. R `datasets` package (`sunspot.year`). Updated series: WDC-SILSO, Royal Observatory of Belgium. |
8
+ | `tep_d00.csv.gz` | Tennessee Eastman process, normal operation, training set (500 samples × 52 variables, XMEAS1–41, XMV1–11, sampling interval 3 min) | Downs, J. J. and Vogel, E. F. (1993), *Comput. Chem. Eng.* 17, 245–255; data of Chiang, L. H., Russell, E. L. and Braatz, R. D. (2001), *Fault Detection and Diagnosis in Industrial Systems*, Springer. Copyright (c) 1998–2002 The Board of Trustees of the University of Illinois; redistributed under its permissive licence (see below). The original `d00.dat` is stored transposed; here it is one row per sample. |
9
+ | `tep_dXX_te.csv.gz` | Test sets (960 samples × 52 variables): normal operation (`d00_te`) and faults 1, 4, 5, 6 and 11; the fault is introduced after sample 160 (8 h) | same as above |
10
+
11
+ Licence notice of the Tennessee Eastman data (University of Illinois):
12
+
13
+ > Copyright (c) 1998-2002 The Board of Trustees of the University of Illinois. All rights reserved.
14
+ > Developed by: Large Scale Systems Research Laboratory, Professor Richard Braatz, Director,
15
+ > Department of Chemical Engineering, University of Illinois.
16
+ > Permission hereby granted, free of charge, to any person obtaining a copy of this software and
17
+ > associated documentation files (the "Software"), to deal with the Software without restriction,
18
+ > including without limitation the rights to use, copy, modify, merge, publish, distribute,
19
+ > sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
20
+ > furnished to do so, subject to the following conditions: 1. Redistributions of source code must
21
+ > retain the above copyright notice, this list of conditions and the following disclaimers.
22
+ > 2. Redistributions in binary form must reproduce the above copyright notice, this list of
23
+ > conditions and the following disclaimers in the documentation and/or other materials provided
24
+ > with the distribution. 3. Neither the names of Large Scale Research Systems Laboratory,
25
+ > University of Illinois, nor the names of its contributors may be used to endorse or promote
26
+ > products derived from this Software without specific prior written permission.
27
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
28
+ > NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29
+ > NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
30
+ > DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
+
33
+ The six-sigma benchmark of Ferenczi, Kummer and Abonyi (2026) is not stored: it is regenerated
34
+ bit for bit by `visibgraph.datasets.load_six_sigma_benchmark()` from the published generator
35
+ (github.com/DataCentricSE/Six-sigma-approach-based-visibility-graph).
@@ -0,0 +1,115 @@
1
+ year,trappings
2
+ 1821,269
3
+ 1822,321
4
+ 1823,585
5
+ 1824,871
6
+ 1825,1475
7
+ 1826,2821
8
+ 1827,3928
9
+ 1828,5943
10
+ 1829,4950
11
+ 1830,2577
12
+ 1831,523
13
+ 1832,98
14
+ 1833,184
15
+ 1834,279
16
+ 1835,409
17
+ 1836,2285
18
+ 1837,2685
19
+ 1838,3409
20
+ 1839,1824
21
+ 1840,409
22
+ 1841,151
23
+ 1842,45
24
+ 1843,68
25
+ 1844,213
26
+ 1845,546
27
+ 1846,1033
28
+ 1847,2129
29
+ 1848,2536
30
+ 1849,957
31
+ 1850,361
32
+ 1851,377
33
+ 1852,225
34
+ 1853,360
35
+ 1854,731
36
+ 1855,1638
37
+ 1856,2725
38
+ 1857,2871
39
+ 1858,2119
40
+ 1859,684
41
+ 1860,299
42
+ 1861,236
43
+ 1862,245
44
+ 1863,552
45
+ 1864,1623
46
+ 1865,3311
47
+ 1866,6721
48
+ 1867,4254
49
+ 1868,687
50
+ 1869,255
51
+ 1870,473
52
+ 1871,358
53
+ 1872,784
54
+ 1873,1594
55
+ 1874,1676
56
+ 1875,2251
57
+ 1876,1426
58
+ 1877,756
59
+ 1878,299
60
+ 1879,201
61
+ 1880,229
62
+ 1881,469
63
+ 1882,736
64
+ 1883,2042
65
+ 1884,2811
66
+ 1885,4431
67
+ 1886,2511
68
+ 1887,389
69
+ 1888,73
70
+ 1889,39
71
+ 1890,49
72
+ 1891,59
73
+ 1892,188
74
+ 1893,377
75
+ 1894,1292
76
+ 1895,4031
77
+ 1896,3495
78
+ 1897,587
79
+ 1898,105
80
+ 1899,153
81
+ 1900,387
82
+ 1901,758
83
+ 1902,1307
84
+ 1903,3465
85
+ 1904,6991
86
+ 1905,6313
87
+ 1906,3794
88
+ 1907,1836
89
+ 1908,345
90
+ 1909,382
91
+ 1910,808
92
+ 1911,1388
93
+ 1912,2713
94
+ 1913,3800
95
+ 1914,3091
96
+ 1915,2985
97
+ 1916,3790
98
+ 1917,674
99
+ 1918,81
100
+ 1919,80
101
+ 1920,108
102
+ 1921,229
103
+ 1922,399
104
+ 1923,1132
105
+ 1924,2432
106
+ 1925,3574
107
+ 1926,2935
108
+ 1927,1537
109
+ 1928,529
110
+ 1929,485
111
+ 1930,662
112
+ 1931,1000
113
+ 1932,1590
114
+ 1933,2657
115
+ 1934,3396
@@ -0,0 +1,101 @@
1
+ year,flow
2
+ 1871,1120
3
+ 1872,1160
4
+ 1873,963
5
+ 1874,1210
6
+ 1875,1160
7
+ 1876,1160
8
+ 1877,813
9
+ 1878,1230
10
+ 1879,1370
11
+ 1880,1140
12
+ 1881,995
13
+ 1882,935
14
+ 1883,1110
15
+ 1884,994
16
+ 1885,1020
17
+ 1886,960
18
+ 1887,1180
19
+ 1888,799
20
+ 1889,958
21
+ 1890,1140
22
+ 1891,1100
23
+ 1892,1210
24
+ 1893,1150
25
+ 1894,1250
26
+ 1895,1260
27
+ 1896,1220
28
+ 1897,1030
29
+ 1898,1100
30
+ 1899,774
31
+ 1900,840
32
+ 1901,874
33
+ 1902,694
34
+ 1903,940
35
+ 1904,833
36
+ 1905,701
37
+ 1906,916
38
+ 1907,692
39
+ 1908,1020
40
+ 1909,1050
41
+ 1910,969
42
+ 1911,831
43
+ 1912,726
44
+ 1913,456
45
+ 1914,824
46
+ 1915,702
47
+ 1916,1120
48
+ 1917,1100
49
+ 1918,832
50
+ 1919,764
51
+ 1920,821
52
+ 1921,768
53
+ 1922,845
54
+ 1923,864
55
+ 1924,862
56
+ 1925,698
57
+ 1926,845
58
+ 1927,744
59
+ 1928,796
60
+ 1929,1040
61
+ 1930,759
62
+ 1931,781
63
+ 1932,865
64
+ 1933,845
65
+ 1934,944
66
+ 1935,984
67
+ 1936,897
68
+ 1937,822
69
+ 1938,1010
70
+ 1939,771
71
+ 1940,676
72
+ 1941,649
73
+ 1942,846
74
+ 1943,812
75
+ 1944,742
76
+ 1945,801
77
+ 1946,1040
78
+ 1947,860
79
+ 1948,874
80
+ 1949,848
81
+ 1950,890
82
+ 1951,744
83
+ 1952,749
84
+ 1953,838
85
+ 1954,1050
86
+ 1955,918
87
+ 1956,986
88
+ 1957,797
89
+ 1958,923
90
+ 1959,975
91
+ 1960,815
92
+ 1961,1020
93
+ 1962,906
94
+ 1963,901
95
+ 1964,1170
96
+ 1965,912
97
+ 1966,746
98
+ 1967,919
99
+ 1968,718
100
+ 1969,714
101
+ 1970,740