@nirs4all/methods 1.0.17 → 1.1.0
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/README.md +37 -2
- package/dist/estimatorRoles.d.ts +139 -0
- package/dist/estimatorRoles.js +531 -0
- package/dist/estimatorRolesGenerated.d.ts +4711 -0
- package/dist/estimatorRolesGenerated.js +3825 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +8 -1
- package/dist/methodResult.d.ts +2 -0
- package/dist/methodResult.js +33 -1
- package/dist/model.d.ts +29 -1
- package/dist/model.js +69 -3
- package/dist/n4m.js +1 -1
- package/dist/n4m.wasm +0 -0
- package/dist/nativeAugmentation.d.ts +29 -0
- package/dist/nativeAugmentation.js +49 -0
- package/dist/nativeModel.d.ts +17 -0
- package/dist/nativeModel.js +100 -0
- package/dist/nativePreprocessingPipeline.d.ts +42 -0
- package/dist/nativePreprocessingPipeline.js +246 -0
- package/dist/nativeSplitter.d.ts +23 -0
- package/dist/nativeSplitter.js +116 -0
- package/dist/selection.d.ts +6 -0
- package/dist/selection.js +245 -0
- package/dist/types.d.ts +14 -13
- package/dist/types.js +14 -13
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
export { loadModule, getModule, makeMatrixView, readArrayView } from "./ffi.js";
|
|
2
2
|
export { Context } from "./context.js";
|
|
3
3
|
export { Config } from "./config.js";
|
|
4
|
-
export { Model, fitPls, predictPls, fitModel, predictModel, fitAom, fitPop, fitAomRidge, fitAomStack, computeSplit, computeSplitIndices, type PlsModel, type FittedModel, type AomModel, type PopModel, type AomRidgeOptions, type AomStackOptions, type SplitKind, type SplitOptions, type SplitIndices } from "./model.js";
|
|
4
|
+
export { Model, fitPls, predictPls, fitModel, predictModel, fitAom, fitAomChain, fitPop, fitAomRidge, fitAomStack, computeSplit, computeSplitIndices, type PlsModel, type FittedModel, type AomModel, type AomChainDescriptor, type AomChainModel, type PopModel, type AomRidgeOptions, type AomStackOptions, type SplitKind, type SplitOptions, type SplitIndices } from "./model.js";
|
|
5
5
|
export { ppCreate, ppFit, ppTransform, ppGetState, ppSetState, ppDestroy, type PpOperator, } from "./preprocessing.js";
|
|
6
6
|
export { MethodResult } from "./methodResult.js";
|
|
7
|
+
export { NativeModel } from "./nativeModel.js";
|
|
8
|
+
export { NativeEstimator, NativeMethod, NativeProcedure, manifest, methodClass, type Augmenter, type Classifier, type FitInputs, type Fold, type ParamType, type ParamValue, type ProbabilisticClassifier, type Procedure, type ProcedureOutput, type Regressor, type SampleFilter, type Selector, type Splitter, type TargetMixingAugmenter, type Transformer } from "./estimatorRoles.js";
|
|
9
|
+
export * from "./estimatorRolesGenerated.js";
|
|
10
|
+
export { splitNative, type NativeSplitterKind, type NativeSplitterOptions, type NativeSplitIndices } from "./nativeSplitter.js";
|
|
11
|
+
export { augmentNative, type NativeAugmentationKind } from "./nativeAugmentation.js";
|
|
12
|
+
export { NativePreprocessingPipeline, PipelineOperatorKind, type PipelineStep, } from "./nativePreprocessingPipeline.js";
|
|
13
|
+
export { selectSpa, selectVariables, selectorMethods } from "./selection.js";
|
|
7
14
|
export { inspectN4mm, SERIALIZED_MODEL_INFO_SCHEMA_V1, SERIALIZED_MODEL_CAPABILITY_PREDICT, SERIALIZED_MODEL_CAPABILITY_TRANSFORM, SERIALIZED_MODEL_CAPABILITY_AFFINE, SERIALIZED_MODEL_CAPABILITY_PIPELINE, PipelineFingerprintAlgorithm, PipelineSemanticProfile, SerializedSavitzkyGolayMode, SerializedPipelineOperatorKind, type SerializedModelInfo, type SerializedPipelineInfo, } from "./serialization.js";
|
|
8
15
|
export { Status, Dtype, Algorithm, Solver, Deflation, N4mError, type Matrix, } from "./types.js";
|
|
9
16
|
/** ABI / project version reported by the loaded WASM module. */
|
package/dist/index.js
CHANGED
|
@@ -13,9 +13,16 @@ import { getModule } from "./ffi.js";
|
|
|
13
13
|
export { loadModule, getModule, makeMatrixView, readArrayView } from "./ffi.js";
|
|
14
14
|
export { Context } from "./context.js";
|
|
15
15
|
export { Config } from "./config.js";
|
|
16
|
-
export { Model, fitPls, predictPls, fitModel, predictModel, fitAom, fitPop, fitAomRidge, fitAomStack, computeSplit, computeSplitIndices } from "./model.js";
|
|
16
|
+
export { Model, fitPls, predictPls, fitModel, predictModel, fitAom, fitAomChain, fitPop, fitAomRidge, fitAomStack, computeSplit, computeSplitIndices } from "./model.js";
|
|
17
17
|
export { ppCreate, ppFit, ppTransform, ppGetState, ppSetState, ppDestroy, } from "./preprocessing.js";
|
|
18
18
|
export { MethodResult } from "./methodResult.js";
|
|
19
|
+
export { NativeModel } from "./nativeModel.js";
|
|
20
|
+
export { NativeEstimator, NativeMethod, NativeProcedure, manifest, methodClass } from "./estimatorRoles.js";
|
|
21
|
+
export * from "./estimatorRolesGenerated.js";
|
|
22
|
+
export { splitNative } from "./nativeSplitter.js";
|
|
23
|
+
export { augmentNative } from "./nativeAugmentation.js";
|
|
24
|
+
export { NativePreprocessingPipeline, PipelineOperatorKind, } from "./nativePreprocessingPipeline.js";
|
|
25
|
+
export { selectSpa, selectVariables, selectorMethods } from "./selection.js";
|
|
19
26
|
export { inspectN4mm, SERIALIZED_MODEL_INFO_SCHEMA_V1, SERIALIZED_MODEL_CAPABILITY_PREDICT, SERIALIZED_MODEL_CAPABILITY_TRANSFORM, SERIALIZED_MODEL_CAPABILITY_AFFINE, SERIALIZED_MODEL_CAPABILITY_PIPELINE, PipelineFingerprintAlgorithm, PipelineSemanticProfile, SerializedSavitzkyGolayMode, SerializedPipelineOperatorKind, } from "./serialization.js";
|
|
20
27
|
export { Status, Dtype, Algorithm, Solver, Deflation, N4mError, } from "./types.js";
|
|
21
28
|
/** ABI / project version reported by the loaded WASM module. */
|
package/dist/methodResult.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export declare class MethodResult {
|
|
|
22
22
|
matrix(name: string): Matrix;
|
|
23
23
|
/** Read a named int32 vector. */
|
|
24
24
|
vectorInt(name: string): Int32Array;
|
|
25
|
+
/** Read a named int64 vector without narrowing its elements to JS numbers. */
|
|
26
|
+
vectorInt64(name: string): BigInt64Array;
|
|
25
27
|
/** Read a named scalar (returns NaN if not present). */
|
|
26
28
|
scalar(name: string): number;
|
|
27
29
|
destroy(): void;
|
package/dist/methodResult.js
CHANGED
|
@@ -73,7 +73,7 @@ export class MethodResult {
|
|
|
73
73
|
m.stringToUTF8(name, namePtr, nameBytes + 1);
|
|
74
74
|
const status = m.ccall("n4m_method_result_get_double_matrix", "number", ["number", "number", "number", "number", "number"], [this._ptr, namePtr, dataPtrPtr, rowsPtr, colsPtr]);
|
|
75
75
|
checkStatus(status);
|
|
76
|
-
const dataPtr = m.getValue(dataPtrPtr, "i32");
|
|
76
|
+
const dataPtr = m.getValue(dataPtrPtr, "i32") >>> 0;
|
|
77
77
|
// i64 lo/hi pair — WASM_BIGINT=1 returns BigInt; use HEAP32 instead.
|
|
78
78
|
const rows = m.getValue(rowsPtr, "i64");
|
|
79
79
|
const cols = m.getValue(colsPtr, "i64");
|
|
@@ -116,6 +116,38 @@ export class MethodResult {
|
|
|
116
116
|
m._free(sizePtr);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
/** Read a named int64 vector without narrowing its elements to JS numbers. */
|
|
120
|
+
vectorInt64(name) {
|
|
121
|
+
const m = getModule();
|
|
122
|
+
const nameBytes = m.lengthBytesUTF8(name);
|
|
123
|
+
const namePtr = m._malloc(nameBytes + 1);
|
|
124
|
+
const dataPtrPtr = m._malloc(4);
|
|
125
|
+
const sizePtr = m._malloc(8);
|
|
126
|
+
try {
|
|
127
|
+
m.stringToUTF8(name, namePtr, nameBytes + 1);
|
|
128
|
+
const status = m.ccall("n4m_method_result_get_int64_vector", "number", ["number", "number", "number", "number"], [this._ptr, namePtr, dataPtrPtr, sizePtr]);
|
|
129
|
+
checkStatus(status);
|
|
130
|
+
const dataPtr = m.getValue(dataPtrPtr, "i32") >>> 0;
|
|
131
|
+
const size64 = BigInt(m.getValue(sizePtr, "i64"));
|
|
132
|
+
if (size64 < 0n || size64 > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
133
|
+
throw new RangeError(`Invalid int64 vector length for '${name}'.`);
|
|
134
|
+
}
|
|
135
|
+
const size = Number(size64);
|
|
136
|
+
if (dataPtr < 0 || size > Math.floor((m.HEAPU8.byteLength - dataPtr) / 8)) {
|
|
137
|
+
throw new RangeError(`Invalid int64 vector buffer for '${name}'.`);
|
|
138
|
+
}
|
|
139
|
+
const out = new BigInt64Array(size);
|
|
140
|
+
const view = new DataView(m.HEAPU8.buffer, m.HEAPU8.byteOffset + dataPtr, size * 8);
|
|
141
|
+
for (let i = 0; i < size; i += 1)
|
|
142
|
+
out[i] = view.getBigInt64(i * 8, true);
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
m._free(namePtr);
|
|
147
|
+
m._free(dataPtrPtr);
|
|
148
|
+
m._free(sizePtr);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
119
151
|
/** Read a named scalar (returns NaN if not present). */
|
|
120
152
|
scalar(name) {
|
|
121
153
|
const m = getModule();
|
package/dist/model.d.ts
CHANGED
|
@@ -44,7 +44,10 @@ export interface FittedModel {
|
|
|
44
44
|
* @param X row-major (n × p) input matrix.
|
|
45
45
|
* @param Y row-major (n × q) target matrix.
|
|
46
46
|
* @param n_components number of latent components (used by the PLS family).
|
|
47
|
-
* @param params positional hyper-parameter vector for the model.
|
|
47
|
+
* @param params positional hyper-parameter vector for the model. For
|
|
48
|
+
* GroupSparsePLS this is `[group_lambda, ...group_assignment]`, with exactly
|
|
49
|
+
* one non-negative integer group ID per input feature; no implicit grouping
|
|
50
|
+
* is applied.
|
|
48
51
|
*/
|
|
49
52
|
export declare function fitModel(model: string, X: Matrix, Y: Matrix, n_components: number, params?: number[]): FittedModel;
|
|
50
53
|
/** Predict from a fitted {@link FittedModel} for new X (row-major n_new × p). */
|
|
@@ -59,6 +62,31 @@ export interface AomModel extends FittedModel {
|
|
|
59
62
|
/** Best internal-CV score of the selected operator. */
|
|
60
63
|
score: number;
|
|
61
64
|
}
|
|
65
|
+
export interface AomChainDescriptor {
|
|
66
|
+
/** Chain boundaries in the flattened operator list (n_chains + 1). */
|
|
67
|
+
chainOffsets: number[];
|
|
68
|
+
/** Flattened n4m_operator_kind_t values. */
|
|
69
|
+
operatorKinds: number[];
|
|
70
|
+
/** Parameter boundaries in the flattened parameter list (n_ops + 1). */
|
|
71
|
+
parameterOffsets: number[];
|
|
72
|
+
/** Flattened operator parameters. */
|
|
73
|
+
parameters: number[];
|
|
74
|
+
}
|
|
75
|
+
export interface AomChainModel extends FittedModel {
|
|
76
|
+
selectedChain: number;
|
|
77
|
+
selectedParameter: number;
|
|
78
|
+
score: number;
|
|
79
|
+
}
|
|
80
|
+
/** Fit one PLS or Ridge head with the configurable native AOM chain sweep.
|
|
81
|
+
* The exact same descriptor can be evaluated by an external HPO implementation,
|
|
82
|
+
* making search-space parity explicit and auditable. */
|
|
83
|
+
export declare function fitAomChain(X: Matrix, Y: Matrix, descriptor: AomChainDescriptor, options: {
|
|
84
|
+
nFolds?: number;
|
|
85
|
+
head: "ridge" | "pls";
|
|
86
|
+
ridgeLambdas?: number[];
|
|
87
|
+
plsComponents?: number[];
|
|
88
|
+
momentPolicy?: "auto" | "materialized" | "moments";
|
|
89
|
+
}): AomChainModel;
|
|
62
90
|
/** Fit AOM-PLS (operator-adaptive PLS) on (X, Y).
|
|
63
91
|
*
|
|
64
92
|
* Screens a bank of strict-linear preprocessing operators by internal k-fold CV
|
package/dist/model.js
CHANGED
|
@@ -106,7 +106,10 @@ export function predictPls(model, X_new) {
|
|
|
106
106
|
* @param X row-major (n × p) input matrix.
|
|
107
107
|
* @param Y row-major (n × q) target matrix.
|
|
108
108
|
* @param n_components number of latent components (used by the PLS family).
|
|
109
|
-
* @param params positional hyper-parameter vector for the model.
|
|
109
|
+
* @param params positional hyper-parameter vector for the model. For
|
|
110
|
+
* GroupSparsePLS this is `[group_lambda, ...group_assignment]`, with exactly
|
|
111
|
+
* one non-negative integer group ID per input feature; no implicit grouping
|
|
112
|
+
* is applied.
|
|
110
113
|
*/
|
|
111
114
|
export function fitModel(model, X, Y, n_components, params = []) {
|
|
112
115
|
if (X.rows !== Y.rows) {
|
|
@@ -134,7 +137,7 @@ export function fitModel(model, X, Y, n_components, params = []) {
|
|
|
134
137
|
coefsBuf.ptr, xmBuf.ptr, ymBuf.ptr, interBuf.ptr,
|
|
135
138
|
hasInterBuf, 0]);
|
|
136
139
|
checkStatus(status);
|
|
137
|
-
// Only models with a genuine affine intercept (
|
|
140
|
+
// Only models with a genuine affine intercept (Ridge and MBPLS) report
|
|
138
141
|
// has_intercept=1; the PLS/PCR family and the PLS-based Tier-B fits
|
|
139
142
|
// predict via the centred form and carry no intercept (kept null so a
|
|
140
143
|
// caller never adds a misleading zero/y_mean term to x.B).
|
|
@@ -176,7 +179,7 @@ export function predictModel(model, X_new) {
|
|
|
176
179
|
// AOM-PLS (and any future affine model) carries input-space coefficients +
|
|
177
180
|
// a genuine intercept and zero means — it predicts on RAW X via the
|
|
178
181
|
// explicit-intercept form pred = intercept + x.B. Every centred model
|
|
179
|
-
// (PLS family +
|
|
182
|
+
// (PLS family + centred Tier-B fits) carries intercept = null
|
|
180
183
|
// and predicts via pred = y_mean + (x - x_mean).B. The C helper picks the
|
|
181
184
|
// form from whether the intercept pointer is non-NULL.
|
|
182
185
|
const useIntercept = model.intercept !== null;
|
|
@@ -208,6 +211,69 @@ export function predictModel(model, X_new) {
|
|
|
208
211
|
M._free(interBuf.ptr);
|
|
209
212
|
}
|
|
210
213
|
}
|
|
214
|
+
/** Fit one PLS or Ridge head with the configurable native AOM chain sweep.
|
|
215
|
+
* The exact same descriptor can be evaluated by an external HPO implementation,
|
|
216
|
+
* making search-space parity explicit and auditable. */
|
|
217
|
+
export function fitAomChain(X, Y, descriptor, options) {
|
|
218
|
+
if (X.rows !== Y.rows)
|
|
219
|
+
throw new Error(`X.rows (${X.rows}) must equal Y.rows (${Y.rows})`);
|
|
220
|
+
const { chainOffsets, operatorKinds, parameterOffsets, parameters } = descriptor;
|
|
221
|
+
if (chainOffsets.length < 2 || operatorKinds.length < 1 || parameterOffsets.length !== operatorKinds.length + 1) {
|
|
222
|
+
throw new Error("Invalid AOM chain descriptor");
|
|
223
|
+
}
|
|
224
|
+
const ridgeLambdas = options.ridgeLambdas ?? [];
|
|
225
|
+
const plsComponents = options.plsComponents ?? [];
|
|
226
|
+
if (options.head === "ridge" && ridgeLambdas.length < 1)
|
|
227
|
+
throw new Error("Ridge AOM requires ridgeLambdas");
|
|
228
|
+
if (options.head === "pls" && plsComponents.length < 1)
|
|
229
|
+
throw new Error("PLS AOM requires plsComponents");
|
|
230
|
+
const M = getModule();
|
|
231
|
+
const n = X.rows, p = X.cols, q = Y.cols;
|
|
232
|
+
const xBuf = _malloc_f64(M, n * p), yBuf = _malloc_f64(M, n * q);
|
|
233
|
+
const chainBuf = M._malloc(chainOffsets.length * 4);
|
|
234
|
+
const kindsBuf = M._malloc(operatorKinds.length * 4);
|
|
235
|
+
const offsetsBuf = M._malloc(parameterOffsets.length * 4);
|
|
236
|
+
const paramsBuf = parameters.length > 0 ? _malloc_f64(M, parameters.length).ptr : 0;
|
|
237
|
+
const ridgeBuf = ridgeLambdas.length > 0 ? _malloc_f64(M, ridgeLambdas.length).ptr : 0;
|
|
238
|
+
const plsBuf = plsComponents.length > 0 ? M._malloc(plsComponents.length * 4) : 0;
|
|
239
|
+
const coefsBuf = _malloc_f64(M, p * q), interBuf = _malloc_f64(M, q);
|
|
240
|
+
const selectedBuf = M._malloc(4), parameterBuf = _malloc_f64(M, 1), scoreBuf = _malloc_f64(M, 1);
|
|
241
|
+
try {
|
|
242
|
+
_copy_in(M, X.data, xBuf.ptr);
|
|
243
|
+
_copy_in(M, Y.data, yBuf.ptr);
|
|
244
|
+
M.HEAP32.set(Int32Array.from(chainOffsets), chainBuf >> 2);
|
|
245
|
+
M.HEAP32.set(Int32Array.from(operatorKinds), kindsBuf >> 2);
|
|
246
|
+
M.HEAP32.set(Int32Array.from(parameterOffsets), offsetsBuf >> 2);
|
|
247
|
+
if (paramsBuf !== 0)
|
|
248
|
+
M.HEAPF64.set(Float64Array.from(parameters), paramsBuf >>> 3);
|
|
249
|
+
if (ridgeBuf !== 0)
|
|
250
|
+
M.HEAPF64.set(Float64Array.from(ridgeLambdas), ridgeBuf >>> 3);
|
|
251
|
+
if (plsBuf !== 0)
|
|
252
|
+
M.HEAP32.set(Int32Array.from(plsComponents), plsBuf >> 2);
|
|
253
|
+
const status = M.ccall("n4m_wasm_aom_chain_fit", "number", Array(25).fill("number"), [xBuf.ptr, yBuf.ptr, n, p, q, options.nFolds ?? 5,
|
|
254
|
+
chainBuf, chainOffsets.length, kindsBuf, operatorKinds.length,
|
|
255
|
+
offsetsBuf, parameterOffsets.length, paramsBuf, parameters.length,
|
|
256
|
+
ridgeBuf, ridgeLambdas.length, plsBuf, plsComponents.length,
|
|
257
|
+
options.head === "ridge" ? 1 : 2,
|
|
258
|
+
options.momentPolicy === "materialized" ? 1 : options.momentPolicy === "moments" ? 2 : 0,
|
|
259
|
+
coefsBuf.ptr, interBuf.ptr, selectedBuf, parameterBuf.ptr, scoreBuf.ptr]);
|
|
260
|
+
checkStatus(status);
|
|
261
|
+
return {
|
|
262
|
+
coefficients: _read_out(M, coefsBuf.ptr, p * q),
|
|
263
|
+
xMean: new Float64Array(p), yMean: new Float64Array(q),
|
|
264
|
+
intercept: _read_out(M, interBuf.ptr, q),
|
|
265
|
+
n_features: p, n_targets: q,
|
|
266
|
+
selectedChain: M.HEAP32[selectedBuf >> 2] ?? -1,
|
|
267
|
+
selectedParameter: _read_out(M, parameterBuf.ptr, 1)[0] ?? NaN,
|
|
268
|
+
score: _read_out(M, scoreBuf.ptr, 1)[0] ?? NaN,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
finally {
|
|
272
|
+
[xBuf.ptr, yBuf.ptr, chainBuf, kindsBuf, offsetsBuf, paramsBuf, ridgeBuf, plsBuf,
|
|
273
|
+
coefsBuf.ptr, interBuf.ptr, selectedBuf, parameterBuf.ptr, scoreBuf.ptr]
|
|
274
|
+
.filter((ptr) => ptr !== 0).forEach((ptr) => M._free(ptr));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
211
277
|
/** Fit AOM-PLS (operator-adaptive PLS) on (X, Y).
|
|
212
278
|
*
|
|
213
279
|
* Screens a bank of strict-linear preprocessing operators by internal k-fold CV
|