coneforest 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.
- coneforest-0.1.0/.gitignore +29 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/coneforest/__init__.py +9 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/coneforest/_forest.py +110 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/coneforest-0.1.0.dist-info/licenses/LICENSE +674 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/jax-0.12.1.dist-info/licenses/LICENSE +202 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/ml_dtypes-0.7.0.dist-info/licenses/LICENSE +238 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/numpy/ma/LICENSE +24 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/numpy-2.5.5.dist-info/licenses/numpy/_core/src/highway/LICENSE +371 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/numpy-2.5.5.dist-info/licenses/numpy/_core/src/umath/svml/LICENSE +30 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/numpy-2.5.5.dist-info/licenses/numpy/ma/LICENSE +24 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/opt_einsum-3.4.0.dist-info/licenses/LICENSE +22 -0
- coneforest-0.1.0/.package-test-venv/lib/python3.12/site-packages/scipy/_lib/_uarray/LICENSE +29 -0
- coneforest-0.1.0/LICENSE +674 -0
- coneforest-0.1.0/PKG-INFO +94 -0
- coneforest-0.1.0/README.md +75 -0
- coneforest-0.1.0/coneforest/__init__.py +9 -0
- coneforest-0.1.0/coneforest/_forest.py +110 -0
- coneforest-0.1.0/pyproject.toml +38 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Python bytecode and caches
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.pytest_cache/
|
|
5
|
+
.mypy_cache/
|
|
6
|
+
.ruff_cache/
|
|
7
|
+
.ipynb_checkpoints/
|
|
8
|
+
|
|
9
|
+
# Packaging artifacts
|
|
10
|
+
build/
|
|
11
|
+
dist/
|
|
12
|
+
*.egg-info/
|
|
13
|
+
|
|
14
|
+
# Virtual environments
|
|
15
|
+
.venv/
|
|
16
|
+
venv/
|
|
17
|
+
|
|
18
|
+
# Test and coverage output
|
|
19
|
+
.coverage
|
|
20
|
+
htmlcov/
|
|
21
|
+
|
|
22
|
+
# Local configuration and secrets
|
|
23
|
+
.env
|
|
24
|
+
.env.*
|
|
25
|
+
|
|
26
|
+
# Editor and OS files
|
|
27
|
+
.idea/
|
|
28
|
+
.vscode/
|
|
29
|
+
.DS_Store
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""ConeForest, an isolation-style anomaly detector built on the JAX library.
|
|
2
|
+
|
|
3
|
+
from coneforest import ConeForest
|
|
4
|
+
scores = ConeForest(seed=0).fit(X_train).decision_function(X_test)
|
|
5
|
+
"""
|
|
6
|
+
from ._forest import ConeForest, cone_forest_fit, cone_forest_predict
|
|
7
|
+
|
|
8
|
+
__all__ = ["ConeForest", "cone_forest_fit", "cone_forest_predict"]
|
|
9
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""ConeForest: random oblivious trees, scored by cell mass at every depth.
|
|
2
|
+
|
|
3
|
+
Each of K levels cuts one feature (drawn with replacement) at a threshold
|
|
4
|
+
uniform in the sample's range. A cell code holds the K cut bits. Its low
|
|
5
|
+
j bits are the cell at depth j. Depth j counts start at offset 2**j - 2.
|
|
6
|
+
"""
|
|
7
|
+
import math
|
|
8
|
+
|
|
9
|
+
import jax
|
|
10
|
+
import jax.numpy as jnp
|
|
11
|
+
import numpy as np
|
|
12
|
+
from jax import jit, random
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _cone_subsample(key, N, b, psi):
|
|
16
|
+
"""Row indices (b, psi) of each tree's sample. Needs psi < N."""
|
|
17
|
+
keys = random.split(key, b)
|
|
18
|
+
if N > 50 * psi:
|
|
19
|
+
# Few repeats at this ratio, and it avoids a full sort of N per tree.
|
|
20
|
+
return jax.vmap(lambda k: random.randint(k, (psi,), 0, N))(keys)
|
|
21
|
+
return jax.vmap(lambda k: random.choice(k, N, (psi,), replace=False))(keys)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _build_cone_forest(key, X, b, psi, K):
|
|
25
|
+
N, d = X.shape
|
|
26
|
+
k_sub, k_feat, k_thr = random.split(key, 3)
|
|
27
|
+
if psi >= N:
|
|
28
|
+
sub = jnp.broadcast_to(jnp.arange(N), (b, N))
|
|
29
|
+
else:
|
|
30
|
+
sub = _cone_subsample(k_sub, N, b, psi)
|
|
31
|
+
m = sub.shape[1]
|
|
32
|
+
|
|
33
|
+
feats = random.randint(k_feat, (b, K), 0, d)
|
|
34
|
+
S = X[sub[:, :, None], feats[:, None, :]] # (b, m, K)
|
|
35
|
+
lo, hi = S.min(axis=1), S.max(axis=1)
|
|
36
|
+
thr = lo + random.uniform(k_thr, (b, K)) * (hi - lo)
|
|
37
|
+
|
|
38
|
+
weights = (1 << jnp.arange(K)).astype(jnp.int32)
|
|
39
|
+
codes = jnp.sum((S > thr[:, None, :]) * weights, axis=2) # (b, m)
|
|
40
|
+
table = jnp.zeros((b, 1 << K), jnp.int32).at[jnp.arange(b)[:, None], codes].add(1)
|
|
41
|
+
flat = jnp.concatenate(
|
|
42
|
+
[table.reshape(b, 1 << (K - j), 1 << j).sum(axis=1) for j in range(1, K + 1)], axis=1
|
|
43
|
+
) # (b, 2**(K+1) - 2)
|
|
44
|
+
log_mass = jnp.log((flat + 1.0) / (m + 2.0))
|
|
45
|
+
|
|
46
|
+
# Fold the mean over depths into one score per full code.
|
|
47
|
+
all_codes = jnp.arange(1 << K, dtype=jnp.int32)
|
|
48
|
+
depth = jnp.arange(1, K + 1, dtype=jnp.int32)
|
|
49
|
+
idx = (1 << depth) - 2 + (all_codes[:, None] & ((1 << depth) - 1)) # (2**K, K)
|
|
50
|
+
cell_score = -jnp.mean(log_mass[:, idx], axis=2) # (b, 2**K)
|
|
51
|
+
return feats, thr, cell_score
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _score_cone_splits(X, feats, thr, cell_score):
|
|
55
|
+
b, K = feats.shape
|
|
56
|
+
weights = (1 << jnp.arange(K)).astype(jnp.int32)
|
|
57
|
+
codes = jnp.sum((X[:, feats] > thr[None]) * weights, axis=2) # (n, b)
|
|
58
|
+
return jnp.mean(cell_score[jnp.arange(b)[None, :], codes], axis=1)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
build_cone_forest = jit(_build_cone_forest, static_argnums=(2, 3, 4))
|
|
62
|
+
score_cone_splits = jit(_score_cone_splits)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def cone_forest_fit(X_train, *, b=256, psi=256, seed=0):
|
|
66
|
+
X_train = jnp.asarray(X_train, dtype=jnp.float32)
|
|
67
|
+
m = min(psi, X_train.shape[0])
|
|
68
|
+
K = max(1, math.ceil(math.log2(m)))
|
|
69
|
+
feats, thr, cell_score = build_cone_forest(random.key(seed), X_train, b, psi, K)
|
|
70
|
+
return {"feats": feats, "thr": thr, "cell_score": cell_score}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def cone_forest_predict(X, model, chunk=32768):
|
|
74
|
+
"""Score in chunks of equal size, so memory stays at about chunk * b * K."""
|
|
75
|
+
X = np.asarray(X, dtype=np.float32)
|
|
76
|
+
n = X.shape[0]
|
|
77
|
+
size = min(chunk, n)
|
|
78
|
+
out = []
|
|
79
|
+
for start in range(0, n, size):
|
|
80
|
+
part = X[start:start + size]
|
|
81
|
+
pad = size - part.shape[0]
|
|
82
|
+
if pad:
|
|
83
|
+
part = np.pad(part, ((0, pad), (0, 0)))
|
|
84
|
+
s = score_cone_splits(jnp.asarray(part), model["feats"], model["thr"], model["cell_score"])
|
|
85
|
+
out.append(np.asarray(s)[:size - pad])
|
|
86
|
+
return np.concatenate(out)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class ConeForest:
|
|
90
|
+
"""Anomaly detector. Higher scores mean more anomalous rows.
|
|
91
|
+
|
|
92
|
+
b: number of trees. psi: rows sampled per tree. seed: fixes all draws.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(self, b=256, psi=256, seed=0):
|
|
96
|
+
self.b = b
|
|
97
|
+
self.psi = psi
|
|
98
|
+
self.seed = seed
|
|
99
|
+
|
|
100
|
+
def fit(self, X, y=None):
|
|
101
|
+
self.model_ = cone_forest_fit(X, b=self.b, psi=self.psi, seed=self.seed)
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
def decision_function(self, X):
|
|
105
|
+
"""Anomaly score of each row of X, as a NumPy array."""
|
|
106
|
+
return cone_forest_predict(X, self.model_)
|
|
107
|
+
|
|
108
|
+
def fit_predict(self, X, y=None):
|
|
109
|
+
"""Fit on X and return the anomaly scores of the same rows."""
|
|
110
|
+
return self.fit(X).decision_function(X)
|