From 5926e0b98c798caa3fa3488866b28609bd84d62f Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Tue, 4 Feb 2025 16:15:47 +0100 Subject: [PATCH] Replace fixed-point notation with exact notation in Matrix and Compound vis --- apps/storybook/src/MatrixVis.stories.tsx | 2 +- .../app/src/vis-packs/core/matrix/utils.ts | 29 +++++++------------ .../app/src/vis-packs/core/scalar/utils.ts | 10 +++---- .../toolbar/controls/NotationToggleGroup.tsx | 2 +- packages/lib/src/vis/matrix/models.ts | 2 +- packages/shared/src/hdf5-models.ts | 4 +-- packages/shared/src/vis-utils.ts | 19 +++--------- 7 files changed, 24 insertions(+), 44 deletions(-) diff --git a/apps/storybook/src/MatrixVis.stories.tsx b/apps/storybook/src/MatrixVis.stories.tsx index 07efd4eca..d60e15de3 100644 --- a/apps/storybook/src/MatrixVis.stories.tsx +++ b/apps/storybook/src/MatrixVis.stories.tsx @@ -13,7 +13,7 @@ const typedTwoD = toTypedNdArray(twoD, Float32Array); const cplxTwoD = mockValues.twoD_cplx(); const formatNum = format('.3e'); -const formatCplx = createComplexFormatter('.2e', true); +const formatCplx = createComplexFormatter(format('.2e')); const meta = { title: 'Visualizations/MatrixVis', diff --git a/packages/app/src/vis-packs/core/matrix/utils.ts b/packages/app/src/vis-packs/core/matrix/utils.ts index ac7634e58..25cc61a09 100644 --- a/packages/app/src/vis-packs/core/matrix/utils.ts +++ b/packages/app/src/vis-packs/core/matrix/utils.ts @@ -7,7 +7,6 @@ import { isIntegerType, } from '@h5web/shared/guards'; import { - type ComplexType, type CompoundType, DTypeClass, type NumericType, @@ -37,8 +36,8 @@ export function createFloatFormatter( notation: Notation, ): (val: number) => string { switch (notation) { - case Notation.FixedPoint: - return format('.3f'); + case Notation.Exact: + return (val) => val.toString(); case Notation.Scientific: return format('.3e'); default: @@ -46,21 +45,10 @@ export function createFloatFormatter( } } -export function createMatrixComplexFormatter( +export function getFormatter( + type: T, notation: Notation, -): (val: ScalarValue) => string { - const formatStr = - notation === Notation.FixedPoint - ? '.2f' - : `.3~${notation === Notation.Scientific ? 'e' : 'g'}`; - - return createComplexFormatter(formatStr, true); -} - -export function getFormatter( - type: PrintableType, - notation: Notation, -): (val: ScalarValue) => string; // override distributivity of `ValueFormatter` +): (val: ScalarValue) => string; // override distributivity of `ValueFormatter` export function getFormatter( type: PrintableType, @@ -83,10 +71,13 @@ export function getFormatter( } if (isComplexType(type)) { - return createMatrixComplexFormatter(notation); + return createComplexFormatter( + getFormatter(type.realType, notation), + getFormatter(type.imagType, notation), + ); } - return (val: string) => val.toString(); // call `toString()` for safety, in case type cast is wrong + return (val: string) => val.toString(); } export function getCellWidth( diff --git a/packages/app/src/vis-packs/core/scalar/utils.ts b/packages/app/src/vis-packs/core/scalar/utils.ts index 159049558..fcc4aa4e6 100644 --- a/packages/app/src/vis-packs/core/scalar/utils.ts +++ b/packages/app/src/vis-packs/core/scalar/utils.ts @@ -7,9 +7,9 @@ import { } from '@h5web/shared/hdf5-models'; import { type ValueFormatter } from '@h5web/shared/vis-models'; import { + createComplexFormatter, createEnumFormatter, formatBool, - formatScalarComplex, } from '@h5web/shared/vis-utils'; export function getFormatter( @@ -19,10 +19,6 @@ export function getFormatter( export function getFormatter( dataset: Dataset, ): ValueFormatter { - if (hasComplexType(dataset)) { - return formatScalarComplex; - } - if (hasBoolType(dataset)) { return formatBool; } @@ -31,5 +27,9 @@ export function getFormatter( return createEnumFormatter(dataset.type.mapping); } + if (hasComplexType(dataset)) { + return createComplexFormatter((val) => val.toString()); + } + return (val: number | bigint | string) => val.toString(); } diff --git a/packages/lib/src/toolbar/controls/NotationToggleGroup.tsx b/packages/lib/src/toolbar/controls/NotationToggleGroup.tsx index 80909b518..4bc09361d 100644 --- a/packages/lib/src/toolbar/controls/NotationToggleGroup.tsx +++ b/packages/lib/src/toolbar/controls/NotationToggleGroup.tsx @@ -25,7 +25,7 @@ function NotationToggleGroup(props: Props) { > - + ); diff --git a/packages/lib/src/vis/matrix/models.ts b/packages/lib/src/vis/matrix/models.ts index b6e4da3ad..ae2cdc651 100644 --- a/packages/lib/src/vis/matrix/models.ts +++ b/packages/lib/src/vis/matrix/models.ts @@ -1,5 +1,5 @@ export enum Notation { Auto = 'Auto', Scientific = 'Scientific', - FixedPoint = 'Fixed-point', + Exact = 'Exact', } diff --git a/packages/shared/src/hdf5-models.ts b/packages/shared/src/hdf5-models.ts index f4f2b2e77..26fe68a6f 100644 --- a/packages/shared/src/hdf5-models.ts +++ b/packages/shared/src/hdf5-models.ts @@ -216,7 +216,7 @@ export interface UnknownType { export type ScalarValue = T extends NumericLikeType ? | number - | (T extends NumericType ? bigint : never) // let providers return bigints + | (T extends IntegerType ? bigint : never) // let providers return bigints | (T extends BooleanType ? boolean : never) // let providers return booleans : T extends StringType ? string @@ -230,7 +230,7 @@ export type ArrayValue = T extends NumericLikeType ? | TypedArray | number[] - | (T extends NumericType ? BigIntTypedArray | bigint[] : never) + | (T extends IntegerType ? BigIntTypedArray | bigint[] : never) | (T extends BooleanType ? boolean[] : never) // don't use `ScalarValue` to avoid `(number | boolean)[]` : ScalarValue[]; diff --git a/packages/shared/src/vis-utils.ts b/packages/shared/src/vis-utils.ts index a8273edd0..e85f9ddff 100644 --- a/packages/shared/src/vis-utils.ts +++ b/packages/shared/src/vis-utils.ts @@ -39,7 +39,7 @@ export const formatBound = format('.3~e'); export const formatBoundInput = format('.5~e'); export const formatTooltipVal = format('.5~g'); export const formatTooltipErr = format('.3~g'); -export const formatScalarComplex = createComplexFormatter('.12~g'); +export const formatScalarComplex = createComplexFormatter(format('.12~g')); const TICK_PRECISION = 3; const TICK_DECIMAL_REGEX = /0\.(\d+)$/u; // can start with minus sign @@ -64,24 +64,13 @@ export function formatBool(value: ScalarValue): string { } export function createComplexFormatter( - specifier: string, - full = false, + formatReal: (val: number) => string, + formatImag = formatReal, ): (val: ScalarValue) => string { - const formatVal = format(specifier); - return (value) => { const [real, imag] = value; - - if (imag === 0 && !full) { - return formatVal(real); - } - - if (real === 0 && !full) { - return `${formatVal(imag)} i`; - } - const sign = Math.sign(imag) >= 0 ? ' + ' : ' − '; - return `${formatVal(real)}${sign}${formatVal(Math.abs(imag))} i`; + return `${formatReal(real)}${sign}${formatImag(Math.abs(imag))} i`; }; }