@nirs4all/methods 1.3.14 → 1.3.16
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 +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 +7 -0
- package/dist/componentAnalysis.d.ts +61 -0
- package/dist/componentAnalysis.js +175 -0
- package/dist/estimatorRoles.js +17 -0
- package/dist/estimatorRolesGenerated.d.ts +58 -1
- package/dist/estimatorRolesGenerated.js +53 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/n4m.js +1 -1
- package/dist/n4m.wasm +0 -0
- package/dist/preprocessing.js +5 -1
- package/dist/ranking.d.ts +61 -0
- package/dist/ranking.js +110 -0
- package/package.json +4 -3
package/dist/n4m.wasm
CHANGED
|
Binary file
|
package/dist/preprocessing.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// 0 mirror, 1 constant, 2 nearest, 3 wrap, 4 interp
|
|
19
19
|
// Derivative : [order] (1 → 1st, ≥2 → 2nd derivative)
|
|
20
20
|
// Detrend : [polyorder]
|
|
21
|
-
//
|
|
21
|
+
// Row L2 uses the generic RowL2Normalize transformer; ppCreate has no Normalize token.
|
|
22
22
|
// GaussianFilter : [sigma]
|
|
23
23
|
import { getModule, checkStatus } from "./ffi.js";
|
|
24
24
|
function pushF64(m, data) {
|
|
@@ -38,6 +38,10 @@ function checkDims(X, n, p) {
|
|
|
38
38
|
}
|
|
39
39
|
/** Create a preprocessing operator by catalog type token + numeric params. */
|
|
40
40
|
export function ppCreate(op, params = []) {
|
|
41
|
+
if (op === "StandardNormalVariate" && !(params === undefined ||
|
|
42
|
+
(Array.isArray(params) && params.length === 0))) {
|
|
43
|
+
throw new TypeError("SNV accepts only an empty parameter Array; use SNV/RolePipeline for options");
|
|
44
|
+
}
|
|
41
45
|
const m = getModule();
|
|
42
46
|
let pPtr = 0;
|
|
43
47
|
let ptr = 0;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Matrix } from "./types.js";
|
|
2
|
+
export type RankingMethod = "average_rank" | "elo" | "bradley_terry" | "trueskill" | "trueskill_1v1" | "openskill" | "weng_lin_bradley_terry";
|
|
3
|
+
export interface RankingOptions {
|
|
4
|
+
method?: RankingMethod;
|
|
5
|
+
higher_is_better?: boolean;
|
|
6
|
+
tie_tolerance?: number;
|
|
7
|
+
initial_mean?: number;
|
|
8
|
+
k_factor?: number;
|
|
9
|
+
elo_scale?: number;
|
|
10
|
+
prior_sigma?: number;
|
|
11
|
+
beta?: number;
|
|
12
|
+
dynamics?: number;
|
|
13
|
+
draw_margin?: number;
|
|
14
|
+
regularization?: number;
|
|
15
|
+
convergence_tolerance?: number;
|
|
16
|
+
max_iterations?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface RankingCandidate {
|
|
19
|
+
index: number;
|
|
20
|
+
score: number | null;
|
|
21
|
+
uncertainty: number | null;
|
|
22
|
+
average_rank: number | null;
|
|
23
|
+
observations: number;
|
|
24
|
+
matches: number;
|
|
25
|
+
component: number;
|
|
26
|
+
wins: number;
|
|
27
|
+
ties: number;
|
|
28
|
+
losses: number;
|
|
29
|
+
rank: number | null;
|
|
30
|
+
}
|
|
31
|
+
export interface RankingResult {
|
|
32
|
+
schema_version: "n4m.ranking/1";
|
|
33
|
+
method: RankingMethod;
|
|
34
|
+
variant: string;
|
|
35
|
+
options: Required<Omit<RankingOptions, "method">>;
|
|
36
|
+
score_direction: "lower_is_better" | "higher_is_better";
|
|
37
|
+
uncertainty_kind: "approximate_posterior_sd" | null;
|
|
38
|
+
candidates: RankingCandidate[];
|
|
39
|
+
coverage: {
|
|
40
|
+
blocks: number;
|
|
41
|
+
complete_blocks: number;
|
|
42
|
+
paired_comparisons: number;
|
|
43
|
+
connected_components: number;
|
|
44
|
+
};
|
|
45
|
+
inference: {
|
|
46
|
+
friedman_statistic: number | null;
|
|
47
|
+
degrees_of_freedom: number | null;
|
|
48
|
+
friedman_p_value: number | null;
|
|
49
|
+
p_value_method: "chi_square_approximation";
|
|
50
|
+
reason: string;
|
|
51
|
+
};
|
|
52
|
+
warnings: string[];
|
|
53
|
+
convergence: {
|
|
54
|
+
converged: boolean;
|
|
55
|
+
iterations: number;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Rank candidates in columns across comparable blocks in rows. The required
|
|
59
|
+
* row-major presence mask contains 0 or 1; masked scores may be nonfinite.
|
|
60
|
+
* Bayesian uncertainty is an approximate posterior SD, not a standard error. */
|
|
61
|
+
export declare function rank(scores: Matrix, present: Uint8Array, options?: RankingOptions): RankingResult;
|
package/dist/ranking.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
|
|
2
|
+
import { checkStatus, getModule, makeMatrixView } from "./ffi.js";
|
|
3
|
+
const METHODS = {
|
|
4
|
+
average_rank: { code: 0, variant: "average_rank" }, elo: { code: 1, variant: "elo" },
|
|
5
|
+
bradley_terry: { code: 2, variant: "bradley_terry" },
|
|
6
|
+
trueskill: { code: 3, variant: "trueskill_1v1" },
|
|
7
|
+
trueskill_1v1: { code: 3, variant: "trueskill_1v1" },
|
|
8
|
+
openskill: { code: 4, variant: "weng_lin_bradley_terry" },
|
|
9
|
+
weng_lin_bradley_terry: { code: 4, variant: "weng_lin_bradley_terry" },
|
|
10
|
+
};
|
|
11
|
+
const DOUBLE_KEYS = ["tie_tolerance", "initial_mean", "k_factor", "elo_scale", "prior_sigma",
|
|
12
|
+
"beta", "dynamics", "draw_margin", "regularization", "convergence_tolerance"];
|
|
13
|
+
const nullable = (value) => Number.isFinite(value) ? value : null;
|
|
14
|
+
/** Rank candidates in columns across comparable blocks in rows. The required
|
|
15
|
+
* row-major presence mask contains 0 or 1; masked scores may be nonfinite.
|
|
16
|
+
* Bayesian uncertainty is an approximate posterior SD, not a standard error. */
|
|
17
|
+
export function rank(scores, present, options = {}) {
|
|
18
|
+
const allowed = new Set(["method", "higher_is_better", "max_iterations", ...DOUBLE_KEYS]);
|
|
19
|
+
for (const key of Object.keys(options)) {
|
|
20
|
+
if (!allowed.has(key))
|
|
21
|
+
throw new Error(`unknown ranking option ${key}`);
|
|
22
|
+
}
|
|
23
|
+
const method = options.method ?? "average_rank";
|
|
24
|
+
if (!Object.prototype.hasOwnProperty.call(METHODS, method))
|
|
25
|
+
throw new Error(`unknown ranking method ${method}`);
|
|
26
|
+
const size = scores.rows * scores.cols;
|
|
27
|
+
if (!Number.isSafeInteger(scores.rows) || !Number.isSafeInteger(scores.cols) ||
|
|
28
|
+
scores.rows < 1 || scores.cols < 1 || !Number.isSafeInteger(size) ||
|
|
29
|
+
size > 0x7fffffff || scores.cols > Math.floor(0x7fffffff / 80) ||
|
|
30
|
+
scores.data.length !== size || !(present instanceof Uint8Array) || present.length !== size ||
|
|
31
|
+
!present.every(value => value === 0 || value === 1)) {
|
|
32
|
+
throw new Error("scores and 0/1 presence mask must have matching nonempty dimensions");
|
|
33
|
+
}
|
|
34
|
+
if (options.higher_is_better !== undefined && typeof options.higher_is_better !== "boolean") {
|
|
35
|
+
throw new Error("higher_is_better must be boolean");
|
|
36
|
+
}
|
|
37
|
+
if (options.max_iterations !== undefined && !Number.isSafeInteger(options.max_iterations)) {
|
|
38
|
+
throw new Error("max_iterations must be a safe integer");
|
|
39
|
+
}
|
|
40
|
+
for (const key of DOUBLE_KEYS) {
|
|
41
|
+
if (options[key] !== undefined && !Number.isFinite(options[key]))
|
|
42
|
+
throw new Error(`${key} must be finite`);
|
|
43
|
+
}
|
|
44
|
+
const m = getModule();
|
|
45
|
+
const view = makeMatrixView(scores.data, scores.rows, scores.cols);
|
|
46
|
+
const allocations = [];
|
|
47
|
+
const alloc = (bytes) => {
|
|
48
|
+
const ptr = m._malloc(bytes);
|
|
49
|
+
if (!ptr)
|
|
50
|
+
throw new Error("ranking allocation failed");
|
|
51
|
+
allocations.push(ptr);
|
|
52
|
+
return ptr;
|
|
53
|
+
};
|
|
54
|
+
try {
|
|
55
|
+
const opt = alloc(104), mask = alloc(size), output = alloc(scores.cols * 10 * 8), summary = alloc(72);
|
|
56
|
+
checkStatus(m.ccall("n4m_ranking_options_init_v1", "number", ["number"], [opt]));
|
|
57
|
+
m.HEAPU8.set(present, mask);
|
|
58
|
+
const data = new DataView(m.HEAPU8.buffer);
|
|
59
|
+
data.setInt32(opt + 8, METHODS[method].code, true);
|
|
60
|
+
if (options.higher_is_better !== undefined)
|
|
61
|
+
data.setInt32(opt + 12, options.higher_is_better ? 1 : 0, true);
|
|
62
|
+
DOUBLE_KEYS.forEach((key, i) => {
|
|
63
|
+
const value = options[key];
|
|
64
|
+
if (value !== undefined)
|
|
65
|
+
data.setFloat64(opt + 16 + 8 * i, value, true);
|
|
66
|
+
});
|
|
67
|
+
if (options.max_iterations !== undefined)
|
|
68
|
+
data.setBigInt64(opt + 96, BigInt(options.max_iterations), true);
|
|
69
|
+
const resolved = { higher_is_better: data.getInt32(opt + 12, true) !== 0,
|
|
70
|
+
...Object.fromEntries(DOUBLE_KEYS.map((key, i) => [key, data.getFloat64(opt + 16 + 8 * i, true)])),
|
|
71
|
+
max_iterations: Number(data.getBigInt64(opt + 96, true)) };
|
|
72
|
+
checkStatus(m.ccall("n4m_ranking_compute_v1", "number", ["number", "number", "number", "number", "i64", "number", "i64"], [view.viewPtr, mask, opt, output, BigInt(scores.cols * 10), summary, 9n]));
|
|
73
|
+
const values = m.HEAPF64;
|
|
74
|
+
const candidates = Array.from({ length: scores.cols }, (_, i) => {
|
|
75
|
+
const p = output / 8 + i * 10;
|
|
76
|
+
return { index: i, score: nullable(values[p]), uncertainty: nullable(values[p + 1]),
|
|
77
|
+
average_rank: nullable(values[p + 2]), observations: values[p + 3], matches: values[p + 4],
|
|
78
|
+
component: values[p + 5], wins: values[p + 6], ties: values[p + 7], losses: values[p + 8],
|
|
79
|
+
rank: nullable(values[p + 9]) };
|
|
80
|
+
});
|
|
81
|
+
const s = summary / 8;
|
|
82
|
+
const warnings = [
|
|
83
|
+
"Mean ranks use all observed blocks; Friedman uses complete blocks only.",
|
|
84
|
+
"Friedman p-value uses a chi-square approximation and assumes independent blocks.",
|
|
85
|
+
];
|
|
86
|
+
if (METHODS[method].code >= 3)
|
|
87
|
+
warnings.push("Pairwise replay within each block is a composite approximation; posterior SD does not account for dependence between comparisons or repeated folds.");
|
|
88
|
+
if (METHODS[method].code === 0 && values[s + 1] < values[s])
|
|
89
|
+
warnings.push("Incomplete design: mean ranks are descriptive and average_rank global ranks are unavailable; use bradley_terry or trueskill for incomplete comparisons.");
|
|
90
|
+
if (values[s + 3] > 1)
|
|
91
|
+
warnings.push("Disconnected comparison graph: global ranks are unavailable.");
|
|
92
|
+
if (values[s + 6] === 0)
|
|
93
|
+
warnings.push("Bradley-Terry did not converge; global ranks are unavailable.");
|
|
94
|
+
return { schema_version: "n4m.ranking/1", method, variant: METHODS[method].variant, options: resolved,
|
|
95
|
+
score_direction: method === "average_rank" ? "lower_is_better" : "higher_is_better",
|
|
96
|
+
uncertainty_kind: METHODS[method].code >= 3 ? "approximate_posterior_sd" : null,
|
|
97
|
+
candidates, coverage: { blocks: values[s], complete_blocks: values[s + 1],
|
|
98
|
+
paired_comparisons: values[s + 2], connected_components: values[s + 3] },
|
|
99
|
+
inference: { friedman_statistic: nullable(values[s + 4]), degrees_of_freedom: nullable(values[s + 5]),
|
|
100
|
+
friedman_p_value: nullable(values[s + 8]),
|
|
101
|
+
p_value_method: "chi_square_approximation",
|
|
102
|
+
reason: "chi_square_approximation_on_complete_blocks; host_must_assess_independence" },
|
|
103
|
+
warnings,
|
|
104
|
+
convergence: { converged: values[s + 6] !== 0, iterations: values[s + 7] } };
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
allocations.forEach(ptr => m._free(ptr));
|
|
108
|
+
view.free();
|
|
109
|
+
}
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nirs4all/methods",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.16",
|
|
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",
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
21
21
|
"build": "npm run clean && tsc -p .",
|
|
22
|
-
"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)}fs.copyFileSync('../../THIRD_PARTY_LICENSES.md','dist/THIRD_PARTY_LICENSES.md');console.log('staged n4m.js + n4m.wasm + THIRD_PARTY_LICENSES.md into dist/')\"",
|
|
22
|
+
"stage:wasm": "node -e \"const fs=require('fs'),p='../../build/'+(process.env.N4M_WASM_PRESET||'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)}fs.copyFileSync('../../THIRD_PARTY_LICENSES.md','dist/THIRD_PARTY_LICENSES.md');fs.cpSync('../../LICENSES','dist/LICENSES',{recursive:true});console.log('staged n4m.js + n4m.wasm + THIRD_PARTY_LICENSES.md into dist/')\"",
|
|
23
23
|
"prepack": "npm run build && npm run stage:wasm",
|
|
24
|
-
"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 && node --experimental-vm-modules test/run_preflight_diagnostics.mjs && node --experimental-vm-modules test/run_pls_scale_equivariance.mjs && node --experimental-vm-modules test/run_pls_small_covariance.mjs && node --experimental-vm-modules test/run_pls_null_consumers.mjs && node --experimental-vm-modules test/run_pls_scale_consumers.mjs && node test/run_intrinsic_diagnostics.mjs"
|
|
24
|
+
"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 && node --experimental-vm-modules test/run_preflight_diagnostics.mjs && node --experimental-vm-modules test/run_pls_scale_equivariance.mjs && node --experimental-vm-modules test/run_pls_small_covariance.mjs && node --experimental-vm-modules test/run_pls_null_consumers.mjs && node --experimental-vm-modules test/run_pls_scale_consumers.mjs && node test/run_intrinsic_diagnostics.mjs && npm run test:shared-scoring && node test/run_snv_parameter_contract.mjs && node test/run_raw_i32_status.mjs && node --experimental-vm-modules test/ranking_mock.mjs && node test/run_ranking.mjs && node test/run_component_analysis.mjs",
|
|
25
|
+
"test:shared-scoring": "node test/run_shared_scoring.mjs"
|
|
25
26
|
},
|
|
26
27
|
"license": "CECILL-2.1 OR AGPL-3.0-or-later",
|
|
27
28
|
"publishConfig": {
|