pysad-kernels 0.1.0__tar.gz
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.
- pysad_kernels-0.1.0/Cargo.lock +228 -0
- pysad_kernels-0.1.0/Cargo.toml +26 -0
- pysad_kernels-0.1.0/PKG-INFO +53 -0
- pysad_kernels-0.1.0/README.md +27 -0
- pysad_kernels-0.1.0/deny.toml +25 -0
- pysad_kernels-0.1.0/pyproject.toml +37 -0
- pysad_kernels-0.1.0/src/lib.rs +369 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 3
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.1"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "heck"
|
|
13
|
+
version = "0.5.0"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "libc"
|
|
19
|
+
version = "0.2.191"
|
|
20
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
21
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
22
|
+
|
|
23
|
+
[[package]]
|
|
24
|
+
name = "matrixmultiply"
|
|
25
|
+
version = "0.3.11"
|
|
26
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
27
|
+
checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7"
|
|
28
|
+
dependencies = [
|
|
29
|
+
"autocfg",
|
|
30
|
+
"rawpointer",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[[package]]
|
|
34
|
+
name = "ndarray"
|
|
35
|
+
version = "0.17.2"
|
|
36
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
37
|
+
checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
|
|
38
|
+
dependencies = [
|
|
39
|
+
"matrixmultiply",
|
|
40
|
+
"num-complex",
|
|
41
|
+
"num-integer",
|
|
42
|
+
"num-traits",
|
|
43
|
+
"portable-atomic",
|
|
44
|
+
"portable-atomic-util",
|
|
45
|
+
"rawpointer",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[[package]]
|
|
49
|
+
name = "num-complex"
|
|
50
|
+
version = "0.4.6"
|
|
51
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
52
|
+
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
|
53
|
+
dependencies = [
|
|
54
|
+
"num-traits",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[[package]]
|
|
58
|
+
name = "num-integer"
|
|
59
|
+
version = "0.1.47"
|
|
60
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
61
|
+
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
|
|
62
|
+
dependencies = [
|
|
63
|
+
"num-traits",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[[package]]
|
|
67
|
+
name = "num-traits"
|
|
68
|
+
version = "0.2.19"
|
|
69
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
70
|
+
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|
71
|
+
dependencies = [
|
|
72
|
+
"autocfg",
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[[package]]
|
|
76
|
+
name = "numpy"
|
|
77
|
+
version = "0.30.0"
|
|
78
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
79
|
+
checksum = "aa0b9111761885449e894981fa8051dcf69e0bd114aecbd4bcdd830b6be4ed92"
|
|
80
|
+
dependencies = [
|
|
81
|
+
"libc",
|
|
82
|
+
"ndarray",
|
|
83
|
+
"num-complex",
|
|
84
|
+
"num-integer",
|
|
85
|
+
"num-traits",
|
|
86
|
+
"pyo3",
|
|
87
|
+
"pyo3-build-config",
|
|
88
|
+
"rustc-hash",
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
[[package]]
|
|
92
|
+
name = "once_cell"
|
|
93
|
+
version = "1.21.4"
|
|
94
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
95
|
+
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
96
|
+
|
|
97
|
+
[[package]]
|
|
98
|
+
name = "portable-atomic"
|
|
99
|
+
version = "1.15.0"
|
|
100
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
101
|
+
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
|
|
102
|
+
|
|
103
|
+
[[package]]
|
|
104
|
+
name = "portable-atomic-util"
|
|
105
|
+
version = "0.2.8"
|
|
106
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
107
|
+
checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715"
|
|
108
|
+
dependencies = [
|
|
109
|
+
"portable-atomic",
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
[[package]]
|
|
113
|
+
name = "proc-macro2"
|
|
114
|
+
version = "1.0.107"
|
|
115
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
116
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
117
|
+
dependencies = [
|
|
118
|
+
"unicode-ident",
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
[[package]]
|
|
122
|
+
name = "pyo3"
|
|
123
|
+
version = "0.30.1"
|
|
124
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
125
|
+
checksum = "229ca13b8a15bb0d1487b81ab975bfcd97513416499a8312011b6036e42e0de4"
|
|
126
|
+
dependencies = [
|
|
127
|
+
"libc",
|
|
128
|
+
"once_cell",
|
|
129
|
+
"portable-atomic",
|
|
130
|
+
"pyo3-build-config",
|
|
131
|
+
"pyo3-ffi",
|
|
132
|
+
"pyo3-macros",
|
|
133
|
+
]
|
|
134
|
+
|
|
135
|
+
[[package]]
|
|
136
|
+
name = "pyo3-build-config"
|
|
137
|
+
version = "0.30.1"
|
|
138
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
139
|
+
checksum = "70130e2b6dd1ee589a8788026a429561ca5c40fa7a2fa7914c2282eddae24700"
|
|
140
|
+
dependencies = [
|
|
141
|
+
"target-lexicon",
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
[[package]]
|
|
145
|
+
name = "pyo3-ffi"
|
|
146
|
+
version = "0.30.1"
|
|
147
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
148
|
+
checksum = "5e7ff73c555c286d212d4ce7e5fbda845a973c4f61bd5f160f5f2c3c2a04d1c4"
|
|
149
|
+
dependencies = [
|
|
150
|
+
"libc",
|
|
151
|
+
"pyo3-build-config",
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
[[package]]
|
|
155
|
+
name = "pyo3-macros"
|
|
156
|
+
version = "0.30.1"
|
|
157
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
158
|
+
checksum = "c46d2e5862c7bb8207e3b639f713dda3c499933444784154c11d013c5a691b60"
|
|
159
|
+
dependencies = [
|
|
160
|
+
"proc-macro2",
|
|
161
|
+
"pyo3-macros-backend",
|
|
162
|
+
"quote",
|
|
163
|
+
"syn",
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
[[package]]
|
|
167
|
+
name = "pyo3-macros-backend"
|
|
168
|
+
version = "0.30.1"
|
|
169
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
170
|
+
checksum = "38b89f066f2ae72a2aef52d1537ca2fcd104440ebfadee06e97aa90738a25b9b"
|
|
171
|
+
dependencies = [
|
|
172
|
+
"heck",
|
|
173
|
+
"proc-macro2",
|
|
174
|
+
"quote",
|
|
175
|
+
"syn",
|
|
176
|
+
]
|
|
177
|
+
|
|
178
|
+
[[package]]
|
|
179
|
+
name = "pysad_kernels"
|
|
180
|
+
version = "0.1.0"
|
|
181
|
+
dependencies = [
|
|
182
|
+
"numpy",
|
|
183
|
+
"pyo3",
|
|
184
|
+
]
|
|
185
|
+
|
|
186
|
+
[[package]]
|
|
187
|
+
name = "quote"
|
|
188
|
+
version = "1.0.47"
|
|
189
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
190
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
191
|
+
dependencies = [
|
|
192
|
+
"proc-macro2",
|
|
193
|
+
]
|
|
194
|
+
|
|
195
|
+
[[package]]
|
|
196
|
+
name = "rawpointer"
|
|
197
|
+
version = "0.2.1"
|
|
198
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
199
|
+
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
|
|
200
|
+
|
|
201
|
+
[[package]]
|
|
202
|
+
name = "rustc-hash"
|
|
203
|
+
version = "2.1.3"
|
|
204
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
205
|
+
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
|
206
|
+
|
|
207
|
+
[[package]]
|
|
208
|
+
name = "syn"
|
|
209
|
+
version = "3.0.10"
|
|
210
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
211
|
+
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
|
212
|
+
dependencies = [
|
|
213
|
+
"proc-macro2",
|
|
214
|
+
"quote",
|
|
215
|
+
"unicode-ident",
|
|
216
|
+
]
|
|
217
|
+
|
|
218
|
+
[[package]]
|
|
219
|
+
name = "target-lexicon"
|
|
220
|
+
version = "0.13.6"
|
|
221
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
222
|
+
checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da"
|
|
223
|
+
|
|
224
|
+
[[package]]
|
|
225
|
+
name = "unicode-ident"
|
|
226
|
+
version = "1.0.27"
|
|
227
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
228
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "pysad_kernels"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
rust-version = "1.74"
|
|
6
|
+
description = "Optional Rust kernels for PySAD streaming anomaly detection"
|
|
7
|
+
license = "BSD-3-Clause"
|
|
8
|
+
repository = "https://github.com/selimfirat/pysad"
|
|
9
|
+
publish = true
|
|
10
|
+
|
|
11
|
+
[lib]
|
|
12
|
+
name = "pysad_kernels"
|
|
13
|
+
crate-type = ["cdylib"]
|
|
14
|
+
|
|
15
|
+
[features]
|
|
16
|
+
default = ["extension-module"]
|
|
17
|
+
extension-module = ["pyo3/extension-module"]
|
|
18
|
+
|
|
19
|
+
[dependencies]
|
|
20
|
+
numpy = "0.30"
|
|
21
|
+
pyo3 = { version = "0.30", features = ["abi3-py311"] }
|
|
22
|
+
|
|
23
|
+
[profile.release]
|
|
24
|
+
lto = true
|
|
25
|
+
codegen-units = 1
|
|
26
|
+
opt-level = 3
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pysad_kernels
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Development Status :: 4 - Beta
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: Intended Audience :: Science/Research
|
|
7
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
10
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Rust
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Summary: Optional Rust kernels for PySAD streaming anomaly detection
|
|
18
|
+
License: BSD-3-Clause
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
21
|
+
Project-URL: Homepage, https://github.com/selimfirat/pysad
|
|
22
|
+
Project-URL: Documentation, https://pysad.readthedocs.io/
|
|
23
|
+
Project-URL: Repository, https://github.com/selimfirat/pysad/tree/master/rust
|
|
24
|
+
Project-URL: Bug Tracker, https://github.com/selimfirat/pysad/issues
|
|
25
|
+
|
|
26
|
+
# pysad_kernels
|
|
27
|
+
|
|
28
|
+
Optional Rust (PyO3) native kernels for [PySAD](https://github.com/selimfirat/pysad) streaming anomaly detection.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
Install alongside PySAD via the `kernels` extra or directly:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install "pysad[kernels]"
|
|
36
|
+
# or
|
|
37
|
+
pip install pysad-kernels
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
PySAD automatically detects `pysad_kernels` at runtime and transparently falls back to NumPy when it is not installed.
|
|
41
|
+
|
|
42
|
+
## Accelerated Kernels
|
|
43
|
+
|
|
44
|
+
- `HalfSpaceTrees`: `half_space_trees_path` routes instances through tree heaps.
|
|
45
|
+
- `RobustRandomCutForest`: `rrcf_route_to_leaf` and `rrcf_codisp_sum` accelerate dynamic tree traversal and collusive displacement scoring.
|
|
46
|
+
- `LODA`: `loda_neg_log_densities` accelerates projection density scoring.
|
|
47
|
+
- `StreamKMPlusPlus`: `streamkm_lloyd` accelerates weighted Lloyd coreset clustering.
|
|
48
|
+
|
|
49
|
+
## Links
|
|
50
|
+
|
|
51
|
+
- Documentation: https://pysad.readthedocs.io/
|
|
52
|
+
- Repository: https://github.com/selimfirat/pysad
|
|
53
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# pysad_kernels
|
|
2
|
+
|
|
3
|
+
Optional Rust (PyO3) native kernels for [PySAD](https://github.com/selimfirat/pysad) streaming anomaly detection.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install alongside PySAD via the `kernels` extra or directly:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install "pysad[kernels]"
|
|
11
|
+
# or
|
|
12
|
+
pip install pysad-kernels
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
PySAD automatically detects `pysad_kernels` at runtime and transparently falls back to NumPy when it is not installed.
|
|
16
|
+
|
|
17
|
+
## Accelerated Kernels
|
|
18
|
+
|
|
19
|
+
- `HalfSpaceTrees`: `half_space_trees_path` routes instances through tree heaps.
|
|
20
|
+
- `RobustRandomCutForest`: `rrcf_route_to_leaf` and `rrcf_codisp_sum` accelerate dynamic tree traversal and collusive displacement scoring.
|
|
21
|
+
- `LODA`: `loda_neg_log_densities` accelerates projection density scoring.
|
|
22
|
+
- `StreamKMPlusPlus`: `streamkm_lloyd` accelerates weighted Lloyd coreset clustering.
|
|
23
|
+
|
|
24
|
+
## Links
|
|
25
|
+
|
|
26
|
+
- Documentation: https://pysad.readthedocs.io/
|
|
27
|
+
- Repository: https://github.com/selimfirat/pysad
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[graph]
|
|
2
|
+
all-features = true
|
|
3
|
+
|
|
4
|
+
[advisories]
|
|
5
|
+
ignore = []
|
|
6
|
+
|
|
7
|
+
[licenses]
|
|
8
|
+
allow = [
|
|
9
|
+
"MIT",
|
|
10
|
+
"Apache-2.0",
|
|
11
|
+
"Apache-2.0 WITH LLVM-exception",
|
|
12
|
+
"BSD-2-Clause",
|
|
13
|
+
"BSD-3-Clause",
|
|
14
|
+
"Unicode-3.0",
|
|
15
|
+
]
|
|
16
|
+
confidence-threshold = 0.8
|
|
17
|
+
|
|
18
|
+
[bans]
|
|
19
|
+
multiple-versions = "warn"
|
|
20
|
+
wildcards = "allow"
|
|
21
|
+
|
|
22
|
+
[sources]
|
|
23
|
+
unknown-registry = "deny"
|
|
24
|
+
unknown-git = "deny"
|
|
25
|
+
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.8,<2"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pysad_kernels"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Optional Rust kernels for PySAD streaming anomaly detection"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Intended Audience :: Science/Research",
|
|
15
|
+
"License :: OSI Approved :: BSD License",
|
|
16
|
+
"Operating System :: POSIX :: Linux",
|
|
17
|
+
"Operating System :: MacOS :: MacOS X",
|
|
18
|
+
"Operating System :: Microsoft :: Windows",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Programming Language :: Rust",
|
|
24
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/selimfirat/pysad"
|
|
30
|
+
Documentation = "https://pysad.readthedocs.io/"
|
|
31
|
+
Repository = "https://github.com/selimfirat/pysad/tree/master/rust"
|
|
32
|
+
"Bug Tracker" = "https://github.com/selimfirat/pysad/issues"
|
|
33
|
+
|
|
34
|
+
[tool.maturin]
|
|
35
|
+
features = ["extension-module"]
|
|
36
|
+
bindings = "pyo3"
|
|
37
|
+
skip-auditwheel = false
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
//! Optional native kernels for PySAD.
|
|
2
|
+
//!
|
|
3
|
+
//! This module contains tree, histogram, and clustering kernels:
|
|
4
|
+
//!
|
|
5
|
+
//! 1. `half_space_trees_path` - routes one point through all half-space trees
|
|
6
|
+
//! in the heap layout used by `HalfSpaceTrees`.
|
|
7
|
+
//!
|
|
8
|
+
//! 2. RRCF (Robust Random Cut Forest) kernels for tree traversal and collusive
|
|
9
|
+
//! displacement (CoDisp) computation. The RRCF trees use a dynamic
|
|
10
|
+
//! structure-of-arrays layout with explicit parent/child/sibling pointers.
|
|
11
|
+
//!
|
|
12
|
+
//! 3. LODA density scoring and StreamKM++ weighted Lloyd iterations.
|
|
13
|
+
|
|
14
|
+
use numpy::ndarray::{Array2, ArrayView1, ArrayView2};
|
|
15
|
+
use numpy::{PyArray1, PyArray2, PyReadonlyArray1, PyReadonlyArray2};
|
|
16
|
+
use pyo3::exceptions::PyValueError;
|
|
17
|
+
use pyo3::prelude::*;
|
|
18
|
+
|
|
19
|
+
/// Compute the negative log density of each LODA projection.
|
|
20
|
+
///
|
|
21
|
+
/// Args:
|
|
22
|
+
/// indices: Bin indices with shape `(num_projections,)` and dtype `float64`.
|
|
23
|
+
/// histograms: Counts with shape `(num_projections, num_bins)` and dtype `float64`.
|
|
24
|
+
/// widths: Bin widths with shape `(num_projections,)` and dtype `float64`.
|
|
25
|
+
/// num_seen: Number of points in these histograms.
|
|
26
|
+
///
|
|
27
|
+
/// Returns:
|
|
28
|
+
/// A `float64` array of shape `(num_projections,)`. Zero-width histograms return zero.
|
|
29
|
+
#[pyfunction]
|
|
30
|
+
fn loda_neg_log_densities(
|
|
31
|
+
py: Python<'_>,
|
|
32
|
+
indices: PyReadonlyArray1<'_, f64>,
|
|
33
|
+
histograms: PyReadonlyArray2<'_, f64>,
|
|
34
|
+
widths: PyReadonlyArray1<'_, f64>,
|
|
35
|
+
num_seen: i64,
|
|
36
|
+
) -> PyResult<Py<PyArray1<f64>>> {
|
|
37
|
+
let indices = indices.as_array();
|
|
38
|
+
let histograms = histograms.as_array();
|
|
39
|
+
let widths = widths.as_array();
|
|
40
|
+
let num_bins = histograms.ncols();
|
|
41
|
+
if indices.len() != histograms.nrows() || widths.len() != indices.len() {
|
|
42
|
+
return Err(PyValueError::new_err(
|
|
43
|
+
"indices, histograms, and widths must have the same number of projections.",
|
|
44
|
+
));
|
|
45
|
+
}
|
|
46
|
+
let mut densities = vec![0.0; indices.len()];
|
|
47
|
+
for i in 0..indices.len() {
|
|
48
|
+
if widths[i] > 0.0 {
|
|
49
|
+
let index = indices[i];
|
|
50
|
+
let count = if index >= 0.0 && index < num_bins as f64 {
|
|
51
|
+
histograms[[i, index as usize]]
|
|
52
|
+
} else {
|
|
53
|
+
0.0
|
|
54
|
+
};
|
|
55
|
+
densities[i] =
|
|
56
|
+
-((count + 1.0) / ((num_seen as f64 + num_bins as f64) * widths[i])).ln();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
Ok(PyArray1::from_vec(py, densities).into())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn nearest_center(
|
|
63
|
+
points: ArrayView2<'_, f64>,
|
|
64
|
+
centers: ArrayView2<'_, f64>,
|
|
65
|
+
sq_norm: f64,
|
|
66
|
+
center_norms: &[f64],
|
|
67
|
+
point_idx: usize,
|
|
68
|
+
) -> usize {
|
|
69
|
+
let num_features = centers.ncols();
|
|
70
|
+
let mut nearest = 0;
|
|
71
|
+
let mut best = f64::INFINITY;
|
|
72
|
+
for (c, ¢er_norm) in center_norms.iter().enumerate() {
|
|
73
|
+
let mut twice_dot = 0.0;
|
|
74
|
+
for f in 0..num_features {
|
|
75
|
+
twice_dot += (2.0 * points[[point_idx, f]]) * centers[[c, f]];
|
|
76
|
+
}
|
|
77
|
+
let distance = (sq_norm - twice_dot + center_norm).max(0.0);
|
|
78
|
+
if distance < best {
|
|
79
|
+
best = distance;
|
|
80
|
+
nearest = c;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
nearest
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fn recompute_centers(centers: &mut Array2<f64>, sums: &[f64], counts: &[f64]) -> bool {
|
|
87
|
+
let num_centers = centers.nrows();
|
|
88
|
+
let num_features = centers.ncols();
|
|
89
|
+
let mut changed = false;
|
|
90
|
+
for c in 0..num_centers {
|
|
91
|
+
if counts[c] <= 0.0 {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
for f in 0..num_features {
|
|
95
|
+
let value = sums[c * num_features + f] / counts[c];
|
|
96
|
+
changed |= value != centers[[c, f]];
|
|
97
|
+
centers[[c, f]] = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
changed
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// Run weighted Lloyd iterations from the supplied centers without changing the inputs.
|
|
104
|
+
///
|
|
105
|
+
/// Args:
|
|
106
|
+
/// points: Points with shape `(num_points, num_features)` and dtype `float64`.
|
|
107
|
+
/// weights: Point weights with shape `(num_points,)` and dtype `float64`.
|
|
108
|
+
/// centers: Initial centers with shape `(num_centers, num_features)` and dtype `float64`.
|
|
109
|
+
/// num_iterations: Maximum number of Lloyd iterations.
|
|
110
|
+
///
|
|
111
|
+
/// Returns:
|
|
112
|
+
/// Centers with shape `(num_centers, num_features)` and dtype `float64`.
|
|
113
|
+
/// Empty clusters keep their old centers. Distance ties select the first center.
|
|
114
|
+
#[pyfunction]
|
|
115
|
+
fn streamkm_lloyd(
|
|
116
|
+
py: Python<'_>,
|
|
117
|
+
points: PyReadonlyArray2<'_, f64>,
|
|
118
|
+
weights: PyReadonlyArray1<'_, f64>,
|
|
119
|
+
centers: PyReadonlyArray2<'_, f64>,
|
|
120
|
+
num_iterations: usize,
|
|
121
|
+
) -> PyResult<Py<PyArray2<f64>>> {
|
|
122
|
+
let points = points.as_array();
|
|
123
|
+
let weights = weights.as_array();
|
|
124
|
+
let mut centers = centers.as_array().to_owned();
|
|
125
|
+
let (num_points, num_features) = points.dim();
|
|
126
|
+
let num_centers = centers.nrows();
|
|
127
|
+
if weights.len() != num_points || centers.ncols() != num_features || num_centers == 0 {
|
|
128
|
+
return Err(PyValueError::new_err(
|
|
129
|
+
"weights must match points, and centers must contain points with the same feature count.",
|
|
130
|
+
));
|
|
131
|
+
}
|
|
132
|
+
let sq_norms: Vec<f64> = points.rows().into_iter().map(|row| row.dot(&row)).collect();
|
|
133
|
+
let mut center_norms = vec![0.0; num_centers];
|
|
134
|
+
let mut sums = vec![0.0; num_centers * num_features];
|
|
135
|
+
let mut counts = vec![0.0; num_centers];
|
|
136
|
+
for _ in 0..num_iterations {
|
|
137
|
+
sums.fill(0.0);
|
|
138
|
+
counts.fill(0.0);
|
|
139
|
+
for c in 0..num_centers {
|
|
140
|
+
center_norms[c] = centers.row(c).dot(¢ers.row(c));
|
|
141
|
+
}
|
|
142
|
+
for i in 0..num_points {
|
|
143
|
+
let nearest = nearest_center(points, centers.view(), sq_norms[i], ¢er_norms, i);
|
|
144
|
+
counts[nearest] += weights[i];
|
|
145
|
+
for f in 0..num_features {
|
|
146
|
+
sums[nearest * num_features + f] += points[[i, f]] * weights[i];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if !recompute_centers(&mut centers, &sums, &counts) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
Ok(PyArray2::from_owned_array(py, centers).into())
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Return the mass cell indices visited by one point in every tree.
|
|
157
|
+
///
|
|
158
|
+
/// The split arrays use the heap layout of `HalfSpaceTrees`. Each internal node
|
|
159
|
+
/// stores one feature index and one split value. A point goes right when its
|
|
160
|
+
/// feature value is greater than the split value.
|
|
161
|
+
///
|
|
162
|
+
/// Args:
|
|
163
|
+
/// x: One point with shape `(num_features,)` and dtype `float64`.
|
|
164
|
+
/// split_atts: Split feature indices with shape `(num_trees, num_splits)`
|
|
165
|
+
/// and dtype `uint16`.
|
|
166
|
+
/// split_values: Split values with shape `(num_trees, num_splits)` and
|
|
167
|
+
/// dtype `float64`.
|
|
168
|
+
/// max_depth: The maximum depth of every tree.
|
|
169
|
+
///
|
|
170
|
+
/// Returns:
|
|
171
|
+
/// An `int64` array with shape `(max_depth + 1, num_trees)`. Each column
|
|
172
|
+
/// contains the flat mass cell indices visited in one tree.
|
|
173
|
+
#[pyfunction]
|
|
174
|
+
#[pyo3(signature = (x, split_atts, split_values, max_depth))]
|
|
175
|
+
fn half_space_trees_path(
|
|
176
|
+
py: Python<'_>,
|
|
177
|
+
x: PyReadonlyArray1<'_, f64>,
|
|
178
|
+
split_atts: PyReadonlyArray2<'_, u16>,
|
|
179
|
+
split_values: PyReadonlyArray2<'_, f64>,
|
|
180
|
+
max_depth: usize,
|
|
181
|
+
) -> PyResult<Py<PyArray2<i64>>> {
|
|
182
|
+
let x = x.as_array();
|
|
183
|
+
let split_atts = split_atts.as_array();
|
|
184
|
+
let split_values = split_values.as_array();
|
|
185
|
+
|
|
186
|
+
if split_atts.shape() != split_values.shape() {
|
|
187
|
+
return Err(PyValueError::new_err(
|
|
188
|
+
"split_atts and split_values must have the same shape.",
|
|
189
|
+
));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let depth =
|
|
193
|
+
u32::try_from(max_depth).map_err(|_| PyValueError::new_err("max_depth is too large."))?;
|
|
194
|
+
let expected_splits = 1usize
|
|
195
|
+
.checked_shl(depth)
|
|
196
|
+
.and_then(|value| value.checked_sub(1))
|
|
197
|
+
.ok_or_else(|| PyValueError::new_err("max_depth is too large."))?;
|
|
198
|
+
|
|
199
|
+
let num_trees = split_atts.shape()[0];
|
|
200
|
+
let num_splits = split_atts.shape()[1];
|
|
201
|
+
if num_splits != expected_splits {
|
|
202
|
+
return Err(PyValueError::new_err(format!(
|
|
203
|
+
"split_atts must have {expected_splits} columns for max_depth {max_depth}."
|
|
204
|
+
)));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let num_nodes = 2 * num_splits + 1;
|
|
208
|
+
let mut paths = vec![vec![0i64; num_trees]; max_depth + 1];
|
|
209
|
+
|
|
210
|
+
for tree in 0..num_trees {
|
|
211
|
+
let mass_base = tree * num_nodes;
|
|
212
|
+
let mut heap_index = 1usize;
|
|
213
|
+
paths[0][tree] = mass_base as i64;
|
|
214
|
+
|
|
215
|
+
for depth_index in 0..max_depth {
|
|
216
|
+
let node = heap_index - 1;
|
|
217
|
+
let feature = split_atts[[tree, node]] as usize;
|
|
218
|
+
let goes_right = x[feature] > split_values[[tree, node]];
|
|
219
|
+
heap_index = 2 * heap_index + usize::from(goes_right);
|
|
220
|
+
paths[depth_index + 1][tree] = (mass_base + heap_index - 1) as i64;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
Ok(PyArray2::from_vec2(py, &paths)?.into())
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fn route_single_tree(
|
|
228
|
+
x: ArrayView1<'_, f64>,
|
|
229
|
+
root: i32,
|
|
230
|
+
dim: ArrayView1<'_, i32>,
|
|
231
|
+
cut: ArrayView1<'_, f64>,
|
|
232
|
+
left: ArrayView1<'_, i32>,
|
|
233
|
+
right: ArrayView1<'_, i32>,
|
|
234
|
+
) -> i32 {
|
|
235
|
+
let mut node = root;
|
|
236
|
+
while node >= 0 {
|
|
237
|
+
let d = dim[node as usize];
|
|
238
|
+
if d < 0 {
|
|
239
|
+
return node;
|
|
240
|
+
}
|
|
241
|
+
node = if x[d as usize] <= cut[node as usize] {
|
|
242
|
+
left[node as usize]
|
|
243
|
+
} else {
|
|
244
|
+
right[node as usize]
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
-1
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/// Route one point down each RRCF tree to find the leaf it falls into.
|
|
251
|
+
///
|
|
252
|
+
/// The RRCF trees use a structure-of-arrays layout with explicit pointers.
|
|
253
|
+
/// Each tree is independent and stored in flat arrays indexed by node ID.
|
|
254
|
+
///
|
|
255
|
+
/// Args:
|
|
256
|
+
/// x: One point with shape `(num_features,)` and dtype `float64`.
|
|
257
|
+
/// roots: Root node index for each tree, shape `(num_trees,)`, dtype `int32`.
|
|
258
|
+
/// A value of -1 means the tree is empty.
|
|
259
|
+
/// dim: Split dimension for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
260
|
+
/// A value of -1 marks a leaf node.
|
|
261
|
+
/// cut: Split value for each node, shape `(num_nodes,)`, dtype `float64`.
|
|
262
|
+
/// left: Left child index for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
263
|
+
/// right: Right child index for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
264
|
+
///
|
|
265
|
+
/// Returns:
|
|
266
|
+
/// An `int32` array with shape `(num_trees,)` containing the leaf node
|
|
267
|
+
/// index for each tree. Empty trees return -1.
|
|
268
|
+
#[pyfunction]
|
|
269
|
+
#[pyo3(signature = (x, roots, dim, cut, left, right))]
|
|
270
|
+
fn rrcf_route_to_leaf(
|
|
271
|
+
_py: Python<'_>,
|
|
272
|
+
x: PyReadonlyArray1<'_, f64>,
|
|
273
|
+
roots: PyReadonlyArray1<'_, i32>,
|
|
274
|
+
dim: PyReadonlyArray1<'_, i32>,
|
|
275
|
+
cut: PyReadonlyArray1<'_, f64>,
|
|
276
|
+
left: PyReadonlyArray1<'_, i32>,
|
|
277
|
+
right: PyReadonlyArray1<'_, i32>,
|
|
278
|
+
) -> PyResult<Py<PyArray1<i32>>> {
|
|
279
|
+
let x = x.as_array();
|
|
280
|
+
let roots = roots.as_array();
|
|
281
|
+
let dim = dim.as_array();
|
|
282
|
+
let cut = cut.as_array();
|
|
283
|
+
let left = left.as_array();
|
|
284
|
+
let right = right.as_array();
|
|
285
|
+
|
|
286
|
+
let leaves: Vec<i32> = roots
|
|
287
|
+
.iter()
|
|
288
|
+
.map(|&root| route_single_tree(x, root, dim, cut, left, right))
|
|
289
|
+
.collect();
|
|
290
|
+
|
|
291
|
+
Ok(PyArray1::from_vec(_py, leaves).into())
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/// Compute the collusive displacement (CoDisp) for a set of leaf nodes.
|
|
295
|
+
///
|
|
296
|
+
/// CoDisp is the maximum ratio `count[sibling] / (count[node] + 1)` along the
|
|
297
|
+
/// path from the leaf to the root.
|
|
298
|
+
///
|
|
299
|
+
/// Args:
|
|
300
|
+
/// leaves: Leaf node indices for each tree, shape `(num_trees,)`, dtype `int32`.
|
|
301
|
+
/// A value of -1 means the tree is empty or the point is a duplicate.
|
|
302
|
+
/// init_counts: Initial count values for each leaf, shape `(num_trees,)`, dtype `int32`.
|
|
303
|
+
/// Use 0 for duplicate leaves, otherwise use the leaf's count.
|
|
304
|
+
/// parent: Parent index for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
305
|
+
/// sibling: Sibling index for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
306
|
+
/// count: Count (number of points in subtree) for each node, shape `(num_nodes,)`, dtype `int32`.
|
|
307
|
+
///
|
|
308
|
+
/// Returns:
|
|
309
|
+
/// A float64 scalar: the sum of CoDisp values across all trees.
|
|
310
|
+
#[pyfunction]
|
|
311
|
+
#[pyo3(signature = (leaves, init_counts, parent, sibling, count))]
|
|
312
|
+
fn rrcf_codisp_sum(
|
|
313
|
+
_py: Python<'_>,
|
|
314
|
+
leaves: PyReadonlyArray1<'_, i32>,
|
|
315
|
+
init_counts: PyReadonlyArray1<'_, i32>,
|
|
316
|
+
parent: PyReadonlyArray1<'_, i32>,
|
|
317
|
+
sibling: PyReadonlyArray1<'_, i32>,
|
|
318
|
+
count: PyReadonlyArray1<'_, i32>,
|
|
319
|
+
) -> PyResult<f64> {
|
|
320
|
+
let leaves = leaves.as_array();
|
|
321
|
+
let init_counts = init_counts.as_array();
|
|
322
|
+
let parent = parent.as_array();
|
|
323
|
+
let sibling = sibling.as_array();
|
|
324
|
+
let count = count.as_array();
|
|
325
|
+
|
|
326
|
+
let num_trees = leaves.len();
|
|
327
|
+
let mut total: f64 = 0.0;
|
|
328
|
+
|
|
329
|
+
for tree in 0..num_trees {
|
|
330
|
+
let leaf = leaves[tree];
|
|
331
|
+
if leaf < 0 {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
let init_count = init_counts[tree] as f64;
|
|
336
|
+
let mut best = init_count;
|
|
337
|
+
let mut curr = leaf;
|
|
338
|
+
|
|
339
|
+
loop {
|
|
340
|
+
let p = parent[curr as usize];
|
|
341
|
+
if p < 0 {
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
let sib = sibling[curr as usize];
|
|
345
|
+
if sib >= 0 {
|
|
346
|
+
let ratio = count[sib as usize] as f64 / (count[curr as usize] as f64 + 1.0);
|
|
347
|
+
if ratio > best {
|
|
348
|
+
best = ratio;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
curr = p;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
total += best;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
Ok(total)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/// Define the `pysad_kernels` Python module.
|
|
361
|
+
#[pymodule]
|
|
362
|
+
fn pysad_kernels(module: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
363
|
+
module.add_function(wrap_pyfunction!(loda_neg_log_densities, module)?)?;
|
|
364
|
+
module.add_function(wrap_pyfunction!(streamkm_lloyd, module)?)?;
|
|
365
|
+
module.add_function(wrap_pyfunction!(half_space_trees_path, module)?)?;
|
|
366
|
+
module.add_function(wrap_pyfunction!(rrcf_route_to_leaf, module)?)?;
|
|
367
|
+
module.add_function(wrap_pyfunction!(rrcf_codisp_sum, module)?)?;
|
|
368
|
+
Ok(())
|
|
369
|
+
}
|