@nirs4all/methods 1.2.0 → 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/rolePipeline.d.ts +9 -4
- package/dist/rolePipeline.js +96 -9
- package/dist/spectralEncoding.d.ts +27 -0
- package/dist/spectralEncoding.js +92 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ The package ships:
|
|
|
13
13
|
- `n4m.js` — Emscripten MODULARIZE/EXPORT_ES6 loader.
|
|
14
14
|
- `dist/` — TypeScript wrappers (`Context`, `Config`, `Model`, `MethodResult`)
|
|
15
15
|
emitted from `src/`.
|
|
16
|
+
- `Optimizer` — an owning typed facade over the native ask/tell HPO engine.
|
|
16
17
|
|
|
17
18
|
## Build
|
|
18
19
|
|
|
@@ -43,14 +44,21 @@ DataTwinning, SystematicCircular). The PLS smoke compares coefficients +
|
|
|
43
44
|
predictions to a frozen native fixture (`test/parity_fixture.json`) at a 1e-9
|
|
44
45
|
isolated band (achieved ~1e-16).
|
|
45
46
|
|
|
47
|
+
The additive raw `MultimodalClassifierPipeline` requires ABI 2.17 and the
|
|
48
|
+
native PLS-logistic head. `fit(blocks, labels)` keeps string or exact integer
|
|
49
|
+
original labels; `predictProba(blocks)` returns columns in `classes()` order.
|
|
50
|
+
`exportState()` writes N4MC bytes without training rows.
|
|
51
|
+
`MultimodalClassifierPipeline.fromState(state, recipe, schemas, { classNames })`
|
|
52
|
+
restores the original typed label table; omitting it exposes native class IDs.
|
|
53
|
+
|
|
46
54
|
## API surface
|
|
47
55
|
|
|
48
56
|
```typescript
|
|
49
57
|
import * as n4m from "@nirs4all/methods";
|
|
50
58
|
|
|
51
59
|
await n4m.loadModule();
|
|
52
|
-
console.log(n4m.version()); // "1.
|
|
53
|
-
console.log(n4m.abiVersion()); // [2,
|
|
60
|
+
console.log(n4m.version()); // "1.2.1+abi.2.17.0"
|
|
61
|
+
console.log(n4m.abiVersion()); // [2, 17, 0]
|
|
54
62
|
|
|
55
63
|
const rows = 40, cols = 6;
|
|
56
64
|
const X = new Float64Array(rows * cols); // row-major
|
|
@@ -103,6 +111,52 @@ OSC/EPO additionally require a training `Y` matrix at `fit`. The optional
|
|
|
103
111
|
positional parameters; applications should also bind the blob to their
|
|
104
112
|
feature schema. Native N4MP v1 supports operator kinds 0–14 only (15 kinds).
|
|
105
113
|
|
|
114
|
+
## Native optimizer
|
|
115
|
+
|
|
116
|
+
`Optimizer` uses the same C ABI and N4MOPT checkpoint as Python and R. It owns
|
|
117
|
+
its context; call `dispose()` after the study. Integer axes, trial ids and
|
|
118
|
+
integer categorical choices use `bigint`, so the browser does not silently
|
|
119
|
+
round the native int64 values. The host supplies the objective and score; no
|
|
120
|
+
search or pruning algorithm is implemented in TypeScript.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const space = {
|
|
124
|
+
components: { kind: "int", low: 1, high: 8 },
|
|
125
|
+
alpha: { kind: "log_float", low: 1e-4, high: 1e-1 },
|
|
126
|
+
branch: { kind: "categorical", type: "string", choices: ["nir", "fusion"] },
|
|
127
|
+
} as const;
|
|
128
|
+
const study = n4m.Optimizer.create(space, { sampler: "tpe", seed: 42n });
|
|
129
|
+
try {
|
|
130
|
+
for (let i = 0; i < 30; i++) {
|
|
131
|
+
const trial = study.ask();
|
|
132
|
+
const score = evaluate(trial.parameters); // application-owned validation
|
|
133
|
+
study.tell(trial, "completed", score);
|
|
134
|
+
}
|
|
135
|
+
console.log(study.best(), study.trialRecords());
|
|
136
|
+
const checkpoint = study.save(); // portable N4MOPT bytes
|
|
137
|
+
const resumed = n4m.Optimizer.load(checkpoint, space);
|
|
138
|
+
resumed.dispose();
|
|
139
|
+
} finally {
|
|
140
|
+
study.dispose();
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`askBatch`, `enqueue`, `intermediate`, terminal outcomes and conditional
|
|
145
|
+
constraints also call native ABI functions. `trials()` returns an owning
|
|
146
|
+
`MethodResult` rich-trace snapshot that the caller destroys; `trialRecords()`
|
|
147
|
+
decodes and releases that snapshot. The search-space declaration passed to
|
|
148
|
+
`load()` is for typed JS decoding; the native checkpoint owns the optimizer
|
|
149
|
+
state. Keep that declaration identical to the original one. The binding does
|
|
150
|
+
not fit a model or tune a DAG by itself.
|
|
151
|
+
|
|
152
|
+
The 14 committed HPO golden traces run through JS/WASM with a fixed score tape
|
|
153
|
+
and exact proposed parameters, statuses, pruning decisions, errors and event
|
|
154
|
+
order. The JS closed-form objective is checked against that tape within a
|
|
155
|
+
relative `1e-12` band: Python `**2` can differ from JS multiplication by one
|
|
156
|
+
binary64 ULP. A separate gate resumes checkpoints Python → JS/WASM and
|
|
157
|
+
JS/WASM → Python exactly. The [cross-binding CI](https://github.com/GBeurier/nirs4all-methods/actions/runs/36548060107)
|
|
158
|
+
also reproduces all 45 sampler-pruner compositions from temporary native traces.
|
|
159
|
+
|
|
106
160
|
## Build options
|
|
107
161
|
|
|
108
162
|
The CMake `emscripten` preset sets:
|
|
@@ -158,6 +212,7 @@ bindings/js/
|
|
|
158
212
|
│ ├── config.ts # Config wrapper
|
|
159
213
|
│ ├── model.ts # Model fit / predict (raw-pointer path)
|
|
160
214
|
│ ├── methodResult.ts # Universal n4m_method_result_t wrapper
|
|
215
|
+
│ ├── optimization.ts # Owning native HPO facade
|
|
161
216
|
│ └── index.ts # Public barrel
|
|
162
217
|
├── examples/
|
|
163
218
|
│ └── consume.mjs # downstream-consumption example
|
|
@@ -3228,6 +3228,23 @@ export declare class SimpleScale extends NativeEstimator implements Transformer
|
|
|
3228
3228
|
constructor(params?: SimpleScaleParams);
|
|
3229
3229
|
transform(X: Matrix): Matrix;
|
|
3230
3230
|
}
|
|
3231
|
+
/** Parameters of StandardScale; unset values take the native defaults. */
|
|
3232
|
+
export interface StandardScaleParams {
|
|
3233
|
+
/** Default true. */
|
|
3234
|
+
with_mean?: boolean;
|
|
3235
|
+
/** Default true. */
|
|
3236
|
+
with_std?: boolean;
|
|
3237
|
+
}
|
|
3238
|
+
/** Native `preprocessing.scaling.standard_scale` (transformer). */
|
|
3239
|
+
export declare class StandardScale extends NativeEstimator implements Transformer {
|
|
3240
|
+
readonly methodId = "preprocessing.scaling.standard_scale";
|
|
3241
|
+
readonly paramTypes: {
|
|
3242
|
+
readonly with_mean: "bool";
|
|
3243
|
+
readonly with_std: "bool";
|
|
3244
|
+
};
|
|
3245
|
+
constructor(params?: StandardScaleParams);
|
|
3246
|
+
transform(X: Matrix): Matrix;
|
|
3247
|
+
}
|
|
3231
3248
|
/** Parameters of AreaNormalization; unset values take the native defaults. */
|
|
3232
3249
|
export interface AreaNormalizationParams {
|
|
3233
3250
|
/** Default "sum". */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: CECILL-2.1
|
|
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.16.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 {
|
|
@@ -2544,6 +2544,22 @@ export class SimpleScale extends NativeEstimator {
|
|
|
2544
2544
|
}
|
|
2545
2545
|
}
|
|
2546
2546
|
NativeMethod.register("preprocessing.scaling.simple_scale", SimpleScale);
|
|
2547
|
+
/** Native `preprocessing.scaling.standard_scale` (transformer). */
|
|
2548
|
+
export class StandardScale extends NativeEstimator {
|
|
2549
|
+
methodId = "preprocessing.scaling.standard_scale";
|
|
2550
|
+
paramTypes = {
|
|
2551
|
+
with_mean: "bool",
|
|
2552
|
+
with_std: "bool",
|
|
2553
|
+
};
|
|
2554
|
+
constructor(params = {}) {
|
|
2555
|
+
super();
|
|
2556
|
+
this.params = { ...params };
|
|
2557
|
+
}
|
|
2558
|
+
transform(X) {
|
|
2559
|
+
return this.transformMatrix(X);
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
NativeMethod.register("preprocessing.scaling.standard_scale", StandardScale);
|
|
2547
2563
|
/** Native `preprocessing.scatter.area_normalization` (transformer). */
|
|
2548
2564
|
export class AreaNormalization extends NativeEstimator {
|
|
2549
2565
|
methodId = "preprocessing.scatter.area_normalization";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { SpectralEncoder, type SpectralEncodingOptions } from "./spectralEncoding.js";
|
|
2
|
+
export { Optimizer, type OptimizerOptions, type OptimizerTrial, type BatchResult, type OptimizerTrialRecord, type SearchAxis, type SearchConstraint, type TrialValue } from "./optimization.js";
|
|
1
3
|
export { loadModule, getModule, makeMatrixView, readArrayView } from "./ffi.js";
|
|
2
4
|
export { Context } from "./context.js";
|
|
3
5
|
export { Config } from "./config.js";
|
|
@@ -8,6 +10,7 @@ export { NativeModel } from "./nativeModel.js";
|
|
|
8
10
|
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
11
|
export * from "./estimatorRolesGenerated.js";
|
|
10
12
|
export { RolePipeline, type ClassLabel, type PipelineRole, type RolePipelineState, type RolePipelineStepInfo, type RoleStep } from "./rolePipeline.js";
|
|
13
|
+
export { MultimodalPipeline, MultimodalClassifierPipeline, type MultimodalClassLabel, type MultimodalRecipe, type MultimodalSourceSchema, type MultimodalSourceSchemas, type MultimodalBlocks, type RawTensor } from "./multimodalPipeline.js";
|
|
11
14
|
export { splitNative, type NativeSplitterKind, type NativeSplitterOptions, type NativeSplitIndices } from "./nativeSplitter.js";
|
|
12
15
|
export { augmentNative, type NativeAugmentationKind } from "./nativeAugmentation.js";
|
|
13
16
|
export { NativePreprocessingPipeline, PipelineOperatorKind, type PipelineStep, } from "./nativePreprocessingPipeline.js";
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
// const model = n4m.fitPls({ data: X, rows, cols }, { data: y, rows, cols: 1 }, 3);
|
|
11
11
|
// const preds = n4m.predictPls(model, { data: X, rows, cols });
|
|
12
12
|
import { getModule } from "./ffi.js";
|
|
13
|
+
export { SpectralEncoder } from "./spectralEncoding.js";
|
|
14
|
+
export { Optimizer } from "./optimization.js";
|
|
13
15
|
export { loadModule, getModule, makeMatrixView, readArrayView } from "./ffi.js";
|
|
14
16
|
export { Context } from "./context.js";
|
|
15
17
|
export { Config } from "./config.js";
|
|
@@ -20,6 +22,7 @@ export { NativeModel } from "./nativeModel.js";
|
|
|
20
22
|
export { NativeEstimator, NativeMethod, NativeProcedure, manifest, methodClass } from "./estimatorRoles.js";
|
|
21
23
|
export * from "./estimatorRolesGenerated.js";
|
|
22
24
|
export { RolePipeline } from "./rolePipeline.js";
|
|
25
|
+
export { MultimodalPipeline, MultimodalClassifierPipeline } from "./multimodalPipeline.js";
|
|
23
26
|
export { splitNative } from "./nativeSplitter.js";
|
|
24
27
|
export { augmentNative } from "./nativeAugmentation.js";
|
|
25
28
|
export { NativePreprocessingPipeline, PipelineOperatorKind, } from "./nativePreprocessingPipeline.js";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Matrix } from "./types.js";
|
|
2
|
+
export interface MultimodalSourceSchema {
|
|
3
|
+
representation_id: string;
|
|
4
|
+
input_shape: number[];
|
|
5
|
+
dtype: string;
|
|
6
|
+
identity: string;
|
|
7
|
+
}
|
|
8
|
+
export type MultimodalSourceSchemas = Record<string, MultimodalSourceSchema>;
|
|
9
|
+
export interface RawTensor {
|
|
10
|
+
data: Float32Array | Float64Array;
|
|
11
|
+
shape: number[];
|
|
12
|
+
strides?: number[];
|
|
13
|
+
}
|
|
14
|
+
export type MultimodalBlocks = Record<string, RawTensor | Array<[number | string, string]>>;
|
|
15
|
+
export interface MultimodalRecipe {
|
|
16
|
+
schema_version: number;
|
|
17
|
+
fusion: string;
|
|
18
|
+
source_order: string[];
|
|
19
|
+
encoders: Record<string, Record<string, unknown>>;
|
|
20
|
+
source_weights: Record<string, number>;
|
|
21
|
+
model: {
|
|
22
|
+
method_id: string;
|
|
23
|
+
params: Record<string, unknown>;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Complete native early-fusion pipeline, portable as a bounded N4MF state. */
|
|
27
|
+
export declare class MultimodalPipeline {
|
|
28
|
+
private ptr;
|
|
29
|
+
private readonly sourceOrder;
|
|
30
|
+
readonly recipe: MultimodalRecipe;
|
|
31
|
+
readonly sourceSchemas: MultimodalSourceSchemas;
|
|
32
|
+
constructor(recipe: MultimodalRecipe, sourceSchemas: MultimodalSourceSchemas);
|
|
33
|
+
private handle;
|
|
34
|
+
fit(blocks: MultimodalBlocks, y: Matrix | Float64Array): this;
|
|
35
|
+
predict(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): Matrix;
|
|
36
|
+
transform(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): Matrix;
|
|
37
|
+
private operation;
|
|
38
|
+
exportState(): Uint8Array;
|
|
39
|
+
static fromState(state: Uint8Array, recipe: MultimodalRecipe, sourceSchemas: MultimodalSourceSchemas): MultimodalPipeline;
|
|
40
|
+
dispose(): void;
|
|
41
|
+
}
|
|
42
|
+
/** Original class labels; numeric labels require lossless JS/int64 transport. */
|
|
43
|
+
export type MultimodalClassLabel = string | number;
|
|
44
|
+
/** Native raw PLS-logistic classifier; N4MC states contain no training rows. */
|
|
45
|
+
export declare class MultimodalClassifierPipeline {
|
|
46
|
+
private ptr;
|
|
47
|
+
private classNames;
|
|
48
|
+
private readonly sourceOrder;
|
|
49
|
+
readonly recipe: MultimodalRecipe;
|
|
50
|
+
readonly sourceSchemas: MultimodalSourceSchemas;
|
|
51
|
+
constructor(recipe: MultimodalRecipe, sourceSchemas: MultimodalSourceSchemas);
|
|
52
|
+
private createHandle;
|
|
53
|
+
private handle;
|
|
54
|
+
private static classIds;
|
|
55
|
+
fit(blocks: MultimodalBlocks, y: readonly MultimodalClassLabel[]): this;
|
|
56
|
+
classes(): MultimodalClassLabel[];
|
|
57
|
+
labelNames(): MultimodalClassLabel[] | undefined;
|
|
58
|
+
predict(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): MultimodalClassLabel[];
|
|
59
|
+
predictProba(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): Matrix;
|
|
60
|
+
decisionFunction(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): Matrix;
|
|
61
|
+
transform(blocks: MultimodalBlocks, schemas?: MultimodalSourceSchemas): Matrix;
|
|
62
|
+
private matrixOperation;
|
|
63
|
+
exportState(): Uint8Array;
|
|
64
|
+
static fromState(state: Uint8Array, recipe: MultimodalRecipe, sourceSchemas: MultimodalSourceSchemas, options?: {
|
|
65
|
+
classNames?: MultimodalClassLabel[];
|
|
66
|
+
}): MultimodalClassifierPipeline;
|
|
67
|
+
dispose(): void;
|
|
68
|
+
}
|