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

Parse HDF5 string padding metadata #1659

Merged
merged 1 commit into from
Jun 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,14 @@ exports[`test file matches snapshot 1`] = `
"cset": 0,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
"shape": null,
"type": {
"charSet": "ASCII",
"class": "String",
"strPad": "null-terminated",
},
"value": null,
},
Expand All @@ -702,12 +704,14 @@ exports[`test file matches snapshot 1`] = `
"cset": 0,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
"shape": [],
"type": {
"charSet": "ASCII",
"class": "String",
"strPad": "null-terminated",
},
"value": "Some text",
},
Expand All @@ -718,13 +722,15 @@ exports[`test file matches snapshot 1`] = `
"cset": 0,
"dtype": "|S9",
"size": 9,
"strpad": 1,
"vlen": false,
},
"shape": [],
"type": {
"charSet": "ASCII",
"class": "String",
"length": 9,
"strPad": "null-terminated",
},
"value": "Some text",
},
Expand All @@ -735,12 +741,14 @@ exports[`test file matches snapshot 1`] = `
"cset": 1,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
"shape": [],
"type": {
"charSet": "UTF-8",
"class": "String",
"strPad": "null-terminated",
},
"value": "Some text",
},
Expand All @@ -751,6 +759,7 @@ exports[`test file matches snapshot 1`] = `
"cset": 1,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
"shape": [
Expand All @@ -759,6 +768,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"charSet": "UTF-8",
"class": "String",
"strPad": "null-terminated",
},
"value": [
"foo",
Expand All @@ -773,13 +783,15 @@ exports[`test file matches snapshot 1`] = `
"cset": 1,
"dtype": "|S9",
"size": 9,
"strpad": 1,
"vlen": false,
},
"shape": [],
"type": {
"charSet": "UTF-8",
"class": "String",
"length": 9,
"strPad": "null-terminated",
},
"value": "Some text",
},
Expand Down Expand Up @@ -1230,6 +1242,7 @@ exports[`test file matches snapshot 1`] = `
"cset": 1,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
},
Expand All @@ -1252,6 +1265,7 @@ exports[`test file matches snapshot 1`] = `
"utf-8": {
"charSet": "UTF-8",
"class": "String",
"strPad": "null-terminated",
},
},
},
Expand Down Expand Up @@ -1289,6 +1303,7 @@ exports[`test file matches snapshot 1`] = `
"cset": 1,
"dtype": "|O",
"size": 8,
"strpad": 0,
"vlen": true,
},
},
Expand All @@ -1313,6 +1328,7 @@ exports[`test file matches snapshot 1`] = `
"utf-8": {
"charSet": "UTF-8",
"class": "String",
"strPad": "null-terminated",
},
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/providers/h5grove/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface H5GroveTimeType extends H5GroveBaseType {
export interface H5GroveStringType extends H5GroveBaseType {
class: 3;
cset: number;
strPad?: number; // optional for backwards compatibility with h5grove <= 2.1.0
vlen: boolean;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import {
arrayType,
bitfieldType,
Expand Down Expand Up @@ -42,13 +42,13 @@ describe('parseDType', () => {
it('should convert string types', () => {
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: false }),
).toStrictEqual(strType(H5T_CSET.ASCII, 6));
).toStrictEqual(strType(H5T_CSET.ASCII, undefined, 6));
expect(
parseDType({ class: 3, size: 6, cset: 0, vlen: true }),
).toStrictEqual(strType(H5T_CSET.ASCII));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: false }),
).toStrictEqual(strType(H5T_CSET.UTF8, 6));
parseDType({ class: 3, size: 6, cset: 1, strPad: 1, vlen: false }),
).toStrictEqual(strType(H5T_CSET.UTF8, H5T_STR.NULLPAD, 6));
expect(
parseDType({ class: 3, size: 6, cset: 1, vlen: true }),
).toStrictEqual(strType(H5T_CSET.UTF8));
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export function parseDType(type: H5GroveType): DType {
}

if (h5tClass === H5T_CLASS.STRING) {
const { cset, vlen } = type;
return strType(cset, vlen ? undefined : size);
const { cset, strPad, vlen } = type;
return strType(cset, strPad, vlen ? undefined : size);
}

if (h5tClass === H5T_CLASS.BITFIELD) {
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/providers/hsds/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { H5T_CSET, H5T_STR } from '@h5web/shared/h5t';
import type { Entity } from '@h5web/shared/hdf5-models';

/* --------------------- */
Expand Down Expand Up @@ -104,7 +105,8 @@ export interface HsdsNumericType {

export interface HsdsStringType {
class: 'H5T_STRING';
charSet: `H5T_CSET_${'ASCII' | 'UTF8'}`;
charSet: `H5T_CSET_${keyof typeof H5T_CSET}`;
strPad: `H5T_STR_${keyof typeof H5T_STR}`;
length: number | 'H5T_VARIABLE';
}

Expand Down
10 changes: 7 additions & 3 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import type { DType } from '@h5web/shared/hdf5-models';
import {
arrayType,
Expand Down Expand Up @@ -52,22 +52,26 @@ describe('convertHsdsType', () => {
const asciiStr: HsdsStringType = {
class: 'H5T_STRING',
charSet: 'H5T_CSET_ASCII',
strPad: 'H5T_STR_NULLTERM',
length: 25,
};

expect(convertHsdsType(asciiStr)).toStrictEqual(
strType(H5T_CSET.ASCII, 25),
strType(H5T_CSET.ASCII, H5T_STR.NULLTERM, 25),
);
});

it('should convert variable-length UTF-8 string type', () => {
const unicodeStr: HsdsStringType = {
class: 'H5T_STRING',
charSet: 'H5T_CSET_UTF8',
strPad: 'H5T_STR_NULLPAD',
length: 'H5T_VARIABLE',
};

expect(convertHsdsType(unicodeStr)).toStrictEqual(strType(H5T_CSET.UTF8));
expect(convertHsdsType(unicodeStr)).toStrictEqual(
strType(H5T_CSET.UTF8, H5T_STR.NULLPAD),
);
});

it('should convert integer types', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertArray, isGroup } from '@h5web/shared/guards';
import { H5T_CSET, H5T_ORDER } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_ORDER, H5T_STR } from '@h5web/shared/h5t';
import type {
ArrayShape,
Attribute,
Expand Down Expand Up @@ -138,11 +138,14 @@ export function convertHsdsType(hsdsType: HsdsType): DType {
return convertHsdsCompoundType(hsdsType);

case 'H5T_STRING': {
const { charSet, length } = hsdsType;
const { charSet, strPad, length } = hsdsType;
return strType(
H5T_CSET[
charSet.slice(charSet.lastIndexOf('_') + 1) as keyof typeof H5T_CSET
],
H5T_STR[
strPad.slice(strPad.lastIndexOf('_') + 1) as keyof typeof H5T_STR
],
length === 'H5T_VARIABLE' ? undefined : length,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/providers/mock/mock-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H5T_CSET } from '@h5web/shared/h5t';
import { H5T_CSET, H5T_STR } from '@h5web/shared/h5t';
import type { GroupWithChildren } from '@h5web/shared/hdf5-models';
import {
arrayType,
Expand Down Expand Up @@ -62,7 +62,7 @@ export function makeMockFile(): GroupWithChildren {
}),
scalar('scalar_compound', ['foo', 2], {
type: compoundType({
str: strType(H5T_CSET.ASCII, 3),
str: strType(H5T_CSET.ASCII, H5T_STR.NULLPAD, 3),
int: intType(8),
}),
attributes: [
Expand Down
2 changes: 1 addition & 1 deletion packages/h5wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"comlink": "4.4.1",
"h5wasm": "0.7.4",
"h5wasm": "0.7.5",
"nanoid": "5.0.7"
},
"devDependencies": {
Expand Down
Loading