Skip to content

Commit 76d9679

Browse files
authored
Merge pull request #1753 from silx-kit/exact-notation
Replace fixed-point with exact notation in Matrix and Compound vis
2 parents dcdcf3c + 10a964e commit 76d9679

File tree

9 files changed

+24
-44
lines changed

9 files changed

+24
-44
lines changed

apps/storybook/src/MatrixVis.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const typedTwoD = toTypedNdArray(twoD, Float32Array);
1313
const cplxTwoD = mockValues.twoD_cplx();
1414

1515
const formatNum = format('.3e');
16-
const formatCplx = createComplexFormatter('.2e', true);
16+
const formatCplx = createComplexFormatter(format('.2e'));
1717

1818
const meta = {
1919
title: 'Visualizations/MatrixVis',
-447 Bytes
Loading
-475 Bytes
Loading

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
isIntegerType,
88
} from '@h5web/shared/guards';
99
import {
10-
type ComplexType,
1110
type CompoundType,
1211
DTypeClass,
1312
type NumericType,
@@ -37,30 +36,19 @@ export function createFloatFormatter(
3736
notation: Notation,
3837
): (val: number) => string {
3938
switch (notation) {
40-
case Notation.FixedPoint:
41-
return format('.3f');
39+
case Notation.Exact:
40+
return (val) => val.toString();
4241
case Notation.Scientific:
4342
return format('.3e');
4443
default:
4544
return format('.5~g');
4645
}
4746
}
4847

49-
export function createMatrixComplexFormatter(
48+
export function getFormatter<T extends PrintableType>(
49+
type: T,
5050
notation: Notation,
51-
): (val: ScalarValue<ComplexType>) => string {
52-
const formatStr =
53-
notation === Notation.FixedPoint
54-
? '.2f'
55-
: `.3~${notation === Notation.Scientific ? 'e' : 'g'}`;
56-
57-
return createComplexFormatter(formatStr, true);
58-
}
59-
60-
export function getFormatter(
61-
type: PrintableType,
62-
notation: Notation,
63-
): (val: ScalarValue<PrintableType>) => string; // override distributivity of `ValueFormatter`
51+
): (val: ScalarValue<T>) => string; // override distributivity of `ValueFormatter`
6452

6553
export function getFormatter(
6654
type: PrintableType,
@@ -83,10 +71,13 @@ export function getFormatter(
8371
}
8472

8573
if (isComplexType(type)) {
86-
return createMatrixComplexFormatter(notation);
74+
return createComplexFormatter(
75+
getFormatter(type.realType, notation),
76+
getFormatter(type.imagType, notation),
77+
);
8778
}
8879

89-
return (val: string) => val.toString(); // call `toString()` for safety, in case type cast is wrong
80+
return (val: string) => val.toString();
9081
}
9182

9283
export function getCellWidth(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
} from '@h5web/shared/hdf5-models';
88
import { type ValueFormatter } from '@h5web/shared/vis-models';
99
import {
10+
createComplexFormatter,
1011
createEnumFormatter,
1112
formatBool,
12-
formatScalarComplex,
1313
} from '@h5web/shared/vis-utils';
1414

1515
export function getFormatter(
@@ -19,10 +19,6 @@ export function getFormatter(
1919
export function getFormatter<T extends PrintableType>(
2020
dataset: Dataset<ArrayShape, T>,
2121
): ValueFormatter<PrintableType> {
22-
if (hasComplexType(dataset)) {
23-
return formatScalarComplex;
24-
}
25-
2622
if (hasBoolType(dataset)) {
2723
return formatBool;
2824
}
@@ -31,5 +27,9 @@ export function getFormatter<T extends PrintableType>(
3127
return createEnumFormatter(dataset.type.mapping);
3228
}
3329

30+
if (hasComplexType(dataset)) {
31+
return createComplexFormatter((val) => val.toString());
32+
}
33+
3434
return (val: number | bigint | string) => val.toString();
3535
}

packages/lib/src/toolbar/controls/NotationToggleGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function NotationToggleGroup(props: Props) {
2525
>
2626
<ToggleGroup.Btn label="Auto" value={Notation.Auto} />
2727
<ToggleGroup.Btn label="Scientific" value={Notation.Scientific} />
28-
<ToggleGroup.Btn label="Fixed-point" value={Notation.FixedPoint} />
28+
<ToggleGroup.Btn label="Exact" value={Notation.Exact} />
2929
</ToggleGroup>
3030
</div>
3131
);

packages/lib/src/vis/matrix/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum Notation {
22
Auto = 'Auto',
33
Scientific = 'Scientific',
4-
FixedPoint = 'Fixed-point',
4+
Exact = 'Exact',
55
}

packages/shared/src/hdf5-models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export interface UnknownType {
216216
export type ScalarValue<T extends DType = DType> = T extends NumericLikeType
217217
?
218218
| number
219-
| (T extends NumericType ? bigint : never) // let providers return bigints
219+
| (T extends IntegerType ? bigint : never) // let providers return bigints
220220
| (T extends BooleanType ? boolean : never) // let providers return booleans
221221
: T extends StringType
222222
? string
@@ -230,7 +230,7 @@ export type ArrayValue<T extends DType = DType> = T extends NumericLikeType
230230
?
231231
| TypedArray
232232
| number[]
233-
| (T extends NumericType ? BigIntTypedArray | bigint[] : never)
233+
| (T extends IntegerType ? BigIntTypedArray | bigint[] : never)
234234
| (T extends BooleanType ? boolean[] : never) // don't use `ScalarValue` to avoid `(number | boolean)[]`
235235
: ScalarValue<T>[];
236236

packages/shared/src/vis-utils.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const formatBound = format('.3~e');
3939
export const formatBoundInput = format('.5~e');
4040
export const formatTooltipVal = format('.5~g');
4141
export const formatTooltipErr = format('.3~g');
42-
export const formatScalarComplex = createComplexFormatter('.12~g');
42+
export const formatScalarComplex = createComplexFormatter(format('.12~g'));
4343

4444
const TICK_PRECISION = 3;
4545
const TICK_DECIMAL_REGEX = /0\.(\d+)$/u; // can start with minus sign
@@ -64,24 +64,13 @@ export function formatBool(value: ScalarValue<BooleanType>): string {
6464
}
6565

6666
export function createComplexFormatter(
67-
specifier: string,
68-
full = false,
67+
formatReal: (val: number) => string,
68+
formatImag = formatReal,
6969
): (val: ScalarValue<ComplexType>) => string {
70-
const formatVal = format(specifier);
71-
7270
return (value) => {
7371
const [real, imag] = value;
74-
75-
if (imag === 0 && !full) {
76-
return formatVal(real);
77-
}
78-
79-
if (real === 0 && !full) {
80-
return `${formatVal(imag)} i`;
81-
}
82-
8372
const sign = Math.sign(imag) >= 0 ? ' + ' : ' − ';
84-
return `${formatVal(real)}${sign}${formatVal(Math.abs(imag))} i`;
73+
return `${formatReal(real)}${sign}${formatImag(Math.abs(imag))} i`;
8574
};
8675
}
8776

0 commit comments

Comments
 (0)