@climateriskcommons/crc-framework 0.0.0-stage โ 0.5.3-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/LICENSE +619 -0
- package/README.md +75 -2
- package/THIRD_PARTY_NOTICES.md +70 -0
- package/package.json +28 -4
- package/src/index.d.ts +75 -0
- package/src/index.js +213 -0
- package/wasm/crc_framework.d.ts +77 -0
- package/wasm/crc_framework.js +357 -0
- package/wasm/crc_framework_bg.wasm +0 -0
- package/wasm/crc_framework_bg.wasm.d.ts +18 -0
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @climateriskcommons/crc-framework
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Official JavaScript/WebAssembly bindings for CRC Framework: consume canonical
|
|
4
|
+
climate curves, evaluate quantiles, and reuse prepared collections. The ESM
|
|
5
|
+
package includes TypeScript declarations and has no runtime npm dependencies.
|
|
6
|
+
|
|
7
|
+
## Contributors
|
|
8
|
+
|
|
9
|
+
Thanks to the people who shape this project through code, reviews, research,
|
|
10
|
+
and internal discussions and ideas.
|
|
11
|
+
|
|
12
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
13
|
+
<!-- prettier-ignore-start -->
|
|
14
|
+
<!-- markdownlint-disable -->
|
|
15
|
+
<table>
|
|
16
|
+
<tbody>
|
|
17
|
+
<tr>
|
|
18
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ChrisHampu"><img src="https://avatars.githubusercontent.com/u/6600532?s=80" width="80" alt="@ChrisHampu" /><br /><sub><b>@ChrisHampu</b></sub></a><br /><a href="https://github.com/RiskThinking/crc-framework-rs/commits?author=ChrisHampu" title="Code">๐ป</a> <a href="https://github.com/RiskThinking/crc-framework-rs/pulls?q=is%3Apr+reviewed-by%3AChrisHampu" title="Reviewed Pull Requests">๐</a></td>
|
|
19
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/woozyking"><img src="https://avatars.githubusercontent.com/u/2837532?s=80" width="80" alt="@woozyking" /><br /><sub><b>@woozyking</b></sub></a><br /><a href="https://github.com/RiskThinking/crc-framework-rs/commits?author=woozyking" title="Code">๐ป</a> <a href="https://github.com/RiskThinking/crc-framework-rs/pulls?q=is%3Apr+reviewed-by%3Awoozyking" title="Reviewed Pull Requests">๐</a></td>
|
|
20
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/awiebe42"><img src="https://avatars.githubusercontent.com/u/65562157?s=80" width="80" alt="@awiebe42" /><br /><sub><b>@awiebe42</b></sub></a><br /><a href="https://github.com/RiskThinking/crc-framework-rs/commits?author=awiebe42" title="Code">๐ป</a> <a href="https://github.com/RiskThinking/crc-framework-rs/pulls?q=is%3Apr+reviewed-by%3Aawiebe42" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/awiebe42" title="Research">๐ฌ</a></td>
|
|
21
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kylalangotsky"><img src="https://avatars.githubusercontent.com/u/91707255?s=80" width="80" alt="@kylalangotsky" /><br /><sub><b>@kylalangotsky</b></sub></a><br /><a href="https://github.com/kylalangotsky" title="Research">๐ฌ</a></td>
|
|
22
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/reshmasoman"><img src="https://avatars.githubusercontent.com/u/102987481?s=80" width="80" alt="@reshmasoman" /><br /><sub><b>@reshmasoman</b></sub></a><br /><a href="https://github.com/reshmasoman" title="Research">๐ฌ</a></td>
|
|
23
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/rkodamana"><img src="https://avatars.githubusercontent.com/u/84455891?s=80" width="80" alt="@rkodamana" /><br /><sub><b>@rkodamana</b></sub></a><br /><a href="https://github.com/rkodamana" title="Research">๐ฌ</a> <a href="https://github.com/RiskThinking/crc-framework-rs/pulls?q=is%3Apr+reviewed-by%3Arkodamana" title="Reviewed Pull Requests">๐</a></td>
|
|
24
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dilshank"><img src="https://avatars.githubusercontent.com/u/1534562?s=80" width="80" alt="@dilshank" /><br /><sub><b>@dilshank</b></sub></a><br /><a href="https://github.com/dilshank" title="Research">๐ฌ</a></td>
|
|
25
|
+
</tr>
|
|
26
|
+
</tbody>
|
|
27
|
+
</table>
|
|
28
|
+
|
|
29
|
+
<!-- markdownlint-restore -->
|
|
30
|
+
<!-- prettier-ignore-end -->
|
|
31
|
+
|
|
32
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
33
|
+
|
|
34
|
+
Contribution types: ๐ป code ยท ๐ review ยท ๐ฌ research. We recognize contributions
|
|
35
|
+
both on and off GitHub using [All Contributors](https://allcontributors.org/).
|
|
36
|
+
|
|
37
|
+
## Installation and quick start
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm install @climateriskcommons/crc-framework@alpha
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import init, { evaluateCurve, evaluateCurves, prepareCurve, prepareCurves } from '@climateriskcommons/crc-framework';
|
|
45
|
+
|
|
46
|
+
await init(); // client startup, once; imports themselves are safe during SSR
|
|
47
|
+
const properties = {
|
|
48
|
+
curve_kind: 'fitted', curve_type: 'gumbel_r',
|
|
49
|
+
curve_location: 1, curve_scale: 2,
|
|
50
|
+
};
|
|
51
|
+
const value = evaluateCurve(properties, 0.99);
|
|
52
|
+
const values = evaluateCurves([properties, properties], 0.99);
|
|
53
|
+
const curve = prepareCurve(properties);
|
|
54
|
+
try {
|
|
55
|
+
const quantiles = curve.quantiles([0.5, 0.9, 0.99]);
|
|
56
|
+
} finally {
|
|
57
|
+
curve.free(); // prepared handles own WASM memory
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Use `@alpha` for prereleases and omit it for stable releases. From
|
|
62
|
+
`0.5.3-alpha.1`, replace imports of `@riskthinking.ai/crc-framework` with
|
|
63
|
+
`@climateriskcommons/crc-framework`, including the WASM asset subpath.
|
|
64
|
+
|
|
65
|
+
## Guides
|
|
66
|
+
|
|
67
|
+
- [JavaScript guide](https://github.com/RiskThinking/crc-framework-rs/blob/v0.5.3-alpha.1/docs/javascript.md): browser/bundler/Node initialization,
|
|
68
|
+
prepared handles, canonical inputs, support ranges, and errors.
|
|
69
|
+
- [Project README](https://github.com/RiskThinking/crc-framework-rs/blob/v0.5.3-alpha.1/README.md): Python quick start and framework scope.
|
|
70
|
+
- [Documentation index](https://github.com/RiskThinking/crc-framework-rs/blob/v0.5.3-alpha.1/docs/README.md): reading order and responsibility boundaries.
|
|
71
|
+
- [Development](https://github.com/RiskThinking/crc-framework-rs/blob/v0.5.3-alpha.1/docs/development.md): build, test, and packaging.
|
|
72
|
+
- [Releases](https://github.com/RiskThinking/crc-framework-rs/blob/v0.5.3-alpha.1/docs/releases.md): versions and publishing.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
AGPL-3.0-or-later. See the packaged `LICENSE` and `THIRD_PARTY_NOTICES.md`.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Third-Party Notices
|
|
2
|
+
|
|
3
|
+
CRC Framework is distributed under the GNU Affero General Public License,
|
|
4
|
+
version 3 or any later version (`AGPL-3.0-or-later`). Portions of the Rust
|
|
5
|
+
implementation are derived from, adapted from, or informed by the projects
|
|
6
|
+
listed below. Their original copyright notices, license conditions, and
|
|
7
|
+
disclaimers remain applicable to those portions.
|
|
8
|
+
|
|
9
|
+
The principal affected source is:
|
|
10
|
+
|
|
11
|
+
- `crates/core/src/reference/curve_fitting.rs`
|
|
12
|
+
|
|
13
|
+
This notice does not imply endorsement by any upstream copyright holder or
|
|
14
|
+
contributor.
|
|
15
|
+
|
|
16
|
+
## SciPy
|
|
17
|
+
|
|
18
|
+
Source: <https://github.com/scipy/scipy>
|
|
19
|
+
|
|
20
|
+
License: BSD 3-Clause
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2001-2002 Enthought, Inc. 2003, SciPy Developers.
|
|
23
|
+
All rights reserved.
|
|
24
|
+
|
|
25
|
+
Redistribution and use in source and binary forms, with or without
|
|
26
|
+
modification, are permitted provided that the following conditions
|
|
27
|
+
are met:
|
|
28
|
+
|
|
29
|
+
1. Redistributions of source code must retain the above copyright
|
|
30
|
+
notice, this list of conditions and the following disclaimer.
|
|
31
|
+
|
|
32
|
+
2. Redistributions in binary form must reproduce the above
|
|
33
|
+
copyright notice, this list of conditions and the following
|
|
34
|
+
disclaimer in the documentation and/or other materials provided
|
|
35
|
+
with the distribution.
|
|
36
|
+
|
|
37
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
38
|
+
contributors may be used to endorse or promote products derived
|
|
39
|
+
from this software without specific prior written permission.
|
|
40
|
+
|
|
41
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
42
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
43
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
44
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
45
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
46
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
47
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
48
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
49
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
50
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
51
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
52
|
+
|
|
53
|
+
### Cephes attribution
|
|
54
|
+
|
|
55
|
+
The SciPy-derived numerical implementation includes coefficients and
|
|
56
|
+
algorithms identified as originating in the Cephes Math Library by Stephen L.
|
|
57
|
+
Moshier. The relevant source comments and this attribution must be retained.
|
|
58
|
+
|
|
59
|
+
Cephes Math Library Release 2.8: June, 2000.
|
|
60
|
+
Copyright 1984, 1995, 2000 by Stephen L. Moshier.
|
|
61
|
+
|
|
62
|
+
The Netlib Cephes notice states:
|
|
63
|
+
|
|
64
|
+
> Some software in this archive may be from the book *Methods and Programs for
|
|
65
|
+
> Mathematical Functions* (Prentice-Hall or Simon & Schuster International,
|
|
66
|
+
> 1989) or from the Cephes Mathematical Library, a commercial product. In
|
|
67
|
+
> either event, it is copyrighted by the author. What you see here may be used
|
|
68
|
+
> freely but it comes with no support or guarantee.
|
|
69
|
+
|
|
70
|
+
Source: <https://netlib.org/cephes/readme>
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@climateriskcommons/crc-framework",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Official CRC Framework WebAssembly bindings for canonical climate curve consumption",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "AGPL-3.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/RiskThinking/crc-framework-rs.git",
|
|
9
|
+
"directory": "js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./src/index.d.ts",
|
|
14
|
+
"import": "./src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./crc_framework_bg.wasm": "./wasm/crc_framework_bg.wasm"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src/",
|
|
20
|
+
"wasm/",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"THIRD_PARTY_NOTICES.md",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"version": "0.5.3-alpha.1"
|
|
30
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export type CurveKind =
|
|
2
|
+
| "fitted"
|
|
3
|
+
| "hurdle"
|
|
4
|
+
| "point_mass"
|
|
5
|
+
| "tabulated"
|
|
6
|
+
| "no_data";
|
|
7
|
+
export type FittedFamily =
|
|
8
|
+
| "genextreme"
|
|
9
|
+
| "genpareto"
|
|
10
|
+
| "weibull_min"
|
|
11
|
+
| "weibull_max"
|
|
12
|
+
| "gumbel_r"
|
|
13
|
+
| "gumbel_l"
|
|
14
|
+
| "skewnorm";
|
|
15
|
+
export type NoDataReason =
|
|
16
|
+
| "below_effective_resolution"
|
|
17
|
+
| "insufficient_informative_support"
|
|
18
|
+
| "degenerate_effective_range"
|
|
19
|
+
| "scientific_exclusion";
|
|
20
|
+
/** Canonical property subset; optional fields may be omitted or null. */
|
|
21
|
+
export interface CurveProperties {
|
|
22
|
+
curve_kind: CurveKind;
|
|
23
|
+
curve_type: FittedFamily | NoDataReason | "point_mass" | "linear_probability";
|
|
24
|
+
curve_shape?: number | null;
|
|
25
|
+
curve_location?: number | null;
|
|
26
|
+
curve_scale?: number | null;
|
|
27
|
+
curve_atom_probability?: number | null;
|
|
28
|
+
curve_atom_location?: number | null;
|
|
29
|
+
curve_probabilities?: readonly number[] | string | null;
|
|
30
|
+
curve_values?: readonly number[] | string | null;
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
}
|
|
33
|
+
/** Untyped MVT properties can be passed directly; Rust validates the canonical subset. */
|
|
34
|
+
export type CurvePropertyRow = { [key: string]: unknown };
|
|
35
|
+
export type InitInput =
|
|
36
|
+
| RequestInfo
|
|
37
|
+
| URL
|
|
38
|
+
| Response
|
|
39
|
+
| BufferSource
|
|
40
|
+
| WebAssembly.Module;
|
|
41
|
+
export default function init(input?: {
|
|
42
|
+
module_or_path: InitInput | Promise<InitInput>;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
export function initSync(input: {
|
|
45
|
+
module: BufferSource | WebAssembly.Module;
|
|
46
|
+
}): void;
|
|
47
|
+
/** RangeError for invalid q; TypeError for malformed records; null for no data/outside support. */
|
|
48
|
+
export function evaluateCurve(
|
|
49
|
+
properties: CurveProperties | CurvePropertyRow,
|
|
50
|
+
q: number,
|
|
51
|
+
): number | null;
|
|
52
|
+
/** A malformed row aborts the batch. */
|
|
53
|
+
export function evaluateCurves(
|
|
54
|
+
properties: readonly (CurveProperties | CurvePropertyRow)[],
|
|
55
|
+
q: number,
|
|
56
|
+
): Array<number | null>;
|
|
57
|
+
export interface PreparedCurve {
|
|
58
|
+
evaluate(q: number): number | null;
|
|
59
|
+
quantiles(
|
|
60
|
+
probabilities: readonly number[] | Float64Array,
|
|
61
|
+
): Array<number | null>;
|
|
62
|
+
free(): void;
|
|
63
|
+
}
|
|
64
|
+
export function prepareCurve(
|
|
65
|
+
properties: CurveProperties | CurvePropertyRow,
|
|
66
|
+
): PreparedCurve;
|
|
67
|
+
|
|
68
|
+
/** Reusable collection: one WASM call per probability, input order preserved. */
|
|
69
|
+
export interface PreparedCurves {
|
|
70
|
+
evaluate(q: number): Array<number | null>;
|
|
71
|
+
free(): void;
|
|
72
|
+
}
|
|
73
|
+
export function prepareCurves(
|
|
74
|
+
properties: readonly (CurveProperties | CurvePropertyRow)[],
|
|
75
|
+
): PreparedCurves;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import initialize, {
|
|
2
|
+
initSync as initializeSync,
|
|
3
|
+
PreparedCurve,
|
|
4
|
+
PreparedCurves,
|
|
5
|
+
curveKindNames,
|
|
6
|
+
curveTypeNames,
|
|
7
|
+
evaluateCurvePacked,
|
|
8
|
+
} from "../wasm/crc_framework.js";
|
|
9
|
+
|
|
10
|
+
let ready = false;
|
|
11
|
+
let pending;
|
|
12
|
+
let kindCodes;
|
|
13
|
+
let typeCodes;
|
|
14
|
+
function finishInit() {
|
|
15
|
+
kindCodes = new Map(curveKindNames().map((name, code) => [name, code]));
|
|
16
|
+
typeCodes = new Map(curveTypeNames().map((name, code) => [name, code]));
|
|
17
|
+
ready = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Initialize once. Importing this module is safe during server rendering. */
|
|
21
|
+
export default function init(input) {
|
|
22
|
+
if (ready) return Promise.resolve();
|
|
23
|
+
if (!pending) {
|
|
24
|
+
pending = initialize(input)
|
|
25
|
+
.then(finishInit)
|
|
26
|
+
.catch((error) => {
|
|
27
|
+
pending = undefined;
|
|
28
|
+
throw error;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return pending;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function initSync(input) {
|
|
35
|
+
if (ready) return;
|
|
36
|
+
initializeSync(input);
|
|
37
|
+
finishInit();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function assertReady() {
|
|
41
|
+
if (!ready)
|
|
42
|
+
throw new Error(
|
|
43
|
+
"CRC Framework is not initialized; await init() before evaluating curves",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function checkProbability(q) {
|
|
48
|
+
if (typeof q !== "number" || !Number.isFinite(q) || q < 0 || q > 1) {
|
|
49
|
+
throw new RangeError("probability must be finite and in [0, 1]");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Synchronous after init. Missing data and queries outside support return null. */
|
|
54
|
+
export function evaluateCurve(properties, q) {
|
|
55
|
+
assertReady();
|
|
56
|
+
checkProbability(q);
|
|
57
|
+
const [rows, knots] = pack([properties]);
|
|
58
|
+
return evaluateCurvePacked(rows, knots, q);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A malformed row aborts the batch; results preserve input order. */
|
|
62
|
+
export function evaluateCurves(properties, q) {
|
|
63
|
+
assertReady();
|
|
64
|
+
checkProbability(q);
|
|
65
|
+
if (!Array.isArray(properties))
|
|
66
|
+
throw new TypeError("curves must be an array");
|
|
67
|
+
const curves = prepareCurves(properties);
|
|
68
|
+
try {
|
|
69
|
+
return curves.evaluate(q);
|
|
70
|
+
} finally {
|
|
71
|
+
curves.free();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Explicit ownership for repeated queries: release the handle with free(). */
|
|
76
|
+
export function prepareCurve(properties) {
|
|
77
|
+
assertReady();
|
|
78
|
+
const [rows, knots] = pack([properties]);
|
|
79
|
+
const handle = PreparedCurve.fromPacked(rows, knots);
|
|
80
|
+
let freed = false;
|
|
81
|
+
function checkAlive() {
|
|
82
|
+
if (freed) throw new Error("prepared curve has been freed");
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
evaluate(q) {
|
|
86
|
+
checkAlive();
|
|
87
|
+
checkProbability(q);
|
|
88
|
+
return handle.evaluate(q);
|
|
89
|
+
},
|
|
90
|
+
quantiles(probabilities) {
|
|
91
|
+
checkAlive();
|
|
92
|
+
if (
|
|
93
|
+
!Array.isArray(probabilities) &&
|
|
94
|
+
!(probabilities instanceof Float64Array)
|
|
95
|
+
) {
|
|
96
|
+
throw new TypeError("probabilities must be an array or Float64Array");
|
|
97
|
+
}
|
|
98
|
+
for (const q of probabilities) checkProbability(q);
|
|
99
|
+
return handle.quantiles(probabilities);
|
|
100
|
+
},
|
|
101
|
+
free() {
|
|
102
|
+
if (!freed) {
|
|
103
|
+
handle.free();
|
|
104
|
+
freed = true;
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Prepare a collection once; each evaluate call crosses the WASM boundary once. */
|
|
111
|
+
export function prepareCurves(properties) {
|
|
112
|
+
assertReady();
|
|
113
|
+
if (!Array.isArray(properties))
|
|
114
|
+
throw new TypeError("curves must be an array");
|
|
115
|
+
const [rows, knots] = pack(properties);
|
|
116
|
+
const handle = PreparedCurves.fromPacked(rows, knots);
|
|
117
|
+
let freed = false;
|
|
118
|
+
return {
|
|
119
|
+
evaluate(q) {
|
|
120
|
+
if (freed) throw new Error("prepared curves have been freed");
|
|
121
|
+
checkProbability(q);
|
|
122
|
+
const values = handle.evaluate(q);
|
|
123
|
+
const result = new Array(values.length);
|
|
124
|
+
for (let i = 0; i < values.length; i++)
|
|
125
|
+
result[i] = Number.isNaN(values[i]) ? null : values[i];
|
|
126
|
+
return result;
|
|
127
|
+
},
|
|
128
|
+
free() {
|
|
129
|
+
if (!freed) {
|
|
130
|
+
handle.free();
|
|
131
|
+
freed = true;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Language conversion only: numbers stay float64 and missing fields get explicit
|
|
138
|
+
// presence bits, so a supplied NaN can never be mistaken for an omitted value.
|
|
139
|
+
const SCALARS = [
|
|
140
|
+
"curve_shape",
|
|
141
|
+
"curve_location",
|
|
142
|
+
"curve_scale",
|
|
143
|
+
"curve_atom_probability",
|
|
144
|
+
"curve_atom_location",
|
|
145
|
+
];
|
|
146
|
+
const ARRAYS = ["curve_probabilities", "curve_values"];
|
|
147
|
+
// MVT repeats identical probability grids and knot strings across tile queries.
|
|
148
|
+
// Internal parsed arrays never escape this adapter. Bound both count and size.
|
|
149
|
+
const parsedArrays = new Map();
|
|
150
|
+
let parsedValues = 0;
|
|
151
|
+
function parseArray(text, field) {
|
|
152
|
+
const cached = parsedArrays.get(text);
|
|
153
|
+
if (cached) return cached;
|
|
154
|
+
let values;
|
|
155
|
+
try {
|
|
156
|
+
values = JSON.parse(text);
|
|
157
|
+
} catch {
|
|
158
|
+
throw new TypeError(`${field} must be a JSON array`);
|
|
159
|
+
}
|
|
160
|
+
if (!Array.isArray(values)) throw new TypeError(`${field} must be an array`);
|
|
161
|
+
if (values.length <= 65_536) {
|
|
162
|
+
while (parsedArrays.size >= 2048 || parsedValues + values.length > 65_536) {
|
|
163
|
+
const oldest = parsedArrays.keys().next().value;
|
|
164
|
+
parsedValues -= parsedArrays.get(oldest).length;
|
|
165
|
+
parsedArrays.delete(oldest);
|
|
166
|
+
}
|
|
167
|
+
parsedArrays.set(text, values);
|
|
168
|
+
parsedValues += values.length;
|
|
169
|
+
}
|
|
170
|
+
return values;
|
|
171
|
+
}
|
|
172
|
+
function pack(properties) {
|
|
173
|
+
const rows = new Float64Array(properties.length * 12);
|
|
174
|
+
const knots = [];
|
|
175
|
+
for (let i = 0; i < properties.length; i++) {
|
|
176
|
+
const row = properties[i];
|
|
177
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
178
|
+
throw new TypeError("curve properties must be an object");
|
|
179
|
+
const offset = i * 12;
|
|
180
|
+
// Unknown names use an invalid code and are rejected by the Rust decoder.
|
|
181
|
+
rows[offset] = kindCodes.get(row.curve_kind) ?? -1;
|
|
182
|
+
rows[offset + 1] = typeCodes.get(row.curve_type) ?? -1;
|
|
183
|
+
let mask = 0;
|
|
184
|
+
for (let j = 0; j < SCALARS.length; j++) {
|
|
185
|
+
const value = row[SCALARS[j]];
|
|
186
|
+
if (value == null) continue;
|
|
187
|
+
if (typeof value !== "number")
|
|
188
|
+
throw new TypeError(`${SCALARS[j]} must be a number`);
|
|
189
|
+
mask |= 1 << j;
|
|
190
|
+
rows[offset + 3 + j] = value;
|
|
191
|
+
}
|
|
192
|
+
rows[offset + 2] = mask;
|
|
193
|
+
for (let j = 0; j < ARRAYS.length; j++) {
|
|
194
|
+
const field = ARRAYS[j];
|
|
195
|
+
let values = row[field];
|
|
196
|
+
rows[offset + 8 + j * 2] = knots.length;
|
|
197
|
+
rows[offset + 9 + j * 2] = -1;
|
|
198
|
+
if (values == null) continue;
|
|
199
|
+
if (typeof values === "string") {
|
|
200
|
+
values = parseArray(values, field);
|
|
201
|
+
}
|
|
202
|
+
if (!Array.isArray(values))
|
|
203
|
+
throw new TypeError(`${field} must be an array`);
|
|
204
|
+
rows[offset + 9 + j * 2] = values.length;
|
|
205
|
+
for (let k = 0; k < values.length; k++) {
|
|
206
|
+
if (typeof values[k] !== "number")
|
|
207
|
+
throw new TypeError(`${field} must contain numbers`);
|
|
208
|
+
knots.push(values[k]);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return [rows, Float64Array.from(knots)];
|
|
213
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An explicitly owned, validated curve; call free() when finished.
|
|
6
|
+
*/
|
|
7
|
+
export class PreparedCurve {
|
|
8
|
+
private constructor();
|
|
9
|
+
free(): void;
|
|
10
|
+
[Symbol.dispose](): void;
|
|
11
|
+
evaluate(q: number): any;
|
|
12
|
+
static fromPacked(row: Float64Array, knots: Float64Array): PreparedCurve;
|
|
13
|
+
/**
|
|
14
|
+
* Multiple queries without reparsing the same record.
|
|
15
|
+
*/
|
|
16
|
+
quantiles(probabilities: Float64Array): Array<any>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A tile-sized collection reconstructed once and evaluated in one WASM call.
|
|
21
|
+
*/
|
|
22
|
+
export class PreparedCurves {
|
|
23
|
+
private constructor();
|
|
24
|
+
free(): void;
|
|
25
|
+
[Symbol.dispose](): void;
|
|
26
|
+
evaluate(q: number): Float64Array;
|
|
27
|
+
static fromPacked(rows: Float64Array, knots: Float64Array): PreparedCurves;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function curveKindNames(): Array<any>;
|
|
31
|
+
|
|
32
|
+
export function curveTypeNames(): Array<any>;
|
|
33
|
+
|
|
34
|
+
export function evaluateCurvePacked(row: Float64Array, knots: Float64Array, q: number): any;
|
|
35
|
+
|
|
36
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
37
|
+
|
|
38
|
+
export interface InitOutput {
|
|
39
|
+
readonly memory: WebAssembly.Memory;
|
|
40
|
+
readonly __wbg_preparedcurve_free: (a: number, b: number) => void;
|
|
41
|
+
readonly __wbg_preparedcurves_free: (a: number, b: number) => void;
|
|
42
|
+
readonly curveKindNames: () => any;
|
|
43
|
+
readonly curveTypeNames: () => any;
|
|
44
|
+
readonly evaluateCurvePacked: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
45
|
+
readonly preparedcurve_evaluate: (a: number, b: number) => [number, number, number];
|
|
46
|
+
readonly preparedcurve_fromPacked: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
47
|
+
readonly preparedcurve_quantiles: (a: number, b: number, c: number) => [number, number, number];
|
|
48
|
+
readonly preparedcurves_evaluate: (a: number, b: number) => [number, number, number, number];
|
|
49
|
+
readonly preparedcurves_fromPacked: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
50
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
51
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
52
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
53
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
54
|
+
readonly __wbindgen_start: () => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
61
|
+
* a precompiled `WebAssembly.Module`.
|
|
62
|
+
*
|
|
63
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
64
|
+
*
|
|
65
|
+
* @returns {InitOutput}
|
|
66
|
+
*/
|
|
67
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
71
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
72
|
+
*
|
|
73
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
74
|
+
*
|
|
75
|
+
* @returns {Promise<InitOutput>}
|
|
76
|
+
*/
|
|
77
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|