Skip to content

Commit

Permalink
Merge pull request #1745 from silx-kit/bigint-new
Browse files Browse the repository at this point in the history
Allow providers to return bigints
  • Loading branch information
axelboc authored Feb 3, 2025
2 parents 02f6f73 + 0748093 commit a31fc50
Show file tree
Hide file tree
Showing 34 changed files with 395 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"typescript.preferences.importModuleSpecifier": "relative",
"typescript.preferences.preferTypeOnlyAutoImports": true,
"typescript.preferences.autoImportSpecifierExcludeRegexes": [
"^(node:)?(console|util)$",
"^(node:)?(console|util|test)$",
"^three/.*$"
],

Expand Down
Binary file modified cypress/snapshots/app.cy.ts/auximage.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/auxspectrum.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/heatmap_flip.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/logspectrum.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nximage.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nximage_complex_2d.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nxline.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nxline_complex_2d_aux.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nxrgb.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/snapshots/app.cy.ts/nxscatter.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export function makeMockFile(): GroupWithChildren {
group('typed_arrays', [
array('uint8', { type: intType(false, 8) }),
array('int16', { type: intType(true, 16) }),
array('int64', { type: intType(true, 64) }),
array('float32', { type: floatType(32) }),
array('float64', { type: floatType(64) }),
withImageAttr(array('uint8_rgb', { type: intType(false, 8) })),
Expand Down Expand Up @@ -275,6 +276,13 @@ export function makeMockFile(): GroupWithChildren {
axes: { X: array('X'), Y_scatter: array('Y_scatter') },
axesAttr: ['X', 'Y_scatter'],
}),
nxData('bigint', {
signal: array('twoD_bigint'),
auxiliary: { secondary_bigint: array('secondary_bigint') },
auxAttr: ['secondary_bigint'],
axes: { X_bigint: array('X_bigint') },
axesAttr: ['.', 'X_bigint'],
}),
nxGroup('old-style', 'NXdata', {
children: [
array('twoD', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
import visualizerStyles from '../../../visualizer/Visualizer.module.css';
import { useToNumArrays } from '../hooks';
import { type LineConfig } from '../line/config';
import { DEFAULT_DOMAIN } from '../utils';
import ComplexLineToolbar from './ComplexLineToolbar';
Expand Down Expand Up @@ -58,6 +59,7 @@ function MappedComplexLineVis(props: Props) {
const { customDomain, yScaleType, xScaleType, curveType, showGrid } =
lineConfig;

const numAxisArrays = useToNumArrays(axisValues);
const [dataArray, ...auxArrays] = useMappedComplexArrays(
[value, ...auxValues],
dims,
Expand Down Expand Up @@ -99,7 +101,7 @@ function MappedComplexLineVis(props: Props) {
showGrid={showGrid}
abscissaParams={{
label: axisLabels[xDimIndex],
value: axisValues[xDimIndex],
value: numAxisArrays[xDimIndex],
scaleType: xScaleType,
}}
ordinateLabel={ordinateLabel}
Expand Down
8 changes: 5 additions & 3 deletions packages/app/src/vis-packs/core/complex/MappedComplexVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
useBaseArray,
useMappedArray,
useSlicedDimsAndMapping,
useToNumArrays,
} from '../hooks';
import { DEFAULT_DOMAIN } from '../utils';
import ComplexToolbar from './ComplexToolbar';
Expand Down Expand Up @@ -62,8 +63,9 @@ function MappedComplexVis(props: Props) {
invertColorMap,
} = heatmapConfig;

const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
const numAxisArrays = useToNumArrays(axisValues);

const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
const mappedArray = useMappedArray(value, slicedDims, slicedMapping);

const { phaseValues, phaseBounds, amplitudeValues, amplitudeBounds } =
Expand Down Expand Up @@ -112,11 +114,11 @@ function MappedComplexVis(props: Props) {
invertColorMap={invertColorMap}
abscissaParams={{
label: axisLabels[xDimIndex],
value: axisValues[xDimIndex],
value: numAxisArrays[xDimIndex],
}}
ordinateParams={{
label: axisLabels[yDimIndex],
value: axisValues[yDimIndex],
value: numAxisArrays[yDimIndex],
}}
alpha={
visType === ComplexVisType.PhaseAmplitude
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/vis-packs/core/heatmap/MappedHeatmapVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
useMappedArray,
useSlicedDimsAndMapping,
useToNumArray,
useToNumArrays,
} from '../hooks';
import { DEFAULT_DOMAIN, formatNumLikeType, getSliceSelection } from '../utils';
import { type HeatmapConfig } from './config';
Expand Down Expand Up @@ -64,6 +65,7 @@ function MappedHeatmapVis(props: Props) {
} = config;

const numArray = useToNumArray(value);
const numAxisArrays = useToNumArrays(axisValues);

const { shape: dims } = dataset;
const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
Expand Down Expand Up @@ -109,11 +111,11 @@ function MappedHeatmapVis(props: Props) {
invertColorMap={invertColorMap}
abscissaParams={{
label: axisLabels[xDimIndex],
value: axisValues[xDimIndex],
value: numAxisArrays[xDimIndex],
}}
ordinateParams={{
label: axisLabels[yDimIndex],
value: axisValues[yDimIndex],
value: numAxisArrays[yDimIndex],
}}
flipXAxis={flipXAxis}
flipYAxis={flipYAxis}
Expand Down
10 changes: 9 additions & 1 deletion packages/app/src/vis-packs/core/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ export const useToNumArray = createMemo(toNumArray);

export function useToNumArrays(
arrays: ArrayValue<NumericLikeType>[],
): NumArray[] {
): NumArray[];

export function useToNumArrays(
arrays: (ArrayValue<NumericLikeType> | undefined)[],
): (NumArray | undefined)[];

export function useToNumArrays(
arrays: (ArrayValue<NumericLikeType> | undefined)[],
): (NumArray | undefined)[] {
return useMemo(() => arrays.map(toNumArray), arrays); // eslint-disable-line react-hooks/exhaustive-deps
}

Expand Down
9 changes: 6 additions & 3 deletions packages/app/src/vis-packs/core/line/MappedLineVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ function MappedLineVis(props: Props) {

const numArray = useToNumArray(value);
const numAuxArrays = useToNumArrays(auxValues);
const numErrorsArray = useToNumArray(errors);
const numAuxErrorsArrays = useToNumArrays(auxErrors);
const numAxisArrays = useToNumArrays(axisValues);

const dataArray = useMappedArray(numArray, ...hookArgs);
const errorsArray = useMappedArray(errors, ...hookArgs);
const errorsArray = useMappedArray(numErrorsArray, ...hookArgs);
const auxArrays = useMappedArrays(numAuxArrays, ...hookArgs);
const auxErrorsArrays = useMappedArrays(auxErrors, ...hookArgs);
const auxErrorsArrays = useMappedArrays(numAuxErrorsArrays, ...hookArgs);

const dataDomain = useDomain(
dataArray,
Expand Down Expand Up @@ -132,7 +135,7 @@ function MappedLineVis(props: Props) {
showGrid={showGrid}
abscissaParams={{
label: axisLabels[xDimIndex],
value: axisValues[xDimIndex],
value: numAxisArrays[xDimIndex],
scaleType: xScaleType,
}}
ordinateLabel={valueLabel}
Expand Down
24 changes: 20 additions & 4 deletions packages/app/src/vis-packs/core/matrix/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
isBoolType,
isComplexType,
isEnumType,
isNumericType,
isFloatType,
isIntegerType,
} from '@h5web/shared/guards';
import {
type ComplexType,
Expand All @@ -21,9 +22,20 @@ import {
} from '@h5web/shared/vis-utils';
import { format } from 'd3-format';

export function createNumericFormatter(
export function createIntegerFormatter(
notation: Notation,
): (val: ScalarValue<NumericType>) => string {
if (notation === Notation.Scientific) {
const formatter = format('.3e');
return (val) => formatter(Number(val));
}

return (val) => val.toString();
}

export function createFloatFormatter(
notation: Notation,
): (val: number) => string {
switch (notation) {
case Notation.FixedPoint:
return format('.3f');
Expand Down Expand Up @@ -54,8 +66,12 @@ export function getFormatter(
type: PrintableType,
notation: Notation,
): ValueFormatter<PrintableType> {
if (isNumericType(type)) {
return createNumericFormatter(notation);
if (isIntegerType(type)) {
return createIntegerFormatter(notation);
}

if (isFloatType(type)) {
return createFloatFormatter(notation);
}

if (isBoolType(type)) {
Expand Down
17 changes: 13 additions & 4 deletions packages/app/src/vis-packs/core/rgb/MappedRgbVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
import visualizerStyles from '../../../visualizer/Visualizer.module.css';
import { useMappedArray, useSlicedDimsAndMapping } from '../hooks';
import {
useMappedArray,
useSlicedDimsAndMapping,
useToNumArray,
useToNumArrays,
} from '../hooks';
import { type RgbVisConfig } from './config';
import RgbToolbar from './RgbToolbar';

Expand Down Expand Up @@ -40,8 +45,12 @@ function MappedRgbVis(props: Props) {
const { showGrid, keepRatio, imageType, flipXAxis, flipYAxis } = config;
const { shape: dims } = dataset;

const numAxisArrays = useToNumArrays(axisValues);

const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
const dataArray = useMappedArray(value, slicedDims, slicedMapping);

const numArray = useToNumArray(value);
const dataArray = useMappedArray(numArray, slicedDims, slicedMapping);

const xDimIndex = dimMapping.indexOf('x');
const yDimIndex = dimMapping.indexOf('y');
Expand All @@ -59,11 +68,11 @@ function MappedRgbVis(props: Props) {
imageType={imageType}
abscissaParams={{
label: axisLabels[xDimIndex],
value: axisValues[xDimIndex],
value: numAxisArrays[xDimIndex],
}}
ordinateParams={{
label: axisLabels[yDimIndex],
value: axisValues[yDimIndex],
value: numAxisArrays[yDimIndex],
}}
flipXAxis={flipXAxis}
flipYAxis={flipYAxis}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/vis-packs/core/scalar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export function getFormatter<T extends PrintableType>(
return createEnumFormatter(dataset.type.mapping);
}

return (val: number | string) => val.toString();
return (val: number | bigint | string) => val.toString();
}
9 changes: 6 additions & 3 deletions packages/app/src/vis-packs/core/scatter/MappedScatterVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type AxisMapping } from '@h5web/shared/nexus-models';
import { createPortal } from 'react-dom';

import visualizerStyles from '../../../visualizer/Visualizer.module.css';
import { useBaseArray } from '../hooks';
import { useBaseArray, useToNumArray, useToNumArrays } from '../hooks';
import { DEFAULT_DOMAIN } from '../utils';
import { type ScatterConfig } from './config';
import ScatterToolbar from './ScatterToolbar';
Expand Down Expand Up @@ -33,13 +33,16 @@ function MappedScatterVis(props: Props) {
yScaleType,
} = config;

const dataArray = useBaseArray(value, [value.length]);
const numArray = useToNumArray(value);
const numAxisArrays = useToNumArrays(axisValues);

const dataArray = useBaseArray(numArray, [value.length]);
const dataDomain = useDomain(dataArray, scaleType) || DEFAULT_DOMAIN;
const visDomain = useVisDomain(customDomain, dataDomain);
const [safeDomain] = useSafeDomain(visDomain, dataDomain, scaleType);

const [xLabel, yLabel] = axisLabels;
const [xValue, yValue] = axisValues;
const [xValue, yValue] = numAxisArrays;
assertDefined(xValue);
assertDefined(yValue);

Expand Down
10 changes: 8 additions & 2 deletions packages/app/src/vis-packs/core/surface/MappedSurfaceVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
import visualizerStyles from '../../../visualizer/Visualizer.module.css';
import { useMappedArray, useSlicedDimsAndMapping } from '../hooks';
import {
useMappedArray,
useSlicedDimsAndMapping,
useToNumArray,
} from '../hooks';
import { DEFAULT_DOMAIN } from '../utils';
import { type SurfaceConfig } from './config';
import SurfaceToolbar from './SurfaceToolbar';
Expand All @@ -29,7 +33,9 @@ function MappedSurfaceVis(props: Props) {

const { shape: dims } = dataset;
const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
const dataArray = useMappedArray(value, slicedDims, slicedMapping);

const numArray = useToNumArray(value);
const dataArray = useMappedArray(numArray, slicedDims, slicedMapping);

const dataDomain = useDomain(dataArray, scaleType) || DEFAULT_DOMAIN;
const visDomain = useVisDomain(customDomain, dataDomain);
Expand Down
30 changes: 28 additions & 2 deletions packages/app/src/vis-packs/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { type InteractionInfo } from '@h5web/lib';
import { isIntegerType, isNumericType } from '@h5web/shared/guards';
import {
isBigIntTypedArray,
isIntegerType,
isNumericType,
} from '@h5web/shared/guards';
import {
type ArrayValue,
DTypeClass,
Expand Down Expand Up @@ -93,11 +97,33 @@ export function getImageInteractions(keepRatio: boolean): InteractionInfo[] {
return keepRatio ? BASE_INTERACTIONS : INTERACTIONS_WITH_AXIAL_ZOOM;
}

function isBigIntArray(val: ArrayValue<NumericLikeType>): val is bigint[] {
return Array.isArray(val) && typeof val[0] === 'bigint';
}

function isBoolArray(val: ArrayValue<NumericLikeType>): val is boolean[] {
return Array.isArray(val) && typeof val[0] === 'boolean';
}

export function toNumArray(arr: ArrayValue<NumericLikeType>): NumArray {
export function toNumArray<T extends ArrayValue<NumericLikeType> | undefined>(
arr: T,
): T extends ArrayValue<NumericLikeType> ? NumArray : undefined;

export function toNumArray(
arr: ArrayValue<NumericLikeType> | undefined,
): NumArray | undefined {
if (!arr) {
return undefined;
}

if (isBigIntTypedArray(arr)) {
return Float64Array.from(arr, Number); // cast to float 64
}

if (isBigIntArray(arr)) {
return arr.map(Number); // cast to float 64
}

if (isBoolArray(arr)) {
return arr.map((val) => (val ? 1 : 0));
}
Expand Down
5 changes: 5 additions & 0 deletions packages/app/src/vis-packs/core/visualizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ const mockStore = {

const scalarInt = dataset('int', intType(), []);
const scalarUint = dataset('uint', intType(false), []);
const scalarBigInt = dataset('bigint', intType(true, 64), []);
const scalarFloat = dataset('float', floatType(), []);
const scalarStr = dataset('float', strType(), []);
const scalarBool = dataset('bool', boolType(intType(true, 8)), []);
const scalarCplx = dataset('cplx', cplxType(floatType()), []);
const scalarCompound = dataset('comp', compoundType({ int: intType() }), []);
const oneDInt = dataset('int_1d', intType(), [5]);
const oneDUint = dataset('uint_1d', intType(false), [5]);
const oneDBigUint = dataset('biguint_1d', intType(false, 64), [5]);
const oneDBool = dataset('bool_1d', boolType(intType(true, 8)), [3]);
const oneDCplx = dataset('cplx_1d', cplxType(floatType()), [10]);
const oneDCompound = dataset('comp_1d', compoundType({ int: intType() }), [5]);
Expand Down Expand Up @@ -82,6 +84,7 @@ describe('Scalar', () => {
it('should support dataset with printable type and scalar shape', () => {
expect(supportsDataset(scalarInt)).toBe(true);
expect(supportsDataset(scalarUint)).toBe(true);
expect(supportsDataset(scalarBigInt)).toBe(true);
expect(supportsDataset(scalarFloat)).toBe(true);
expect(supportsDataset(scalarStr)).toBe(true);
expect(supportsDataset(scalarBool)).toBe(true);
Expand All @@ -103,6 +106,7 @@ describe('Matrix', () => {
it('should support array dataset with printable type and at least one dimension', () => {
expect(supportsDataset(oneDInt)).toBe(true);
expect(supportsDataset(oneDUint)).toBe(true);
expect(supportsDataset(oneDBigUint)).toBe(true);
expect(supportsDataset(twoDStr)).toBe(true);
expect(supportsDataset(twoDCplx)).toBe(true);
expect(supportsDataset(threeDFloat)).toBe(true);
Expand All @@ -124,6 +128,7 @@ describe('Line', () => {
it('should support array dataset with numeric-like type and at least one dimension', () => {
expect(supportsDataset(oneDInt)).toBe(true);
expect(supportsDataset(oneDUint)).toBe(true);
expect(supportsDataset(oneDBigUint)).toBe(true);
expect(supportsDataset(oneDBool)).toBe(true);
expect(supportsDataset(twoDBool)).toBe(true);
expect(supportsDataset(threeDFloat)).toBe(true);
Expand Down
Loading

0 comments on commit a31fc50

Please sign in to comment.