Skip to content

Commit d529403

Browse files
authored
Merge pull request #1743 from silx-kit/bigint-prep
Assert values of enum datasets and other tweaks
2 parents 34e0a67 + fc10e8e commit d529403

File tree

17 files changed

+123
-121
lines changed

17 files changed

+123
-121
lines changed

apps/storybook/src/HeatmapVisDisplay.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { type Meta, type StoryFn, type StoryObj } from '@storybook/react';
1010
import HeatmapVisStoriesMeta from './HeatmapVis.stories';
1111

1212
const { dataArray } = HeatmapVisStoriesMeta.args;
13-
const asymTwoD = mockValues.twoDAsym();
13+
const asymTwoD = mockValues.twoD_asym();
1414

1515
const meta = {
1616
...HeatmapVisStoriesMeta,

packages/app/src/metadata-viewer/utils.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2+
isFloatType,
23
isH5WebComplex,
34
isIntegerType,
4-
isNumericType,
55
isScalarShape,
66
} from '@h5web/shared/guards';
77
import {
@@ -28,17 +28,15 @@ export function renderShape(shape: Shape): string {
2828
}
2929

3030
export function renderType(type: DType): string {
31-
if (isNumericType(type)) {
32-
const { endianness, size } = type;
33-
34-
const endiannessStr = endianness ? `, ${endianness}` : '';
35-
const signStr = isIntegerType(type)
36-
? type.signed
37-
? ' (signed)'
38-
: ' (unsigned)'
39-
: '';
31+
if (isIntegerType(type)) {
32+
const sign = type.signed ? ' (signed)' : ' (unsigned)';
33+
const endianness = type.endianness ? `, ${type.endianness}` : '';
34+
return `Integer${sign}, ${type.size}-bit${endianness}`;
35+
}
4036

41-
return `${type.class}${signStr}, ${size}-bit${endiannessStr}`;
37+
if (isFloatType(type)) {
38+
const endianness = type.endianness ? `, ${type.endianness}` : '';
39+
return `Float, ${type.size}-bit${endianness}`;
4240
}
4341

4442
if (type.class === DTypeClass.String) {

packages/app/src/vis-packs/core/complex/MappedComplexLineVis.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function MappedComplexLineVis(props: Props) {
4646
auxValues = [],
4747
dims,
4848
dimMapping,
49-
axisLabels,
50-
axisValues,
49+
axisLabels = [],
50+
axisValues = [],
5151
title,
5252
toolbarContainer,
5353
config,
@@ -98,8 +98,8 @@ function MappedComplexLineVis(props: Props) {
9898
curveType={curveType}
9999
showGrid={showGrid}
100100
abscissaParams={{
101-
label: axisLabels?.[xDimIndex],
102-
value: axisValues?.[xDimIndex],
101+
label: axisLabels[xDimIndex],
102+
value: axisValues[xDimIndex],
103103
scaleType: xScaleType,
104104
}}
105105
ordinateLabel={ordinateLabel}

packages/app/src/vis-packs/core/complex/MappedComplexVis.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function MappedComplexVis(props: Props) {
4444
value,
4545
dims,
4646
dimMapping,
47-
axisLabels,
48-
axisValues,
47+
axisLabels = [],
48+
axisValues = [],
4949
title,
5050
toolbarContainer,
5151
config,
@@ -111,12 +111,12 @@ function MappedComplexVis(props: Props) {
111111
showGrid={showGrid}
112112
invertColorMap={invertColorMap}
113113
abscissaParams={{
114-
label: axisLabels?.[xDimIndex],
115-
value: axisValues?.[xDimIndex],
114+
label: axisLabels[xDimIndex],
115+
value: axisValues[xDimIndex],
116116
}}
117117
ordinateParams={{
118-
label: axisLabels?.[yDimIndex],
119-
value: axisValues?.[yDimIndex],
118+
label: axisLabels[yDimIndex],
119+
value: axisValues[yDimIndex],
120120
}}
121121
alpha={
122122
visType === ComplexVisType.PhaseAmplitude

packages/app/src/vis-packs/core/heatmap/MappedHeatmapVis.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function MappedHeatmapVis(props: Props) {
3838
dataset,
3939
value,
4040
dimMapping,
41-
axisLabels,
42-
axisValues,
41+
axisLabels = [],
42+
axisValues = [],
4343
title,
4444
toolbarContainer,
4545
config,
@@ -57,8 +57,9 @@ function MappedHeatmapVis(props: Props) {
5757
flipYAxis,
5858
} = config;
5959

60-
const { shape: dims } = dataset;
6160
const numArray = useToNumArray(value);
61+
62+
const { shape: dims } = dataset;
6263
const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
6364
const dataArray = useMappedArray(numArray, slicedDims, slicedMapping);
6465

@@ -101,12 +102,12 @@ function MappedHeatmapVis(props: Props) {
101102
showGrid={showGrid}
102103
invertColorMap={invertColorMap}
103104
abscissaParams={{
104-
label: axisLabels?.[xDimIndex],
105-
value: axisValues?.[xDimIndex],
105+
label: axisLabels[xDimIndex],
106+
value: axisValues[xDimIndex],
106107
}}
107108
ordinateParams={{
108-
label: axisLabels?.[yDimIndex],
109-
value: axisValues?.[yDimIndex],
109+
label: axisLabels[yDimIndex],
110+
value: axisValues[yDimIndex],
110111
}}
111112
flipXAxis={flipXAxis}
112113
flipYAxis={flipYAxis}

packages/app/src/vis-packs/core/line/LineVisContainer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function LineVisContainer(props: VisContainerProps) {
4343
<MappedLineVis
4444
dataset={entity}
4545
value={value}
46-
dims={dims}
4746
dimMapping={dimMapping}
4847
title={entity.name}
4948
toolbarContainer={toolbarContainer}

packages/app/src/vis-packs/core/line/MappedLineVis.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ interface Props {
3838
auxLabels?: string[];
3939
auxValues?: ArrayValue<NumericLikeType>[];
4040
auxErrors?: (ArrayValue<NumericType> | undefined)[];
41-
dims: number[];
4241
dimMapping: DimensionMapping;
4342
axisLabels?: AxisMapping<string>;
4443
axisValues?: AxisMapping<ArrayValue<NumericType>>;
@@ -57,10 +56,9 @@ function MappedLineVis(props: Props) {
5756
auxLabels = [],
5857
auxValues = [],
5958
auxErrors = [],
60-
dims,
6159
dimMapping,
62-
axisLabels,
63-
axisValues,
60+
axisLabels = [],
61+
axisValues = [],
6462
title,
6563
toolbarContainer,
6664
config,
@@ -76,20 +74,22 @@ function MappedLineVis(props: Props) {
7674
showErrors,
7775
} = config;
7876

77+
const { shape: dims } = dataset;
78+
const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
79+
const hookArgs = [slicedDims, slicedMapping] as const;
80+
7981
const numArray = useToNumArray(value);
8082
const numAuxArrays = useToNumArrays(auxValues);
81-
const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
8283

83-
const hookArgs = [slicedDims, slicedMapping] as const;
8484
const dataArray = useMappedArray(numArray, ...hookArgs);
85-
const errorArray = useMappedArray(errors, ...hookArgs);
85+
const errorsArray = useMappedArray(errors, ...hookArgs);
8686
const auxArrays = useMappedArrays(numAuxArrays, ...hookArgs);
8787
const auxErrorsArrays = useMappedArrays(auxErrors, ...hookArgs);
8888

8989
const dataDomain = useDomain(
9090
dataArray,
9191
yScaleType,
92-
showErrors ? errorArray : undefined,
92+
showErrors ? errorsArray : undefined,
9393
ignoreValue,
9494
);
9595

@@ -130,14 +130,14 @@ function MappedLineVis(props: Props) {
130130
curveType={curveType}
131131
showGrid={showGrid}
132132
abscissaParams={{
133-
label: axisLabels?.[xDimIndex],
134-
value: axisValues?.[xDimIndex],
133+
label: axisLabels[xDimIndex],
134+
value: axisValues[xDimIndex],
135135
scaleType: xScaleType,
136136
}}
137137
ordinateLabel={valueLabel}
138138
title={title}
139139
dtype={formatNumLikeType(dataset.type)}
140-
errorsArray={errorArray}
140+
errorsArray={errorsArray}
141141
showErrors={showErrors}
142142
auxiliaries={auxArrays.map((array, i) => ({
143143
label: auxLabels[i],

packages/app/src/vis-packs/core/matrix/utils.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
isNumericType,
77
} from '@h5web/shared/guards';
88
import {
9-
type BooleanType,
109
type ComplexType,
1110
type CompoundType,
1211
DTypeClass,
1312
type NumericType,
1413
type PrintableType,
14+
type ScalarValue,
1515
} from '@h5web/shared/hdf5-models';
1616
import { type ValueFormatter } from '@h5web/shared/vis-models';
1717
import {
@@ -23,7 +23,7 @@ import { format } from 'd3-format';
2323

2424
export function createNumericFormatter(
2525
notation: Notation,
26-
): ValueFormatter<NumericType> {
26+
): (val: ScalarValue<NumericType>) => string {
2727
switch (notation) {
2828
case Notation.FixedPoint:
2929
return format('.3f');
@@ -36,7 +36,7 @@ export function createNumericFormatter(
3636

3737
export function createMatrixComplexFormatter(
3838
notation: Notation,
39-
): ValueFormatter<ComplexType> {
39+
): (val: ScalarValue<ComplexType>) => string {
4040
const formatStr =
4141
notation === Notation.FixedPoint
4242
? '.2f'
@@ -48,24 +48,29 @@ export function createMatrixComplexFormatter(
4848
export function getFormatter(
4949
type: PrintableType,
5050
notation: Notation,
51-
): ValueFormatter<PrintableType> {
52-
if (isComplexType(type)) {
53-
return createMatrixComplexFormatter(notation);
54-
}
51+
): (val: ScalarValue<PrintableType>) => string; // override distributivity of `ValueFormatter`
5552

53+
export function getFormatter(
54+
type: PrintableType,
55+
notation: Notation,
56+
): ValueFormatter<PrintableType> {
5657
if (isNumericType(type)) {
5758
return createNumericFormatter(notation);
5859
}
5960

6061
if (isBoolType(type)) {
61-
return formatBool as ValueFormatter<BooleanType>;
62+
return formatBool;
6263
}
6364

6465
if (isEnumType(type)) {
6566
return createEnumFormatter(type.mapping);
6667
}
6768

68-
return (val) => (val as string).toString(); // call `toString()` for safety, in case type cast is wrong
69+
if (isComplexType(type)) {
70+
return createMatrixComplexFormatter(notation);
71+
}
72+
73+
return (val: string) => val.toString(); // call `toString()` for safety, in case type cast is wrong
6974
}
7075

7176
export function getCellWidth(

packages/app/src/vis-packs/core/rgb/MappedRgbVis.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function MappedRgbVis(props: Props) {
2929
const {
3030
dataset,
3131
value,
32-
axisLabels,
33-
axisValues,
32+
axisLabels = [],
33+
axisValues = [],
3434
dimMapping,
3535
title,
3636
toolbarContainer,
@@ -58,12 +58,12 @@ function MappedRgbVis(props: Props) {
5858
aspect={keepRatio ? 'equal' : 'auto'}
5959
imageType={imageType}
6060
abscissaParams={{
61-
label: axisLabels?.[xDimIndex],
62-
value: axisValues?.[xDimIndex],
61+
label: axisLabels[xDimIndex],
62+
value: axisValues[xDimIndex],
6363
}}
6464
ordinateParams={{
65-
label: axisLabels?.[yDimIndex],
66-
value: axisValues?.[yDimIndex],
65+
label: axisLabels[yDimIndex],
66+
value: axisValues[yDimIndex],
6767
}}
6868
flipXAxis={flipXAxis}
6969
flipYAxis={flipYAxis}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { hasBoolType, hasComplexType, hasEnumType } from '@h5web/shared/guards';
22
import {
33
type ArrayShape,
4-
type BooleanType,
54
type Dataset,
65
type PrintableType,
6+
type ScalarValue,
77
} from '@h5web/shared/hdf5-models';
88
import { type ValueFormatter } from '@h5web/shared/vis-models';
99
import {
@@ -14,18 +14,22 @@ import {
1414

1515
export function getFormatter(
1616
dataset: Dataset<ArrayShape, PrintableType>,
17+
): (val: ScalarValue<PrintableType>) => string; // override distributivity of `ValueFormatter`
18+
19+
export function getFormatter<T extends PrintableType>(
20+
dataset: Dataset<ArrayShape, T>,
1721
): ValueFormatter<PrintableType> {
1822
if (hasComplexType(dataset)) {
1923
return formatScalarComplex;
2024
}
2125

2226
if (hasBoolType(dataset)) {
23-
return formatBool as ValueFormatter<BooleanType>;
27+
return formatBool;
2428
}
2529

2630
if (hasEnumType(dataset)) {
2731
return createEnumFormatter(dataset.type.mapping);
2832
}
2933

30-
return (val) => (val as number | string).toString();
34+
return (val: number | string) => val.toString();
3135
}

0 commit comments

Comments
 (0)