Search
Question: hdf5load equivalent for RHDF5
0
gravatar for manfredodiporcia
20 months ago by
manfredodiporcia0 wrote:

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. 

 

ADD COMMENTlink modified 20 months ago by Bernd Fischer540 • written 20 months ago by manfredodiporcia0
3
gravatar for Bernd Fischer
20 months ago by
Bernd Fischer540
Germany / Heidelberg / DKFZ
Bernd Fischer540 wrote:

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 COMMENTlink modified 20 months ago • written 20 months ago by Bernd Fischer540

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 REPLYlink modified 18 months ago • written 18 months ago by manfredodiporcia0

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 REPLYlink written 18 months ago by Bernd Fischer540
0
gravatar for Bernd Fischer
20 months ago by
Bernd Fischer540
Germany / Heidelberg / DKFZ
Bernd Fischer540 wrote:

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 COMMENTlink written 20 months ago by Bernd Fischer540

Thank you Bernd,

I'll try soon and let you know.

ADD REPLYlink written 20 months ago by manfredodiporcia0
0
gravatar for Bernd Fischer
20 months ago by
Bernd Fischer540
Germany / Heidelberg / DKFZ
Bernd Fischer540 wrote:

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 COMMENTlink written 20 months ago by Bernd Fischer540
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 199 users visited in the last hour