@nirs4all/methods 1.3.13 → 1.3.15
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.
- package/LICENSE +235 -0
- package/LICENSE-CeCILL-2.1 +518 -0
- package/README.md +154 -2
- package/dist/LICENSES/AGPL-3.0-or-later.txt +235 -0
- package/dist/LICENSES/Apache-2.0.txt +73 -0
- package/dist/LICENSES/BSD-3-Clause.txt +11 -0
- package/dist/LICENSES/COMMERCIAL-LICENSE.md +14 -0
- package/dist/LICENSES/COMMERCIAL-LICENSE_FR.md +15 -0
- package/dist/LICENSES/CeCILL-2.1.txt +518 -0
- package/dist/LICENSES/MIT.txt +18 -0
- package/dist/LICENSES/mixeff-dependencies.txt +13459 -0
- package/dist/LICENSES/mixeff-rs.txt +21 -0
- package/dist/LICENSES/rfr-cereal.txt +24 -0
- package/dist/LICENSES/rfr-rapidjson.txt +57 -0
- package/dist/LICENSES/rfr.txt +33 -0
- package/dist/LICENSES/rust-standard-library.txt +8266 -0
- package/dist/THIRD_PARTY_LICENSES.md +140 -0
- package/dist/componentAnalysis.d.ts +61 -0
- package/dist/componentAnalysis.js +175 -0
- package/dist/config.js +1 -1
- package/dist/context.js +1 -1
- package/dist/estimatorRoles.js +18 -1
- package/dist/estimatorRolesGenerated.d.ts +48 -1
- package/dist/estimatorRolesGenerated.js +41 -2
- package/dist/ffi.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/methodResult.js +1 -1
- package/dist/model.js +1 -1
- package/dist/multimodalPipeline.js +1 -1
- package/dist/n4m.js +1 -1
- package/dist/n4m.wasm +0 -0
- package/dist/nativeAugmentation.js +1 -1
- package/dist/nativeModel.js +1 -1
- package/dist/nativePreprocessingPipeline.js +1 -1
- package/dist/nativeSplitter.js +1 -1
- package/dist/optimization.js +1 -1
- package/dist/preflightDiagnostics.js +1 -1
- package/dist/preprocessing.js +5 -1
- package/dist/ranking.d.ts +61 -0
- package/dist/ranking.js +110 -0
- package/dist/rolePipeline.js +1 -1
- package/dist/selection.js +1 -1
- package/dist/serialization.js +1 -1
- package/dist/spectralEncoding.js +1 -1
- package/dist/types.js +1 -1
- package/package.json +7 -5
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# THIRD_PARTY_LICENSES — full attribution for vendored material
|
|
2
|
+
|
|
3
|
+
## FITPACK (SPDX: BSD-3-Clause)
|
|
4
|
+
|
|
5
|
+
**Upstream**: https://github.com/scipy/scipy/tree/main/scipy/interpolate/fitpack
|
|
6
|
+
**Vendored at**: `cpp/src/core/common/_vendored/fitpack`
|
|
7
|
+
**Notes**: C translation of SciPy's interpolate/fitpack/ Fortran spline kernels (curfit/splev path). The CECILL-2.1 OR AGPL-3.0-or-later carrier is compatible because BSD-3 is permissive.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Copyright (c) 2001-2002 Enthought, Inc. 2003, SciPy Developers.
|
|
11
|
+
All rights reserved.
|
|
12
|
+
|
|
13
|
+
Redistribution and use in source and binary forms, with or without
|
|
14
|
+
modification, are permitted provided that the following conditions
|
|
15
|
+
are met:
|
|
16
|
+
|
|
17
|
+
1. Redistributions of source code must retain the above copyright
|
|
18
|
+
notice, this list of conditions and the following disclaimer.
|
|
19
|
+
|
|
20
|
+
2. Redistributions in binary form must reproduce the above
|
|
21
|
+
copyright notice, this list of conditions and the following
|
|
22
|
+
disclaimer in the documentation and/or other materials provided
|
|
23
|
+
with the distribution.
|
|
24
|
+
|
|
25
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
26
|
+
contributors may be used to endorse or promote products derived
|
|
27
|
+
from this software without specific prior written permission.
|
|
28
|
+
|
|
29
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
30
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
31
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
32
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
33
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
34
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
35
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
36
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
37
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
38
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
39
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Isolation Forest + LOF + Minimum Covariance Determinant (SPDX: BSD-3-Clause)
|
|
43
|
+
|
|
44
|
+
**Upstream**: https://github.com/scikit-learn/scikit-learn
|
|
45
|
+
**Vendored at**: `cpp/src/core/filters/_vendored`
|
|
46
|
+
**Notes**: Reimplemented from scikit-learn algorithms; no scikit-learn source copied verbatim. Treat as 'derived' under BSD-3 attribution.
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
Vendored algorithm reimplementations — derived from scikit-learn.
|
|
50
|
+
================================================================
|
|
51
|
+
|
|
52
|
+
The C implementations of IsolationForest, Local Outlier Factor (LOF), and
|
|
53
|
+
Minimum Covariance Determinant (MCD) under this directory are NOT verbatim
|
|
54
|
+
copies of scikit-learn source code; they are independent C reimplementations
|
|
55
|
+
written specifically for the n4m / nirs4all-methods library, following the
|
|
56
|
+
algorithmic descriptions in:
|
|
57
|
+
|
|
58
|
+
- scikit-learn library (BSD-3-Clause)
|
|
59
|
+
https://github.com/scikit-learn/scikit-learn
|
|
60
|
+
|
|
61
|
+
- Original references:
|
|
62
|
+
Liu, F. T., Ting, K. M., & Zhou, Z.-H. (2008). Isolation forest. ICDM.
|
|
63
|
+
Breunig, M. M., Kriegel, H.-P., Ng, R. T., & Sander, J. (2000). LOF.
|
|
64
|
+
Rousseeuw, P. J., & Van Driessen, K. (1999). A fast algorithm for the
|
|
65
|
+
minimum covariance determinant estimator. Technometrics, 41(3).
|
|
66
|
+
|
|
67
|
+
In recognition of the BSD-3-Clause license that governs scikit-learn (the
|
|
68
|
+
algorithmic reference), the following attribution is provided per BSD-3
|
|
69
|
+
clause 2:
|
|
70
|
+
|
|
71
|
+
This product includes software developed by:
|
|
72
|
+
- scikit-learn developers (https://scikit-learn.org/) — algorithmic
|
|
73
|
+
descriptions of IsolationForest, LOF, and MCD.
|
|
74
|
+
|
|
75
|
+
The n4m source files in this directory are licensed under CECILL-2.1 OR
|
|
76
|
+
AGPL-3.0-or-later (see the repository-level LICENSING.md). This NOTICE serves as
|
|
77
|
+
clarification that the algorithms are derived from BSD-3-licensed
|
|
78
|
+
reference implementations; the C code itself is original.
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
## HDBSCAN* (scikit-learn + scikit-learn-contrib/hdbscan) (SPDX: BSD-3-Clause)
|
|
83
|
+
|
|
84
|
+
**Upstream**: https://github.com/scikit-learn/scikit-learn (1.9.1, `sklearn/cluster/_hdbscan`) and
|
|
85
|
+
https://github.com/scikit-learn-contrib/hdbscan (0.8.44, GLOSH outlier scores and cluster persistence)
|
|
86
|
+
**Vendored at**: `cpp/src/core/clustering/_vendored` (full notice in `LICENSE.scikit_learn.txt` there)
|
|
87
|
+
**Notes**: Independent C++ port reproducing the reference control flow and floating-point order; no
|
|
88
|
+
source copied verbatim. Only difference: equal-weight MST edges are stably sorted.
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
Copyright (c) 2007-2026 The scikit-learn developers.
|
|
92
|
+
Copyright (c) 2015, Leland McInnes.
|
|
93
|
+
Licensed under the BSD-3-Clause license; the algorithms reproduced here are
|
|
94
|
+
attributed to both projects. The n4m source files are CECILL-2.1 OR
|
|
95
|
+
AGPL-3.0-or-later (see the repository LICENSING.md); BSD-3 is permissive and compatible.
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## SciPy rectangular linear sum assignment (SPDX: BSD-3-Clause)
|
|
99
|
+
|
|
100
|
+
**Upstream**: https://github.com/scipy/scipy (1.18.1, `scipy/optimize/rectangular_lsap/rectangular_lsap.cpp`, author PM Larsen)
|
|
101
|
+
**Vendored at**: `cpp/src/core/optimization/_vendored` (full notice in `LICENSE.scipy.txt` there)
|
|
102
|
+
**Notes**: C++17 port keeping the upstream control flow and tie rules; index types, the -1 sentinels
|
|
103
|
+
and casts were adapted, and duals and path costs are exact 192-bit fixed-point integers, so the
|
|
104
|
+
result is an exact optimum. The two vendored files keep the BSD-3-Clause
|
|
105
|
+
SPDX identifier. Algorithm: D. F. Crouse (2016), doi:10.1109/TAES.2016.140952.
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
Copyright (c) 2001-2002 Enthought, Inc. 2003, SciPy Developers.
|
|
109
|
+
All rights reserved.
|
|
110
|
+
Licensed under the BSD-3-Clause license (full text in
|
|
111
|
+
cpp/src/core/optimization/_vendored/LICENSE.scipy.txt). BSD-3 is permissive
|
|
112
|
+
and compatible with the CECILL-2.1 OR AGPL-3.0-or-later licence of the rest of n4m.
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## NumPy random (PCG64 / SeedSequence / Ziggurat / bounded + choice samplers) (SPDX: BSD-3-Clause)
|
|
116
|
+
|
|
117
|
+
**Upstream**: https://github.com/numpy/numpy (numpy/random)
|
|
118
|
+
**Reproduced in**: `cpp/src/core/common/rng_pcg64.c`, `ziggurat*.{c,h}`,
|
|
119
|
+
`cpp/src/core/augmentation/aug_rng_utils.c`
|
|
120
|
+
**Notes**: The PCG64 bit generator, the NumPy `SeedSequence` pool expansion,
|
|
121
|
+
the Ziggurat standard-normal sampler and its constants, and the bounded-integer
|
|
122
|
+
and `choice(replace=False)` samplers reproduce NumPy's `numpy.random`
|
|
123
|
+
algorithms and constants for bit-exact parity (verified by the parity
|
|
124
|
+
fixtures + `n4m_internal_tests`). They are independent C reimplementations —
|
|
125
|
+
no NumPy source is copied verbatim — derived under BSD-3 attribution. The PCG
|
|
126
|
+
family is by Melissa O'Neill (https://www.pcg-random.org/).
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
Copyright (c) 2005-2024, NumPy Developers.
|
|
130
|
+
Licensed under the BSD-3-Clause license; the algorithms reproduced here are
|
|
131
|
+
attributed to the NumPy project. The n4m source files are CECILL-2.1 OR
|
|
132
|
+
AGPL-3.0-or-later (see the repository LICENSING.md); BSD-3 is permissive and compatible.
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
## Optional component-analysis static engines
|
|
137
|
+
|
|
138
|
+
The analysis-enabled artifacts statically link mixeff-rs 1.0.0-rc.5 (MIT, default features disabled; no NLopt) and the reviewed pyrfr/rfr header closure at 1ba17650e734edd3ea536efc91d32815c4874e72 (BSD-3-Clause, with cereal and RapidJSON notices). No code from the restricted-license Python fANOVA package is included.
|
|
139
|
+
|
|
140
|
+
Full binary redistribution notices are bundled in `LICENSES/mixeff-rs.txt`, `LICENSES/mixeff-dependencies.txt`, `LICENSES/rust-standard-library.txt`, `LICENSES/rfr.txt`, `LICENSES/rfr-cereal.txt` and `LICENSES/rfr-rapidjson.txt`. The Rust dependency manifest and exact header checksums live under `cpp/backends/` in the source distribution.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Matrix } from "./types.js";
|
|
2
|
+
export interface MixedEffectsOptions {
|
|
3
|
+
reml?: boolean;
|
|
4
|
+
max_evaluations?: number;
|
|
5
|
+
tolerance?: number;
|
|
6
|
+
rank_tolerance?: number;
|
|
7
|
+
boundary_tolerance?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface MixedEffectsResult {
|
|
10
|
+
schema_version: "n4m.mixed_effects/1";
|
|
11
|
+
state: "ready" | "unavailable";
|
|
12
|
+
reason: string | null;
|
|
13
|
+
beta: (number | null)[] | null;
|
|
14
|
+
standard_errors: (number | null)[] | null;
|
|
15
|
+
covariance: (number | null)[][] | null;
|
|
16
|
+
variances: (number | null)[] | null;
|
|
17
|
+
diagnostics: Record<string, number | string | null>;
|
|
18
|
+
options: Record<string, number | null>;
|
|
19
|
+
uncertainty_kind: "conditional_plugin_standard_error";
|
|
20
|
+
warnings: string[];
|
|
21
|
+
}
|
|
22
|
+
/** X includes any intercept explicitly. Group IDs preserve int64 with BigInt64Array. */
|
|
23
|
+
export declare function mixedEffects(X: Matrix, y: Matrix, groups: {
|
|
24
|
+
data: BigInt64Array;
|
|
25
|
+
rows: number;
|
|
26
|
+
cols: number;
|
|
27
|
+
}, options?: MixedEffectsOptions): MixedEffectsResult;
|
|
28
|
+
export interface FanovaOptions {
|
|
29
|
+
n_trees?: number;
|
|
30
|
+
max_depth?: number;
|
|
31
|
+
min_samples_leaf?: number;
|
|
32
|
+
max_features?: number;
|
|
33
|
+
seed?: number;
|
|
34
|
+
max_order?: number;
|
|
35
|
+
max_marginal_evaluations?: number;
|
|
36
|
+
X_valid?: Matrix;
|
|
37
|
+
y_valid?: Matrix;
|
|
38
|
+
}
|
|
39
|
+
export interface FanovaResult {
|
|
40
|
+
schema_version: "n4m.fanova/1";
|
|
41
|
+
state: "ready" | "unavailable";
|
|
42
|
+
reason: string | null;
|
|
43
|
+
feature_importance: {
|
|
44
|
+
index: number;
|
|
45
|
+
fraction: number | null;
|
|
46
|
+
tree_sd: number | null;
|
|
47
|
+
}[];
|
|
48
|
+
interactions: {
|
|
49
|
+
indices: [number, number];
|
|
50
|
+
fraction: number | null;
|
|
51
|
+
tree_sd: number | null;
|
|
52
|
+
}[];
|
|
53
|
+
diagnostics: Record<string, number | null>;
|
|
54
|
+
options: Record<string, number | null>;
|
|
55
|
+
domain_measure: "uniform_product";
|
|
56
|
+
aggregation: "mean_per_tree_normalized_components";
|
|
57
|
+
uncertainty_kind: "tree_dispersion_not_confidence";
|
|
58
|
+
warnings: string[];
|
|
59
|
+
}
|
|
60
|
+
export declare function fanova(X: Matrix, y: Matrix, featureTypes: Uint32Array, bounds: Matrix, options?: FanovaOptions): FanovaResult;
|
|
61
|
+
export declare function normalizeLoss(values: Float64Array, baselines: Float64Array): Float64Array;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
|
+
/** Matrix-only adapters. Numerical fitting and normalization stay in the C ABI. */
|
|
3
|
+
import { checkStatus, getModule, makeMatrixView } from "./ffi.js";
|
|
4
|
+
const nullable = (v) => Number.isFinite(v) ? v : null;
|
|
5
|
+
class Arena {
|
|
6
|
+
m = getModule();
|
|
7
|
+
ptrs = [];
|
|
8
|
+
views = [];
|
|
9
|
+
alloc(bytes) {
|
|
10
|
+
if (!Number.isSafeInteger(bytes) || bytes < 1 || bytes > 0x7fffffff)
|
|
11
|
+
throw new RangeError("analysis allocation exceeds WASM limit");
|
|
12
|
+
const p = this.m._malloc(bytes);
|
|
13
|
+
if (!p)
|
|
14
|
+
throw new Error("analysis allocation failed");
|
|
15
|
+
this.ptrs.push(p);
|
|
16
|
+
return p;
|
|
17
|
+
}
|
|
18
|
+
matrix(x) {
|
|
19
|
+
if (!Number.isSafeInteger(x.rows) || !Number.isSafeInteger(x.cols) || x.rows < 1 || x.cols < 1 ||
|
|
20
|
+
x.data.length !== x.rows * x.cols)
|
|
21
|
+
throw new Error("invalid nonempty matrix");
|
|
22
|
+
const v = makeMatrixView(x.data, x.rows, x.cols);
|
|
23
|
+
this.views.push(v);
|
|
24
|
+
return v.viewPtr;
|
|
25
|
+
}
|
|
26
|
+
free() { for (const v of this.views)
|
|
27
|
+
v.free(); for (const p of this.ptrs)
|
|
28
|
+
this.m._free(p); }
|
|
29
|
+
read(p, n) {
|
|
30
|
+
return Array.from(this.m.HEAPF64.subarray(p / 8, p / 8 + n), nullable);
|
|
31
|
+
}
|
|
32
|
+
options(init, size, fields, values) {
|
|
33
|
+
const p = this.alloc(size);
|
|
34
|
+
checkStatus(this.m.ccall(init, "number", ["number"], [p]));
|
|
35
|
+
for (const [key, value] of Object.entries(values)) {
|
|
36
|
+
if (value === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
const field = fields.find(f => f[0] === key);
|
|
39
|
+
if (!field)
|
|
40
|
+
throw new Error(`unknown component analysis option ${key}`);
|
|
41
|
+
const [, offset, type] = field;
|
|
42
|
+
const v = key === "reml" && typeof value === "boolean" ? Number(value) : value;
|
|
43
|
+
if (typeof v !== "number" || !Number.isFinite(v))
|
|
44
|
+
throw new Error(`${key} must be finite`);
|
|
45
|
+
const d = new DataView(this.m.HEAPU8.buffer);
|
|
46
|
+
if (type === "f64")
|
|
47
|
+
d.setFloat64(p + offset, v, true);
|
|
48
|
+
else {
|
|
49
|
+
if (!Number.isSafeInteger(v))
|
|
50
|
+
throw new Error(`${key} must be a safe integer`);
|
|
51
|
+
if (type === "i32") {
|
|
52
|
+
if (v < -2147483648 || v > 2147483647)
|
|
53
|
+
throw new RangeError(`${key} exceeds int32 range`);
|
|
54
|
+
d.setInt32(p + offset, v, true);
|
|
55
|
+
}
|
|
56
|
+
else if (type === "u64") {
|
|
57
|
+
if (v < 0)
|
|
58
|
+
throw new RangeError(`${key} must be nonnegative`);
|
|
59
|
+
d.setBigUint64(p + offset, BigInt(v), true);
|
|
60
|
+
}
|
|
61
|
+
else
|
|
62
|
+
d.setBigInt64(p + offset, BigInt(v), true);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return p;
|
|
66
|
+
}
|
|
67
|
+
fields(p, fields) {
|
|
68
|
+
const d = new DataView(this.m.HEAPU8.buffer);
|
|
69
|
+
return Object.fromEntries(fields.map(([key, offset, type]) => [key,
|
|
70
|
+
type === "f64" ? nullable(d.getFloat64(p + offset, true)) : type === "i32" ? d.getInt32(p + offset, true) :
|
|
71
|
+
Number(type === "u64" ? d.getBigUint64(p + offset, true) : d.getBigInt64(p + offset, true))]));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const MIXED_OPTIONS = [["reml", 8, "i32"], ["max_evaluations", 16, "i64"],
|
|
75
|
+
["tolerance", 24, "f64"], ["rank_tolerance", 32, "f64"], ["boundary_tolerance", 40, "f64"]];
|
|
76
|
+
const MIXED_SUMMARY = [["fit_state", 8, "i32"], ["reason", 12, "i32"], ["converged", 16, "i32"],
|
|
77
|
+
["singular", 20, "i32"], ["boundary", 24, "i32"], ["optimizer_status", 28, "i32"],
|
|
78
|
+
["observations", 32, "i64"], ["fixed_effects", 40, "i64"], ["group_terms", 48, "i64"],
|
|
79
|
+
["design_rank", 56, "i64"], ["total_group_levels", 64, "i64"], ["evaluations", 72, "i64"],
|
|
80
|
+
["objective", 80, "f64"], ["log_likelihood", 88, "f64"], ["residual_df", 96, "f64"], ["condition_number", 104, "f64"]];
|
|
81
|
+
/** X includes any intercept explicitly. Group IDs preserve int64 with BigInt64Array. */
|
|
82
|
+
export function mixedEffects(X, y, groups, options = {}) {
|
|
83
|
+
const a = new Arena();
|
|
84
|
+
try {
|
|
85
|
+
const opt = a.options("n4m_mixed_effects_options_init_v1", 48, MIXED_OPTIONS, { ...options });
|
|
86
|
+
const base = { schema_version: "n4m.mixed_effects/1", options: a.fields(opt, MIXED_OPTIONS),
|
|
87
|
+
uncertainty_kind: "conditional_plugin_standard_error",
|
|
88
|
+
warnings: ["Gaussian random-intercept associations are not causal effects.", "Plug-in SE excludes variance-parameter uncertainty."] };
|
|
89
|
+
if (!a.m.ccall("n4m_mixed_effects_backend_available_v1", "number", [], [])) {
|
|
90
|
+
return { ...base, state: "unavailable", reason: "mixed_effects_backend_not_compiled", beta: null,
|
|
91
|
+
covariance: null, standard_errors: null, variances: null, diagnostics: {} };
|
|
92
|
+
}
|
|
93
|
+
const xv = a.matrix(X), yv = a.matrix(y);
|
|
94
|
+
if (!(groups.data instanceof BigInt64Array) || !Number.isSafeInteger(groups.rows) || !Number.isSafeInteger(groups.cols) ||
|
|
95
|
+
groups.rows < 1 || groups.cols < 1 || groups.data.length !== groups.rows * groups.cols)
|
|
96
|
+
throw new Error("invalid int64 group matrix");
|
|
97
|
+
const gp = a.alloc(groups.data.byteLength), gv = a.alloc(48);
|
|
98
|
+
a.m.HEAPU8.set(new Uint8Array(groups.data.buffer, groups.data.byteOffset, groups.data.byteLength), gp);
|
|
99
|
+
checkStatus(a.m.ccall("n4m_matrix_view_init_rowmajor", "number", ["number", "number", "i64", "i64", "number"], [gv, gp, BigInt(groups.rows), BigInt(groups.cols), 4]));
|
|
100
|
+
const p = X.cols, k = groups.cols, beta = a.alloc(p * 8), cov = a.alloc(p * p * 8), se = a.alloc(p * 8), variances = a.alloc((k + 1) * 8), summary = a.alloc(112);
|
|
101
|
+
new DataView(a.m.HEAPU8.buffer).setBigUint64(summary, 112n, true);
|
|
102
|
+
checkStatus(a.m.ccall("n4m_mixed_effects_fit_v1", "number", ["number", "number", "number", "number", "number", "i64", "number", "i64", "number", "i64", "number", "i64", "number"], [xv, yv, gv, opt, beta, BigInt(p), cov, BigInt(p * p), se, BigInt(p), variances, BigInt(k + 1), summary]));
|
|
103
|
+
const diagnostics = a.fields(summary, MIXED_SUMMARY);
|
|
104
|
+
const state = diagnostics.fit_state, reason = diagnostics.reason;
|
|
105
|
+
diagnostics.fit_state = ["fitted", "fitted_boundary", "rank_deficient", "unidentifiable", "nonconverged", "numerical_refusal"][state];
|
|
106
|
+
return { ...base, state: state <= 1 ? "ready" : "unavailable", reason: [null, "fixed_rank", "no_residual_df", "constant_response",
|
|
107
|
+
"insufficient_group_levels", "confounded_variances", "zero_residual", "optimizer_budget", "optimizer_roundoff",
|
|
108
|
+
"optimizer_failure", "nonfinite_result", "engine_rejected"][reason],
|
|
109
|
+
beta: a.read(beta, p), standard_errors: a.read(se, p), covariance: Array.from({ length: p }, (_, i) => a.read(cov + i * p * 8, p)),
|
|
110
|
+
variances: a.read(variances, k + 1), diagnostics };
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
a.free();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const FANOVA_OPTIONS = [["num_trees", 8, "i32"], ["max_depth", 12, "i32"], ["min_samples_leaf", 16, "i32"],
|
|
117
|
+
["max_features", 20, "i32"], ["seed", 24, "u64"], ["max_order", 32, "i32"], ["max_marginal_evaluations", 40, "i64"]];
|
|
118
|
+
const FANOVA_SUMMARY = [["fit_state", 8, "i32"], ["observations", 16, "i64"], ["features", 24, "i64"],
|
|
119
|
+
["trees", 32, "i64"], ["informative_trees", 40, "i64"], ["marginal_evaluations", 48, "i64"],
|
|
120
|
+
["total_variance_mean", 56, "f64"], ["remaining_fraction", 64, "f64"], ["train_rmse", 72, "f64"],
|
|
121
|
+
["validation_rmse", 80, "f64"], ["validation_r2", 88, "f64"]];
|
|
122
|
+
export function fanova(X, y, featureTypes, bounds, options = {}) {
|
|
123
|
+
const a = new Arena();
|
|
124
|
+
try {
|
|
125
|
+
const { n_trees = 32, X_valid, y_valid, ...rest } = options;
|
|
126
|
+
if ((X_valid === undefined) !== (y_valid === undefined))
|
|
127
|
+
throw new Error("X_valid and y_valid are required together");
|
|
128
|
+
const opt = a.options("n4m_fanova_options_init_v1", 48, FANOVA_OPTIONS, { num_trees: n_trees, ...rest });
|
|
129
|
+
const resolved = a.fields(opt, FANOVA_OPTIONS);
|
|
130
|
+
const base = { schema_version: "n4m.fanova/1", options: resolved, domain_measure: "uniform_product",
|
|
131
|
+
aggregation: "mean_per_tree_normalized_components", uncertainty_kind: "tree_dispersion_not_confidence",
|
|
132
|
+
warnings: ["Surrogate attribution under independent uniform domains is not causal.", "Tree dispersion is not a confidence interval."] };
|
|
133
|
+
if (!a.m.ccall("n4m_fanova_backend_available_v1", "number", [], []))
|
|
134
|
+
return { ...base, state: "unavailable",
|
|
135
|
+
reason: "fanova_backend_not_compiled", feature_importance: [], interactions: [], diagnostics: {} };
|
|
136
|
+
const xv = a.matrix(X), yv = a.matrix(y), bv = a.matrix(bounds), p = X.cols;
|
|
137
|
+
if (!(featureTypes instanceof Uint32Array) || featureTypes.length !== p)
|
|
138
|
+
throw new Error("one feature type is required per column");
|
|
139
|
+
const types = a.alloc(p * 4);
|
|
140
|
+
a.m.HEAPU8.set(new Uint8Array(featureTypes.buffer, featureTypes.byteOffset, featureTypes.byteLength), types);
|
|
141
|
+
const vx = X_valid ? a.matrix(X_valid) : 0, vy = y_valid ? a.matrix(y_valid) : 0;
|
|
142
|
+
const mains = a.alloc(p * 16), pairs = a.alloc(p * p * 16), summary = a.alloc(96);
|
|
143
|
+
new DataView(a.m.HEAPU8.buffer).setBigUint64(summary, 96n, true);
|
|
144
|
+
checkStatus(a.m.ccall("n4m_fanova_fit_v1", "number", ["number", "number", "number", "number", "number", "number", "number", "number", "i64", "number", "i64", "number"], [xv, yv, bv, types, vx, vy, opt, mains, BigInt(p * 2), pairs, BigInt(p * p * 2), summary]));
|
|
145
|
+
const diagnostics = a.fields(summary, FANOVA_SUMMARY), state = diagnostics.fit_state;
|
|
146
|
+
const feature_importance = Array.from({ length: p }, (_, i) => ({ index: i, fraction: a.read(mains + i * 16, 1)[0], tree_sd: a.read(mains + i * 16 + 8, 1)[0] }));
|
|
147
|
+
const interactions = [];
|
|
148
|
+
if (resolved.max_order === 2)
|
|
149
|
+
for (let i = 0; i < p; i++)
|
|
150
|
+
for (let j = i + 1; j < p; j++) {
|
|
151
|
+
const v = a.read(pairs + (i * p + j) * 16, 2);
|
|
152
|
+
interactions.push({ indices: [i, j], fraction: v[0], tree_sd: v[1] });
|
|
153
|
+
}
|
|
154
|
+
return { ...base, state: state === 0 ? "ready" : "unavailable", reason: [null, "zero_surrogate_variance", "zero_surrogate_variance", "numerical_refusal"][state],
|
|
155
|
+
feature_importance, interactions, diagnostics };
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
a.free();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export function normalizeLoss(values, baselines) {
|
|
162
|
+
if (values.length !== baselines.length || values.length < 1)
|
|
163
|
+
throw new Error("values and baselines must have the same nonempty length");
|
|
164
|
+
const a = new Arena();
|
|
165
|
+
try {
|
|
166
|
+
const x = a.alloc(values.byteLength), b = a.alloc(baselines.byteLength), out = a.alloc(values.byteLength);
|
|
167
|
+
a.m.HEAPF64.set(values, x / 8);
|
|
168
|
+
a.m.HEAPF64.set(baselines, b / 8);
|
|
169
|
+
checkStatus(a.m.ccall("n4m_component_normalize_loss_v1", "number", ["number", "number", "i64", "number"], [x, b, BigInt(values.length), out]));
|
|
170
|
+
return a.m.HEAPF64.slice(out / 8, out / 8 + values.length);
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
a.free();
|
|
174
|
+
}
|
|
175
|
+
}
|
package/dist/config.js
CHANGED
package/dist/context.js
CHANGED
package/dist/estimatorRoles.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier: CECILL-2.1
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
2
|
// Generic native estimator roles (ABI 2.13).
|
|
3
3
|
//
|
|
4
4
|
// The helpers exported below (contexts, allocations, typed parameters, fit
|
|
@@ -61,8 +61,25 @@ export function cString(s) {
|
|
|
61
61
|
m.stringToUTF8(s, ptr, n);
|
|
62
62
|
return { ptr, free: () => m._free(ptr) };
|
|
63
63
|
}
|
|
64
|
+
/** SNV host types must survive marshalling without coercion or i32 overflow. */
|
|
65
|
+
function validateSnvParams(method) {
|
|
66
|
+
if (method.methodId !== "preprocessing.scatter.snv")
|
|
67
|
+
return;
|
|
68
|
+
for (const name of ["with_mean", "with_std"]) {
|
|
69
|
+
const value = method.params[name];
|
|
70
|
+
if (value !== undefined && typeof value !== "boolean") {
|
|
71
|
+
throw new TypeError(`SNV ${name} must be a boolean`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const ddof = method.params.ddof;
|
|
75
|
+
if (ddof !== undefined && (typeof ddof !== "number" ||
|
|
76
|
+
!Number.isSafeInteger(ddof) || ddof < 0 || ddof > 2147483647)) {
|
|
77
|
+
throw new TypeError("SNV ddof must be an integer number in [0, INT32_MAX]");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
64
80
|
/** Validated native parameters of a method; the caller destroys them. */
|
|
65
81
|
export function nativeParams(ctx, method) {
|
|
82
|
+
validateSnvParams(method);
|
|
66
83
|
const m = getModule();
|
|
67
84
|
const indexPtr = m._malloc(4);
|
|
68
85
|
const out = m._malloc(4);
|
|
@@ -2300,6 +2300,33 @@ export declare class KernelPLS extends NativeEstimator implements Regressor {
|
|
|
2300
2300
|
constructor(params?: KernelPLSParams);
|
|
2301
2301
|
predict(X: Matrix): Matrix;
|
|
2302
2302
|
}
|
|
2303
|
+
/** Parameters of OPLS; unset values take the native defaults. */
|
|
2304
|
+
export interface OPLSParams {
|
|
2305
|
+
/** Default 2. */
|
|
2306
|
+
n_components?: number;
|
|
2307
|
+
/** Default true. */
|
|
2308
|
+
center_x?: boolean;
|
|
2309
|
+
/** Default true. */
|
|
2310
|
+
scale_x?: boolean;
|
|
2311
|
+
/** Default true. */
|
|
2312
|
+
center_y?: boolean;
|
|
2313
|
+
/** Default true. */
|
|
2314
|
+
scale_y?: boolean;
|
|
2315
|
+
}
|
|
2316
|
+
/** Native `models.pls.opls` (transformer, regressor). */
|
|
2317
|
+
export declare class OPLS extends NativeEstimator implements Regressor, Transformer {
|
|
2318
|
+
readonly methodId = "models.pls.opls";
|
|
2319
|
+
readonly paramTypes: {
|
|
2320
|
+
readonly n_components: "int";
|
|
2321
|
+
readonly center_x: "bool";
|
|
2322
|
+
readonly scale_x: "bool";
|
|
2323
|
+
readonly center_y: "bool";
|
|
2324
|
+
readonly scale_y: "bool";
|
|
2325
|
+
};
|
|
2326
|
+
constructor(params?: OPLSParams);
|
|
2327
|
+
predict(X: Matrix): Matrix;
|
|
2328
|
+
transform(X: Matrix): Matrix;
|
|
2329
|
+
}
|
|
2303
2330
|
/** Parameters of PCR; unset values take the native defaults. */
|
|
2304
2331
|
export interface PCRParams {
|
|
2305
2332
|
/** Default 2. */
|
|
@@ -3337,6 +3364,8 @@ export interface StandardScaleParams {
|
|
|
3337
3364
|
with_mean?: boolean;
|
|
3338
3365
|
/** Default true. */
|
|
3339
3366
|
with_std?: boolean;
|
|
3367
|
+
/** Default 0. */
|
|
3368
|
+
ddof?: number;
|
|
3340
3369
|
}
|
|
3341
3370
|
/** Native `preprocessing.scaling.standard_scale` (transformer). */
|
|
3342
3371
|
export declare class StandardScale extends NativeEstimator implements Transformer {
|
|
@@ -3344,6 +3373,7 @@ export declare class StandardScale extends NativeEstimator implements Transforme
|
|
|
3344
3373
|
readonly paramTypes: {
|
|
3345
3374
|
readonly with_mean: "bool";
|
|
3346
3375
|
readonly with_std: "bool";
|
|
3376
|
+
readonly ddof: "int";
|
|
3347
3377
|
};
|
|
3348
3378
|
constructor(params?: StandardScaleParams);
|
|
3349
3379
|
transform(X: Matrix): Matrix;
|
|
@@ -4599,14 +4629,17 @@ export declare class KBinsStratified extends NativeProcedure implements Splitter
|
|
|
4599
4629
|
}
|
|
4600
4630
|
/** Parameters of KennardStone; unset values take the native defaults. */
|
|
4601
4631
|
export interface KennardStoneParams {
|
|
4602
|
-
/**
|
|
4632
|
+
/** Optional: unset by default. */
|
|
4603
4633
|
test_size?: number;
|
|
4634
|
+
/** Optional: unset by default. */
|
|
4635
|
+
n_train?: number;
|
|
4604
4636
|
}
|
|
4605
4637
|
/** Native `splitters.kennard_stone` (splitter). */
|
|
4606
4638
|
export declare class KennardStone extends NativeProcedure implements Splitter {
|
|
4607
4639
|
readonly methodId = "splitters.kennard_stone";
|
|
4608
4640
|
readonly paramTypes: {
|
|
4609
4641
|
readonly test_size: "double";
|
|
4642
|
+
readonly n_train: "int";
|
|
4610
4643
|
};
|
|
4611
4644
|
constructor(params?: KennardStoneParams);
|
|
4612
4645
|
split(X: Matrix, y?: Float64Array | ArrayLike<number>, groups?: number[]): Fold[];
|
|
@@ -4716,6 +4749,20 @@ export declare class SystematicCircular extends NativeProcedure implements Split
|
|
|
4716
4749
|
constructor(params?: SystematicCircularParams);
|
|
4717
4750
|
split(X: Matrix, y?: Float64Array | ArrayLike<number>, groups?: number[]): Fold[];
|
|
4718
4751
|
}
|
|
4752
|
+
/** Parameters of FiniteColumnQuantiles; unset values take the native defaults. */
|
|
4753
|
+
export interface FiniteColumnQuantilesParams {
|
|
4754
|
+
/** Required. */
|
|
4755
|
+
probabilities?: number[];
|
|
4756
|
+
}
|
|
4757
|
+
/** Native `utilities.finite_column_quantiles` (generic). */
|
|
4758
|
+
export declare class FiniteColumnQuantiles extends NativeProcedure implements Procedure {
|
|
4759
|
+
readonly methodId = "utilities.finite_column_quantiles";
|
|
4760
|
+
readonly paramTypes: {
|
|
4761
|
+
readonly probabilities: "double_array";
|
|
4762
|
+
};
|
|
4763
|
+
constructor(params?: FiniteColumnQuantilesParams);
|
|
4764
|
+
run(X: Matrix, y?: Matrix | Float64Array | ArrayLike<number>, inputs?: FitInputs): Record<string, ProcedureOutput>;
|
|
4765
|
+
}
|
|
4719
4766
|
/** Parameters of HotellingT2; unset values take the native defaults. */
|
|
4720
4767
|
export interface HotellingT2Params {
|
|
4721
4768
|
/** Default 5. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// SPDX-License-Identifier: CECILL-2.1
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
2
|
// Generated by scripts/generate_js_roles.py from `n4m_cli --manifest-json`. Do not edit.
|
|
3
|
-
// Native ABI 2.
|
|
3
|
+
// Native ABI 2.22.0.
|
|
4
4
|
import { NativeEstimator, NativeMethod, NativeProcedure, } from "./estimatorRoles.js";
|
|
5
5
|
/** Native `aom_pop.aom_chain_fixed_fit` (regressor). */
|
|
6
6
|
export class AOMFixedCandidate extends NativeEstimator {
|
|
@@ -1724,6 +1724,28 @@ export class KernelPLS extends NativeEstimator {
|
|
|
1724
1724
|
}
|
|
1725
1725
|
}
|
|
1726
1726
|
NativeMethod.register("models.pls.kernel", KernelPLS);
|
|
1727
|
+
/** Native `models.pls.opls` (transformer, regressor). */
|
|
1728
|
+
export class OPLS extends NativeEstimator {
|
|
1729
|
+
methodId = "models.pls.opls";
|
|
1730
|
+
paramTypes = {
|
|
1731
|
+
n_components: "int",
|
|
1732
|
+
center_x: "bool",
|
|
1733
|
+
scale_x: "bool",
|
|
1734
|
+
center_y: "bool",
|
|
1735
|
+
scale_y: "bool",
|
|
1736
|
+
};
|
|
1737
|
+
constructor(params = {}) {
|
|
1738
|
+
super();
|
|
1739
|
+
this.params = { ...params };
|
|
1740
|
+
}
|
|
1741
|
+
predict(X) {
|
|
1742
|
+
return this.predictMatrix(X);
|
|
1743
|
+
}
|
|
1744
|
+
transform(X) {
|
|
1745
|
+
return this.transformMatrix(X);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
NativeMethod.register("models.pls.opls", OPLS);
|
|
1727
1749
|
/** Native `models.pls.pcr` (transformer, regressor). */
|
|
1728
1750
|
export class PCR extends NativeEstimator {
|
|
1729
1751
|
methodId = "models.pls.pcr";
|
|
@@ -2636,6 +2658,7 @@ export class StandardScale extends NativeEstimator {
|
|
|
2636
2658
|
paramTypes = {
|
|
2637
2659
|
with_mean: "bool",
|
|
2638
2660
|
with_std: "bool",
|
|
2661
|
+
ddof: "int",
|
|
2639
2662
|
};
|
|
2640
2663
|
constructor(params = {}) {
|
|
2641
2664
|
super();
|
|
@@ -3719,6 +3742,7 @@ export class KennardStone extends NativeProcedure {
|
|
|
3719
3742
|
methodId = "splitters.kennard_stone";
|
|
3720
3743
|
paramTypes = {
|
|
3721
3744
|
test_size: "double",
|
|
3745
|
+
n_train: "int",
|
|
3722
3746
|
};
|
|
3723
3747
|
constructor(params = {}) {
|
|
3724
3748
|
super();
|
|
@@ -3826,6 +3850,21 @@ export class SystematicCircular extends NativeProcedure {
|
|
|
3826
3850
|
}
|
|
3827
3851
|
}
|
|
3828
3852
|
NativeMethod.register("splitters.systematic_circular", SystematicCircular);
|
|
3853
|
+
/** Native `utilities.finite_column_quantiles` (generic). */
|
|
3854
|
+
export class FiniteColumnQuantiles extends NativeProcedure {
|
|
3855
|
+
methodId = "utilities.finite_column_quantiles";
|
|
3856
|
+
paramTypes = {
|
|
3857
|
+
probabilities: "double_array",
|
|
3858
|
+
};
|
|
3859
|
+
constructor(params = {}) {
|
|
3860
|
+
super();
|
|
3861
|
+
this.params = { ...params };
|
|
3862
|
+
}
|
|
3863
|
+
run(X, y, inputs = {}) {
|
|
3864
|
+
return this.runOutputs(X, y, inputs);
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3867
|
+
NativeMethod.register("utilities.finite_column_quantiles", FiniteColumnQuantiles);
|
|
3829
3868
|
/** Native `utilities.hotelling_t2` (generic). */
|
|
3830
3869
|
export class HotellingT2 extends NativeProcedure {
|
|
3831
3870
|
methodId = "utilities.hotelling_t2";
|
package/dist/ffi.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -22,3 +22,5 @@ export { Status, Dtype, Algorithm, Solver, Deflation, ModelArray, N4mError, type
|
|
|
22
22
|
export declare function version(): string;
|
|
23
23
|
/** ABI MAJOR.MINOR.PATCH triple. */
|
|
24
24
|
export declare function abiVersion(): readonly [number, number, number];
|
|
25
|
+
export { rank, type RankingMethod, type RankingOptions, type RankingCandidate, type RankingResult } from "./ranking.js";
|
|
26
|
+
export { mixedEffects, fanova, normalizeLoss, type MixedEffectsOptions, type MixedEffectsResult, type FanovaOptions, type FanovaResult } from "./componentAnalysis.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier: CECILL-2.1
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
2
|
//
|
|
3
3
|
// Public TypeScript API for the @nirs4all/methods binding — a
|
|
4
4
|
// non-idiomatic function library over libn4m (raw typed arrays in/out). See
|
|
@@ -45,3 +45,5 @@ export function abiVersion() {
|
|
|
45
45
|
m.ccall("n4m_get_abi_version_patch", "number", [], []),
|
|
46
46
|
];
|
|
47
47
|
}
|
|
48
|
+
export { rank } from "./ranking.js";
|
|
49
|
+
export { mixedEffects, fanova, normalizeLoss } from "./componentAnalysis.js";
|
package/dist/methodResult.js
CHANGED
package/dist/model.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// SPDX-License-Identifier: CECILL-2.1
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
2
|
// Raw tensor marshalling only. Learned encoders/fusion/Ridge live in libn4m.
|
|
3
3
|
import { checkStatus, getModule, makeMatrixView } from "./ffi.js";
|
|
4
4
|
import { nativeParams, withContext } from "./estimatorRoles.js";
|