@nirs4all/methods 1.1.0 → 1.2.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 +2 -2
- package/dist/estimatorRoles.d.ts +30 -3
- package/dist/estimatorRoles.js +98 -13
- package/dist/estimatorRolesGenerated.d.ts +62 -62
- package/dist/estimatorRolesGenerated.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/n4m.js +1 -1
- package/dist/n4m.wasm +0 -0
- package/dist/rolePipeline.d.ts +102 -0
- package/dist/rolePipeline.js +357 -0
- package/package.json +2 -2
package/dist/n4m.wasm
CHANGED
|
Binary file
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type FitInputs, type ParamValue } from "./estimatorRoles.js";
|
|
2
|
+
import type { Matrix } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* A recipe step: a method id ("models.pls.cppls" or "n4m:models.pls.cppls"),
|
|
5
|
+
* [methodId, params], {methodId, params} or a v8 recipe token
|
|
6
|
+
* {class: "n4m:<id>", params}.
|
|
7
|
+
*/
|
|
8
|
+
export type RoleStep = string | [string, Record<string, ParamValue>] | {
|
|
9
|
+
methodId: string;
|
|
10
|
+
params?: Record<string, ParamValue>;
|
|
11
|
+
} | {
|
|
12
|
+
class: string;
|
|
13
|
+
params?: Record<string, ParamValue>;
|
|
14
|
+
};
|
|
15
|
+
/** Role a step plays in the pipeline. */
|
|
16
|
+
export type PipelineRole = "transformer" | "regressor" | "classifier" | "selector" | "sample_filter";
|
|
17
|
+
export interface RolePipelineStepInfo {
|
|
18
|
+
methodId: string;
|
|
19
|
+
role: PipelineRole;
|
|
20
|
+
/** Index among the stateful steps; -1 for a sample filter (train-only). */
|
|
21
|
+
stateIndex: number;
|
|
22
|
+
/** The fitted state embeds training rows (export needs the opt-in). */
|
|
23
|
+
containsTrainingRows: boolean;
|
|
24
|
+
nFeaturesIn: number;
|
|
25
|
+
nFeaturesOut: number;
|
|
26
|
+
}
|
|
27
|
+
export interface RolePipelineState {
|
|
28
|
+
methodId: string;
|
|
29
|
+
n4me: Uint8Array;
|
|
30
|
+
containsTrainingRows: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** Class labels: integer ids, or names mapped to ids in sorted order. */
|
|
33
|
+
export type ClassLabel = number | string;
|
|
34
|
+
/** Native trained recipe of role steps, portable as N4ME states. */
|
|
35
|
+
export declare class RolePipeline {
|
|
36
|
+
readonly steps: ReadonlyArray<RoleStep>;
|
|
37
|
+
private ptr;
|
|
38
|
+
private names;
|
|
39
|
+
private classNames;
|
|
40
|
+
private constructor();
|
|
41
|
+
/** An unfitted pipeline; the recipe is validated natively. */
|
|
42
|
+
static fromSteps(steps: ReadonlyArray<RoleStep>): RolePipeline;
|
|
43
|
+
/**
|
|
44
|
+
* A fitted pipeline rebuilt from one N4ME state per stateful step. The
|
|
45
|
+
* native import refuses states that contradict the recipe (count, method,
|
|
46
|
+
* parameters, role, widths). classNames restores string class labels.
|
|
47
|
+
*/
|
|
48
|
+
static fromStates(steps: ReadonlyArray<RoleStep>, states: ReadonlyArray<Uint8Array | RolePipelineState>, options?: {
|
|
49
|
+
featureNames?: string[];
|
|
50
|
+
classNames?: string[];
|
|
51
|
+
}): RolePipeline;
|
|
52
|
+
get fitted(): boolean;
|
|
53
|
+
/** Input column names stored at fit or import (undefined: positional). */
|
|
54
|
+
get featureNames(): string[] | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Fits every step natively. y holds responses for a final regressor (a
|
|
57
|
+
* vector or a row-major matrix) or class labels for a final classifier;
|
|
58
|
+
* featureNames are stored and checked at every later call. Returns this;
|
|
59
|
+
* on failure the previous fit is kept.
|
|
60
|
+
*/
|
|
61
|
+
fit(X: Matrix, y?: Matrix | Float64Array | ArrayLike<number> | ArrayLike<ClassLabel>, inputs?: FitInputs & {
|
|
62
|
+
featureNames?: string[];
|
|
63
|
+
}): this;
|
|
64
|
+
/** Predictions of a final regressor. */
|
|
65
|
+
predict(X: Matrix, featureNames?: string[]): Matrix;
|
|
66
|
+
/** Rows after the transformers and selectors (the final step's input). */
|
|
67
|
+
transform(X: Matrix, featureNames?: string[]): Matrix;
|
|
68
|
+
/** Class scores of a final classifier, one column per class in classes() order. */
|
|
69
|
+
decisionFunction(X: Matrix, featureNames?: string[]): Matrix;
|
|
70
|
+
/** Class probabilities, for final classifiers that define them. */
|
|
71
|
+
predictProba(X: Matrix, featureNames?: string[]): Matrix;
|
|
72
|
+
/** Class labels of a final classifier (names when trained on names). */
|
|
73
|
+
predictLabels(X: Matrix, featureNames?: string[]): ClassLabel[];
|
|
74
|
+
/**
|
|
75
|
+
* Label table of a classifier fitted on names (index = class id), else
|
|
76
|
+
* undefined. Unlike classes() it keeps labels whose rows a sample filter
|
|
77
|
+
* removed, so an exported pipeline can restore every name.
|
|
78
|
+
*/
|
|
79
|
+
labelNames(): string[] | undefined;
|
|
80
|
+
/** Fitted classes, ascending ids (names when trained on names). */
|
|
81
|
+
classes(): ClassLabel[];
|
|
82
|
+
/** Per step: method, role played, state index, fitted widths, training rows. */
|
|
83
|
+
stepsInfo(): RolePipelineStepInfo[];
|
|
84
|
+
/**
|
|
85
|
+
* One N4ME state per stateful step. A state that embeds training rows is
|
|
86
|
+
* refused unless allowTrainingRows is set.
|
|
87
|
+
*/
|
|
88
|
+
exportStates(options?: {
|
|
89
|
+
allowTrainingRows?: boolean;
|
|
90
|
+
}): RolePipelineState[];
|
|
91
|
+
/** Releases the native pipeline. */
|
|
92
|
+
dispose(): void;
|
|
93
|
+
private create;
|
|
94
|
+
private publish;
|
|
95
|
+
private handle;
|
|
96
|
+
private label;
|
|
97
|
+
private static setFeatureNames;
|
|
98
|
+
/** Native width and feature-name check of X (names optional: positional). */
|
|
99
|
+
private checkFeatures;
|
|
100
|
+
private static stepInfo;
|
|
101
|
+
private matrixOp;
|
|
102
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1
|
|
2
|
+
// Native role pipeline (ABI 2.14): a trained linear recipe of catalog
|
|
3
|
+
// estimators (sample filters, transformers / selectors, one regressor or
|
|
4
|
+
// classifier). Recipe validation, fit-input routing, the feature-name check
|
|
5
|
+
// and the per-step N4ME states are native (n4m_role_pipeline_*); this file
|
|
6
|
+
// marshals only. Class label names stay here, as the other bindings do.
|
|
7
|
+
import { checkStatus, getModule, makeMatrixView } from "./ffi.js";
|
|
8
|
+
import { cString, methodClass, nativeParams, readI64, withContext, withFitInputs, } from "./estimatorRoles.js";
|
|
9
|
+
const ROLES = {
|
|
10
|
+
1: "transformer", 2: "regressor", 4: "classifier", 8: "selector", 16: "sample_filter",
|
|
11
|
+
};
|
|
12
|
+
const EXPORT_ALLOW_TRAINING_ROWS = 1;
|
|
13
|
+
// n4m_role_pipeline_step_info_v1_t on wasm32 (pointers 4 bytes, int64 8-aligned).
|
|
14
|
+
const STEP_INFO_SIZE = 40;
|
|
15
|
+
function parseStep(step) {
|
|
16
|
+
const id = (s) => (s.startsWith("n4m:") ? s.slice(4) : s);
|
|
17
|
+
if (typeof step === "string")
|
|
18
|
+
return { methodId: id(step), params: {} };
|
|
19
|
+
if (Array.isArray(step))
|
|
20
|
+
return { methodId: id(step[0]), params: { ...step[1] } };
|
|
21
|
+
const name = "methodId" in step ? step.methodId : step.class;
|
|
22
|
+
return { methodId: id(name), params: { ...(step.params ?? {}) } };
|
|
23
|
+
}
|
|
24
|
+
/** A NUL-terminated string array; `n` pointers after the array head. */
|
|
25
|
+
function cStrings(values, allocs) {
|
|
26
|
+
const m = getModule();
|
|
27
|
+
const head = m._malloc(Math.max(1, values.length) * 4);
|
|
28
|
+
allocs.push({ ptr: head, free: () => m._free(head) });
|
|
29
|
+
values.forEach((v, i) => {
|
|
30
|
+
const s = cString(v);
|
|
31
|
+
allocs.push(s);
|
|
32
|
+
m.setValue(head + 4 * i, s.ptr, "i32");
|
|
33
|
+
});
|
|
34
|
+
return head;
|
|
35
|
+
}
|
|
36
|
+
function encodeLabels(y) {
|
|
37
|
+
const labels = Array.from(y);
|
|
38
|
+
if (labels.every((v) => typeof v === "number" && Number.isInteger(v))) {
|
|
39
|
+
return { ids: labels };
|
|
40
|
+
}
|
|
41
|
+
if (!labels.every((v) => typeof v === "string")) {
|
|
42
|
+
throw new TypeError("class labels must be all integers or all strings");
|
|
43
|
+
}
|
|
44
|
+
const names = [...new Set(labels)].sort();
|
|
45
|
+
const index = new Map(names.map((name, i) => [name, i]));
|
|
46
|
+
return { ids: labels.map((v) => index.get(v)), names };
|
|
47
|
+
}
|
|
48
|
+
/** Native trained recipe of role steps, portable as N4ME states. */
|
|
49
|
+
export class RolePipeline {
|
|
50
|
+
steps;
|
|
51
|
+
ptr = 0;
|
|
52
|
+
names;
|
|
53
|
+
classNames;
|
|
54
|
+
constructor(steps) {
|
|
55
|
+
this.steps = [...steps];
|
|
56
|
+
}
|
|
57
|
+
/** An unfitted pipeline; the recipe is validated natively. */
|
|
58
|
+
static fromSteps(steps) {
|
|
59
|
+
const pipeline = new RolePipeline(steps);
|
|
60
|
+
getModule().ccall("n4m_role_pipeline_destroy", null, ["number"], [pipeline.create()]);
|
|
61
|
+
return pipeline;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A fitted pipeline rebuilt from one N4ME state per stateful step. The
|
|
65
|
+
* native import refuses states that contradict the recipe (count, method,
|
|
66
|
+
* parameters, role, widths). classNames restores string class labels.
|
|
67
|
+
*/
|
|
68
|
+
static fromStates(steps, states, options = {}) {
|
|
69
|
+
const m = getModule();
|
|
70
|
+
const pipeline = new RolePipeline(steps);
|
|
71
|
+
const handle = pipeline.create();
|
|
72
|
+
const payloads = states.map((s) => (s instanceof Uint8Array ? s : s.n4me));
|
|
73
|
+
const allocs = [];
|
|
74
|
+
try {
|
|
75
|
+
const buffers = m._malloc(Math.max(1, payloads.length) * 4);
|
|
76
|
+
allocs.push({ ptr: buffers, free: () => m._free(buffers) });
|
|
77
|
+
const sizes = m._malloc(Math.max(1, payloads.length) * 4);
|
|
78
|
+
allocs.push({ ptr: sizes, free: () => m._free(sizes) });
|
|
79
|
+
payloads.forEach((payload, i) => {
|
|
80
|
+
const data = m._malloc(Math.max(1, payload.byteLength));
|
|
81
|
+
allocs.push({ ptr: data, free: () => m._free(data) });
|
|
82
|
+
m.HEAPU8.set(payload, data);
|
|
83
|
+
m.setValue(buffers + 4 * i, data, "i32");
|
|
84
|
+
m.setValue(sizes + 4 * i, payload.byteLength, "i32");
|
|
85
|
+
});
|
|
86
|
+
withContext((ctx) => {
|
|
87
|
+
RolePipeline.setFeatureNames(ctx, handle, options.featureNames);
|
|
88
|
+
checkStatus(m.ccall("n4m_role_pipeline_import_states", "number", ["number", "number", "number", "number", "number"], [ctx, handle, payloads.length, buffers, sizes]), ctx);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
m.ccall("n4m_role_pipeline_destroy", null, ["number"], [handle]);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
allocs.forEach((a) => a.free());
|
|
97
|
+
}
|
|
98
|
+
pipeline.publish(handle, options.featureNames, options.classNames);
|
|
99
|
+
return pipeline;
|
|
100
|
+
}
|
|
101
|
+
get fitted() {
|
|
102
|
+
return this.ptr !== 0;
|
|
103
|
+
}
|
|
104
|
+
/** Input column names stored at fit or import (undefined: positional). */
|
|
105
|
+
get featureNames() {
|
|
106
|
+
return this.names === undefined ? undefined : [...this.names];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Fits every step natively. y holds responses for a final regressor (a
|
|
110
|
+
* vector or a row-major matrix) or class labels for a final classifier;
|
|
111
|
+
* featureNames are stored and checked at every later call. Returns this;
|
|
112
|
+
* on failure the previous fit is kept.
|
|
113
|
+
*/
|
|
114
|
+
fit(X, y, inputs = {}) {
|
|
115
|
+
const m = getModule();
|
|
116
|
+
const handle = this.create();
|
|
117
|
+
let classNames;
|
|
118
|
+
try {
|
|
119
|
+
const classifier = RolePipeline.stepInfo(handle, this.steps.length - 1).role === "classifier";
|
|
120
|
+
let target = y;
|
|
121
|
+
if (classifier && y !== undefined) {
|
|
122
|
+
const encoded = encodeLabels(y);
|
|
123
|
+
classNames = encoded.names;
|
|
124
|
+
target = encoded.ids;
|
|
125
|
+
}
|
|
126
|
+
const { featureNames, ...fitInputs } = inputs;
|
|
127
|
+
withFitInputs(X, target, classifier, fitInputs, (struct) => withContext((ctx) => {
|
|
128
|
+
RolePipeline.setFeatureNames(ctx, handle, featureNames);
|
|
129
|
+
checkStatus(m.ccall("n4m_role_pipeline_fit", "number", ["number", "number", "number"], [ctx, handle, struct]), ctx);
|
|
130
|
+
}));
|
|
131
|
+
this.publish(handle, featureNames, classNames);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (this.ptr !== handle)
|
|
135
|
+
m.ccall("n4m_role_pipeline_destroy", null, ["number"], [handle]);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
/** Predictions of a final regressor. */
|
|
141
|
+
predict(X, featureNames) {
|
|
142
|
+
return this.matrixOp("n4m_role_pipeline_predict", "n4m_role_pipeline_n_outputs", X, featureNames);
|
|
143
|
+
}
|
|
144
|
+
/** Rows after the transformers and selectors (the final step's input). */
|
|
145
|
+
transform(X, featureNames) {
|
|
146
|
+
return this.matrixOp("n4m_role_pipeline_transform", "n4m_role_pipeline_transform_cols", X, featureNames);
|
|
147
|
+
}
|
|
148
|
+
/** Class scores of a final classifier, one column per class in classes() order. */
|
|
149
|
+
decisionFunction(X, featureNames) {
|
|
150
|
+
return this.matrixOp("n4m_role_pipeline_decision_function", "n4m_role_pipeline_n_outputs", X, featureNames);
|
|
151
|
+
}
|
|
152
|
+
/** Class probabilities, for final classifiers that define them. */
|
|
153
|
+
predictProba(X, featureNames) {
|
|
154
|
+
return this.matrixOp("n4m_role_pipeline_predict_proba", "n4m_role_pipeline_n_outputs", X, featureNames);
|
|
155
|
+
}
|
|
156
|
+
/** Class labels of a final classifier (names when trained on names). */
|
|
157
|
+
predictLabels(X, featureNames) {
|
|
158
|
+
const m = getModule();
|
|
159
|
+
const handle = this.handle();
|
|
160
|
+
const xv = makeMatrixView(X.data, X.rows, X.cols);
|
|
161
|
+
const buf = m._malloc(Math.max(1, X.rows) * 8);
|
|
162
|
+
const allocs = [];
|
|
163
|
+
try {
|
|
164
|
+
withContext((ctx) => {
|
|
165
|
+
this.checkFeatures(ctx, X, featureNames, allocs);
|
|
166
|
+
checkStatus(m.ccall("n4m_role_pipeline_predict_labels", "number", ["number", "number", "number", "number", "i64"], [ctx, handle, xv.viewPtr, buf, BigInt(X.rows)]), ctx);
|
|
167
|
+
});
|
|
168
|
+
return Array.from({ length: X.rows }, (_, i) => this.label(readI64(buf + 8 * i)));
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
xv.free();
|
|
172
|
+
m._free(buf);
|
|
173
|
+
allocs.forEach((a) => a.free());
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Label table of a classifier fitted on names (index = class id), else
|
|
178
|
+
* undefined. Unlike classes() it keeps labels whose rows a sample filter
|
|
179
|
+
* removed, so an exported pipeline can restore every name.
|
|
180
|
+
*/
|
|
181
|
+
labelNames() {
|
|
182
|
+
this.handle();
|
|
183
|
+
return this.classNames === undefined ? undefined : [...this.classNames];
|
|
184
|
+
}
|
|
185
|
+
/** Fitted classes, ascending ids (names when trained on names). */
|
|
186
|
+
classes() {
|
|
187
|
+
const m = getModule();
|
|
188
|
+
const handle = this.handle();
|
|
189
|
+
const countPtr = m._malloc(8);
|
|
190
|
+
try {
|
|
191
|
+
checkStatus(m.ccall("n4m_role_pipeline_classes", "number", ["number", "number", "i64", "number"], [handle, 0, BigInt(0), countPtr]));
|
|
192
|
+
const count = readI64(countPtr);
|
|
193
|
+
const buf = m._malloc(Math.max(1, count) * 8);
|
|
194
|
+
try {
|
|
195
|
+
checkStatus(m.ccall("n4m_role_pipeline_classes", "number", ["number", "number", "i64", "number"], [handle, buf, BigInt(count), countPtr]));
|
|
196
|
+
return Array.from({ length: count }, (_, i) => this.label(readI64(buf + 8 * i)));
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
m._free(buf);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
m._free(countPtr);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Per step: method, role played, state index, fitted widths, training rows. */
|
|
207
|
+
stepsInfo() {
|
|
208
|
+
const handle = this.handle();
|
|
209
|
+
return this.steps.map((_, i) => RolePipeline.stepInfo(handle, i));
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* One N4ME state per stateful step. A state that embeds training rows is
|
|
213
|
+
* refused unless allowTrainingRows is set.
|
|
214
|
+
*/
|
|
215
|
+
exportStates(options = {}) {
|
|
216
|
+
const m = getModule();
|
|
217
|
+
const handle = this.handle();
|
|
218
|
+
const flags = options.allowTrainingRows ? EXPORT_ALLOW_TRAINING_ROWS : 0;
|
|
219
|
+
return withContext((ctx) => this.stepsInfo().filter((s) => s.stateIndex >= 0).map((step) => {
|
|
220
|
+
const sizePtr = m._malloc(4);
|
|
221
|
+
try {
|
|
222
|
+
checkStatus(m.ccall("n4m_role_pipeline_export_state_size", "number", ["number", "number", "number", "number", "number"], [ctx, handle, step.stateIndex, flags, sizePtr]), ctx);
|
|
223
|
+
const size = m.getValue(sizePtr, "i32");
|
|
224
|
+
const buf = m._malloc(Math.max(1, size));
|
|
225
|
+
try {
|
|
226
|
+
checkStatus(m.ccall("n4m_role_pipeline_export_state_to_buffer", "number", ["number", "number", "number", "number", "number", "number", "number"], [ctx, handle, step.stateIndex, flags, buf, size, sizePtr]), ctx);
|
|
227
|
+
return {
|
|
228
|
+
methodId: step.methodId,
|
|
229
|
+
n4me: m.HEAPU8.slice(buf, buf + m.getValue(sizePtr, "i32")),
|
|
230
|
+
containsTrainingRows: step.containsTrainingRows,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
m._free(buf);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
m._free(sizePtr);
|
|
239
|
+
}
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
/** Releases the native pipeline. */
|
|
243
|
+
dispose() {
|
|
244
|
+
if (this.ptr !== 0) {
|
|
245
|
+
getModule().ccall("n4m_role_pipeline_destroy", null, ["number"], [this.ptr]);
|
|
246
|
+
this.ptr = 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
create() {
|
|
250
|
+
const m = getModule();
|
|
251
|
+
const parsed = this.steps.map(parseStep);
|
|
252
|
+
const allocs = [];
|
|
253
|
+
const params = [];
|
|
254
|
+
return withContext((ctx) => {
|
|
255
|
+
try {
|
|
256
|
+
for (const step of parsed) {
|
|
257
|
+
const method = new (methodClass(step.methodId))();
|
|
258
|
+
method.params = step.params;
|
|
259
|
+
params.push(nativeParams(ctx, method));
|
|
260
|
+
}
|
|
261
|
+
const ids = cStrings(parsed.map((s) => s.methodId), allocs);
|
|
262
|
+
const paramsPtr = m._malloc(Math.max(1, params.length) * 4);
|
|
263
|
+
allocs.push({ ptr: paramsPtr, free: () => m._free(paramsPtr) });
|
|
264
|
+
params.forEach((p, i) => m.setValue(paramsPtr + 4 * i, p, "i32"));
|
|
265
|
+
const out = m._malloc(4);
|
|
266
|
+
allocs.push({ ptr: out, free: () => m._free(out) });
|
|
267
|
+
m.setValue(out, 0, "i32");
|
|
268
|
+
checkStatus(m.ccall("n4m_role_pipeline_create", "number", ["number", "number", "number", "number", "number"], [ctx, parsed.length, ids, paramsPtr, out]), ctx);
|
|
269
|
+
return m.getValue(out, "i32");
|
|
270
|
+
}
|
|
271
|
+
finally {
|
|
272
|
+
params.forEach((p) => m.ccall("n4m_params_destroy", null, ["number"], [p]));
|
|
273
|
+
allocs.forEach((a) => a.free());
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
publish(handle, featureNames, classNames) {
|
|
278
|
+
this.dispose();
|
|
279
|
+
this.ptr = handle;
|
|
280
|
+
this.names = featureNames === undefined ? undefined : [...featureNames];
|
|
281
|
+
this.classNames = classNames === undefined ? undefined : [...classNames];
|
|
282
|
+
}
|
|
283
|
+
handle() {
|
|
284
|
+
if (this.ptr === 0)
|
|
285
|
+
throw new Error("RolePipeline is not fitted");
|
|
286
|
+
return this.ptr;
|
|
287
|
+
}
|
|
288
|
+
label(id) {
|
|
289
|
+
return this.classNames === undefined ? id : this.classNames[id];
|
|
290
|
+
}
|
|
291
|
+
static setFeatureNames(ctx, handle, names) {
|
|
292
|
+
if (names === undefined)
|
|
293
|
+
return;
|
|
294
|
+
const allocs = [];
|
|
295
|
+
try {
|
|
296
|
+
checkStatus(getModule().ccall("n4m_role_pipeline_set_feature_names", "number", ["number", "number", "number", "i64"], [ctx, handle, cStrings(names, allocs), BigInt(names.length)]), ctx);
|
|
297
|
+
}
|
|
298
|
+
finally {
|
|
299
|
+
allocs.forEach((a) => a.free());
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** Native width and feature-name check of X (names optional: positional). */
|
|
303
|
+
checkFeatures(ctx, X, names, allocs) {
|
|
304
|
+
if (names !== undefined && names.length !== X.cols) {
|
|
305
|
+
throw new RangeError(`${names.length} feature names for ${X.cols} columns`);
|
|
306
|
+
}
|
|
307
|
+
const namesPtr = names === undefined ? 0 : cStrings(names, allocs);
|
|
308
|
+
checkStatus(getModule().ccall("n4m_role_pipeline_check_features", "number", ["number", "number", "i64", "number"], [ctx, this.handle(), BigInt(X.cols), namesPtr]), ctx);
|
|
309
|
+
}
|
|
310
|
+
static stepInfo(handle, step) {
|
|
311
|
+
const m = getModule();
|
|
312
|
+
const info = m._malloc(STEP_INFO_SIZE);
|
|
313
|
+
try {
|
|
314
|
+
m.HEAPU8.fill(0, info, info + STEP_INFO_SIZE);
|
|
315
|
+
m.setValue(info, STEP_INFO_SIZE, "i32");
|
|
316
|
+
checkStatus(m.ccall("n4m_role_pipeline_step_info_v1", "number", ["number", "number", "number"], [handle, step, info]));
|
|
317
|
+
return {
|
|
318
|
+
methodId: m.UTF8ToString(m.getValue(info + 8, "i32")),
|
|
319
|
+
role: ROLES[m.getValue(info + 12, "i32")],
|
|
320
|
+
stateIndex: m.getValue(info + 16, "i32"),
|
|
321
|
+
containsTrainingRows: m.getValue(info + 20, "i32") !== 0,
|
|
322
|
+
nFeaturesIn: readI64(info + 24),
|
|
323
|
+
nFeaturesOut: readI64(info + 32),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
m._free(info);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
matrixOp(symbol, widthSymbol, X, featureNames) {
|
|
331
|
+
const m = getModule();
|
|
332
|
+
const handle = this.handle();
|
|
333
|
+
const widthPtr = m._malloc(8);
|
|
334
|
+
const allocs = [];
|
|
335
|
+
try {
|
|
336
|
+
checkStatus(m.ccall(widthSymbol, "number", ["number", "number"], [handle, widthPtr]));
|
|
337
|
+
const cols = readI64(widthPtr);
|
|
338
|
+
const xv = makeMatrixView(X.data, X.rows, X.cols);
|
|
339
|
+
const ov = makeMatrixView(new Float64Array(X.rows * cols), X.rows, cols);
|
|
340
|
+
try {
|
|
341
|
+
withContext((ctx) => {
|
|
342
|
+
this.checkFeatures(ctx, X, featureNames, allocs);
|
|
343
|
+
checkStatus(m.ccall(symbol, "number", ["number", "number", "number", "number"], [ctx, handle, xv.viewPtr, ov.viewPtr]), ctx);
|
|
344
|
+
});
|
|
345
|
+
return { data: m.HEAPF64.slice(ov.dataPtr / 8, ov.dataPtr / 8 + X.rows * cols), rows: X.rows, cols };
|
|
346
|
+
}
|
|
347
|
+
finally {
|
|
348
|
+
xv.free();
|
|
349
|
+
ov.free();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
finally {
|
|
353
|
+
m._free(widthPtr);
|
|
354
|
+
allocs.forEach((a) => a.free());
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nirs4all/methods",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.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"
|
|
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"
|
|
24
24
|
},
|
|
25
25
|
"license": "CECILL-2.1",
|
|
26
26
|
"publishConfig": {
|