@alleninstitute/warpsz 0.0.0-stage → 0.1.2

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.
@@ -0,0 +1,70 @@
1
+ /**
2
+ * warpsz codec for JavaScript/TypeScript, backed by the Rust implementation
3
+ * compiled to WebAssembly. Bit-identical to the Python and native Rust codecs.
4
+ *
5
+ * const wf = await WarpSZ.instantiate(wasmBytesOrResponse);
6
+ * const field = wf.decode(chunkBytes, [64, 64, 64, 3], "float32", config);
7
+ *
8
+ * `config` is the codec's `configuration` object from the zarr array metadata
9
+ * (or the whole `{name: "warpsz", configuration}` entry).
10
+ *
11
+ * The module has no imports and uses no JS glue beyond this file. One instance
12
+ * is single-threaded; use one instance per worker.
13
+ */
14
+ export type AxisRole = "spatial" | "smooth" | "independent" | "component";
15
+ export type DataType = "float32" | "float64";
16
+ export interface JacobianConfig {
17
+ grid_spacing: readonly number[];
18
+ min_det?: number;
19
+ tight_error_bound?: number | number[] | null;
20
+ }
21
+ export interface WarpSZConfig {
22
+ axis_roles: readonly AxisRole[];
23
+ error_bound?: number;
24
+ face_error_bound?: number | null;
25
+ predictor?: "linear" | "cubic" | "eno";
26
+ boundary?: "clamped" | "extrapolate";
27
+ jacobian?: JacobianConfig | null;
28
+ }
29
+ export type ConfigLike = WarpSZConfig | {
30
+ name: "warpsz" | "warpfield";
31
+ configuration: WarpSZConfig;
32
+ } | string;
33
+ interface Exports {
34
+ memory: WebAssembly.Memory;
35
+ wf_alloc(n: number): number;
36
+ wf_free(p: number, n: number): void;
37
+ wf_error_ptr(): number;
38
+ wf_error_len(): number;
39
+ wf_result_ptr(): number;
40
+ wf_result_len(): number;
41
+ wf_result_clear(): void;
42
+ wf_decode(cfgPtr: number, cfgLen: number, bufPtr: number, bufLen: number, shapePtr: number, rank: number, dtype: number, outPtr: number): number;
43
+ wf_encode(cfgPtr: number, cfgLen: number, dataPtr: number, shapePtr: number, rank: number, dtype: number, zstdLevel: number, alpha: number, beta: number, effort: number): number;
44
+ }
45
+ /** Encoder effort: "fast" (default) tunes on the chunk's central 33^d; "max" on 65^d. */
46
+ export type Effort = "fast" | "max";
47
+ export declare class WarpSZError extends Error {
48
+ }
49
+ type WasmSource = BufferSource | WebAssembly.Module | Response | PromiseLike<Response>;
50
+ export declare class WarpSZ {
51
+ readonly exports: Exports;
52
+ constructor(instance: WebAssembly.Instance);
53
+ /** Instantiate from wasm bytes, a compiled module, or a fetch() response. */
54
+ static instantiate(source: WasmSource): Promise<WarpSZ>;
55
+ private bytes;
56
+ private put;
57
+ private fail;
58
+ private configBytes;
59
+ private shapeBytes;
60
+ /** Decode one chunk. Returns a new typed array of prod(shape) values, C order. */
61
+ decode(chunk: Uint8Array, shape: readonly number[], dataType: DataType, config: ConfigLike): Float32Array | Float64Array;
62
+ /**
63
+ * Encode one chunk (C order). `zstdLevel` is ignored by the pure-Rust zstd in wasm.
64
+ * `schedule` fixes the level schedule [alpha, beta] (both >= 1; the level with
65
+ * interpolation step 2^L is coded at error_bound / min(alpha^L, beta)); by default
66
+ * it is tuned per chunk, on a sample whose size `effort` sets.
67
+ */
68
+ encode(data: Float32Array | Float64Array, shape: readonly number[], config: ConfigLike, zstdLevel?: number, schedule?: readonly [number, number], effort?: Effort): Uint8Array;
69
+ }
70
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ /**
2
+ * warpsz codec for JavaScript/TypeScript, backed by the Rust implementation
3
+ * compiled to WebAssembly. Bit-identical to the Python and native Rust codecs.
4
+ *
5
+ * const wf = await WarpSZ.instantiate(wasmBytesOrResponse);
6
+ * const field = wf.decode(chunkBytes, [64, 64, 64, 3], "float32", config);
7
+ *
8
+ * `config` is the codec's `configuration` object from the zarr array metadata
9
+ * (or the whole `{name: "warpsz", configuration}` entry).
10
+ *
11
+ * The module has no imports and uses no JS glue beyond this file. One instance
12
+ * is single-threaded; use one instance per worker.
13
+ */
14
+ export class WarpSZError extends Error {
15
+ }
16
+ const encoder = new TextEncoder();
17
+ const decoder = new TextDecoder();
18
+ export class WarpSZ {
19
+ exports;
20
+ constructor(instance) {
21
+ this.exports = instance.exports;
22
+ }
23
+ /** Instantiate from wasm bytes, a compiled module, or a fetch() response. */
24
+ static async instantiate(source) {
25
+ let module;
26
+ if (source instanceof WebAssembly.Module) {
27
+ module = source;
28
+ }
29
+ else if (typeof source.then === "function" ||
30
+ (typeof Response !== "undefined" && source instanceof Response)) {
31
+ const resp = await source;
32
+ module = await WebAssembly.compile(await resp.arrayBuffer());
33
+ }
34
+ else {
35
+ module = await WebAssembly.compile(source);
36
+ }
37
+ return new WarpSZ(await WebAssembly.instantiate(module, {}));
38
+ }
39
+ bytes() {
40
+ return new Uint8Array(this.exports.memory.buffer);
41
+ }
42
+ put(data) {
43
+ const p = this.exports.wf_alloc(data.byteLength);
44
+ this.bytes().set(data, p); // view taken after alloc: memory may have grown
45
+ return [p, data.byteLength];
46
+ }
47
+ fail() {
48
+ const p = this.exports.wf_error_ptr();
49
+ const n = this.exports.wf_error_len();
50
+ throw new WarpSZError(decoder.decode(this.bytes().slice(p, p + n)));
51
+ }
52
+ configBytes(config) {
53
+ return encoder.encode(typeof config === "string" ? config : JSON.stringify(config));
54
+ }
55
+ shapeBytes(shape) {
56
+ return new Uint8Array(Uint32Array.from(shape).buffer);
57
+ }
58
+ /** Decode one chunk. Returns a new typed array of prod(shape) values, C order. */
59
+ decode(chunk, shape, dataType, config) {
60
+ const n = shape.reduce((a, b) => a * b, 1);
61
+ const size = dataType === "float32" ? 4 : 8;
62
+ const allocs = [];
63
+ try {
64
+ const cfg = this.put(this.configBytes(config));
65
+ allocs.push(cfg);
66
+ const buf = this.put(chunk);
67
+ allocs.push(buf);
68
+ const shp = this.put(this.shapeBytes(shape));
69
+ allocs.push(shp);
70
+ const outLen = n * size;
71
+ const out = this.exports.wf_alloc(outLen);
72
+ allocs.push([out, outLen]);
73
+ const status = this.exports.wf_decode(cfg[0], cfg[1], buf[0], buf[1], shp[0], shape.length, dataType === "float32" ? 0 : 1, out);
74
+ if (status !== 0)
75
+ this.fail();
76
+ const mem = this.exports.memory.buffer;
77
+ return dataType === "float32"
78
+ ? new Float32Array(mem.slice(out, out + outLen))
79
+ : new Float64Array(mem.slice(out, out + outLen));
80
+ }
81
+ finally {
82
+ for (const [p, len] of allocs)
83
+ this.exports.wf_free(p, len);
84
+ }
85
+ }
86
+ /**
87
+ * Encode one chunk (C order). `zstdLevel` is ignored by the pure-Rust zstd in wasm.
88
+ * `schedule` fixes the level schedule [alpha, beta] (both >= 1; the level with
89
+ * interpolation step 2^L is coded at error_bound / min(alpha^L, beta)); by default
90
+ * it is tuned per chunk, on a sample whose size `effort` sets.
91
+ */
92
+ encode(data, shape, config, zstdLevel = 3, schedule, effort = "fast") {
93
+ if (effort !== "fast" && effort !== "max") {
94
+ throw new WarpSZError(`effort must be "fast" or "max", not ${effort}`);
95
+ }
96
+ const n = shape.reduce((a, b) => a * b, 1);
97
+ if (data.length !== n) {
98
+ throw new WarpSZError(`data has ${data.length} values, shape needs ${n}`);
99
+ }
100
+ const allocs = [];
101
+ try {
102
+ const cfg = this.put(this.configBytes(config));
103
+ allocs.push(cfg);
104
+ const raw = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
105
+ const d = this.put(raw);
106
+ allocs.push(d);
107
+ const shp = this.put(this.shapeBytes(shape));
108
+ allocs.push(shp);
109
+ const status = this.exports.wf_encode(cfg[0], cfg[1], d[0], shp[0], shape.length, data instanceof Float32Array ? 0 : 1, zstdLevel, schedule ? schedule[0] : 0, schedule ? schedule[1] : 0, effort === "max" ? 1 : 0);
110
+ if (status !== 0)
111
+ this.fail();
112
+ const p = this.exports.wf_result_ptr();
113
+ const len = this.exports.wf_result_len();
114
+ const out = this.bytes().slice(p, p + len);
115
+ this.exports.wf_result_clear();
116
+ return out;
117
+ }
118
+ finally {
119
+ for (const [p, len] of allocs)
120
+ this.exports.wf_free(p, len);
121
+ }
122
+ }
123
+ }
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@alleninstitute/warpsz",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
4
+ "description": "warpsz codec (error-bounded, seam-free registration-field compression) in WebAssembly",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AllenInstitute/warpsz.git",
9
+ "directory": "rust/warpsz-wasm/js"
10
+ },
11
+ "type": "module",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
16
+ "./warpsz.wasm": "./warpsz.wasm"
17
+ },
18
+ "files": ["dist", "src", "warpsz.wasm"],
19
+ "scripts": {
20
+ "build:wasm": "cargo build --manifest-path ../../Cargo.toml -p warpsz-wasm --target wasm32-unknown-unknown --profile release-wasm && cp ../../target/wasm32-unknown-unknown/release-wasm/warpsz_wasm.wasm warpsz.wasm",
21
+ "build": "tsc -p .",
22
+ "typecheck": "tsc -p tsconfig.test.json",
23
+ "test": "node --test test/*.test.ts"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "7.0.2",
27
+ "@types/node": "22.20.7"
28
+ },
29
+ "engines": { "node": ">=22.18" }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,227 @@
1
+ /**
2
+ * warpsz codec for JavaScript/TypeScript, backed by the Rust implementation
3
+ * compiled to WebAssembly. Bit-identical to the Python and native Rust codecs.
4
+ *
5
+ * const wf = await WarpSZ.instantiate(wasmBytesOrResponse);
6
+ * const field = wf.decode(chunkBytes, [64, 64, 64, 3], "float32", config);
7
+ *
8
+ * `config` is the codec's `configuration` object from the zarr array metadata
9
+ * (or the whole `{name: "warpsz", configuration}` entry).
10
+ *
11
+ * The module has no imports and uses no JS glue beyond this file. One instance
12
+ * is single-threaded; use one instance per worker.
13
+ */
14
+
15
+ export type AxisRole = "spatial" | "smooth" | "independent" | "component";
16
+ export type DataType = "float32" | "float64";
17
+
18
+ export interface JacobianConfig {
19
+ grid_spacing: readonly number[];
20
+ min_det?: number;
21
+ tight_error_bound?: number | number[] | null;
22
+ }
23
+
24
+ export interface WarpSZConfig {
25
+ axis_roles: readonly AxisRole[];
26
+ error_bound?: number;
27
+ face_error_bound?: number | null;
28
+ predictor?: "linear" | "cubic" | "eno";
29
+ boundary?: "clamped" | "extrapolate";
30
+ jacobian?: JacobianConfig | null;
31
+ }
32
+
33
+ export type ConfigLike =
34
+ | WarpSZConfig
35
+ | { name: "warpsz" | "warpfield"; configuration: WarpSZConfig }
36
+ | string;
37
+
38
+ interface Exports {
39
+ memory: WebAssembly.Memory;
40
+ wf_alloc(n: number): number;
41
+ wf_free(p: number, n: number): void;
42
+ wf_error_ptr(): number;
43
+ wf_error_len(): number;
44
+ wf_result_ptr(): number;
45
+ wf_result_len(): number;
46
+ wf_result_clear(): void;
47
+ wf_decode(
48
+ cfgPtr: number,
49
+ cfgLen: number,
50
+ bufPtr: number,
51
+ bufLen: number,
52
+ shapePtr: number,
53
+ rank: number,
54
+ dtype: number,
55
+ outPtr: number,
56
+ ): number;
57
+ wf_encode(
58
+ cfgPtr: number,
59
+ cfgLen: number,
60
+ dataPtr: number,
61
+ shapePtr: number,
62
+ rank: number,
63
+ dtype: number,
64
+ zstdLevel: number,
65
+ alpha: number,
66
+ beta: number,
67
+ effort: number,
68
+ ): number;
69
+ }
70
+
71
+ /** Encoder effort: "fast" (default) tunes on the chunk's central 33^d; "max" on 65^d. */
72
+ export type Effort = "fast" | "max";
73
+
74
+ export class WarpSZError extends Error {}
75
+
76
+ const encoder = new TextEncoder();
77
+ const decoder = new TextDecoder();
78
+
79
+ type WasmSource =
80
+ | BufferSource
81
+ | WebAssembly.Module
82
+ | Response
83
+ | PromiseLike<Response>;
84
+
85
+ export class WarpSZ {
86
+ readonly exports: Exports;
87
+
88
+ constructor(instance: WebAssembly.Instance) {
89
+ this.exports = instance.exports as unknown as Exports;
90
+ }
91
+
92
+ /** Instantiate from wasm bytes, a compiled module, or a fetch() response. */
93
+ static async instantiate(source: WasmSource): Promise<WarpSZ> {
94
+ let module: WebAssembly.Module;
95
+ if (source instanceof WebAssembly.Module) {
96
+ module = source;
97
+ } else if (
98
+ typeof (source as PromiseLike<Response>).then === "function" ||
99
+ (typeof Response !== "undefined" && source instanceof Response)
100
+ ) {
101
+ const resp = await (source as PromiseLike<Response>);
102
+ module = await WebAssembly.compile(await resp.arrayBuffer());
103
+ } else {
104
+ module = await WebAssembly.compile(source as BufferSource);
105
+ }
106
+ return new WarpSZ(await WebAssembly.instantiate(module, {}));
107
+ }
108
+
109
+ private bytes(): Uint8Array {
110
+ return new Uint8Array(this.exports.memory.buffer);
111
+ }
112
+
113
+ private put(data: Uint8Array): [number, number] {
114
+ const p = this.exports.wf_alloc(data.byteLength);
115
+ this.bytes().set(data, p); // view taken after alloc: memory may have grown
116
+ return [p, data.byteLength];
117
+ }
118
+
119
+ private fail(): never {
120
+ const p = this.exports.wf_error_ptr();
121
+ const n = this.exports.wf_error_len();
122
+ throw new WarpSZError(decoder.decode(this.bytes().slice(p, p + n)));
123
+ }
124
+
125
+ private configBytes(config: ConfigLike): Uint8Array {
126
+ return encoder.encode(
127
+ typeof config === "string" ? config : JSON.stringify(config),
128
+ );
129
+ }
130
+
131
+ private shapeBytes(shape: readonly number[]): Uint8Array {
132
+ return new Uint8Array(Uint32Array.from(shape).buffer);
133
+ }
134
+
135
+ /** Decode one chunk. Returns a new typed array of prod(shape) values, C order. */
136
+ decode(
137
+ chunk: Uint8Array,
138
+ shape: readonly number[],
139
+ dataType: DataType,
140
+ config: ConfigLike,
141
+ ): Float32Array | Float64Array {
142
+ const n = shape.reduce((a, b) => a * b, 1);
143
+ const size = dataType === "float32" ? 4 : 8;
144
+ const allocs: [number, number][] = [];
145
+ try {
146
+ const cfg = this.put(this.configBytes(config));
147
+ allocs.push(cfg);
148
+ const buf = this.put(chunk);
149
+ allocs.push(buf);
150
+ const shp = this.put(this.shapeBytes(shape));
151
+ allocs.push(shp);
152
+ const outLen = n * size;
153
+ const out = this.exports.wf_alloc(outLen);
154
+ allocs.push([out, outLen]);
155
+ const status = this.exports.wf_decode(
156
+ cfg[0],
157
+ cfg[1],
158
+ buf[0],
159
+ buf[1],
160
+ shp[0],
161
+ shape.length,
162
+ dataType === "float32" ? 0 : 1,
163
+ out,
164
+ );
165
+ if (status !== 0) this.fail();
166
+ const mem = this.exports.memory.buffer;
167
+ return dataType === "float32"
168
+ ? new Float32Array(mem.slice(out, out + outLen))
169
+ : new Float64Array(mem.slice(out, out + outLen));
170
+ } finally {
171
+ for (const [p, len] of allocs) this.exports.wf_free(p, len);
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Encode one chunk (C order). `zstdLevel` is ignored by the pure-Rust zstd in wasm.
177
+ * `schedule` fixes the level schedule [alpha, beta] (both >= 1; the level with
178
+ * interpolation step 2^L is coded at error_bound / min(alpha^L, beta)); by default
179
+ * it is tuned per chunk, on a sample whose size `effort` sets.
180
+ */
181
+ encode(
182
+ data: Float32Array | Float64Array,
183
+ shape: readonly number[],
184
+ config: ConfigLike,
185
+ zstdLevel = 3,
186
+ schedule?: readonly [number, number],
187
+ effort: Effort = "fast",
188
+ ): Uint8Array {
189
+ if (effort !== "fast" && effort !== "max") {
190
+ throw new WarpSZError(`effort must be "fast" or "max", not ${effort}`);
191
+ }
192
+ const n = shape.reduce((a, b) => a * b, 1);
193
+ if (data.length !== n) {
194
+ throw new WarpSZError(`data has ${data.length} values, shape needs ${n}`);
195
+ }
196
+ const allocs: [number, number][] = [];
197
+ try {
198
+ const cfg = this.put(this.configBytes(config));
199
+ allocs.push(cfg);
200
+ const raw = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
201
+ const d = this.put(raw);
202
+ allocs.push(d);
203
+ const shp = this.put(this.shapeBytes(shape));
204
+ allocs.push(shp);
205
+ const status = this.exports.wf_encode(
206
+ cfg[0],
207
+ cfg[1],
208
+ d[0],
209
+ shp[0],
210
+ shape.length,
211
+ data instanceof Float32Array ? 0 : 1,
212
+ zstdLevel,
213
+ schedule ? schedule[0] : 0,
214
+ schedule ? schedule[1] : 0,
215
+ effort === "max" ? 1 : 0,
216
+ );
217
+ if (status !== 0) this.fail();
218
+ const p = this.exports.wf_result_ptr();
219
+ const len = this.exports.wf_result_len();
220
+ const out = this.bytes().slice(p, p + len);
221
+ this.exports.wf_result_clear();
222
+ return out;
223
+ } finally {
224
+ for (const [p, len] of allocs) this.exports.wf_free(p, len);
225
+ }
226
+ }
227
+ }
package/warpsz.wasm ADDED
Binary file
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.