@nirs4all/methods 1.2.1 → 1.3.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 +57 -2
- package/dist/estimatorRolesGenerated.d.ts +17 -0
- package/dist/estimatorRolesGenerated.js +17 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/multimodalPipeline.d.ts +68 -0
- package/dist/multimodalPipeline.js +579 -0
- package/dist/n4m.js +1 -1
- package/dist/n4m.wasm +0 -0
- package/dist/optimization.d.ts +114 -0
- package/dist/optimization.js +637 -0
- package/dist/spectralEncoding.d.ts +27 -0
- package/dist/spectralEncoding.js +92 -0
- package/package.json +2 -2
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1
|
|
2
|
+
import { checkStatus, getModule } from "./ffi.js";
|
|
3
|
+
/** Fitted native LVSE/GCU. Call dispose() to release its WASM handle. */
|
|
4
|
+
export class SpectralEncoder {
|
|
5
|
+
ptr = 0;
|
|
6
|
+
features = 0;
|
|
7
|
+
outputs = 0;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
if (options.kind !== "lvse" && options.kind !== "gcu")
|
|
10
|
+
throw new Error("Unknown encoder kind");
|
|
11
|
+
const width = options.width ?? 64;
|
|
12
|
+
const rank = options.rank ?? (options.kind === "lvse" ? 4 : 16);
|
|
13
|
+
const iterations = options.maxIter ?? 60;
|
|
14
|
+
for (const v of [width, rank, iterations]) {
|
|
15
|
+
if (!Number.isInteger(v) || v < 1 || v > 2147483647)
|
|
16
|
+
throw new Error("Invalid encoder integer parameter");
|
|
17
|
+
}
|
|
18
|
+
const m = getModule(), out = m._malloc(4);
|
|
19
|
+
try {
|
|
20
|
+
checkStatus(m.ccall("n4m_decomposition_spectral_create", "number", Array(9).fill("number"), [options.kind === "lvse" ? 0 : 1, width, rank,
|
|
21
|
+
options.overlap ?? 0, Number(options.standardize ?? true),
|
|
22
|
+
Number(options.snv ?? false), iterations, options.tolerance ?? 1e-3, out]));
|
|
23
|
+
this.ptr = m.HEAP32[out / 4];
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
m._free(out);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
check(X) {
|
|
30
|
+
if (!this.ptr)
|
|
31
|
+
throw new Error("Encoder is disposed");
|
|
32
|
+
if (!Number.isInteger(X.rows) || !Number.isInteger(X.cols) || X.rows < 1 || X.cols < 1
|
|
33
|
+
|| X.rows > 2147483647 || X.cols > 2147483647 || X.data.length !== X.rows * X.cols)
|
|
34
|
+
throw new Error("Invalid spectral matrix dimensions");
|
|
35
|
+
}
|
|
36
|
+
fit(X) {
|
|
37
|
+
this.check(X);
|
|
38
|
+
const m = getModule(), xp = m._malloc(X.data.length * 8), count = m._malloc(8);
|
|
39
|
+
try {
|
|
40
|
+
m.HEAPF64.set(X.data, xp / 8);
|
|
41
|
+
checkStatus(m.ccall("n4m_wasm_spectral_fit", "number", Array(4).fill("number"), [this.ptr, xp, X.rows, X.cols]));
|
|
42
|
+
checkStatus(m.ccall("n4m_decomposition_spectral_output_cols", "number", ["number", "number"], [this.ptr, count]));
|
|
43
|
+
const view = new DataView(m.HEAPU8.buffer);
|
|
44
|
+
this.outputs = Number(view.getBigInt64(count, true));
|
|
45
|
+
this.features = X.cols;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
m._free(xp);
|
|
50
|
+
m._free(count);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
transform(X) {
|
|
54
|
+
this.check(X);
|
|
55
|
+
if (!this.outputs || X.cols !== this.features)
|
|
56
|
+
throw new Error("Unfitted encoder or feature mismatch");
|
|
57
|
+
const m = getModule(), xp = m._malloc(X.data.length * 8), out = m._malloc(X.rows * this.outputs * 8);
|
|
58
|
+
try {
|
|
59
|
+
m.HEAPF64.set(X.data, xp / 8);
|
|
60
|
+
checkStatus(m.ccall("n4m_wasm_spectral_transform", "number", Array(6).fill("number"), [this.ptr, xp, X.rows, X.cols, out, this.outputs]));
|
|
61
|
+
return { data: Float64Array.from(m.HEAPF64.subarray(out / 8, out / 8 + X.rows * this.outputs)),
|
|
62
|
+
rows: X.rows, cols: this.outputs };
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
m._free(xp);
|
|
66
|
+
m._free(out);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** LVSE without SNV: transform(X) = X @ operator.T + offset. */
|
|
70
|
+
exportAffine() {
|
|
71
|
+
if (!this.ptr || !this.outputs)
|
|
72
|
+
throw new Error("Encoder is not fitted");
|
|
73
|
+
const m = getModule(), size = this.outputs * this.features;
|
|
74
|
+
const op = m._malloc(size * 8), offset = m._malloc(this.outputs * 8);
|
|
75
|
+
try {
|
|
76
|
+
checkStatus(m.ccall("n4m_wasm_spectral_affine", "number", Array(5).fill("number"), [this.ptr, op, offset, this.features, this.outputs]));
|
|
77
|
+
return { operator: { data: Float64Array.from(m.HEAPF64.subarray(op / 8, op / 8 + size)),
|
|
78
|
+
rows: this.outputs, cols: this.features },
|
|
79
|
+
offset: Float64Array.from(m.HEAPF64.subarray(offset / 8, offset / 8 + this.outputs)) };
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
m._free(op);
|
|
83
|
+
m._free(offset);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
dispose() {
|
|
87
|
+
if (this.ptr) {
|
|
88
|
+
getModule().ccall("n4m_decomposition_spectral_destroy", null, ["number"], [this.ptr]);
|
|
89
|
+
this.ptr = 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nirs4all/methods",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "WebAssembly build of libn4m (nirs4all-methods) — portable PLS/NIRS engine and native method bindings.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"build": "npm run clean && tsc -p .",
|
|
21
21
|
"stage:wasm": "node -e \"const fs=require('fs'),p='../../build/emscripten/bindings/js/';fs.mkdirSync('dist',{recursive:true});for(const f of ['n4m.js','n4m.wasm']){if(!fs.existsSync(p+f))throw new Error('missing WASM artifact '+p+f+' — run: cmake --preset emscripten && cmake --build --preset emscripten --target n4m_wasm');fs.copyFileSync(p+f,'dist/'+f)}console.log('staged n4m.js + n4m.wasm into dist/')\"",
|
|
22
22
|
"prepack": "npm run build && npm run stage:wasm",
|
|
23
|
-
"test": "node --experimental-vm-modules test/run_smoke.mjs && node --experimental-vm-modules test/run_api.mjs && node --experimental-vm-modules test/run_generic_method.mjs && node --experimental-vm-modules test/run_pop_aom.mjs && node --experimental-vm-modules test/run_new_pack.mjs && node --experimental-vm-modules test/run_serialization_inspect.mjs && node --experimental-vm-modules test/run_method_result_model.mjs && node --experimental-vm-modules test/run_native_preprocessing_pipeline.mjs && node --experimental-vm-modules test/run_native_splitter.mjs && node --experimental-vm-modules test/run_native_augmentation.mjs && node --experimental-vm-modules test/run_estimator_roles.mjs && node --experimental-vm-modules test/run_estimator_roles_negative.mjs && node --experimental-vm-modules test/run_role_pipeline.mjs"
|
|
23
|
+
"test": "node --experimental-vm-modules test/run_smoke.mjs && node --experimental-vm-modules test/run_api.mjs && node --experimental-vm-modules test/run_generic_method.mjs && node --experimental-vm-modules test/run_pop_aom.mjs && node --experimental-vm-modules test/run_new_pack.mjs && node --experimental-vm-modules test/run_serialization_inspect.mjs && node --experimental-vm-modules test/run_method_result_model.mjs && node --experimental-vm-modules test/run_native_preprocessing_pipeline.mjs && node --experimental-vm-modules test/run_native_splitter.mjs && node --experimental-vm-modules test/run_native_augmentation.mjs && node --experimental-vm-modules test/run_estimator_roles.mjs && node --experimental-vm-modules test/run_estimator_roles_negative.mjs && node --experimental-vm-modules test/run_role_pipeline.mjs && node --experimental-vm-modules test/run_spectral_encoding.mjs && node --experimental-vm-modules test/run_optimization.mjs"
|
|
24
24
|
},
|
|
25
25
|
"license": "CECILL-2.1",
|
|
26
26
|
"publishConfig": {
|