简体   繁体   English

关闭文件后如何将 h5py 组保留在内存中?

[英]How do I keep an h5py group in memory after closing the file?

How do I keep an h5py group in memory after closing the file?关闭文件后如何将 h5py 组保留在内存中?

After the following code:在以下代码之后:

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"]

I can access worm_features as it is an h5py group ( <HDF5 group "/worm" (4 members)> )我可以访问worm_features因为它是一个h5py 组<HDF5 group "/worm" (4 members)>

But after I run the line:但是在我运行这条线之后:

feature_file.close()

I can no longer access worm_features .我无法再访问worm_features It now appears as <Closed HDF5 group> .它现在显示为<Closed HDF5 group>

Since I need to load the worm_features h5py group for about 20 files, I'd like to close those files before doing processing on the data I've loaded into memory.因为我需要为大约 20 个文件加载 worm_features h5py 组,所以我想在处理我加载到内存中的数据之前关闭这些文件。 Is this not possible?这不可能吗?

Use .value to pull the variable you want from the dataset.使用 .value 从数据集中提取所需的变量。

Example:例子:

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"].value
feature_file.close()
print worm_features

Use [:] to copy the value of dataset to a variable to have access the dataset after closing the hdf5 file.使用[:]将数据集的值复制到变量中,以便在关闭 hdf5 文件后访问数据集。

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"] [:]
feature_file.close()
print (worm_features)

.value does not work for me and raised following error: .value对我不起作用并引发以下错误:

AttributeError: 'Dataset' object has no attribute 'value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM