【问题标题】:Updating one field in HDF File更新 HDF 文件中的一个字段
【发布时间】:2018-02-15 07:25:58
【问题描述】:

我似乎无法让它工作。所有示例和线程都有人创建新数据集。我只想更新已经创建的数据集中的一个字段。

这是我所拥有的:

h5_file = h5py.File(event_file_path, "r+") #this works

event_processing_status = int(h5_file[PATH][STATUS].value[0]['Status']) #this works
print({"{0:b}".format(event_processing_status)) #this works
event_processing_status = (event_processing_status | STATUS_UPDATE) #this works
h5_file[PATH][STATUS].value[0]['Status'] = event_processing_status #updating???, no error
event_processing_status = int(h5_file[PATH][STATUS].value[0]['Status']) #this works
print({"{0:b}".format(event_processing_status)) #not the update value

h5_file.close()

我做错了什么?

更多信息: 数据集列的dtypes:

dset = h5_file[PATH][STATUS] 
print(dset.dtype) gives:
[('Status', '<u8'), ('Segments', '<u4'), ('Characterized', '<u4'), ('More_Segments', '<u4'), ('ID', '<i4'), ('Releases', '<u2'), ('Type', '|u1'), ('Track', '<i4')]

dset[0,'Status'] = event_processing_status gives:
TypeError: Field name selections are not allowed for write.

【问题讨论】:

  • 在这一行h5_file[PATH][STATUS].value[0]['Status']) = event_processing_status 有一个额外的 ) 左侧赋值。它真的在那里吗?还是错字?
  • 复制粘贴错字,抱歉
  • 尝试更改索引顺序,[Status'][0]。我假设Status 是结构化数组字段名称。 h5py让您将记录号和字段名称组合到一个语句中:dset[0, 'Status'] = statusdocs.h5py.org/en/latest/high/dataset.html#reading-writing-data。如果我的猜测没有帮助,请向我们展示此数据集的 dtype
  • ['Status'][0] 不起作用。数据类型是 uint64。我也试过 h5_file[PATH][STATUS]['Status'] = event_processing_status 但得到“TypeError: Field name selections are not allowed for write.
  • 给我们更多关于这个数据集结构的信息。

标签: python hdf5 h5py


【解决方案1】:

跟进我的评论,假设您的数据集是结构化/复合数据类型

In [144]: f = h5py.File('test.h5','w')
In [145]: arr = np.ones((3,), dtype='i,f')  # structured array
In [146]: arr
Out[146]: 
array([(1,  1.), (1,  1.), (1,  1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

用数据创建数据集

In [147]: ds = f.create_dataset('arr',data=arr)
In [148]: ds
Out[148]: <HDF5 dataset "arr": shape (3,), type "|V8">
In [149]: ds.value
Out[149]: 
array([(1,  1.), (1,  1.), (1,  1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

我可以用记录ID和字段名来索引它;这不适用于ds.valuearr

In [151]: ds[0,'f0']
Out[151]: 1
In [152]: ds[0,'f0'] = 2    # and I can assign values
In [153]: ds.value
Out[153]: 
array([(2,  1.), (1,  1.), (1,  1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

我可以使用单独的记录和字段条目进行索引;但不能以这种方式更改值:

In [154]: ds[0]['f1']
Out[154]: 1.0
In [155]: ds[0]['f1'] = 234
In [156]: ds.value
Out[156]: 
array([(2,  1.), (1,  1.), (1,  1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])
In [157]: ds['f1'][0] = 234
In [158]: ds.value
Out[158]: 
array([(2,  1.), (1,  1.), (1,  1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

赋值需要组合索引

In [159]: ds[0,'f1'] = 234
In [160]: ds.value
Out[160]: 
array([(2,  234.), (1,    1.), (1,    1.)],
      dtype=[('f0', '<i4'), ('f1', '<f4')])

【讨论】:

  • 尝试了您的作业定义,但没有奏效。我用更多信息更新了原始帖子
  • @lr100,你的dsetdtype 对我有用。你的h5py 版本号是多少?我的是2.6.0
  • 我建议的解决方案是 2.2 的新解决方案,docs.h5py.org/en/latest/whatsnew/… 我不知道旧版本有什么可能。
【解决方案2】:

这是我必须做的:

h5_file = h5py.File(event_file_path, "r+") #this works

#Retrieve the dataset containing Event_Status
data= h5_file[PATH][STATUS]

#Get the Event_Status (np.array)
event_status = data['Event_Status']

#Update the value of Event_Status
event_status[0] = np.bitwise_or(event_status[0],np.uint64(STATUS_UPDATE))

#Write updated value to file
elements = data[0]
elements['Event_Status'] = event_status
data[0] = elements

h5_file.close() 

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多