Skip to content

Commit 4fef55e

Browse files
committed
Fetch int-64 values from h5grove as binary to maintain precision
1 parent a31fc50 commit 4fef55e

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

packages/app/src/providers/h5grove/__snapshots__/h5grove-api.test.ts.snap

+20-20
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ exports[`test file matches snapshot 1`] = `
174174
"signed": true,
175175
"size": 64,
176176
},
177-
"value": -9223372036854776000,
177+
"value": -9223372036854775808n,
178178
},
179179
{
180180
"name": "int64_2D",
@@ -195,14 +195,14 @@ exports[`test file matches snapshot 1`] = `
195195
"signed": true,
196196
"size": 64,
197197
},
198-
"value": [
199-
0,
200-
1,
201-
2,
202-
3,
203-
4,
204-
5,
205-
],
198+
"value": BigInt64Array {
199+
"0": 0n,
200+
"1": 1n,
201+
"2": 2n,
202+
"3": 3n,
203+
"4": 4n,
204+
"5": 5n,
205+
},
206206
},
207207
{
208208
"name": "uint8_scalar",
@@ -358,7 +358,7 @@ exports[`test file matches snapshot 1`] = `
358358
"signed": false,
359359
"size": 64,
360360
},
361-
"value": 18446744073709552000,
361+
"value": 18446744073709551615n,
362362
},
363363
{
364364
"name": "uint64_3D",
@@ -380,16 +380,16 @@ exports[`test file matches snapshot 1`] = `
380380
"signed": false,
381381
"size": 64,
382382
},
383-
"value": [
384-
0,
385-
1,
386-
2,
387-
3,
388-
4,
389-
5,
390-
6,
391-
18446744073709552000,
392-
],
383+
"value": BigUint64Array {
384+
"0": 0n,
385+
"1": 1n,
386+
"2": 2n,
387+
"3": 3n,
388+
"4": 4n,
389+
"5": 5n,
390+
"6": 6n,
391+
"7": 18446744073709551615n,
392+
},
393393
},
394394
{
395395
"name": "float16_scalar",

packages/app/src/providers/h5grove/utils.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ import {
2323
timeType,
2424
unknownType,
2525
} from '@h5web/shared/hdf5-utils';
26-
import { type TypedArrayConstructor } from '@h5web/shared/vis-models';
26+
import {
27+
type BigIntTypedArrayConstructor,
28+
type TypedArrayConstructor,
29+
} from '@h5web/shared/vis-models';
2730

28-
import { typedArrayFromDType } from '../utils';
31+
import { bigIntTypedArrayFromDType, typedArrayFromDType } from '../utils';
2932
import {
3033
type H5GroveAttribute,
3134
type H5GroveEntity,
@@ -232,7 +235,7 @@ export function parseDType(type: H5GroveType): DType {
232235
*/
233236
export function h5groveTypedArrayFromDType(
234237
dtype: DType,
235-
): TypedArrayConstructor | undefined {
238+
): TypedArrayConstructor | BigIntTypedArrayConstructor | undefined {
236239
const { class: dtypeClass } = dtype;
237240

238241
if (dtypeClass === DTypeClass.Float && dtype.size === 16) {
@@ -243,5 +246,5 @@ export function h5groveTypedArrayFromDType(
243246
return Float64Array;
244247
}
245248

246-
return typedArrayFromDType(dtype);
249+
return typedArrayFromDType(dtype) || bigIntTypedArrayFromDType(dtype);
247250
}

packages/app/src/providers/utils.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
type ScalarShape,
1212
} from '@h5web/shared/hdf5-models';
1313
import { type OnProgress } from '@h5web/shared/react-suspense-fetch';
14-
import { type TypedArrayConstructor } from '@h5web/shared/vis-models';
14+
import {
15+
type BigIntTypedArrayConstructor,
16+
type TypedArrayConstructor,
17+
} from '@h5web/shared/vis-models';
1518
import { type AxiosProgressEvent, isAxiosError } from 'axios';
1619

1720
import { type DataProviderApi } from './api';
@@ -33,7 +36,7 @@ export function typedArrayFromDType(
3336
return Int16Array;
3437
case 32:
3538
return Int32Array;
36-
case 64: // No support for 64-bit integer values in JS
39+
case 64: // combine with `bigIntTypedArrayFromDType` when relevant
3740
return undefined;
3841
}
3942
}
@@ -46,14 +49,15 @@ export function typedArrayFromDType(
4649
return Uint16Array;
4750
case 32:
4851
return Uint32Array;
49-
case 64: // No support for 64-bit unsigned integer values in JS
52+
case 64: // combine with `bigIntTypedArrayFromDType` when relevant
5053
return undefined;
5154
}
5255
}
5356

5457
if (isFloatType(dtype)) {
5558
switch (dtype.size) {
56-
case 16: // No support for 16-bit floating values in JS
59+
case 16: // no support for 16-bit floating values in JS
60+
case 128: // no support for 128-bit floating values in JS
5761
return undefined;
5862
case 32:
5963
return Float32Array;
@@ -65,6 +69,16 @@ export function typedArrayFromDType(
6569
return undefined;
6670
}
6771

72+
export function bigIntTypedArrayFromDType(
73+
dtype: DType,
74+
): BigIntTypedArrayConstructor | undefined {
75+
if (!isIntegerType(dtype) || dtype.size !== 64) {
76+
return undefined;
77+
}
78+
79+
return dtype.signed ? BigInt64Array : BigUint64Array;
80+
}
81+
6882
export async function getValueOrError(
6983
api: DataProviderApi,
7084
dataset: Dataset<ArrayShape | ScalarShape>,

packages/app/src/vis-packs/core/hooks.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import { useMemo } from 'react';
1616
import { type DimensionMapping } from '../../dimension-mapper/models';
1717
import { isAxis } from '../../dimension-mapper/utils';
1818
import { useDataContext } from '../../providers/DataProvider';
19-
import { typedArrayFromDType } from '../../providers/utils';
19+
import {
20+
bigIntTypedArrayFromDType,
21+
typedArrayFromDType,
22+
} from '../../providers/utils';
2023
import {
2124
applyMapping,
2225
getBaseArray,
@@ -191,9 +194,12 @@ export function useIgnoreFillValue(dataset: Dataset): IgnoreValue | undefined {
191194
const rawFillValue = attrValuesStore.getSingle(dataset, '_FillValue');
192195

193196
return useMemo(() => {
194-
const DTypedArray = typedArrayFromDType(dataset.type);
195197
const wrappedFillValue = castArray(rawFillValue);
196198

199+
const DTypedArray = bigIntTypedArrayFromDType(dataset.type)
200+
? Float64Array // matches `useToNumArray` logic
201+
: typedArrayFromDType(dataset.type);
202+
197203
// Cast fillValue in the type of the dataset values to be able to use `===` for the comparison
198204
const fillValue =
199205
DTypedArray && typeof wrappedFillValue[0] === 'number'

packages/shared/src/vis-models.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export type TypedArrayConstructor =
2222
| Float64ArrayConstructor;
2323

2424
export type BigIntTypedArray = MaybeBigInt64Array | MaybeBigUint64Array;
25+
export type BigIntTypedArrayConstructor =
26+
| BigInt64ArrayConstructor
27+
| BigUint64ArrayConstructor;
2528

2629
export type Domain = [min: number, max: number];
2730
export type Axis = 'x' | 'y';

0 commit comments

Comments
 (0)