From b0d1ac91c8760330800c544acba703bcf3448d96 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Tue, 23 Jan 2024 14:27:47 +0100 Subject: [PATCH 1/3] Move sample.h5 script to root of monorepo --- .../h5wasm/create_h5_sample.py => create_h5_sample.py | 2 +- packages/h5wasm/src/h5wasm-api.test.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) rename packages/h5wasm/create_h5_sample.py => create_h5_sample.py (99%) diff --git a/packages/h5wasm/create_h5_sample.py b/create_h5_sample.py similarity index 99% rename from packages/h5wasm/create_h5_sample.py rename to create_h5_sample.py index 0bfaf49f1..4457a347a 100644 --- a/packages/h5wasm/create_h5_sample.py +++ b/create_h5_sample.py @@ -7,7 +7,7 @@ raise RuntimeError("Python 2 is not supported") BASE_PATH = os.path.dirname(os.path.realpath(sys.argv[0])) -DIST_PATH = os.path.join(BASE_PATH, "dist-h5") +DIST_PATH = os.path.join(BASE_PATH, "dist") os.makedirs(DIST_PATH, exist_ok=True) h5py.get_config().track_order = True diff --git a/packages/h5wasm/src/h5wasm-api.test.ts b/packages/h5wasm/src/h5wasm-api.test.ts index 42dc4a1b6..282ace81b 100644 --- a/packages/h5wasm/src/h5wasm-api.test.ts +++ b/packages/h5wasm/src/h5wasm-api.test.ts @@ -1,3 +1,7 @@ +import { existsSync } from 'node:fs'; +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; + import { assertDataset, assertGroup, @@ -9,14 +13,11 @@ import type { Dataset, ScalarShape, } from '@h5web/shared/hdf5-models'; -import { existsSync } from 'fs'; -import { readFile } from 'fs/promises'; -import path from 'path'; import { expect, test } from 'vitest'; import { H5WasmApi } from './h5wasm-api'; -const LOCAL_TEST_FILE = path.resolve(__dirname, '../dist-h5/sample.h5'); +const LOCAL_TEST_FILE = path.resolve(process.cwd(), 'dist/sample.h5'); const REMOTE_TEST_FILE = 'http://www.silx.org/pub/h5web/sample.h5'; // `https` would complicate things... async function loadTestFile(): Promise { From 1e19e23a99931f14620f9465bbf4c02440737336 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Tue, 23 Jan 2024 15:02:32 +0100 Subject: [PATCH 2/3] Test `H5GroveApi` --- packages/app/src/index.ts | 7 +- .../__snapshots__/h5grove-api.test.ts.snap | 1088 +++++++++++++++++ .../src/providers/h5grove/h5grove-api.test.ts | 38 + packages/app/src/providers/utils.ts | 12 + packages/h5wasm/src/h5wasm-api.test.ts | 17 +- 5 files changed, 1145 insertions(+), 17 deletions(-) create mode 100644 packages/app/src/providers/h5grove/__snapshots__/h5grove-api.test.ts.snap create mode 100644 packages/app/src/providers/h5grove/h5grove-api.test.ts diff --git a/packages/app/src/index.ts b/packages/app/src/index.ts index dae25242d..ee982098d 100644 --- a/packages/app/src/index.ts +++ b/packages/app/src/index.ts @@ -22,5 +22,10 @@ export { assertEnvVar } from '@h5web/shared/guards'; export { default as DataProvider } from './providers/DataProvider'; export { DataProviderApi } from './providers/api'; export type { ValuesStoreParams } from './providers/models'; -export { flattenValue, getNameFromPath, sliceValue } from './providers/utils'; +export { + flattenValue, + getNameFromPath, + sliceValue, + getValueOrError, +} from './providers/utils'; export { assertNonNull } from '@h5web/shared/guards'; diff --git a/packages/app/src/providers/h5grove/__snapshots__/h5grove-api.test.ts.snap b/packages/app/src/providers/h5grove/__snapshots__/h5grove-api.test.ts.snap new file mode 100644 index 000000000..247c44bab --- /dev/null +++ b/packages/app/src/providers/h5grove/__snapshots__/h5grove-api.test.ts.snap @@ -0,0 +1,1088 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`test file matches snapshot 1`] = ` +[ + { + "name": "int8_scalar", + "rawType": "|i1", + "shape": [], + "type": { + "class": "Integer", + "endianness": "little-endian", + "size": 8, + }, + "value": -128, + }, + { + "name": "int8_2D", + "rawType": "|i1", + "shape": [ + 2, + 3, + ], + "type": { + "class": "Integer", + "endianness": "little-endian", + "size": 8, + }, + "value": Int8Array [ + 0, + 1, + 2, + 3, + 4, + 5, + ], + }, + { + "name": "int16_scalar", + "rawType": "i4", + "shape": [], + "type": { + "class": "Integer", + "endianness": "big-endian", + "size": 32, + }, + "value": 0, + }, + { + "name": "int32_2D", + "rawType": "f4", + "shape": [], + "type": { + "class": "Float", + "endianness": "big-endian", + "size": 32, + }, + "value": 0, + }, + { + "name": "float32_2D", + "rawType": "c16", + "shape": [], + "type": { + "class": "Complex", + "imagType": { + "class": "Float", + "endianness": "big-endian", + "size": 64, + }, + "realType": { + "class": "Float", + "endianness": "big-endian", + "size": 64, + }, + }, + "value": [ + 1, + 2, + ], + }, + { + "name": "complex128_2D", + "rawType": "", + }, + { + "name": "reference_region_scalar", + "rawType": "|O", + "shape": [], + "type": { + "charSet": "UTF-8", + "class": "String", + }, + "value": "", + }, + { + "name": "bool_empty", + "rawType": "|b1", + "shape": null, + "type": { + "class": "Boolean", + }, + "value": null, + }, + { + "name": "bool_false_scalar", + "rawType": "|b1", + "shape": [], + "type": { + "class": "Boolean", + }, + "value": false, + }, + { + "name": "bool_true_scalar", + "rawType": "|b1", + "shape": [], + "type": { + "class": "Boolean", + }, + "value": true, + }, + { + "name": "bool_2D", + "rawType": "|b1", + "shape": [ + 2, + 4, + ], + "type": { + "class": "Boolean", + }, + "value": [ + true, + false, + true, + true, + false, + false, + true, + false, + ], + }, + { + "name": "enum_uint8_scalar", + "rawType": "|u1", + "shape": [], + "type": { + "class": "Integer (unsigned)", + "endianness": "little-endian", + "size": 8, + }, + "value": 1, + }, + { + "name": "enum_int32_scalar", + "rawType": " { + const api = new H5GroveApi(H5GROVE_URL, TEST_FILE, { + params: { file: TEST_FILE }, + }); + + const root = await api.getEntity('/'); + assertGroup(root); + assertGroupWithChildren(root); + + const children = await Promise.all( + root.children.map(async (child) => { + assertDataset(child); + const { name, shape, type, rawType } = child; + + const value = hasNonNullShape(child) + ? await getValueOrError(api, child) + : null; + + return { name, shape, type, rawType, value }; + }), + ); + + expect(children).toMatchSnapshot(); +}); diff --git a/packages/app/src/providers/utils.ts b/packages/app/src/providers/utils.ts index b17cbccf7..3caf0cc06 100644 --- a/packages/app/src/providers/utils.ts +++ b/packages/app/src/providers/utils.ts @@ -10,6 +10,7 @@ import { DTypeClass } from '@h5web/shared/hdf5-models'; import { AxiosError } from 'axios'; import ndarray from 'ndarray'; +import type { DataProviderApi } from '..'; import { applyMapping } from '../vis-packs/core/utils'; export const CANCELLED_ERROR_MSG = 'Request cancelled'; @@ -112,3 +113,14 @@ export function typedArrayFromDType(dtype: DType) { return undefined; } + +export async function getValueOrError( + api: DataProviderApi, + dataset: Dataset, +): Promise { + try { + return await api.getValue({ dataset }); + } catch (error) { + return error; + } +} diff --git a/packages/h5wasm/src/h5wasm-api.test.ts b/packages/h5wasm/src/h5wasm-api.test.ts index 282ace81b..15b3b9ae4 100644 --- a/packages/h5wasm/src/h5wasm-api.test.ts +++ b/packages/h5wasm/src/h5wasm-api.test.ts @@ -2,17 +2,13 @@ import { existsSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import path from 'node:path'; +import { DataProviderApi, getValueOrError } from '@h5web/app'; import { assertDataset, assertGroup, assertGroupWithChildren, hasNonNullShape, } from '@h5web/shared/guards'; -import type { - ArrayShape, - Dataset, - ScalarShape, -} from '@h5web/shared/hdf5-models'; import { expect, test } from 'vitest'; import { H5WasmApi } from './h5wasm-api'; @@ -29,17 +25,6 @@ async function loadTestFile(): Promise { return resp.arrayBuffer(); } -async function getValueOrError( - api: H5WasmApi, - dataset: Dataset, -): Promise { - try { - return await api.getValue({ dataset }); - } catch (error) { - return error; - } -} - test('test file matches snapshot', async () => { const buffer = await loadTestFile(); const api = new H5WasmApi('sample.h5', buffer); From 0f679302397ed4113de4c61c1dfa9420283f821d Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Tue, 23 Jan 2024 15:02:50 +0100 Subject: [PATCH 3/3] Add `sample.h5` to the demo's homepage --- apps/demo/src/Home.tsx | 46 ++++++++++++++++---------- packages/app/src/providers/utils.ts | 2 +- packages/h5wasm/src/h5wasm-api.test.ts | 2 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/apps/demo/src/Home.tsx b/apps/demo/src/Home.tsx index 351c40ec1..f42ca8009 100644 --- a/apps/demo/src/Home.tsx +++ b/apps/demo/src/Home.tsx @@ -87,16 +87,22 @@ function Home() { group (Argonne National Laboratory).
  • - grove.h5 - a file used - to test the H5GroveProvider provider; it contains - datasets with NaN, Infinity, boolean - and complex values, as well as RGB images and 4D stacks. + grove.h5 - a file + designed to demonstrate the capabilities of{' '} + H5GroveProvider; it contains datasets with{' '} + NaN, Infinity, boolean and complex + values, as well as RGB images and 4D stacks.
  • links.h5 - a file with external links, soft links and a virtual dataset to test link resolution.
  • +
  • + sample.h5 - the file + used to test H5GroveApi and H5WasmApi{' '} + internally. +
  • tall.h5 - the demo file of HSDS. @@ -144,20 +150,24 @@ function Home() { silx.org :{' '} - {['water_224.h5', 'epics.h5', 'grove.h5', 'tall.h5 '].map( - (filename, index) => ( - - {index > 0 && ', '} - - {filename} - - - ), - )} + {[ + 'water_224.h5', + 'epics.h5', + 'grove.h5', + 'sample.h5', + 'tall.h5 ', + ].map((filename, index) => ( + + {index > 0 && ', '} + + {filename} + + + ))}

    diff --git a/packages/app/src/providers/utils.ts b/packages/app/src/providers/utils.ts index 3caf0cc06..07a56891a 100644 --- a/packages/app/src/providers/utils.ts +++ b/packages/app/src/providers/utils.ts @@ -10,8 +10,8 @@ import { DTypeClass } from '@h5web/shared/hdf5-models'; import { AxiosError } from 'axios'; import ndarray from 'ndarray'; -import type { DataProviderApi } from '..'; import { applyMapping } from '../vis-packs/core/utils'; +import type { DataProviderApi } from './api'; export const CANCELLED_ERROR_MSG = 'Request cancelled'; diff --git a/packages/h5wasm/src/h5wasm-api.test.ts b/packages/h5wasm/src/h5wasm-api.test.ts index 15b3b9ae4..b65aea7c7 100644 --- a/packages/h5wasm/src/h5wasm-api.test.ts +++ b/packages/h5wasm/src/h5wasm-api.test.ts @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import path from 'node:path'; -import { DataProviderApi, getValueOrError } from '@h5web/app'; +import { getValueOrError } from '@h5web/app'; import { assertDataset, assertGroup,