@nirs4all/preflight 0.0.0-stage → 0.1.0-alpha.1

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/index.js ADDED
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: CECILL-2.1 OR AGPL-3.0-or-later
2
+ // Host transport only: Rust owns protocols; Methods owns numeric kernels.
3
+ import init, * as rust from './native/nirs4all_preflight_wasm.js';
4
+ import * as methods from '@nirs4all/methods';
5
+
6
+ function matrix(rows) {
7
+ const n = rows.length;
8
+ const p = rows[0]?.length ?? 0;
9
+ if (!n || !p || n * p > 10_000_000 || rows.some(row => row.length !== p)) {
10
+ throw new RangeError('PROVIDER_MATRIX_BUDGET_OR_SHAPE');
11
+ }
12
+ return { data: Float64Array.from(rows.flat()), rows: n, cols: p };
13
+ }
14
+ function nested(values, width) {
15
+ const rows = [];
16
+ for (let i = 0; i < values.length; i += width) rows.push(Array.from(values.slice(i, i + width)));
17
+ return rows;
18
+ }
19
+ const nullable = value => Number.isFinite(value) ? value : null;
20
+
21
+ /** Create in a worker; the host cancels by terminating that worker. */
22
+ export async function createQualificationRuntime(options = {}) {
23
+ await init(options.wasmModule ? { module_or_path: options.wasmModule } : undefined);
24
+ await methods.loadModule();
25
+ const abi = methods.abiVersion();
26
+ if (abi[0] !== 2 || abi[1] < 18) throw new Error('METHODS_ABI_2_18_REQUIRED');
27
+ const backend_version = methods.version();
28
+ const methodsCapabilities = ['pca', 'repeatability', 'summary', 'nearest_neighbors', 'local_contrast'].map(id => ({
29
+ capability_id: `methods.${id}`, available: true, qualified: false,
30
+ reason: `${backend_version}; alpha protocol`,
31
+ }));
32
+ const callback = (operation, payload) => {
33
+ const request = JSON.parse(payload);
34
+ let result;
35
+ switch (operation) {
36
+ case 'capabilities': result = methodsCapabilities; break;
37
+ case 'pca': {
38
+ const output = methods.preflightPca(matrix(request.matrix), request.n_components);
39
+ result = { eigenvalues: Array.from(output.eigenvalues), scores: nested(output.scores.data, output.scores.cols), full_moments: output.fullMoments, backend_version };
40
+ break;
41
+ }
42
+ case 'repeatability': {
43
+ const output = methods.preflightRepeatability(matrix(request.matrix), request.group_ids, Uint8Array.from(request.valid_mask.flat(), Boolean));
44
+ result = { pooled_sd: Array.from(output.pooledSd, nullable), icc: Array.from(output.icc, nullable), n_complete_groups: output.nCompleteGroups, band_status: Array.from(output.bandStatus), backend_version };
45
+ break;
46
+ }
47
+ case 'summary': {
48
+ if (!request.values.length || request.values.length > 10_000_000) throw new RangeError('SUMMARY_WORK_BUDGET');
49
+ const output = methods.preflightSummary(Float64Array.from(request.values));
50
+ result = { count: output[0], min: output[1], max: output[2], mean: output[3], sample_sd: nullable(output[4]), q25: output[5], median: output[6], q75: output[7], backend_version };
51
+ break;
52
+ }
53
+ case 'nearest_neighbors': {
54
+ const query = matrix(request.query), reference = matrix(request.reference);
55
+ if (query.rows * reference.rows * query.cols > 250_000_000 || request.k > 100) throw new RangeError('KNN_WORK_BUDGET');
56
+ const output = methods.preflightNearestNeighbors(query, reference, request.k, request.query_group_ids, request.reference_group_ids);
57
+ result = { indices: nested(output.indices, output.k), distances: nested(Array.from(output.distances, nullable), output.k), backend_version };
58
+ break;
59
+ }
60
+ case 'local_contrast': {
61
+ const output = methods.preflightLocalContrast(Float64Array.from(request.targets), request.neighbor_indices.flat(), request.neighbor_indices[0]?.length ?? 0, request.categorical, request.group_ids);
62
+ result = { contrast: nullable(output.contrast), fraction_rows_with_neighbors: output.fractionRowsWithNeighbors, observed_same_class_fraction: nullable(output.observedSameClassFraction), expected_same_class_fraction: nullable(output.expectedSameClassFraction), backend_version };
63
+ break;
64
+ }
65
+ default: throw new Error(`METHODS_OPERATION_UNSUPPORTED: ${operation}`);
66
+ }
67
+ return JSON.stringify(result);
68
+ };
69
+ return {
70
+ analyze: input => JSON.parse(rust.analyze_json(JSON.stringify(input), callback)),
71
+ validate: input => JSON.parse(rust.validate_json(JSON.stringify(input))),
72
+ reportHtml: report => rust.report_html_json(JSON.stringify(report)),
73
+ capabilities: () => JSON.parse(rust.capabilities_json(callback)),
74
+ };
75
+ }
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@nirs4all/preflight",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "Rust NIRS dataset qualification with reusable Methods diagnostics",
5
+ "type": "module",
6
+ "license": "(CECILL-2.1 OR AGPL-3.0-or-later)",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/GBeurier/nirs4all-preflight.git",
10
+ "directory": "bindings/wasm"
11
+ },
12
+ "homepage": "https://gbeurier.github.io/nirs4all-preflight/",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./index.d.ts",
16
+ "default": "./index.js"
17
+ },
18
+ "./native/*": "./native/*"
19
+ },
20
+ "files": [
21
+ "index.js",
22
+ "index.d.ts",
23
+ "native",
24
+ "README.md",
25
+ "LICENSE",
26
+ "LICENSES",
27
+ "THIRD_PARTY_NOTICES.md"
28
+ ],
29
+ "dependencies": {
30
+ "@nirs4all/methods": "1.3.5"
31
+ },
32
+ "scripts": {
33
+ "build:native": "wasm-pack build . --target web --out-dir native --release",
34
+ "test": "node --test tests/*.test.mjs"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }