'Is it possible to write a boolean to an hdf5 file in R that Python will recognise as an 8 bit enum

I want to be able to save an R TRUE/FALSE value in HDF5 such that when reading the file into Python and checking the data type is Boolean the test will pass. At the moment I can't do this. If I use:

library(rhdf5)
h5file = H5Fcreate("newfile.h5")
h5space = H5Screate_simple(1,NULL, native = TRUE)
h5dataset1 = H5Dcreate(h5file, "dataset1", "H5T_NATIVE_HBOOL", h5space)
H5Dwrite(h5dataset1, TRUE)
h5closeAll()

If I then inspect the variable using HDFView (3.1.3) I can see the saved object is stored as an 8-bit unsigned integer.

In order to pass a Python data type test along the lines of np.array(getattr(x,attr)).dtype == bool the type needs to register in HDFView as follows: 8-bit enum (0=FALSE, 1=TRUE).

How can I write an object of this type using either of the two R HDF5 packages rhdf5 or hdf5r?



Solution 1:[1]

You may want to explore a third option/package which is HDFql. To create an 8 bit enum dataset named dataset1 containing two members (FALSE with value 0 and TRUE with value 1) it can be done as follows using HDFql in R:

source("HDFql.R")

hdfql_execute("CREATE FILE newfile.h5")

hdfql_execute("CREATE DATASET newfile.h5 dataset1 AS ENUMERATION(FALSE AS 0, TRUE AS 1)")

For additional information, please check HDFql reference manual and examples on how it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SOG