Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

r - 空字符串用rhdf5写入数据(Empty strings writing data with rhdf5)

I'm having trouble writing strings to groups in hdf5 files using rhdf5 using the low-level API, and specifically the functions

(我在使用低级API(特别是功能)使用rhdf5将字符串写入hdf5文件中的组时遇到麻烦)

  • H5Fcreate

    (H5F创建)

  • H5Gcreate

    (H5Gcreate)

  • H5Screate

    (H5S创建)

  • H5Dcreate

    (H5D创建)

  • H5Dwrite

    (H5D写)

Here's my data:

(这是我的数据:)

# strings
v <- c("val1", "val2", "cat", "dog")

I'd like this vector v to exist within an hdf5 group called "metadata."

(我希望此向量v存在于称为“元数据”的hdf5组中。)

Here, I try to write this 1d char array to file:

(在这里,我尝试将此1d char数组写入文件:)

filename <- '/tmp/test.hdf5'

if(file.exists(filename)) {
  file.remove(filename)
}

h5createFile(filename)
fid <- H5Fcreate(filename)    
g2 <- H5Gcreate(fid, "/metadata") 
dtype <- "H5T_C_S1"
sid <- H5Screate_simple(NROW(v))
g <- H5Dcreate(g2, "v", dtype, sid) 
H5Dwrite(g, v, h5spaceMem = sid, h5spaceFile = sid)
H5Dclose(g)
H5Sclose(sid)
h5closeAll()

But when I read it:

(但是当我阅读它时:)

> h5read(filename,"/metadata/", bit64conversion="bit64")
$v
[1] "" "" "" ""

It's totally blank.

(完全空白。)

The dimension and type are both right, but there are no contents.

(尺寸和类型都正确,但是没有内容。)

You can see it's /there/ but I cannot extract the data:

(您可以看到它是/ there /,但是我无法提取数据:)

> h5ls(filename, all=TRUE)
      group     name         ltype corder_valid corder cset       otype
0         / metadata H5L_TYPE_HARD        FALSE      0    0   H5I_GROUP
1 /metadata        v H5L_TYPE_HARD        FALSE      0    0 H5I_DATASET
  num_attrs dclass      dtype  stype rank dim maxdim
0         0                             0           
1         0 STRING H5T_STRING SIMPLE    1   4      4

I can /read/ hdf5 files with rhdf5, and I can write to/from using h5py in python, so I know the machine is setup with the right binaries for hdf5 access.

(我可以使用rhdf5 / read / hdf5文件,并且可以在python中使用h5py来写入/写入,因此我知道该机器已设置了正确的二进制文件以进行hdf5访问。)

But what am I doing wrong that I cannot write hdf5 character vectors in R?

(但是,我不能在R中编写hdf5字符向量,这是我做错了什么?)

  ask by Mittenchops translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why not just do something like:

(为什么不做这样的事情:)

if(file.exists(filename)) {
  file.remove(filename)
}
h5createFile(filename)
h5createGroup(file = filename, group = "/metadata")
h5write(file = filename, obj = v, name = "/metadata/v")
h5closeAll()
h5ls(filename)
h5read(file = filename, name = "/metadata/v")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...