From a1e4e8bb7b9822a4689ae8d8154b2d27dac7e5c1 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Tue, 3 Sep 2024 14:23:37 +0200 Subject: [PATCH] Simplify `MatrixVis` API --- apps/storybook/src/MatrixVis.stories.tsx | 16 ++++++------ .../core/compound/MappedCompoundVis.tsx | 11 ++++---- .../vis-packs/core/matrix/MappedMatrixVis.tsx | 10 +++++--- packages/lib/src/vis/matrix/MatrixVis.tsx | 20 +++++---------- packages/shared/src/guards.ts | 25 +++++++++++-------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/apps/storybook/src/MatrixVis.stories.tsx b/apps/storybook/src/MatrixVis.stories.tsx index b7c5c7b92..37deecc5d 100644 --- a/apps/storybook/src/MatrixVis.stories.tsx +++ b/apps/storybook/src/MatrixVis.stories.tsx @@ -1,5 +1,4 @@ import { MatrixVis, mockValues } from '@h5web/lib'; -import type { H5WebComplex } from '@h5web/shared/hdf5-models'; import { createComplexFormatter, toTypedNdArray, @@ -10,6 +9,7 @@ import { format } from 'd3-format'; import FillHeight from './decorators/FillHeight'; const dataArray = mockValues.twoD(); +const typedDataArray = toTypedNdArray(dataArray, Float32Array); const complexDataArray = mockValues.twoD_cplx(); const formatMatrixValue = format('.3e'); @@ -30,8 +30,8 @@ type Story = StoryObj; export const Default = { args: { - dataArray, - formatter: (val) => formatMatrixValue(val as number), + dims: dataArray.shape, + cellFormatter: (row, col) => formatMatrixValue(dataArray.get(row, col)), }, } satisfies Story; @@ -51,16 +51,18 @@ export const StaticHeaderCells = { export const Complex = { args: { - dataArray: complexDataArray, - formatter: (val) => formatMatrixComplex(val as H5WebComplex), + dims: complexDataArray.shape, + cellFormatter: (row, col) => + formatMatrixComplex(complexDataArray.get(row, col)), cellWidth: 232, }, } satisfies Story; export const TypedArray = { args: { - dataArray: toTypedNdArray(dataArray, Float32Array), - formatter: (val) => formatMatrixValue(val as number), + dims: typedDataArray.shape, + cellFormatter: (row, col) => + formatMatrixValue(typedDataArray.get(row, col)), }, } satisfies Story; diff --git a/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx b/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx index db95c18fc..7b9d0fa2e 100644 --- a/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx +++ b/packages/app/src/vis-packs/core/compound/MappedCompoundVis.tsx @@ -2,10 +2,9 @@ import { MatrixVis } from '@h5web/lib'; import type { ArrayShape, Dataset, - Primitive, PrintableCompoundType, - PrintableType, ScalarShape, + Value, } from '@h5web/shared/hdf5-models'; import { createPortal } from 'react-dom'; @@ -20,7 +19,7 @@ import { getSliceSelection } from '../utils'; interface Props { dataset: Dataset; - value: Primitive[] | Primitive[][]; + value: Value; dimMapping: DimensionMapping; toolbarContainer: HTMLDivElement | undefined; config: MatrixVisConfig; @@ -70,8 +69,10 @@ function MappedCompoundVis(props: Props) { )} fieldFormatters[colIndex](val)} + dims={mappedArray.shape} + cellFormatter={(row, col) => + fieldFormatters[col](mappedArray.get(row, col)) + } cellWidth={customCellWidth ?? cellWidth} columnHeaders={fieldNames} sticky={sticky} diff --git a/packages/app/src/vis-packs/core/matrix/MappedMatrixVis.tsx b/packages/app/src/vis-packs/core/matrix/MappedMatrixVis.tsx index e8faf14d4..38e26b1b2 100644 --- a/packages/app/src/vis-packs/core/matrix/MappedMatrixVis.tsx +++ b/packages/app/src/vis-packs/core/matrix/MappedMatrixVis.tsx @@ -1,9 +1,9 @@ import { MatrixVis } from '@h5web/lib'; import type { ArrayShape, - ArrayValue, Dataset, PrintableType, + Value, } from '@h5web/shared/hdf5-models'; import { createPortal } from 'react-dom'; @@ -18,7 +18,7 @@ import { getCellWidth, getFormatter } from './utils'; interface Props { dataset: Dataset; - value: ArrayValue; + value: Value; dimMapping: DimensionMapping; toolbarContainer: HTMLDivElement | undefined; config: MatrixVisConfig; @@ -56,8 +56,10 @@ function MappedMatrixVis(props: Props) { + formatter(mappedArray.get(row, col)) + } cellWidth={customCellWidth ?? cellWidth} sticky={sticky} /> diff --git a/packages/lib/src/vis/matrix/MatrixVis.tsx b/packages/lib/src/vis/matrix/MatrixVis.tsx index 864727436..4c0fc966c 100644 --- a/packages/lib/src/vis/matrix/MatrixVis.tsx +++ b/packages/lib/src/vis/matrix/MatrixVis.tsx @@ -1,7 +1,6 @@ -import type { ArrayValue, Primitive } from '@h5web/shared/hdf5-models'; -import type { NdArray } from 'ndarray'; +import type { ArrayShape } from '@h5web/shared/hdf5-models'; -import type { ClassStyleAttrs, PrintableType } from '../models'; +import type { ClassStyleAttrs } from '../models'; import GridProvider from './context'; import Grid from './Grid'; @@ -9,8 +8,8 @@ const ROW_HEADERS_WIDTH = 80; const CELL_HEIGHT = 32; interface Props extends ClassStyleAttrs { - dataArray: NdArray>; - formatter: (value: Primitive, colIndex: number) => string; + dims: ArrayShape; + cellFormatter: (row: number, col: number) => string; cellWidth: number; sticky?: boolean; columnHeaders?: string[]; @@ -18,23 +17,16 @@ interface Props extends ClassStyleAttrs { function MatrixVis(props: Props) { const { - dataArray, - formatter, + dims, + cellFormatter, cellWidth, sticky = true, columnHeaders, className = '', style, } = props; - const dims = dataArray.shape; - const [rowCount, columnCount = 1] = dims; - const cellFormatter = - dims.length === 1 - ? (row: number) => formatter(dataArray.get(row), 0) - : (row: number, col: number) => formatter(dataArray.get(row, col), col); - return ( ( - dataset: D, +function assertPrimitiveValue( + type: T, value: unknown, ): asserts value is Primitive { - if (hasNumericType(dataset)) { + if (isNumericType(type)) { assertNum(value); - } else if (hasStringType(dataset)) { + } else if (isStringType(type)) { assertStr(value); - } else if (hasBoolType(dataset)) { + } else if (isBoolType(type)) { assertNumOrBool(value); - } else if (hasComplexType(dataset)) { + } else if (isComplexType(type)) { assertComplex(value); + } else if (isCompoundType(type)) { + assertArray(value); } } @@ -440,15 +442,16 @@ export function assertDatasetValue>( value: unknown, dataset: D, ): asserts value is Value { - if (hasArrayShape(dataset)) { + const { type } = dataset; + + if (hasScalarShape(dataset)) { + assertPrimitiveValue(type, value); + } else { assertArrayOrTypedArray(value); if (value.length > 0) { - assertPrimitiveValue(dataset, value[0]); + assertPrimitiveValue(type, value[0]); } - } else { - // Scalar shape - assertPrimitiveValue(dataset, value); } }