hdf5load equivalent for RHDF5
3
0
Entering edit mode
@manfredodiporcia-9952
Last seen 6.7 years ago

Since the standard R package to manage HDF5 format has changed to rhdf5, some features are no longer available. One of those is hdf5load:

h5pointer = hdf5load(file=h5file,load=FALSE,verbosity=0,tidy=TRUE)
variable  = h5pointer$element

where h5file is the HDF5 file to read in. One could solve the problem this way:

variable = h5read (h5file, "element")

The problem is that I have a huge number of those lines so this would mean to change a lot of code lines.

Is there a way to have an object like h5pointer from which I can later dereference the elements of?

 

Sorry for copy pasting from http://stackoverflow.com/questions/35095937/new-equivalent-of-hdf5load but it's the very same question to which I could not find answer so far.

Thanks for your attention. 

 

hdf5 rhdf5 hdf • 2.7k views
ADD COMMENT
3
Entering edit mode
Bernd Fischer ▴ 550
@bernd-fischer-5348
Last seen 8.0 years ago
Germany / Heidelberg / DKFZ

I updated the rhdf5 package.

From version 2.15.5 on, one can now use the $-operator to read data:

h5file = H5Fopen("file.h5")
h5file$A
h5f$'A'

The &-operator to get a reference to a HDF5 group or dataset and list the content of a group

h5f
h5f&'A'
h5f&'/g1/g2/A'
h5d = h5f&'A'
h5d

and the [-operator to subset a dataset

h5d[2,]

Writing works in the same way. However, the content of the file is only guaranteed to change after the file is closed or after a call to H5Fflush().

h5d[2,] = 201:204
H5Fflush(h5file)
h5file$A = 101:112

You always should remind to close all file/group/dataset handles. Either by

H5Dclose(h5d)
H5Fclose(h5file)

or by closing all open HDF5 handles with

H5close()

 

ADD COMMENT
0
Entering edit mode

Since I am using R installed via macports I couldn't test the solution sooner (the R3.3.0 came out in the last days). It works so thank you very much for saving me a lot of time Bernd.

Just a side thing; I am actually opening/reading the hdf files inside a for loop. I noticed that the last one to be opened/read take much longer than the first ones. I was thinking that it might be due to the fact that there are more and more opened files. However with the previous hdf5 packet this was not a problem and I used to close everything at the end. 

Do you think I should modify the procedure? 

Thank you again

ADD REPLY
0
Entering edit mode

You can use H5close() or H5Fclose(h5file) to close the file in between. Check h5listIdentifier() for a list of all open HDF5 handles. Close them, once you don't need them anymore.

ADD REPLY
0
Entering edit mode
Bernd Fischer ▴ 550
@bernd-fischer-5348
Last seen 8.0 years ago
Germany / Heidelberg / DKFZ

You can define the following functions that simulate the behavior:

library(rhdf5)

hdf5load <- function(filename) {
  h5file <- H5Fopen(filename)
  h5file
}

`$.H5IdComponent` <- function(h5id, name) {
  variable = h5read(h5id, name)
}

The following code shows an example how to use it.

h5createFile("file.h5")

h5write(file = "file.h5",obj = matrix(1:12,nrow=3,ncol=4),name = "A")
h5write(file = "file.h5",obj = matrix(13:24,nrow=3,ncol=4),name = "B")

h5file = hdf5load("file.h5")
h5file$A
h5file$B

Bernd

 

ADD COMMENT
0
Entering edit mode

Thank you Bernd,

I'll try soon and let you know.

ADD REPLY
0
Entering edit mode
Bernd Fischer ▴ 550
@bernd-fischer-5348
Last seen 8.0 years ago
Germany / Heidelberg / DKFZ

The functionality seems to be useful. I will try to add such these functions to rhdf5 as well. This requires a little bit more work to catch errors etc..

 

ADD COMMENT

Login before adding your answer.

Traffic: 312 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6