Skip to content

Commit

Permalink
Normalise mapped components' props with ArrayValue
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Jan 23, 2025
1 parent e4dd359 commit 44ee4e4
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 58 deletions.
4 changes: 2 additions & 2 deletions packages/app/src/providers/mock/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Dataset,
type Entity,
type GroupWithChildren,
type NumArrayDataset,
type NumericType,
type ProvidedEntity,
type Value,
} from '@h5web/shared/hdf5-models';
Expand Down Expand Up @@ -123,7 +123,7 @@ export class MockApi extends DataProviderApi {
) {
return async () => {
let csv = '';
(value as Value<NumArrayDataset>).forEach((val) => {
(value as Value<Dataset<ArrayShape, NumericType>>).forEach((val) => {
csv += `${val.toString()}\n`;
});

Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/providers/mock/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
type Dataset,
type DType,
type GroupWithChildren,
type Primitive,
type ProvidedEntity,
type ScalarShape,
type ScalarValue,
} from '@h5web/shared/hdf5-models';
import { getChildEntity } from '@h5web/shared/hdf5-utils';
import ndarray from 'ndarray';
Expand Down Expand Up @@ -50,9 +50,9 @@ export function sliceValue<T extends DType>(
value: unknown,
dataset: Dataset<ArrayShape | ScalarShape, T>,
selection: string,
): Primitive<T>[] {
): ScalarValue<T>[] {
const { shape } = dataset;
const dataArray = ndarray(value as Primitive<typeof dataset.type>[], shape);
const dataArray = ndarray(value as ScalarValue<typeof dataset.type>[], shape);
const mappedArray = applyMapping(
dataArray,
selection.split(',').map((s) => (s === ':' ? s : Number.parseInt(s))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {
useSafeDomain,
useVisDomain,
} from '@h5web/lib';
import { type ArrayValue, type ComplexType } from '@h5web/shared/hdf5-models';
import {
type ArrayValue,
type ComplexType,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
Expand All @@ -28,7 +31,7 @@ interface Props {
dims: number[];
dimMapping: DimensionMapping;
axisLabels?: AxisMapping<string>;
axisValues?: AxisMapping<NumArray>;
axisValues?: AxisMapping<ArrayValue<NumericType>>;
title: string;
toolbarContainer: HTMLDivElement | undefined;
config: ComplexLineConfig;
Expand Down
12 changes: 8 additions & 4 deletions packages/app/src/vis-packs/core/complex/MappedComplexVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import {
useValidDomainForScale,
useVisDomain,
} from '@h5web/lib';
import { type H5WebComplex } from '@h5web/shared/hdf5-models';
import {
type ArrayValue,
type ComplexType,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { ComplexVisType, type NumArray } from '@h5web/shared/vis-models';
import { ComplexVisType } from '@h5web/shared/vis-models';
import { useMemo } from 'react';
import { createPortal } from 'react-dom';

Expand All @@ -24,11 +28,11 @@ import { type ComplexConfig } from './config';
import { COMPLEX_VIS_TYPE_LABELS, getPhaseAmplitudeValues } from './utils';

interface Props {
value: H5WebComplex[];
value: ArrayValue<ComplexType>;
dims: number[];
dimMapping: DimensionMapping;
axisLabels?: AxisMapping<string>;
axisValues?: AxisMapping<NumArray>;
axisValues?: AxisMapping<ArrayValue<NumericType>>;
title: string;
toolbarContainer: HTMLDivElement | undefined;
config: ComplexConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MatrixVis } from '@h5web/lib';
import {
type ArrayShape,
type ArrayValue,
type CompoundType,
type Dataset,
type PrintableType,
type ScalarShape,
type Value,
type ScalarValue,
} from '@h5web/shared/hdf5-models';
import { createPortal } from 'react-dom';

Expand All @@ -20,7 +21,9 @@ import { getSliceSelection } from '../utils';

interface Props {
dataset: Dataset<ScalarShape | ArrayShape, CompoundType<PrintableType>>;
value: Value<Props['dataset']>;
value:
| ScalarValue<CompoundType<PrintableType>>
| ArrayValue<CompoundType<PrintableType>>;
dimMapping: DimensionMapping;
toolbarContainer: HTMLDivElement | undefined;
config: MatrixVisConfig;
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/vis-packs/core/heatmap/MappedHeatmapVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
type ArrayValue,
type Dataset,
type NumericLikeType,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
Expand All @@ -25,7 +25,7 @@ interface Props {
dataset: Dataset<ArrayShape, NumericLikeType>;
value: ArrayValue<NumericLikeType>;
axisLabels?: AxisMapping<string>;
axisValues?: AxisMapping<NumArray>;
axisValues?: AxisMapping<ArrayValue<NumericType>>;
dimMapping: DimensionMapping;
title: string;
toolbarContainer: HTMLDivElement | undefined;
Expand Down
14 changes: 7 additions & 7 deletions packages/app/src/vis-packs/core/line/MappedLineVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
} from '@h5web/lib';
import {
type ArrayShape,
type ArrayValue,
type Dataset,
type NumericLikeType,
type Value,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
Expand All @@ -32,16 +32,16 @@ import LineToolbar from './LineToolbar';

interface Props {
dataset: Dataset<ArrayShape, NumericLikeType>;
value: Value<Props['dataset']>;
value: ArrayValue<NumericLikeType>;
valueLabel?: string;
errors?: NumArray;
errors?: ArrayValue<NumericType>;
auxLabels?: string[];
auxValues?: Value<Props['dataset']>[];
auxErrors?: (NumArray | undefined)[];
auxValues?: ArrayValue<NumericLikeType>[];
auxErrors?: (ArrayValue<NumericType> | undefined)[];
dims: number[];
dimMapping: DimensionMapping;
axisLabels?: AxisMapping<string>;
axisValues?: AxisMapping<NumArray>;
axisValues?: AxisMapping<ArrayValue<NumericType>>;
title: string;
toolbarContainer?: HTMLDivElement | undefined;
config: LineConfig;
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/vis-packs/core/matrix/MappedMatrixVis.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MatrixVis } from '@h5web/lib';
import {
type ArrayShape,
type ArrayValue,
type Dataset,
type PrintableType,
type Value,
} from '@h5web/shared/hdf5-models';
import { createPortal } from 'react-dom';

Expand All @@ -18,7 +18,7 @@ import { getCellWidth, getFormatter } from './utils';

interface Props {
dataset: Dataset<ArrayShape, PrintableType>;
value: Value<Props['dataset']>;
value: ArrayValue<PrintableType>;
dimMapping: DimensionMapping;
toolbarContainer: HTMLDivElement | undefined;
config: MatrixVisConfig;
Expand Down
15 changes: 9 additions & 6 deletions packages/app/src/vis-packs/core/rgb/MappedRgbVis.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { RgbVis } from '@h5web/lib';
import { type NumArrayDataset } from '@h5web/shared/hdf5-models';
import {
type ArrayShape,
type ArrayValue,
type Dataset,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { type TypedArray } from 'ndarray';
import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
Expand All @@ -12,10 +15,10 @@ import { type RgbVisConfig } from './config';
import RgbToolbar from './RgbToolbar';

interface Props {
dataset: NumArrayDataset;
value: number[] | TypedArray;
dataset: Dataset<ArrayShape, NumericType>;
value: ArrayValue<NumericType>;
axisLabels?: AxisMapping<string>;
axisValues?: AxisMapping<NumArray>;
axisValues?: AxisMapping<ArrayValue<NumericType>>;
dimMapping: DimensionMapping;
title: string;
toolbarContainer: HTMLDivElement | undefined;
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/vis-packs/core/scatter/MappedScatterVis.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ScatterVis, useDomain, useSafeDomain, useVisDomain } from '@h5web/lib';
import { assertDefined } from '@h5web/shared/guards';
import { type ArrayValue, type NumericType } from '@h5web/shared/hdf5-models';
import { type AxisMapping } from '@h5web/shared/nexus-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { createPortal } from 'react-dom';

import visualizerStyles from '../../../visualizer/Visualizer.module.css';
Expand All @@ -11,9 +11,9 @@ import { type ScatterConfig } from './config';
import ScatterToolbar from './ScatterToolbar';

interface Props {
value: NumArray;
value: ArrayValue<NumericType>;
axisLabels: AxisMapping<string>;
axisValues: AxisMapping<NumArray>;
axisValues: AxisMapping<ArrayValue<NumericType>>;
title: string;
toolbarContainer: HTMLDivElement | undefined;
config: ScatterConfig;
Expand Down
12 changes: 8 additions & 4 deletions packages/app/src/vis-packs/core/surface/MappedSurfaceVis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { SurfaceVis, useDomain, useSafeDomain, useVisDomain } from '@h5web/lib';
import { type NumArrayDataset } from '@h5web/shared/hdf5-models';
import { type TypedArray } from 'ndarray';
import {
type ArrayShape,
type ArrayValue,
type Dataset,
type NumericType,
} from '@h5web/shared/hdf5-models';
import { createPortal } from 'react-dom';

import { type DimensionMapping } from '../../../dimension-mapper/models';
Expand All @@ -11,8 +15,8 @@ import { type SurfaceConfig } from './config';
import SurfaceToolbar from './SurfaceToolbar';

interface Props {
dataset: NumArrayDataset;
value: number[] | TypedArray;
dataset: Dataset<ArrayShape, NumericType>;
value: ArrayValue<NumericType>;
dimMapping: DimensionMapping;
toolbarContainer: HTMLDivElement | undefined;
config: SurfaceConfig;
Expand Down
3 changes: 1 addition & 2 deletions packages/app/src/vis-packs/nexus/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type ArrayValue,
type ComplexType,
type Dataset,
type NumArrayDataset,
type NumericLikeType,
type NumericType,
type ScalarShape,
Expand Down Expand Up @@ -36,7 +35,7 @@ export interface DatasetDef<
}

type WithError<T extends DatasetDef> = T & {
errorDataset?: NumArrayDataset;
errorDataset?: Dataset<ArrayShape, NumericType>;
};

export type AxisDef = DatasetDef<NumericType>;
Expand Down
10 changes: 5 additions & 5 deletions packages/app/src/vis-packs/nexus/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
type Dataset,
type Group,
type GroupWithChildren,
type NumArrayDataset,
type NumericLikeType,
type NumericType,
type ScalarShape,
type StringType,
} from '@h5web/shared/hdf5-models';
Expand Down Expand Up @@ -93,7 +93,7 @@ export function findSignalDataset(
export function findErrorDataset(
group: GroupWithChildren,
signalName: string,
): NumArrayDataset | undefined {
): Dataset<ArrayShape, NumericType> | undefined {
const dataset =
getChildEntity(group, `${signalName}_errors`) ||
getChildEntity(group, 'errors');
Expand All @@ -111,7 +111,7 @@ export function findErrorDataset(
export function findAuxErrorDataset(
group: GroupWithChildren,
auxSignalName: string,
): NumArrayDataset | undefined {
): Dataset<ArrayShape, NumericType> | undefined {
const dataset = getChildEntity(group, `${auxSignalName}_errors`);

if (!dataset) {
Expand Down Expand Up @@ -172,7 +172,7 @@ function findOldStyleAxesDatasets(
group: GroupWithChildren,
signal: Dataset,
attrValuesStore: AttrValuesStore,
): NumArrayDataset[] {
): Dataset<ArrayShape, NumericType>[] {
const axesList = attrValuesStore.getSingle(signal, 'axes');
const axesNames = parseAxesList(axesList);

Expand All @@ -190,7 +190,7 @@ export function findAxesDatasets(
group: GroupWithChildren,
signal: Dataset,
attrValuesStore: AttrValuesStore,
): (NumArrayDataset | undefined)[] {
): (Dataset<ArrayShape, NumericType> | undefined)[] {
if (!hasAttribute(group, 'axes')) {
return findOldStyleAxesDatasets(group, signal, attrValuesStore);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
type IntegerType,
type NumericLikeType,
type NumericType,
type Primitive,
type PrintableType,
type ScalarShape,
type ScalarValue,
type Shape,
type StringType,
type Value,
Expand Down Expand Up @@ -423,7 +423,7 @@ export function isComplexValue(
function assertPrimitiveValue(
type: DType,
value: unknown,
): asserts value is Primitive {
): asserts value is ScalarValue {
if (isNumericType(type)) {
assertNum(value);
} else if (isStringType(type)) {
Expand Down
12 changes: 5 additions & 7 deletions packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ export interface VirtualSource {
path: string;
}

export type NumArrayDataset = Dataset<ArrayShape, NumericType>;

/* ----------------- */
/* ----- SHAPE ----- */

Expand Down Expand Up @@ -207,24 +205,24 @@ export interface UnknownType {
/* ----------------- */
/* ----- VALUE ----- */

export type Primitive<T extends DType = DType> = T extends NumericLikeType
export type ScalarValue<T extends DType = DType> = T extends NumericLikeType
? number | (T extends BooleanType ? boolean : never) // let providers choose how to return booleans
: T extends StringType
? string
: T extends ComplexType
? H5WebComplex
: T extends CompoundType<infer TFields>
? Primitive<TFields>[]
? ScalarValue<TFields>[]
: unknown;

export type ArrayValue<T extends DType = DType> = T extends NumericLikeType
? TypedArray | number[] | (T extends BooleanType ? boolean[] : never) // don't use `Primitive` to avoid `(number | boolean)[]`
: Primitive<T>[];
? TypedArray | number[] | (T extends BooleanType ? boolean[] : never) // don't use `ScalarValue` to avoid `(number | boolean)[]`
: ScalarValue<T>[];

export type Value<D extends Dataset> =
D extends Dataset<infer S, infer T>
? S extends ScalarShape
? Primitive<T>
? ScalarValue<T>
: S extends ArrayShape
? ArrayValue<T>
: never
Expand Down
Loading

0 comments on commit 44ee4e4

Please sign in to comment.