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

Fetch boolean data from h5grove as binary #1709

Merged
merged 1 commit into from
Sep 13, 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 @@ -1431,6 +1431,11 @@ exports[`test file matches snapshot 1`] = `
"size": 64,
},
"bool": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
loichuder marked this conversation as resolved.
Show resolved Hide resolved
"class": "Boolean",
},
"cplx": {
Expand Down Expand Up @@ -1605,6 +1610,11 @@ exports[`test file matches snapshot 1`] = `
},
"shape": null,
"type": {
"base": {
"class": "Integer (unsigned)",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": null,
Expand All @@ -1629,9 +1639,14 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": false,
"value": 0,
},
{
"name": "bool_true_scalar",
Expand All @@ -1653,9 +1668,14 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": true,
"value": 1,
},
{
"name": "bool_2D",
Expand All @@ -1680,17 +1700,22 @@ exports[`test file matches snapshot 1`] = `
4,
],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": [
true,
false,
true,
true,
false,
false,
true,
false,
"value": Int8Array [
1,
0,
1,
1,
0,
0,
1,
0,
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/providers/h5grove/h5grove-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
export class H5GroveApi extends DataProviderApi {
private readonly client: AxiosInstance;

/* API compatible with h5grove@2.1.0 */
/* API compatible with h5grove@2.3.0 */
public constructor(
url: string,
filepath: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ describe('parseDType', () => {
parseDType({
class: 8,
size: 2,
base: { class: 0, size: 1, order: 0, sign: 0 },
base: { class: 0, size: 1, order: 0, sign: 1 },
members: { FALSE: 0, TRUE: 1 },
}),
).toStrictEqual(boolType());
).toStrictEqual(boolType(intType(8)));
});

it('should convert array types', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('convertHsdsType', () => {
mapping: { FALSE: 0, TRUE: 1 },
};

expect(convertHsdsType(boolEnum)).toStrictEqual(boolType());
expect(convertHsdsType(boolEnum)).toStrictEqual(boolType(intType(8)));
});

it('should convert the complex compound type into Complex type', () => {
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
Expand Up @@ -120,7 +120,7 @@ export function makeMockFile(): GroupWithChildren {
string: strType(),
int: intType(),
float: floatType(),
bool: boolType(),
bool: boolType(intType(8)),
complex: cplxType(floatType()),
}),
}),
Expand All @@ -134,7 +134,7 @@ export function makeMockFile(): GroupWithChildren {
string: strType(),
int: intType(),
float: floatType(),
bool: boolType(),
bool: boolType(intType(8)),
complex: cplxType(floatType()),
}),
}),
Expand Down
7 changes: 4 additions & 3 deletions packages/app/src/providers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEnumType, isNumericType } from '@h5web/shared/guards';
import { isBoolType, isEnumType, isNumericType } from '@h5web/shared/guards';
import type {
ArrayShape,
Dataset,
Expand All @@ -8,13 +8,14 @@ import type {
import { DTypeClass } from '@h5web/shared/hdf5-models';
import type { OnProgress } from '@h5web/shared/react-suspense-fetch';
import type { AxiosProgressEvent } from 'axios';
import { isAxiosError } from 'axios';

import type { DataProviderApi } from './api';

export const CANCELLED_ERROR_MSG = 'Request cancelled';

export function typedArrayFromDType(dtype: DType) {
if (isEnumType(dtype)) {
if (isEnumType(dtype) || isBoolType(dtype)) {
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the dumb question: isn't bool a particular type of enum ? So that isEnumType should encompass bool ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We split the two in our internal types and the purpose of these guards is to narrow our internal types. We need two separate types so Primitive<BooleanType> can resolve to number | boolean. But we may be able to reconsider this choice with #1679 (comment)

return typedArrayFromDType(dtype.base);
}

Expand Down Expand Up @@ -72,7 +73,7 @@ export async function getValueOrError(
try {
return await api.getValue({ dataset });
} catch (error) {
return error;
return isAxiosError(error) ? error.message : error;
loichuder marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/vis-packs/core/visualizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ const scalarInt = dataset('int', intType(), []);
const scalarUint = dataset('uint', uintType(), []);
const scalarFloat = dataset('float', floatType(), []);
const scalarStr = dataset('float', strType(), []);
const scalarBool = dataset('bool', boolType(), []);
const scalarBool = dataset('bool', boolType(intType(8)), []);
const scalarCplx = dataset('cplx', cplxType(floatType()), []);
const scalarCompound = dataset('comp', compoundType({ int: intType() }), []);
const oneDInt = dataset('int_1d', intType(), [5]);
const oneDUint = dataset('uint_1d', uintType(), [5]);
const oneDBool = dataset('bool_1d', boolType(), [3]);
const oneDBool = dataset('bool_1d', boolType(intType(8)), [3]);
const oneDCplx = dataset('cplx_1d', cplxType(floatType()), [10]);
const oneDCompound = dataset('comp_1d', compoundType({ int: intType() }), [5]);
const twoDInt = dataset('int_2d', intType(), [5, 3]);
const twoDUint = dataset('uint_2d', uintType(), [5, 3]);
const twoDBool = dataset('bool_2d', boolType(), [3, 2]);
const twoDBool = dataset('bool_2d', boolType(intType(8)), [3, 2]);
const twoDCplx = dataset('cplx_2d', cplxType(floatType()), [2, 2]);
const twoDStr = dataset('str_2d', strType(), [5, 3]);
const threeDFloat = dataset('float_3d', intType(), [5, 3, 1]);
Expand Down
25 changes: 25 additions & 0 deletions packages/h5wasm/src/__snapshots__/h5wasm-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,11 @@ exports[`test file matches snapshot 1`] = `
"size": 64,
},
"bool": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"cplx": {
Expand Down Expand Up @@ -1812,6 +1817,11 @@ exports[`test file matches snapshot 1`] = `
},
"shape": null,
"type": {
"base": {
"class": "Integer (unsigned)",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": null,
Expand All @@ -1836,6 +1846,11 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": 0,
Expand All @@ -1860,6 +1875,11 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": 1,
Expand Down Expand Up @@ -1887,6 +1907,11 @@ exports[`test file matches snapshot 1`] = `
4,
],
"type": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"size": 8,
},
"class": "Boolean",
},
"value": Int8Array [
Expand Down
12 changes: 0 additions & 12 deletions packages/lib/src/vis/models.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import type {
BooleanType,
ComplexType,
NumericType,
StringType,
} from '@h5web/shared/hdf5-models';
import type {
AxisScaleType,
ColorScaleType,
Expand Down Expand Up @@ -87,12 +81,6 @@ export interface AxisParams {

export type Coords = [x: number, y: number];

export type PrintableType =
loichuder marked this conversation as resolved.
Show resolved Hide resolved
| BooleanType
| NumericType
| ComplexType
| StringType;

export interface HistogramParams {
values: NumArray;
bins: NumArray;
Expand Down
9 changes: 5 additions & 4 deletions packages/shared/src/hdf5-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export type DType =
| ReferenceType
| UnknownType;

export interface BooleanType {
class: DTypeClass.Bool;
}

export interface NumericType {
class: DTypeClass.Integer | DTypeClass.Unsigned | DTypeClass.Float;
size: number;
Expand Down Expand Up @@ -168,6 +164,11 @@ export interface ArrayType<T extends DType = DType> {
dims?: number[];
}

export interface BooleanType {
class: DTypeClass.Bool;
base: NumericType; // typically int8 with h5py
}

export interface EnumType {
class: DTypeClass.Enum;
base: NumericType; // technically, only int/uint
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/src/hdf5-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ export function strType(
};
}

export function boolType(): BooleanType {
return { class: DTypeClass.Bool };
}

export function cplxType(
realType: NumericType,
imagType = realType,
Expand Down Expand Up @@ -139,6 +135,10 @@ export function arrayType<T extends DType>(
};
}

export function boolType(baseType: NumericType): BooleanType {
return { class: DTypeClass.Bool, base: baseType };
}

export function enumType(
baseType: NumericType,
hdf5Mapping: Record<string, number>,
Expand All @@ -162,7 +162,7 @@ export function enumOrBoolType(
hdf5Mapping.FALSE === 0 &&
hdf5Mapping.TRUE === 1
) {
return boolType();
return boolType(baseType);
}

return enumType(baseType, hdf5Mapping);
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/mock-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
buildEntityPath,
cplxType,
floatType,
intType,
strType,
unknownType,
} from './hdf5-utils';
Expand Down Expand Up @@ -297,7 +298,7 @@ function guessType(value: unknown): DType {
}

if (typeof value === 'boolean') {
return boolType();
return boolType(intType(8));
}

if (
Expand Down
Loading