|
| 1 | + |
| 2 | +import h5py |
| 3 | +import numpy as np |
| 4 | +from numpy.core.defchararray import encode, decode |
| 5 | + |
| 6 | + |
| 7 | +class hdf5_wrapper(): |
| 8 | + """ |
| 9 | + @brief a class for reading/writing hdf5 files, which behaves similar to a native dict |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, fname='', target='', mode='r'): |
| 13 | + """ |
| 14 | + @brief initialize the hdf5_wrapper class |
| 15 | + @param fname the filename of a new or existing hdf5 database |
| 16 | + @param target the handle of an existing hdf5 dataset |
| 17 | + @param mode the read/write behavior of the database (default='r') |
| 18 | +
|
| 19 | + @details If the fname is supplied (either by a positional or keyword argument), |
| 20 | + the wrapper will open a hdf5 database from the filesystem. The reccomended |
| 21 | + options for the mode flag include 'r' for read-only and 'a' for read/write |
| 22 | + access. If write mode is enabled, and the fname does not point |
| 23 | + to an existing file, a new database will be created. |
| 24 | +
|
| 25 | + If the target is supplied, then a new instance of the wrapper will |
| 26 | + be created using an existing database handle. |
| 27 | + """ |
| 28 | + |
| 29 | + self.mode = mode |
| 30 | + self.target = target |
| 31 | + if fname: |
| 32 | + self.target = h5py.File(fname, self.mode) |
| 33 | + |
| 34 | + def __getitem__(self, k): |
| 35 | + """ |
| 36 | + @brief get a target from the database |
| 37 | + @param k name of target group or array |
| 38 | +
|
| 39 | + @return the returned value depends on the type of the target: |
| 40 | + - An existing hdf5 group will return an instance of hdf5_wrapper |
| 41 | + - An existing array will return an numpy ndarray |
| 42 | + - If the target is not present in the datastructure and the |
| 43 | + database is open in read/write mode, the wrapper will create a |
| 44 | + new group and return an hdf5_wrapper |
| 45 | + - Otherwise, this will throw an error |
| 46 | + """ |
| 47 | + if (k not in self.target): |
| 48 | + if (self.mode in ['w', 'a']): |
| 49 | + self.target.create_group(k) |
| 50 | + else: |
| 51 | + raise ValueError('Entry does not exist in database: %s' % (k)) |
| 52 | + |
| 53 | + tmp = self.target[k] |
| 54 | + |
| 55 | + if isinstance(tmp, h5py._hl.group.Group): |
| 56 | + return hdf5_wrapper(target=tmp, mode=self.mode) |
| 57 | + elif isinstance(tmp, h5py._hl.dataset.Dataset): |
| 58 | + tmp = np.array(tmp) |
| 59 | + |
| 60 | + # Decode any string types |
| 61 | + if (tmp.dtype.kind in ['S', 'U', 'O']): |
| 62 | + tmp = decode(tmp) |
| 63 | + |
| 64 | + # Convert any 0-length arrays to native types |
| 65 | + if not tmp.shape: |
| 66 | + tmp = tmp[()] |
| 67 | + |
| 68 | + return tmp |
| 69 | + else: |
| 70 | + return tmp |
| 71 | + |
| 72 | + def __setitem__(self, k, value): |
| 73 | + """ |
| 74 | + @brief write an object to the database if write-mode is enabled |
| 75 | + @param k the name of the object |
| 76 | + @param value the object to be written |
| 77 | + """ |
| 78 | + |
| 79 | + if (self.mode in ['w', 'a']): |
| 80 | + if isinstance(value, dict): |
| 81 | + # Recursively add groups and their children |
| 82 | + if (k not in self.target): |
| 83 | + self.target.create_group(k) |
| 84 | + new_group = self[k] |
| 85 | + for x in value: |
| 86 | + new_group[x] = value[x] |
| 87 | + else: |
| 88 | + # Delete the old copy if necessary |
| 89 | + if (k in self.target): |
| 90 | + del(self.target[k]) |
| 91 | + |
| 92 | + # Add everything else as an ndarray |
| 93 | + tmp = np.array(value) |
| 94 | + if (tmp.dtype.kind in ['S', 'U', 'O']): |
| 95 | + tmp = encode(tmp) |
| 96 | + self.target[k] = tmp |
| 97 | + else: |
| 98 | + raise ValueError('Cannot write to an hdf5 opened in read-only mode! This can be changed by overriding the default mode argument for the wrapper.') |
| 99 | + |
| 100 | + def link(self, k, target): |
| 101 | + """ |
| 102 | + @brief link an external hdf5 file to this location in the database |
| 103 | + @param k the name of the new link in the database |
| 104 | + @param target the path to the external database |
| 105 | + """ |
| 106 | + self.target[k] = h5py.ExternalLink(target, '/') |
| 107 | + |
| 108 | + def keys(self): |
| 109 | + """ |
| 110 | + @brief get a list of groups and arrays located at the current level |
| 111 | + @return a list of strings |
| 112 | + """ |
| 113 | + if isinstance(self.target, h5py._hl.group.Group): |
| 114 | + return list(self.target) |
| 115 | + else: |
| 116 | + raise ValueError('Object not a group!') |
| 117 | + |
| 118 | + def __enter__(self): |
| 119 | + """ |
| 120 | + @brief entry point for an iterator |
| 121 | + """ |
| 122 | + return self |
| 123 | + |
| 124 | + def __exit__(self, type, value, traceback): |
| 125 | + """ |
| 126 | + @brief end point for an iterator |
| 127 | + """ |
| 128 | + self.target.close() |
| 129 | + |
| 130 | + def __del__(self): |
| 131 | + """ |
| 132 | + @brief closes the database on wrapper deletion |
| 133 | + """ |
| 134 | + try: |
| 135 | + if isinstance(self.target, h5py._hl.files.File): |
| 136 | + self.target.close() |
| 137 | + except: |
| 138 | + pass |
| 139 | + |
| 140 | + def close(self): |
| 141 | + """ |
| 142 | + @brief closes the database |
| 143 | + """ |
| 144 | + if isinstance(self.target, h5py._hl.files.File): |
| 145 | + self.target.close() |
| 146 | + |
| 147 | + def get_copy(self): |
| 148 | + """ |
| 149 | + @brief copy the entire database into memory |
| 150 | + @return a dictionary holding the database contents |
| 151 | + """ |
| 152 | + tmp = {} |
| 153 | + self.copy(tmp) |
| 154 | + return tmp |
| 155 | + |
| 156 | + def copy(self, output): |
| 157 | + """ |
| 158 | + @brief pack the contents of the current database level onto the target dict |
| 159 | + @param output the dictionary to pack objects into |
| 160 | + """ |
| 161 | + for k in self.keys(): |
| 162 | + tmp = self[k] |
| 163 | + |
| 164 | + if isinstance(tmp, hdf5_wrapper): |
| 165 | + output[k] = {} |
| 166 | + tmp.copy(output[k]) |
| 167 | + else: |
| 168 | + output[k] = tmp |
| 169 | + |
| 170 | + |
0 commit comments