-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathhooks.ts
209 lines (176 loc) · 5.8 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { createMemo } from '@h5web/shared/createMemo';
import { assertDatasetValue, isDefined } from '@h5web/shared/guards';
import {
type ArrayShape,
type ArrayValue,
type Dataset,
type NumericLikeType,
type ScalarShape,
type Value,
} from '@h5web/shared/hdf5-models';
import { type NumArray } from '@h5web/shared/vis-models';
import { castArray } from '@h5web/shared/vis-utils';
import { type NdArray } from 'ndarray';
import { useMemo } from 'react';
import { type DimensionMapping } from '../../dimension-mapper/models';
import { isAxis } from '../../dimension-mapper/utils';
import { useDataContext } from '../../providers/DataProvider';
import { typedArrayFromDType } from '../../providers/utils';
import {
applyMapping,
getBaseArray,
getSliceSelection,
toNumArray,
} from './utils';
export const useToNumArray = createMemo(toNumArray);
export function useToNumArrays(
arrays: ArrayValue<NumericLikeType>[],
): NumArray[];
export function useToNumArrays(
arrays: (ArrayValue<NumericLikeType> | undefined)[],
): (NumArray | undefined)[];
export function useToNumArrays(
arrays: (ArrayValue<NumericLikeType> | undefined)[],
): (NumArray | undefined)[] {
return useMemo(() => arrays.map(toNumArray), arrays); // eslint-disable-line react-hooks/exhaustive-deps
}
export function useValuesInCache(
...datasets: (Dataset<ScalarShape | ArrayShape> | undefined)[]
): (dimMapping: DimensionMapping) => boolean {
const { valuesStore } = useDataContext();
return (dimMapping) => {
const selection = getSliceSelection(dimMapping);
return datasets.every(
(dataset) => !dataset || valuesStore.has({ dataset, selection }),
);
};
}
export function usePrefetchValues(
datasets: (Dataset<ScalarShape | ArrayShape> | undefined)[],
selection?: string,
): void {
const { valuesStore } = useDataContext();
datasets.filter(isDefined).forEach((dataset) => {
valuesStore.prefetch({ dataset, selection });
});
}
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>(
dataset: D,
selection?: string,
): Value<D>;
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>(
dataset: D | undefined,
selection?: string,
): Value<D> | undefined;
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>(
dataset: D | undefined,
selection?: string,
): Value<D> | undefined {
const { valuesStore } = useDataContext();
if (!dataset) {
return undefined;
}
// If `selection` is undefined, the entire dataset will be fetched
const value = valuesStore.get({ dataset, selection });
assertDatasetValue(value, dataset);
return value;
}
export function useDatasetValues<D extends Dataset<ArrayShape | ScalarShape>>(
datasets: D[],
selection?: string,
): Value<D>[];
export function useDatasetValues<D extends Dataset<ArrayShape | ScalarShape>>(
datasets: (D | undefined)[],
selection?: string,
): (Value<D> | undefined)[];
export function useDatasetValues<D extends Dataset<ArrayShape | ScalarShape>>(
datasets: (D | undefined)[],
selection?: string,
): (Value<D> | undefined)[] {
const { valuesStore } = useDataContext();
return datasets.map((dataset) => {
if (!dataset) {
return undefined;
}
const value = valuesStore.get({ dataset, selection });
assertDatasetValue(value, dataset);
return value;
});
}
export function useBaseArray<T extends ArrayValue | undefined>(
value: T,
rawDims: number[],
): T extends ArrayValue ? NdArray<T> : undefined;
export function useBaseArray(
value: ArrayValue | undefined,
rawDims: number[],
): NdArray<ArrayValue> | undefined {
return useMemo(() => getBaseArray(value, rawDims), [value, rawDims]);
}
export function useMappedArray<T extends ArrayValue | undefined>(
value: T,
dims: number[],
mapping: DimensionMapping,
): T extends ArrayValue ? NdArray<T> : undefined;
export function useMappedArray(
value: ArrayValue | undefined,
dims: number[],
mapping: DimensionMapping,
): NdArray<ArrayValue> | undefined {
const baseArray = useBaseArray(value, dims);
return useMemo(() => applyMapping(baseArray, mapping), [baseArray, mapping]);
}
export function useMappedArrays<T extends ArrayValue>(
values: T[],
dims: number[],
mapping: DimensionMapping,
): NdArray<T>[];
export function useMappedArrays<T extends ArrayValue>(
values: (T | undefined)[],
dims: number[],
mapping: DimensionMapping,
): (NdArray<T> | undefined)[];
export function useMappedArrays(
values: (ArrayValue | undefined)[],
dims: number[],
mapping: DimensionMapping,
): (NdArray<ArrayValue> | undefined)[] {
const baseArrays = useMemo(
() => values.map((arr) => getBaseArray(arr, dims)),
[dims, ...values], // eslint-disable-line react-hooks/exhaustive-deps
);
return useMemo(
() => baseArrays.map((ndArr) => applyMapping(ndArr, mapping)),
[baseArrays, mapping],
);
}
export function useSlicedDimsAndMapping(
dims: number[],
dimMapping: DimensionMapping,
): [number[], DimensionMapping] {
return useMemo(
() => [
dims.filter((_, i) => isAxis(dimMapping[i])),
dimMapping.filter(isAxis),
],
[dimMapping, dims],
);
}
export function useIgnoreFillValue(
dataset: Dataset,
): ((val: number) => boolean) | undefined {
const { attrValuesStore } = useDataContext();
const rawFillValue = attrValuesStore.getSingle(dataset, '_FillValue');
const wrappedFillValue = castArray(rawFillValue);
const DTypedArray = typedArrayFromDType(dataset.type);
// Cast fillValue in the type of the dataset values to be able to use `===` for the comparison
const fillValue =
DTypedArray && typeof wrappedFillValue[0] === 'number'
? new DTypedArray(wrappedFillValue as number[])[0]
: undefined;
return useMemo(() => {
return fillValue !== undefined
? (val: number) => val === fillValue
: undefined;
}, [fillValue]);
}