Skip to content

Commit 48de07b

Browse files
committed
added support for boolean types
1 parent 7387b38 commit 48de07b

File tree

6 files changed

+131
-8
lines changed

6 files changed

+131
-8
lines changed

data/json/bool_attr.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"apiVersion": "1.1.0",
3+
"groups": {
4+
"a58ed768-0370-11e7-926f-3c15c2da029e": {
5+
"alias": [
6+
"/"
7+
],
8+
"attributes": [
9+
{
10+
"name": "attr1",
11+
"shape": {
12+
"class": "H5S_SIMPLE",
13+
"dims": [
14+
4
15+
]
16+
},
17+
"type": {
18+
"base": {
19+
"base": "H5T_STD_I8LE",
20+
"class": "H5T_INTEGER"
21+
},
22+
"class": "H5T_ENUM",
23+
"mapping": {
24+
"FALSE": 0,
25+
"TRUE": 1
26+
}
27+
},
28+
"value": [
29+
false,
30+
true,
31+
false,
32+
true
33+
]
34+
}
35+
]
36+
}
37+
},
38+
"root": "a58ed768-0370-11e7-926f-3c15c2da029e"
39+
}

data/json/bool_dset.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"apiVersion": "1.1.0",
3+
"datasets": {
4+
"b59c0308-0370-11e7-a673-3c15c2da029e": {
5+
"alias": [
6+
"/DS1"
7+
],
8+
"creationProperties": {
9+
"allocTime": "H5D_ALLOC_TIME_LATE",
10+
"fillTime": "H5D_FILL_TIME_IFSET",
11+
"layout": {
12+
"class": "H5D_CONTIGUOUS"
13+
}
14+
},
15+
"shape": {
16+
"class": "H5S_SIMPLE",
17+
"dims": [
18+
4
19+
]
20+
},
21+
"type": {
22+
"base": {
23+
"base": "H5T_STD_I8LE",
24+
"class": "H5T_INTEGER"
25+
},
26+
"class": "H5T_ENUM",
27+
"mapping": {
28+
"FALSE": 0,
29+
"TRUE": 1
30+
}
31+
},
32+
"value": [
33+
false,
34+
true,
35+
false,
36+
true
37+
]
38+
}
39+
},
40+
"groups": {
41+
"b59a29f0-0370-11e7-86c9-3c15c2da029e": {
42+
"alias": [
43+
"/"
44+
],
45+
"links": [
46+
{
47+
"class": "H5L_TYPE_HARD",
48+
"collection": "datasets",
49+
"id": "b59c0308-0370-11e7-a673-3c15c2da029e",
50+
"title": "DS1"
51+
}
52+
]
53+
}
54+
},
55+
"root": "b59a29f0-0370-11e7-86c9-3c15c2da029e"
56+
}

h5json/hdf5db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,6 @@ def createTypeFromItem(self, attr_type):
10291029
raise IOError(errno.ENXIO, msg)
10301030
dt = tgt # can use the object as the dt parameter
10311031
else:
1032-
10331032
try:
10341033
dt = createDataType(attr_type)
10351034
except KeyError as ke:

h5json/hdf5dtype.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,21 @@ def getTypeItem(dt):
266266

267267
elif dt.kind == 'b':
268268
# boolean type - h5py stores as enum
269-
if dt.base == dt:
270-
raise TypeError("Expected base type to be different than parent")
271-
baseType = getBaseType(dt)
269+
# assume LE unless the numpy byteorder is '>'
270+
byteorder = 'LE'
271+
if dt.base.byteorder == '>':
272+
byteorder = 'BE'
273+
# this mapping is an h5py convention for boolean support
274+
mapping = {
275+
"FALSE": 0,
276+
"TRUE": 1
277+
}
272278
type_info['class'] = 'H5T_ENUM'
273-
type_info['mapping'] = {"false": 0, "true": 1}
274-
type_info['base'] = getTypeItem(dt.base)
279+
type_info['mapping'] = mapping
280+
base_info = { "class": "H5T_INTEGER" }
281+
base_info['base'] = "H5T_STD_I8" + byteorder
282+
type_info["base"] = base_info
283+
275284
elif dt.kind == 'f':
276285
# floating point type
277286
type_info['class'] = 'H5T_FLOAT'
@@ -525,6 +534,22 @@ def createDataType(typeItem):
525534
subtypes.append((field['name'], dt)) # append tuple
526535

527536
dtRet = np.dtype(subtypes)
537+
elif typeClass == 'H5T_ENUM':
538+
if 'base' not in typeItem:
539+
raise KeyError("Expected 'base' to be provided for enum type")
540+
base_json = typeItem["base"]
541+
if 'class' not in base_json:
542+
raise KeyError("Expected class field in base type")
543+
if base_json['class'] != 'H5T_INTEGER':
544+
raise TypeError("Only integer base types can be used with enum type")
545+
if 'mapping' not in typeItem:
546+
raise KeyError("'mapping' not provided for enum type")
547+
mapping = typeItem["mapping"]
548+
if len(mapping) == 0:
549+
raise KeyError("empty enum map")
550+
dt = createBaseDataType(base_json)
551+
dtRet = special_dtype(enum=(dt, mapping))
552+
528553
else:
529554
dtRet = createBaseDataType(typeItem) # create non-compound dt
530555
return dtRet

test/integ/h5tojson_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
# bitfields not supported yet
3333
# "bitfield_attr.h5",
3434
# "bitfield_dset.h5",
35+
"bool_attr.h5",
36+
"bool_dset.h5",
3537
"committed_type.h5",
3638
"compound.h5",
3739
"compound_array.h5",

test/integ/jsontoh5_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
# bitfields not supported yet
3232
# "bitfield_attr.json",
3333
# "bitfield_dset.json",
34+
"bool_attr.json",
35+
"bool_dset.json",
3436
"committed_type.json",
3537
"compound.json",
3638
# "compound_array.json",
@@ -44,8 +46,8 @@
4446
"dset_creationprop.json",
4547
# "dset_gzip.json",
4648
"empty.json",
47-
# "enum_attr.json",
48-
# "enum_dset.json",
49+
"enum_attr.json",
50+
"enum_dset.json",
4951
"fillvalue.json",
5052
"h5ex_d_alloc.json",
5153
"h5ex_d_checksum.json",

0 commit comments

Comments
 (0)