Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close an open h5py data file

In our lab we store our data in hdf5 files trough the python package h5py.

At the beginning of an experiment we create an hdf5 file and store array after array of array of data in the file (among other things). When an experiment fails or is interrupted the file is not correctly closed. Because our experiments run from iPython the reference to the data object remains (somewhere) in memory.

Is there a way to scan for all open h5py data objects and close them?

like image 999
Adriaan Rol Avatar asked Apr 25 '15 09:04

Adriaan Rol


People also ask

How do I open an H5 file?

Open a HDF5/H5 file in HDFView hdf5 file on your computer. Open this file in HDFView. If you click on the name of the HDF5 file in the left hand window of HDFView, you can view metadata for the file.

What are .H5 files?

An H5 file is a data file saved in the Hierarchical Data Format (HDF). It contains multidimensional arrays of scientific data. H5 files are commonly used in aerospace, physics, engineering, finance, academic research, genomics, astronomy, electronics instruments, and medical fields.


2 Answers

This is how it could be done (I could not figure out how to check for closed-ness of the file without exceptions, maybe you will find):

import gc
for obj in gc.get_objects():   # Browse through ALL objects
    if isinstance(obj, h5py.File):   # Just HDF5 files
        try:
            obj.close()
        except:
            pass # Was already closed

Another idea:

Dpending how you use the files, what about using the context manager and the with keyword like this?

with h5py.File("some_path.h5") as f:
   f["data1"] = some_data

When the program flow exits the with-block, the file is closed regardless of what happens, including exceptions etc.

like image 97
honza_p Avatar answered Nov 02 '22 00:11

honza_p


pytables (which h5py uses) keeps track of all open files and provides an easy method to force-close all open hdf5 files.

import tables
tables.file._open_files.close_all()

That attribute _open_files also has helpful methods to give you information and handlers for the open files.

like image 25
OrangeSherbet Avatar answered Nov 02 '22 00:11

OrangeSherbet