@climateriskcommons/crc-framework 0.5.4 → 1.0.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.
@@ -0,0 +1,200 @@
1
+ {
2
+ "schema_version": "1.0",
3
+ "accepted": [
4
+ {
5
+ "name": "open-empty",
6
+ "document": {
7
+ "namespace": "customer",
8
+ "presets": {},
9
+ "revision": "v1",
10
+ "scenarios": [],
11
+ "schema_version": "1.0",
12
+ "variables": []
13
+ }
14
+ },
15
+ {
16
+ "name": "custom-science",
17
+ "document": {
18
+ "namespace": "customer",
19
+ "presets": {
20
+ "censored": {
21
+ "censor_location": 0,
22
+ "constraints": null,
23
+ "family": "genextreme",
24
+ "kind": "censored_quantiles",
25
+ "minimum_knots": 4,
26
+ "residual_scale_floor_ratio": 0.001
27
+ },
28
+ "hurdle": {
29
+ "atom_location": 0.0,
30
+ "atom_probability": 0.5,
31
+ "constraints": null,
32
+ "family": "gumbel_r",
33
+ "kind": "hurdle_quantiles",
34
+ "minimum_knots": 4
35
+ },
36
+ "quantiles": {
37
+ "constraints": null,
38
+ "family": "gumbel_r",
39
+ "kind": "quantiles",
40
+ "minimum_knots": 4,
41
+ "weights": null
42
+ },
43
+ "sample": {
44
+ "constraints": null,
45
+ "estimator": "mle",
46
+ "fallbacks": [],
47
+ "family": "gumbel_r",
48
+ "kind": "samples",
49
+ "minimum_samples": 3
50
+ },
51
+ "selection": {
52
+ "candidates": [
53
+ {
54
+ "constraints": null,
55
+ "estimator": "mle",
56
+ "fallbacks": [],
57
+ "family": "gumbel_r",
58
+ "kind": "samples",
59
+ "minimum_samples": 3
60
+ },
61
+ {
62
+ "constraints": null,
63
+ "estimator": "mle",
64
+ "fallbacks": [],
65
+ "family": "gumbel_l",
66
+ "kind": "samples",
67
+ "minimum_samples": 3
68
+ }
69
+ ],
70
+ "criterion": "ks",
71
+ "kind": "model_selection"
72
+ }
73
+ },
74
+ "revision": "v1",
75
+ "scenarios": [
76
+ {
77
+ "definition": {
78
+ "delta": 1.5
79
+ },
80
+ "id": "custom scenario"
81
+ }
82
+ ],
83
+ "schema_version": "1.0",
84
+ "variables": [
85
+ {
86
+ "id": "用户:custom",
87
+ "measurement": {
88
+ "fields": {
89
+ "duration": 2
90
+ },
91
+ "kind": "measurement",
92
+ "quantity": "user quantity",
93
+ "unit": "user unit"
94
+ }
95
+ }
96
+ ]
97
+ }
98
+ }
99
+ ],
100
+ "rejected": [
101
+ {
102
+ "name": "historical-alias-table",
103
+ "document": {
104
+ "namespace": "customer",
105
+ "presets": {},
106
+ "revision": "v1",
107
+ "scenarios": [],
108
+ "schema_version": "1.0",
109
+ "variables": [],
110
+ "aliases": {}
111
+ }
112
+ },
113
+ {
114
+ "name": "unsupported-version",
115
+ "document": {
116
+ "namespace": "customer",
117
+ "presets": {},
118
+ "revision": "v1",
119
+ "scenarios": [],
120
+ "schema_version": "2.0",
121
+ "variables": []
122
+ }
123
+ },
124
+ {
125
+ "name": "boundary-whitespace",
126
+ "document": {
127
+ "namespace": " customer",
128
+ "presets": {},
129
+ "revision": "v1",
130
+ "scenarios": [],
131
+ "schema_version": "1.0",
132
+ "variables": []
133
+ }
134
+ },
135
+ {
136
+ "name": "unknown-variable-field",
137
+ "document": {
138
+ "namespace": "customer",
139
+ "presets": {},
140
+ "revision": "v1",
141
+ "scenarios": [],
142
+ "schema_version": "1.0",
143
+ "variables": [
144
+ {
145
+ "id": "v",
146
+ "hazard_name": "rx1day"
147
+ }
148
+ ]
149
+ }
150
+ },
151
+ {
152
+ "name": "unknown-scenario-field",
153
+ "document": {
154
+ "namespace": "customer",
155
+ "presets": {},
156
+ "revision": "v1",
157
+ "scenarios": [
158
+ {
159
+ "id": "s",
160
+ "pathway": "historical"
161
+ }
162
+ ],
163
+ "schema_version": "1.0",
164
+ "variables": []
165
+ }
166
+ },
167
+ {
168
+ "name": "irrelevant-null-label",
169
+ "document": {
170
+ "namespace": "customer",
171
+ "presets": {},
172
+ "revision": "v1",
173
+ "scenarios": [],
174
+ "schema_version": "1.0",
175
+ "variables": [
176
+ {
177
+ "id": "v",
178
+ "label": null
179
+ }
180
+ ]
181
+ }
182
+ },
183
+ {
184
+ "name": "unknown-fitting-family",
185
+ "document": {
186
+ "namespace": "customer",
187
+ "presets": {
188
+ "fit": {
189
+ "kind": "samples",
190
+ "family": "auto"
191
+ }
192
+ },
193
+ "revision": "v1",
194
+ "scenarios": [],
195
+ "schema_version": "1.0",
196
+ "variables": []
197
+ }
198
+ }
199
+ ]
200
+ }
@@ -0,0 +1,313 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://climateriskcommons.org/schemas/taxonomy/1.0",
4
+ "type": "object",
5
+ "required": [
6
+ "schema_version",
7
+ "namespace",
8
+ "revision",
9
+ "variables",
10
+ "scenarios",
11
+ "presets"
12
+ ],
13
+ "additionalProperties": false,
14
+ "properties": {
15
+ "schema_version": {
16
+ "const": "1.0"
17
+ },
18
+ "namespace": {
19
+ "type": "string",
20
+ "minLength": 1,
21
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
22
+ },
23
+ "revision": {
24
+ "type": "string",
25
+ "minLength": 1,
26
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
27
+ },
28
+ "variables": {
29
+ "type": "array",
30
+ "items": {
31
+ "$ref": "#/$defs/variable"
32
+ },
33
+ "uniqueItems": true
34
+ },
35
+ "scenarios": {
36
+ "type": "array",
37
+ "items": {
38
+ "$ref": "#/$defs/scenario"
39
+ },
40
+ "uniqueItems": true
41
+ },
42
+ "presets": {
43
+ "type": "object",
44
+ "propertyNames": {
45
+ "minLength": 1,
46
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
47
+ },
48
+ "additionalProperties": {
49
+ "$ref": "https://climateriskcommons.org/schemas/fitting/1.0#/$defs/specification"
50
+ }
51
+ }
52
+ },
53
+ "$defs": {
54
+ "scientific_value": {
55
+ "anyOf": [
56
+ {
57
+ "type": [
58
+ "null",
59
+ "boolean",
60
+ "number",
61
+ "string"
62
+ ]
63
+ },
64
+ {
65
+ "type": "array",
66
+ "items": {
67
+ "$ref": "#/$defs/scientific_value"
68
+ }
69
+ },
70
+ {
71
+ "type": "object",
72
+ "additionalProperties": {
73
+ "$ref": "#/$defs/scientific_value"
74
+ }
75
+ }
76
+ ]
77
+ },
78
+ "variable": {
79
+ "type": "object",
80
+ "required": [
81
+ "id"
82
+ ],
83
+ "properties": {
84
+ "id": {
85
+ "type": "string",
86
+ "minLength": 1,
87
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
88
+ },
89
+ "measurement": {
90
+ "$ref": "https://climateriskcommons.org/schemas/measurement-1.0.schema.json"
91
+ },
92
+ "label": {
93
+ "type": "string"
94
+ }
95
+ },
96
+ "additionalProperties": false
97
+ },
98
+ "scenario": {
99
+ "type": "object",
100
+ "required": [
101
+ "id"
102
+ ],
103
+ "properties": {
104
+ "id": {
105
+ "type": "string",
106
+ "minLength": 1,
107
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
108
+ },
109
+ "definition": {
110
+ "type": "object",
111
+ "additionalProperties": {
112
+ "$ref": "#/$defs/scientific_value"
113
+ }
114
+ },
115
+ "schema": {
116
+ "type": "object",
117
+ "additionalProperties": {
118
+ "$ref": "#/$defs/scientific_value"
119
+ }
120
+ },
121
+ "label": {
122
+ "type": "string"
123
+ }
124
+ },
125
+ "additionalProperties": false,
126
+ "dependentRequired": {
127
+ "schema": [
128
+ "definition"
129
+ ]
130
+ }
131
+ },
132
+ "period": {
133
+ "oneOf": [
134
+ {
135
+ "type": "object",
136
+ "required": [
137
+ "id",
138
+ "kind",
139
+ "start",
140
+ "end"
141
+ ],
142
+ "properties": {
143
+ "id": {
144
+ "type": "string",
145
+ "pattern": "^period:[0-9a-f]{64}$"
146
+ },
147
+ "kind": {
148
+ "const": "window"
149
+ },
150
+ "start": {
151
+ "type": "integer"
152
+ },
153
+ "end": {
154
+ "type": "integer"
155
+ },
156
+ "calendar": {
157
+ "enum": [
158
+ "gregorian",
159
+ "no_leap",
160
+ "360_day",
161
+ "all_leap",
162
+ "julian",
163
+ "proleptic_gregorian"
164
+ ]
165
+ }
166
+ },
167
+ "additionalProperties": false
168
+ },
169
+ {
170
+ "type": "object",
171
+ "required": [
172
+ "id",
173
+ "kind"
174
+ ],
175
+ "properties": {
176
+ "id": {
177
+ "type": "string",
178
+ "pattern": "^period:[0-9a-f]{64}$"
179
+ },
180
+ "kind": {
181
+ "enum": [
182
+ "timeless",
183
+ "unknown"
184
+ ]
185
+ }
186
+ },
187
+ "additionalProperties": false
188
+ }
189
+ ]
190
+ },
191
+ "probability": {
192
+ "oneOf": [
193
+ {
194
+ "type": "object",
195
+ "required": [
196
+ "kind",
197
+ "tail"
198
+ ],
199
+ "properties": {
200
+ "kind": {
201
+ "const": "annual"
202
+ },
203
+ "tail": {
204
+ "enum": [
205
+ "upper",
206
+ "lower"
207
+ ]
208
+ }
209
+ },
210
+ "additionalProperties": false
211
+ },
212
+ {
213
+ "type": "object",
214
+ "required": [
215
+ "kind"
216
+ ],
217
+ "properties": {
218
+ "kind": {
219
+ "enum": [
220
+ "distribution",
221
+ "confidence",
222
+ "projection"
223
+ ]
224
+ }
225
+ },
226
+ "additionalProperties": false
227
+ }
228
+ ]
229
+ },
230
+ "series": {
231
+ "type": "object",
232
+ "required": [
233
+ "variable_id",
234
+ "scenario_id",
235
+ "period_id"
236
+ ],
237
+ "properties": {
238
+ "variable_id": {
239
+ "type": "string",
240
+ "minLength": 1,
241
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
242
+ },
243
+ "scenario_id": {
244
+ "anyOf": [
245
+ {
246
+ "type": "string",
247
+ "minLength": 1,
248
+ "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
249
+ },
250
+ {
251
+ "type": "null"
252
+ }
253
+ ]
254
+ },
255
+ "period_id": {
256
+ "anyOf": [
257
+ {
258
+ "type": "string",
259
+ "pattern": "^period:[0-9a-f]{64}$"
260
+ },
261
+ {
262
+ "type": "null"
263
+ }
264
+ ]
265
+ },
266
+ "probability": {
267
+ "$ref": "#/$defs/probability"
268
+ }
269
+ },
270
+ "additionalProperties": false
271
+ },
272
+ "data_spec": {
273
+ "type": "object",
274
+ "required": [
275
+ "variables",
276
+ "scenarios",
277
+ "periods",
278
+ "series"
279
+ ],
280
+ "properties": {
281
+ "variables": {
282
+ "type": "array",
283
+ "items": {
284
+ "$ref": "#/$defs/variable"
285
+ },
286
+ "uniqueItems": true
287
+ },
288
+ "scenarios": {
289
+ "type": "array",
290
+ "items": {
291
+ "$ref": "#/$defs/scenario"
292
+ },
293
+ "uniqueItems": true
294
+ },
295
+ "periods": {
296
+ "type": "array",
297
+ "items": {
298
+ "$ref": "#/$defs/period"
299
+ },
300
+ "uniqueItems": true
301
+ },
302
+ "series": {
303
+ "type": "array",
304
+ "items": {
305
+ "$ref": "#/$defs/series"
306
+ },
307
+ "uniqueItems": true
308
+ }
309
+ },
310
+ "additionalProperties": false
311
+ }
312
+ }
313
+ }
package/package.json CHANGED
@@ -11,13 +11,16 @@
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./src/index.d.ts",
14
- "import": "./src/index.js"
14
+ "node": "./src/node.js",
15
+ "default": "./src/index.js"
15
16
  },
16
- "./crc_framework_bg.wasm": "./wasm/crc_framework_bg.wasm"
17
+ "./crc_framework_bg.wasm": "./wasm/crc_framework_bg.wasm",
18
+ "./contracts/*": "./contracts/*"
17
19
  },
18
20
  "files": [
19
21
  "src/",
20
22
  "wasm/",
23
+ "contracts/",
21
24
  "LICENSE",
22
25
  "THIRD_PARTY_NOTICES.md",
23
26
  "README.md"
@@ -26,5 +29,5 @@
26
29
  "publishConfig": {
27
30
  "access": "public"
28
31
  },
29
- "version": "0.5.4"
32
+ "version": "1.0.0-alpha.1"
30
33
  }
@@ -0,0 +1,107 @@
1
+ import initWasm, {
2
+ PreparedNumerical,
3
+ PreparedNumericals,
4
+ validateDocument as validateNative,
5
+ } from '../wasm/crc_framework.js';
6
+
7
+ export const DocumentSchema = Object.freeze({
8
+ NUMERICAL: 'https://climateriskcommons.org/schemas/numerical/1.0',
9
+ FITTING_RECORD: 'https://climateriskcommons.org/schemas/fitting/1.0',
10
+ FITTING_SPECIFICATION: 'https://climateriskcommons.org/schemas/fitting/1.0#/$defs/specification',
11
+ DATASET: 'https://climateriskcommons.org/schemas/dataset/2.0',
12
+ CATALOG: 'https://climateriskcommons.org/schemas/taxonomy/1.0',
13
+ MEASUREMENT: 'https://climateriskcommons.org/schemas/measurement-1.0.schema.json',
14
+ IMPACT_CONTEXT: 'https://climateriskcommons.org/schemas/impact-context-1.0.schema.json',
15
+ IMPACT: 'https://climateriskcommons.org/schemas/impact-2.0.schema.json',
16
+ IMPACT_REFERENCE: 'https://climateriskcommons.org/schemas/impact-reference-2.0.schema.json',
17
+ });
18
+
19
+ let ready = false;
20
+ let pending;
21
+
22
+ export function initialize(loadInput) {
23
+ pending ??= Promise.resolve().then(loadInput)
24
+ .then(input => initWasm({ module_or_path: input }))
25
+ .then(() => { ready = true; })
26
+ .catch(error => {
27
+ pending = undefined;
28
+ throw error;
29
+ });
30
+ return pending;
31
+ }
32
+
33
+ function assertReady() {
34
+ if (!ready) throw new Error('await init() before using the CRC Framework');
35
+ }
36
+
37
+ function assertText(text) {
38
+ if (typeof text !== 'string') throw new TypeError('document must be JSON text');
39
+ }
40
+
41
+ function assertNumber(probability) {
42
+ if (typeof probability !== 'number') throw new TypeError('probability must be a number');
43
+ }
44
+
45
+ /** Validate a current dataset, numerical, measurement, context, or impact JSON document. */
46
+ export function validateDocument(schema, text) {
47
+ assertReady();
48
+ if (typeof schema !== 'string') throw new TypeError('schema must be a DocumentSchema value');
49
+ assertText(text);
50
+ validateNative(schema, text);
51
+ }
52
+
53
+ /** Parse one current numerical JSON document, retaining its exact representation. */
54
+ export function prepareNumerical(text) {
55
+ assertReady();
56
+ assertText(text);
57
+ const native = new PreparedNumerical(text);
58
+ let freed = false;
59
+ return {
60
+ quantile(probabilities) {
61
+ if (freed) throw new Error('prepared numerical has been freed');
62
+ if (typeof probabilities === 'number') return native.quantile(probabilities);
63
+ if (Array.isArray(probabilities)) {
64
+ for (const probability of probabilities) assertNumber(probability);
65
+ } else if (!(probabilities instanceof Float64Array)) {
66
+ throw new TypeError('probabilities must be a number, array of numbers, or Float64Array');
67
+ }
68
+ return native.quantileBatch(probabilities);
69
+ },
70
+ free() {
71
+ if (!freed) {
72
+ native.free();
73
+ freed = true;
74
+ }
75
+ },
76
+ };
77
+ }
78
+
79
+ export function quantile(text, probabilities) {
80
+ const prepared = prepareNumerical(text);
81
+ try {
82
+ return prepared.quantile(probabilities);
83
+ } finally {
84
+ prepared.free();
85
+ }
86
+ }
87
+
88
+ /** Parse an array of numerical documents for repeated queries in one WASM call. */
89
+ export function prepareNumericals(text) {
90
+ assertReady();
91
+ assertText(text);
92
+ const native = new PreparedNumericals(text);
93
+ let freed = false;
94
+ return {
95
+ quantile(probability) {
96
+ if (freed) throw new Error('prepared numericals have been freed');
97
+ assertNumber(probability);
98
+ return native.quantile(probability);
99
+ },
100
+ free() {
101
+ if (!freed) {
102
+ native.free();
103
+ freed = true;
104
+ }
105
+ },
106
+ };
107
+ }