Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nD datasets in CompoundMatrix #1169

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified cypress/snapshots/app.spec.ts/bgr_image.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.spec.ts/edit_domain.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.spec.ts/heatmap_2D.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.spec.ts/heatmap_2D_complex.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.spec.ts/heatmap_2D_inverted_cmap.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.spec.ts/heatmap_4d_default.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.spec.ts/heatmap_4d_remapped.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.spec.ts/heatmap_4d_sliced.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.spec.ts/heatmap_4d_zeros.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.spec.ts/line_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.spec.ts/line_complex_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.spec.ts/rgb_image.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions packages/app/src/providers/mock/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
assertDefined,
assertMockAttribute,
assertMockDataset,
assertPrintableType,
findMockEntity,
hasArrayShape,
isTypedArray,
Expand Down Expand Up @@ -71,7 +70,6 @@ export class MockApi extends DataProviderApi {
}

assertArrayShape(dataset);
assertPrintableType(dataset);
loichuder marked this conversation as resolved.
Show resolved Hide resolved

return sliceValue(value, dataset, selection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
assertPrintableCompoundType,
} from '@h5web/shared';

import DimensionMapper from '../../../dimension-mapper/DimensionMapper';
import { useDimMappingState } from '../../../dimension-mapper/hooks';
import VisBoundary from '../../VisBoundary';
import type { VisContainerProps } from '../../models';
import ValueFetcher from '../ValueFetcher';
import { getSliceSelection } from '../utils';
import MappedCompoundMatrixVis from './MappedCompoundMatrixVis';

function CompoundMatrixVisContainer(props: VisContainerProps) {
Expand All @@ -17,19 +20,31 @@ function CompoundMatrixVisContainer(props: VisContainerProps) {
assertCompoundType(entity);
assertPrintableCompoundType(entity);

const { shape: dims } = entity;
const [dimMapping, setDimMapping] = useDimMappingState(dims, 1);

return (
<VisBoundary loadingMessage="Loading current data">
<ValueFetcher
dataset={entity}
render={(value) => (
<MappedCompoundMatrixVis
dataset={entity}
value={value}
toolbarContainer={toolbarContainer}
/>
)}
<>
<DimensionMapper
rawDims={dims}
mapperState={dimMapping}
onChange={setDimMapping}
/>
</VisBoundary>
<VisBoundary loadingMessage="Loading current slice">
<ValueFetcher
dataset={entity}
selection={getSliceSelection(dimMapping)}
render={(value) => (
<MappedCompoundMatrixVis
dataset={entity}
value={value}
toolbarContainer={toolbarContainer}
dimMapping={dimMapping}
/>
)}
/>
</VisBoundary>
</>
axelboc marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,57 @@ import type {
PrintableCompoundType,
PrintableType,
} from '@h5web/shared';
import ndarray from 'ndarray';
import { createPortal } from 'react-dom';
import shallow from 'zustand/shallow';

import type { DimensionMapping } from '../../../dimension-mapper/models';
import { useMappedArray, useSlicedDimsAndMapping } from '../hooks';
import MatrixToolbar from '../matrix/MatrixToolbar';
import { useMatrixConfig } from '../matrix/config';
import { getCellWidth } from '../matrix/utils';
import { getSliceSelection } from '../utils';
import { compoundFormatter } from './utils';

interface Props {
dataset: Dataset<ArrayShape, PrintableCompoundType>;
value: Primitive<PrintableType>[][];
dimMapping: DimensionMapping;
toolbarContainer: HTMLDivElement | undefined;
}

function MappedCompoundMatrixVis(props: Props) {
const { value, dataset, toolbarContainer } = props;
const { fields } = dataset.type;

const fieldNames = Object.keys(fields);
const cellWidth = getCellWidth(dataset.type);

const dataArray = ndarray(value.flat(1), [
...dataset.shape,
fieldNames.length,
]);
const { value, dataset, toolbarContainer, dimMapping } = props;

const { sticky, customCellWidth } = useMatrixConfig(
(state) => state,
shallow
);

const { type, shape: dims } = dataset;
const { fields } = type;
const fieldNames = Object.keys(fields);
const cellWidth = getCellWidth(type);

const [slicedDims, slicedMapping] = useSlicedDimsAndMapping(dims, dimMapping);
const [mappedArray] = useMappedArray(
value.flat(1),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given our dimMapping, value will always be a 1D array of tuples. So value.flat(1) is still valid to flatten the tuples.

[...slicedDims, fieldNames.length],
slicedMapping
);

return (
<>
{toolbarContainer &&
createPortal(
<MatrixToolbar dataset={dataset} cellWidth={cellWidth} />,
<MatrixToolbar
dataset={dataset}
selection={getSliceSelection(dimMapping)}
cellWidth={cellWidth}
/>,
toolbarContainer
)}
<MatrixVis
dataArray={dataArray}
dataArray={mappedArray}
formatter={compoundFormatter}
cellWidth={customCellWidth ?? cellWidth}
columnHeaders={fieldNames}
Expand Down
3 changes: 1 addition & 2 deletions packages/app/src/vis-packs/core/visualizations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Dataset } from '@h5web/shared';
import { hasPrintableCompoundType } from '@h5web/shared';
import { hasCompoundType } from '@h5web/shared';
import { hasNumDims } from '@h5web/shared';
import {
hasScalarShape,
hasArrayShape,
Expand Down Expand Up @@ -161,7 +160,7 @@ export const CORE_VIS: Record<Vis, CoreVisDef> = {
hasCompoundType(dataset) &&
hasPrintableCompoundType(dataset) &&
hasArrayShape(dataset) &&
hasNumDims(dataset, 1)
hasMinDims(dataset, 1)
loichuder marked this conversation as resolved.
Show resolved Hide resolved
);
},
},
Expand Down
12 changes: 12 additions & 0 deletions packages/shared/src/mock/metadata-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
LinkClass,
UnresolvedEntity,
GroupWithChildren,
PrintableCompoundType,
} from '../models-hdf5';
import { EntityKind, DTypeClass, Endianness } from '../models-hdf5';
import type { NxInterpretation, SilxStyle } from '../models-nexus';
Expand Down Expand Up @@ -64,6 +65,17 @@ export const complexType: ComplexType = {
imagType: floatType,
};

export const printableCompoundType: PrintableCompoundType = {
class: DTypeClass.Compound,
fields: {
string: stringType,
int: intType,
float: floatType,
bool: booleanType,
complex: complexType,
},
};

/* ---------------------- */
/* ----- ATTRIBUTES ----- */

Expand Down
18 changes: 3 additions & 15 deletions packages/shared/src/mock/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DTypeClass } from '../models-hdf5';
import { ScaleType } from '../models-vis';
import {
compoundType,
Expand All @@ -19,6 +18,7 @@ import {
withImageAttributes,
withAttributes,
makeIntAttr,
printableCompoundType,
} from './metadata-utils';

export const mockFilepath = 'source.h5';
Expand Down Expand Up @@ -50,22 +50,10 @@ export const mockMetadata = makeNxGroup(mockFilepath, 'NXroot', {
makeDataset('oneD_linear', intType, [41]),
makeDataset('oneD', intType, [41]),
makeDataset('oneD_cplx', complexType, [10]),
makeDataset(
'oneD_compound',
{
class: DTypeClass.Compound,
fields: {
string: stringType,
int: intType,
float: floatType,
bool: booleanType,
complex: complexType,
},
},
[5]
),
makeDataset('oneD_compound', printableCompoundType, [5]),
makeDataset('twoD', intType, [20, 41]),
makeDataset('twoD_cplx', complexType, [2, 2]),
makeDataset('twoD_compound', printableCompoundType, [2, 5]),
makeDataset('threeD', intType, [9, 20, 41]),
makeDataset('threeD_bool', booleanType, [2, 3, 4]),
makeDataset('threeD_cplx', complexType, [2, 3, 4]),
Expand Down
20 changes: 17 additions & 3 deletions packages/shared/src/mock/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ const oneD_cplx = range(1, 11).map((val) => [
val * Math.sin((val * 3.14) / 10),
]);

const oneD_compound = ['Hydrogen', 'Lithum', 'Carbon', 'Sodium', 'Argon'].map(
(v, i) => [v, i, oneD[i], i % 2 === 0, oneD_cplx[i]]
);

const twoD_compound = [
oneD_compound,
['Vanadium', 'Niobium', 'Tantalum', 'Silicon', 'Germanium'].map((v, i) => [
v,
i * 10,
oneD[i],
i % 2 === 1,
oneD_cplx[i],
]),
];

export const mockValues = {
null: null,
raw: { int: 42 },
Expand Down Expand Up @@ -144,7 +159,6 @@ export const mockValues = {
position: [-1, 1],
scatter_data: arr1.map((val) => Math.cos((val * 3.14) / 40)),
Y_scatter: arr1.map((v, i) => (i % 10) + (i % 5)),
oneD_compound: ['Hydrogen', 'Lithum', 'Carbon', 'Sodium', 'Argon'].map(
(v, i) => [v, i, oneD[i], i % 2 === 0, oneD_cplx[i]]
),
oneD_compound,
twoD_compound,
};