Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion packages/app/src/vis-packs/nexus/NxValuesFetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ function NxValuesFetcher<T extends NumericLikeType | ComplexType>(
const errors = useDatasetValue(signalDef.errorDataset, selection);
const auxValues = useDatasetsValues(auxDatasets, selection);
const auxErrors = useDatasetsValues(auxErrorDatasets, selection);
const axisValues = useDatasetsValues(axisDatasets);
const rawAxisValues = useDatasetsValues(axisDatasets);
const axisValues = rawAxisValues.map((value, i) => {
const axisScalingFactor = axisDefs[i]?.scalingFactor;
const axisOffset = axisDefs[i]?.offset;
if (value && (axisScalingFactor || axisOffset)) {
const offset = axisOffset ?? 0;
const scalingFactor = axisScalingFactor ?? 1;
const scaledValue: number[] = [];
value.forEach((v) => {
scaledValue.push(scalingFactor * Number(v) + offset);
});
return scaledValue;
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to use value.map but to no avail: I got errors saying that BigInt could not be converted to number.

I think it has to do with the savage conversions I am doing in this part but I am not versed in TS enough to do better 😓

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be best to skip bigint arrays, since it will not work anyway? I'll push something.

}

return value;
});

return render({ title, signal, errors, auxValues, auxErrors, axisValues });
}
Expand Down
9 changes: 7 additions & 2 deletions packages/app/src/vis-packs/nexus/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import {
findTitleDataset,
getDatasetInfo,
getDefaultSlice,
getScalingInfo,
getSilxStyle,
} from './utils';

export const useDefaultSlice = createMemo(getDefaultSlice);

export function useNxData(group: GroupWithChildren): NxData {
const { attrValuesStore } = useDataContext();
const { attrValuesStore, valuesStore } = useDataContext();

assertNxDataGroup(group, attrValuesStore);
const signalDataset = findSignalDataset(group, attrValuesStore);
Expand All @@ -46,7 +47,11 @@ export function useNxData(group: GroupWithChildren): NxData {
})),
axisDefs: axisDatasets.map(
(dataset) =>
dataset && { dataset, ...getDatasetInfo(dataset, attrValuesStore) },
dataset && {
dataset,
...getDatasetInfo(dataset, attrValuesStore),
...getScalingInfo(group, dataset.name, valuesStore),
},
Comment on lines +50 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe time to make a getAxisDef function instead of adding getScalingInfo?

),
defaultSlice: useDefaultSlice(group, signalDataset.shape, attrValuesStore),
silxStyle: getSilxStyle(group, attrValuesStore),
Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/vis-packs/nexus/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface DatasetInfo {
unit: string | undefined;
}

export interface ScalingInfo {
scalingFactor: number | undefined;
offset: number | undefined;
}

export interface DatasetDef<
T extends NumericLikeType | ComplexType = NumericLikeType | ComplexType,
> extends DatasetInfo {
Expand All @@ -38,7 +43,7 @@ type WithError<T extends DatasetDef> = T & {
errorDataset?: Dataset<ArrayShape, NumericType>;
};

export type AxisDef = DatasetDef<NumericType>;
export type AxisDef = DatasetDef<NumericType> & ScalingInfo;
Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

interface AxisDef extends DatasetDef<NumericType> {
  scalingFactor: number | undefined;
  offset: number | undefined;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, makes sense if we replace getScalingInfo by getAxisDef that will return AxisDef


export type DefaultSlice = (number | '.')[];
export interface SilxStyle {
Expand Down
24 changes: 21 additions & 3 deletions packages/app/src/vis-packs/nexus/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@ import { describe, expect, it } from 'vitest';
import { applyDefaultSlice, guessKeepRatio } from './utils';

const axisDataset = dataset('foo', intType(), [5]);
const axisDefNoUnit = { label: 'foo', unit: undefined, dataset: axisDataset };
const axisDefUnitX = { label: 'foo', unit: 'mm', dataset: axisDataset };
const axisDefUnitY = { label: 'foo', unit: 'degrees', dataset: axisDataset };
const axisDefNoUnit = {
label: 'foo',
unit: undefined,
dataset: axisDataset,
scalingFactor: undefined,
offset: undefined,
};
const axisDefUnitX = {
label: 'foo',
unit: 'mm',
dataset: axisDataset,
scalingFactor: undefined,
offset: undefined,
};
const axisDefUnitY = {
label: 'foo',
unit: 'degrees',
dataset: axisDataset,
scalingFactor: undefined,
offset: undefined,
};

describe('guessKeepRatio', () => {
it('should return `cover` if units of both axes are provided and equal', () => {
Expand Down
56 changes: 55 additions & 1 deletion packages/app/src/vis-packs/nexus/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import {
} from '@h5web/shared/hdf5-models';
import { getChildEntity } from '@h5web/shared/hdf5-utils';

import { type AttrValuesStore } from '../../providers/models';
import { type AttrValuesStore, type ValuesStore } from '../../providers/models';
import { hasAttribute } from '../../utils';
import {
type AxisDef,
type DatasetInfo,
type DefaultSlice,
type ScalingInfo,
type SilxStyle,
} from './models';

Expand Down Expand Up @@ -130,6 +131,38 @@ export function findAuxErrorDataset(
return dataset;
}

export function findScalingFactor(
Copy link
Contributor

Choose a reason for hiding this comment

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

findScalingFactorDataset and findOffsetDataset

group: GroupWithChildren,
datasetName: string,
): Dataset<ScalarShape, NumericType> | undefined {
const dataset = getChildEntity(group, `${datasetName}_scaling_factor`);

if (!dataset) {
return undefined;
}

assertDataset(dataset);
assertScalarShape(dataset);
assertNumericType(dataset);
return dataset;
}

export function findOffset(
group: GroupWithChildren,
datasetName: string,
): Dataset<ScalarShape, NumericType> | undefined {
const dataset = getChildEntity(group, `${datasetName}_offset`);

if (!dataset) {
return undefined;
}

assertDataset(dataset);
assertScalarShape(dataset);
assertNumericType(dataset);
return dataset;
}

export function findAssociatedDatasets(
group: GroupWithChildren,
type: 'axes' | 'auxiliary_signals',
Expand Down Expand Up @@ -312,6 +345,27 @@ export function getSilxStyle(
}
}

export function getScalingInfo(
group: GroupWithChildren,
datasetName: string,
valuesStore: ValuesStore,
): ScalingInfo {
const scalingFactorDataset = findScalingFactor(group, datasetName);
const scalingFactor = scalingFactorDataset
? Number(valuesStore.get({ dataset: scalingFactorDataset }))
: undefined;

const offsetDataset = findOffset(group, datasetName);
const offset = offsetDataset
? Number(valuesStore.get({ dataset: offsetDataset }))
Copy link
Member Author

Choose a reason for hiding this comment

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

The cast as Number could probably be replaced by an assert instead ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, a couple of assertions would be best I think.

: undefined;

return {
scalingFactor,
offset,
};
}

export function getDatasetInfo(
dataset: Dataset,
attrValuesStore: AttrValuesStore,
Expand Down