Skip to content

Commit

Permalink
Merge pull request #1753 from silx-kit/exact-notation
Browse files Browse the repository at this point in the history
Replace fixed-point with exact notation in Matrix and Compound vis
  • Loading branch information
axelboc authored Feb 5, 2025
2 parents dcdcf3c + 10a964e commit 76d9679
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 44 deletions.
2 changes: 1 addition & 1 deletion apps/storybook/src/MatrixVis.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Binary file modified cypress/snapshots/app.cy.ts/compound_1D.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/matrix_1D.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 10 additions & 19 deletions packages/app/src/vis-packs/core/matrix/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
isIntegerType,
} from '@h5web/shared/guards';
import {
type ComplexType,
type CompoundType,
DTypeClass,
type NumericType,
Expand Down Expand Up @@ -37,30 +36,19 @@ 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:
return format('.5~g');
}
}

export function createMatrixComplexFormatter(
export function getFormatter<T extends PrintableType>(
type: T,
notation: Notation,
): (val: ScalarValue<ComplexType>) => 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<PrintableType>) => string; // override distributivity of `ValueFormatter`
): (val: ScalarValue<T>) => string; // override distributivity of `ValueFormatter`

export function getFormatter(
type: PrintableType,
Expand All @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions packages/app/src/vis-packs/core/scalar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -19,10 +19,6 @@ export function getFormatter(
export function getFormatter<T extends PrintableType>(
dataset: Dataset<ArrayShape, T>,
): ValueFormatter<PrintableType> {
if (hasComplexType(dataset)) {
return formatScalarComplex;
}

if (hasBoolType(dataset)) {
return formatBool;
}
Expand All @@ -31,5 +27,9 @@ export function getFormatter<T extends PrintableType>(
return createEnumFormatter(dataset.type.mapping);
}

if (hasComplexType(dataset)) {
return createComplexFormatter((val) => val.toString());
}

return (val: number | bigint | string) => val.toString();
}
2 changes: 1 addition & 1 deletion packages/lib/src/toolbar/controls/NotationToggleGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function NotationToggleGroup(props: Props) {
>
<ToggleGroup.Btn label="Auto" value={Notation.Auto} />
<ToggleGroup.Btn label="Scientific" value={Notation.Scientific} />
<ToggleGroup.Btn label="Fixed-point" value={Notation.FixedPoint} />
<ToggleGroup.Btn label="Exact" value={Notation.Exact} />
</ToggleGroup>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/vis/matrix/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum Notation {
Auto = 'Auto',
Scientific = 'Scientific',
FixedPoint = 'Fixed-point',
Exact = 'Exact',
}
4 changes: 2 additions & 2 deletions packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export interface UnknownType {
export type ScalarValue<T extends DType = DType> = 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
Expand All @@ -230,7 +230,7 @@ export type ArrayValue<T extends DType = DType> = 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<T>[];

Expand Down
19 changes: 4 additions & 15 deletions packages/shared/src/vis-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,24 +64,13 @@ export function formatBool(value: ScalarValue<BooleanType>): string {
}

export function createComplexFormatter(
specifier: string,
full = false,
formatReal: (val: number) => string,
formatImag = formatReal,
): (val: ScalarValue<ComplexType>) => 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`;
};
}

Expand Down

0 comments on commit 76d9679

Please sign in to comment.