Skip to content

Commit e0b25d0

Browse files
committed
Assert image subclass if present in RGB vis container
1 parent a699000 commit e0b25d0

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

packages/app/src/vis-packs/core/rgb/RgbVisContainer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import {
88

99
import { useDimMappingState } from '../../../dim-mapping-store';
1010
import { useValuesInCache } from '../../../hooks';
11+
import { useDataContext } from '../../../providers/DataProvider';
1112
import visualizerStyles from '../../../visualizer/Visualizer.module.css';
1213
import { type VisContainerProps } from '../../models';
1314
import VisBoundary from '../../VisBoundary';
1415
import ValueFetcher from '../ValueFetcher';
1516
import { useRgbConfig } from './config';
1617
import MappedRgbVis from './MappedRgbVis';
18+
import { assertSubclassIfPresent } from './utils';
1719

1820
function RgbVisContainer(props: VisContainerProps) {
1921
const { entity, toolbarContainer } = props;
@@ -22,6 +24,9 @@ function RgbVisContainer(props: VisContainerProps) {
2224
assertMinDims(entity, 3);
2325
assertNumericType(entity);
2426

27+
const { attrValuesStore } = useDataContext();
28+
assertSubclassIfPresent(entity, attrValuesStore);
29+
2530
const { dims } = entity.shape;
2631
const [dimMapping, setDimMapping] = useDimMappingState({
2732
dims,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { hasScalarShape, hasStringType } from '@h5web/shared/guards';
2+
import { type Dataset } from '@h5web/shared/hdf5-models';
3+
4+
import { type AttrValuesStore } from '../../../providers/models';
5+
import { findAttribute, getAttributeValue } from '../../../utils';
6+
7+
export function assertSubclassIfPresent(
8+
dataset: Dataset,
9+
attrValuesStore: AttrValuesStore,
10+
): boolean {
11+
const subClassAttr = findAttribute(dataset, 'IMAGE_SUBCLASS');
12+
13+
if (
14+
!subClassAttr ||
15+
!hasScalarShape(subClassAttr) ||
16+
!hasStringType(subClassAttr)
17+
) {
18+
return true;
19+
}
20+
21+
const subClass = getAttributeValue(dataset, subClassAttr, attrValuesStore);
22+
return subClass === 'IMAGE_TRUECOLOR';
23+
}

packages/app/src/vis-packs/core/visualizations.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,14 @@ export const CORE_VIS = {
149149
ConfigProvider: RgbConfigProvider,
150150
supportsDataset: (dataset, attrValuesStore) => {
151151
const classAttr = findAttribute(dataset, 'CLASS');
152-
const subClassAttr = findAttribute(dataset, 'IMAGE_SUBCLASS');
153152

154153
const classVal =
155154
classAttr && hasScalarShape(classAttr) && hasStringType(classAttr)
156155
? getAttributeValue(dataset, classAttr, attrValuesStore)
157156
: undefined;
158157

159-
const subClassVal =
160-
subClassAttr &&
161-
hasScalarShape(subClassAttr) &&
162-
hasStringType(subClassAttr)
163-
? getAttributeValue(dataset, subClassAttr, attrValuesStore)
164-
: undefined;
165-
166158
return (
167159
classVal === 'IMAGE' &&
168-
(!subClassVal || subClassVal === 'IMAGE_TRUECOLOR') &&
169160
hasArrayShape(dataset) &&
170161
hasMinDims(dataset, 3) && // 2 for axes + 1 for RGB channels
171162
dataset.shape.dims[dataset.shape.dims.length - 1] === 3 && // 3 channels on last dim

packages/app/src/vis-packs/nexus/containers/NxRgbContainer.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { DimensionMapper, getSliceSelection } from '@h5web/lib';
22
import { assertGroup, assertMinDims } from '@h5web/shared/guards';
33

44
import { useDimMappingState } from '../../../dim-mapping-store';
5+
import { useDataContext } from '../../../providers/DataProvider';
56
import visualizerStyles from '../../../visualizer/Visualizer.module.css';
67
import { useRgbConfig } from '../../core/rgb/config';
78
import MappedRgbVis from '../../core/rgb/MappedRgbVis';
9+
import { assertSubclassIfPresent } from '../../core/rgb/utils';
810
import { type VisContainerProps } from '../../models';
911
import VisBoundary from '../../VisBoundary';
1012
import { assertNumericNxData } from '../guards';
@@ -21,11 +23,10 @@ function NxRgbContainer(props: VisContainerProps) {
2123
const { signalDef, axisDefs, defaultSlice } = nxData;
2224
assertMinDims(signalDef.dataset, 3);
2325

24-
const { dims } = signalDef.dataset.shape;
25-
if (dims[dims.length - 1] !== 3) {
26-
throw new Error('Expected last dimension to have size 3');
27-
}
26+
const { attrValuesStore } = useDataContext();
27+
assertSubclassIfPresent(signalDef.dataset, attrValuesStore);
2828

29+
const { dims } = signalDef.dataset.shape;
2930
const [dimMapping, setDimMapping] = useDimMappingState({
3031
dims,
3132
axesCount: 2,

packages/app/src/vis-packs/nexus/visualizations.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,14 @@ export const NX_DATA_VIS = {
9595
ConfigProvider: RgbConfigProvider,
9696
supports: (_, signal, interpretation, attrValuesStore) => {
9797
const classAttr = findAttribute(signal, 'CLASS');
98-
const subClassAttr = findAttribute(signal, 'IMAGE_SUBCLASS');
9998

10099
const classVal =
101100
classAttr && hasScalarShape(classAttr) && hasStringType(classAttr)
102101
? getAttributeValue(signal, classAttr, attrValuesStore)
103102
: undefined;
104103

105-
const subClassVal =
106-
subClassAttr &&
107-
hasScalarShape(subClassAttr) &&
108-
hasStringType(subClassAttr)
109-
? getAttributeValue(signal, subClassAttr, attrValuesStore)
110-
: undefined;
111-
112104
return (
113105
(interpretation === NxInterpretation.RGB || classVal === 'IMAGE') &&
114-
(!subClassVal || subClassVal === 'IMAGE_TRUECOLOR') &&
115106
hasMinDims(signal, 3) && // 2 for axes + 1 for RGB channels
116107
signal.shape.dims[signal.shape.dims.length - 1] === 3 && // 3 channels
117108
hasNumericType(signal)

0 commit comments

Comments
 (0)