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

Fetch attribute values on demand in resolvePath #850

Merged
merged 3 commits into from
Nov 16, 2021
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/auxspectrum.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/logspectrum.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/nximage.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/nxrgb.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/nxspectrum.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions packages/app/src/__tests__/NexusPack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ test('visualize NXdata group with "rgb-image" interpretation', async () => {
expect(tabs[0]).toHaveTextContent(NexusVis.NxRGB);
});

test('visualize NXdata group with unknown interpretation', async () => {
await renderApp();
await selectExplorerNode('nexus_entry/unknown');

const tabs = await findVisSelectorTabs();
expect(tabs).toHaveLength(1);
expect(tabs[0]).toHaveTextContent(NexusVis.NxImage); // support check falls back on signal dataset dimensions
});

test('visualize NXdata group with "rgb-image" interpretation but incompatible signal', async () => {
await renderApp();
await selectExplorerNode('nexus_entry/rgb_incompatible');

const tabs = await findVisSelectorTabs();
expect(tabs).toHaveLength(1);
expect(tabs[0]).toHaveTextContent(NexusVis.NxSpectrum); // support check falls back on signal dataset dimensions
});

test('visualize NXentry group with implicit default child NXdata group', async () => {
await renderApp();
await selectExplorerNode('nexus_no_default');
Expand Down
6 changes: 0 additions & 6 deletions packages/app/src/vis-packs/nexus/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ export type NxAttribute =
| 'SILX_style'
| 'auxiliary_signals';

export enum NxInterpretation {
Spectrum = 'spectrum',
Image = 'image',
RGB = 'rgb-image',
}

export interface NxData<
T extends NumericType | ComplexType = NumericType | ComplexType
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/visualizer/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Props {
function Visualizer(props: Props) {
const { path } = props;

const { entitiesStore } = useContext(ProviderContext);
const { entitiesStore, attrValuesStore } = useContext(ProviderContext);

function getEntity(entityPath: string): Entity {
return handleError(
Expand All @@ -25,7 +25,7 @@ function Visualizer(props: Props) {
);
}

const resolution = resolvePath(path, getEntity);
const resolution = resolvePath(path, getEntity, attrValuesStore);

if (!resolution) {
return (
Expand Down
174 changes: 0 additions & 174 deletions packages/app/src/visualizer/utils.test.ts

This file was deleted.

38 changes: 24 additions & 14 deletions packages/app/src/visualizer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Entity } from '@h5web/shared';
import type { AttributeValues, Entity } from '@h5web/shared';
import {
assertGroupWithChildren,
hasComplexType,
Expand All @@ -8,10 +8,10 @@ import {
isDataset,
assertStr,
buildEntityPath,
NxInterpretation,
} from '@h5web/shared';
import { NxInterpretation } from '@h5web/shared/src/models-nexus';
import type { FetchStore } from 'react-suspense-fetch';

import { getAttributeValue } from '../utils';
import type { CoreVisDef } from '../vis-packs/core/visualizations';
import { Vis, CORE_VIS } from '../vis-packs/core/visualizations';
import type { VisDef } from '../vis-packs/models';
Expand All @@ -24,38 +24,45 @@ import { NexusVis, NEXUS_VIS } from '../vis-packs/nexus/visualizations';

export function resolvePath(
path: string,
getEntity: (path: string) => Entity
getEntity: (path: string) => Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): { entity: Entity; supportedVis: VisDef[] } | undefined {
const entity = getEntity(path);

const supportedVis = findSupportedVis(entity);
const supportedVis = findSupportedVis(entity, attrValueStore);
if (supportedVis.length > 0) {
return { entity, supportedVis };
}

const nxDefaultPath = getNxDefaultPath(entity);
const nxDefaultPath = getNxDefaultPath(entity, attrValueStore);
if (nxDefaultPath) {
return resolvePath(nxDefaultPath, getEntity);
return resolvePath(nxDefaultPath, getEntity, attrValueStore);
}

return undefined;
}

function findSupportedVis(entity: Entity): VisDef[] {
const nxVis = getSupportedNxVis(entity);
function findSupportedVis(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): VisDef[] {
const nxVis = getSupportedNxVis(entity, attrValueStore);
if (nxVis) {
return [nxVis];
}

return getSupportedCoreVis(entity);
}

function getNxDefaultPath(entity: Entity): string | undefined {
function getNxDefaultPath(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): string | undefined {
if (!isGroup(entity)) {
return undefined;
}

const defaultPath = getAttributeValue(entity, 'default');
const { default: defaultPath } = attrValueStore.get(entity);

if (defaultPath) {
assertStr(defaultPath, `Expected 'default' attribute to be a string`);
Expand All @@ -69,7 +76,7 @@ function getNxDefaultPath(entity: Entity): string | undefined {
return getImplicitDefaultChild(entity.children)?.path;
}

export function getSupportedCoreVis(entity: Entity): CoreVisDef[] {
function getSupportedCoreVis(entity: Entity): CoreVisDef[] {
const supportedVis = Object.values(CORE_VIS).filter(
(vis) => isDataset(entity) && vis.supportsDataset(entity)
);
Expand All @@ -79,15 +86,18 @@ export function getSupportedCoreVis(entity: Entity): CoreVisDef[] {
: supportedVis;
}

export function getSupportedNxVis(entity: Entity): VisDef | undefined {
function getSupportedNxVis(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): VisDef | undefined {
if (!isGroup(entity) || !isNxDataGroup(entity)) {
return undefined;
}

assertGroupWithChildren(entity);
const dataset = findSignalDataset(entity);
const isCplx = hasComplexType(dataset);
const interpretation = getAttributeValue(dataset, 'interpretation');
const { interpretation } = attrValueStore.get(dataset);

if (
interpretation === NxInterpretation.RGB &&
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './guards';
export * from './models-hdf5';
export * from './models-nexus';
export * from './models-vis';
export * from './utils';
export * from './mock/metadata';
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/mock/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ export const mockMetadata = makeNxGroup(mockFilepath, 'NXroot', {
})
),
}),
makeNxDataGroup('unknown', {
signal: makeNxDataset('fourD', intType, [3, 9, 20, 41], {
interpretation: 'unknown',
}),
}),
makeNxDataGroup('rgb_incompatible', {
signal: makeNxDataset('oneD', intType, [41], {
interpretation: 'rgb-image',
}),
}),
],
}),
makeNxGroup('nexus_no_default', 'NXprocess', {
Expand Down