| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
"""This demo shows how to use Traits TreeEditors with h5py to walk the |
| 12 |
hierarchy of several HDF5 files in a folder. All Datasets and Groups are shown for each file. |
| 13 |
|
| 14 |
In the demo, the path to the selected item is printed whenever the selection |
| 15 |
changes. An example HDF5 file is provided here, but you could easily change |
| 16 |
the path given at the bottom of this file to a path to your own HDF5 file. |
| 17 |
|
| 18 |
To run this demonstration successfully, you must have the following packages |
| 19 |
installed: |
| 20 |
|
| 21 |
- **PyTables** (``tables``) |
| 22 |
- **HDF5 for Python** (``h5py``) |
| 23 |
|
| 24 |
Note that PyTables can't read HDF5 files created with h5py, |
| 25 |
but h5py can read HDF5 files created with PyTables. See HDF5_tree_demo for |
| 26 |
an example using PyTables. |
| 27 |
""" |
| 28 |
|
| 29 |
import os |
| 30 |
import warnings |
| 31 |
import h5py |
| 32 |
|
| 33 |
from traits import api |
| 34 |
import traitsui.api as ui |
| 35 |
|
| 36 |
|
| 37 |
ROOT = os.path.dirname(__file__) |
| 38 |
|
| 39 |
|
| 40 |
# View for objects that aren't edited |
| 41 |
no_view = ui.View() |
| 42 |
|
| 43 |
|
| 44 |
# HDF5 Nodes in the tree |
| 45 |
class Hdf5ArrayNode(api.HasTraits): |
| 46 |
name = api.Str('<unknown>') |
| 47 |
path = api.Str('<unknown>') |
| 48 |
parent_path = api.Str('<unknown>') |
| 49 |
|
| 50 |
|
| 51 |
class Hdf5GroupNode(api.HasTraits): |
| 52 |
name = api.Str('<unknown>') |
| 53 |
path = api.Str('<unknown>') |
| 54 |
parent_path = api.Str('<unknown>') |
| 55 |
# Can't have recursive traits? Really? |
| 56 |
# groups = api.List( Hdf5GroupNode ) |
| 57 |
groups = api.List() |
| 58 |
arrays = api.List(Hdf5ArrayNode) |
| 59 |
groups_and_arrays = api.List() |
| 60 |
|
| 61 |
|
| 62 |
class Hdf5FileNode(api.HasTraits): |
| 63 |
name = api.Str('<unknown>') |
| 64 |
path = api.Str('/') |
| 65 |
groups = api.List(Hdf5GroupNode) |
| 66 |
arrays = api.List(Hdf5ArrayNode) |
| 67 |
groups_and_arrays = api.List() |
| 68 |
|
| 69 |
|
| 70 |
class Hdf5FilesNode(api.HasTraits): |
| 71 |
name = api.Str('<unknown>') |
| 72 |
path = api.Str('/') |
| 73 |
files = api.List(Hdf5FileNode) |
| 74 |
groups_and_arrays = api.List() |
| 75 |
|
| 76 |
|
| 77 |
# Recursively build tree, there is probably a better way of doing this. |
| 78 |
|
| 79 |
|
| 80 |
def _get_sub_arrays(group, parent_path): |
| 81 |
"""Return a list of all arrays immediately below a group in an HDF5 file.""" |
| 82 |
return [ |
| 83 |
Hdf5ArrayNode( |
| 84 |
name=name, path=parent_path + name, parent_path=parent_path |
| 85 |
) |
| 86 |
for name, array in group.items() |
| 87 |
if isinstance(array, h5py.Dataset) |
| 88 |
] |
| 89 |
|
| 90 |
|
| 91 |
def _get_sub_groups(group, parent_path): |
| 92 |
"""Return a list of all subgroups and arrays immediately below a group in an HDF5 file.""" |
| 93 |
subgroups = [] |
| 94 |
|
| 95 |
for name, subgroup in group.items(): |
| 96 |
if isinstance(subgroup, h5py.Group): |
| 97 |
path = parent_path + name + '/' |
| 98 |
subsubarrays = _get_sub_arrays(subgroup, path) |
| 99 |
subsubgroups = _get_sub_groups(subgroup, path) |
| 100 |
subgroups.append( |
| 101 |
Hdf5GroupNode( |
| 102 |
name=name, |
| 103 |
path=path, |
| 104 |
parent_path=parent_path, |
| 105 |
arrays=subsubarrays, |
| 106 |
subgroups=subsubgroups, |
| 107 |
groups_and_arrays=subsubgroups + subsubarrays, |
| 108 |
) |
| 109 |
) |
| 110 |
|
| 111 |
return subgroups |
| 112 |
|
| 113 |
|
| 114 |
def _hdf5_tree(filename): |
| 115 |
with h5py.File(filename, 'r') as h5file: |
| 116 |
path = ( |
| 117 |
filename + '#' |
| 118 |
) # separate dataset name from name of hdf5-filename |
| 119 |
subgroups = _get_sub_groups(h5file, path) |
| 120 |
subarrays = _get_sub_arrays(h5file, path) |
| 121 |
file_tree = Hdf5FileNode( |
| 122 |
name=os.path.basename(filename), |
| 123 |
path=filename, |
| 124 |
groups=subgroups, |
| 125 |
arrays=subarrays, |
| 126 |
groups_and_arrays=subgroups + subarrays, |
| 127 |
) |
| 128 |
|
| 129 |
return file_tree |
| 130 |
|
| 131 |
|
| 132 |
def _hdf5_trees(filenames): |
| 133 |
"""Return a list of all groups and arrays below the root group of an HDF5 file.""" |
| 134 |
if isinstance(filenames, str): |
| 135 |
filenames = [filenames] |
| 136 |
files_tree = Hdf5FilesNode() |
| 137 |
files_tree.name = root = os.path.dirname(filenames[0]) |
| 138 |
files_tree.path = root |
| 139 |
|
| 140 |
for filename in filenames: |
| 141 |
folder = os.path.dirname(filename) |
| 142 |
if folder != root: |
| 143 |
warnings.warn( |
| 144 |
"Expected same folder for all files, but got {} != {}".format( |
| 145 |
root, folder |
| 146 |
) |
| 147 |
) |
| 148 |
file_tree = _hdf5_tree(filename) |
| 149 |
|
| 150 |
files_tree.files.append(file_tree) |
| 151 |
files_tree.groups_and_arrays.extend(file_tree.groups_and_arrays) |
| 152 |
return files_tree |
| 153 |
|
| 154 |
|
| 155 |
# Get a tree editor |
| 156 |
|
| 157 |
|
| 158 |
def _hdf5_tree_editor(selected=''): |
| 159 |
"""Return a ui.TreeEditor specifically for HDF5 file trees.""" |
| 160 |
return ui.TreeEditor( |
| 161 |
nodes=[ |
| 162 |
ui.TreeNode( |
| 163 |
node_for=[Hdf5FilesNode], |
| 164 |
auto_open=True, |
| 165 |
children='files', |
| 166 |
label='name', |
| 167 |
view=no_view, |
| 168 |
), |
| 169 |
ui.TreeNode( |
| 170 |
node_for=[Hdf5FileNode], |
| 171 |
auto_open=True, |
| 172 |
children='groups_and_arrays', |
| 173 |
label='name', |
| 174 |
view=no_view, |
| 175 |
), |
| 176 |
ui.TreeNode( |
| 177 |
node_for=[Hdf5GroupNode], |
| 178 |
auto_open=False, |
| 179 |
children='groups_and_arrays', |
| 180 |
label='name', |
| 181 |
view=no_view, |
| 182 |
), |
| 183 |
ui.TreeNode( |
| 184 |
node_for=[Hdf5ArrayNode], |
| 185 |
auto_open=False, |
| 186 |
children='', |
| 187 |
label='name', |
| 188 |
view=no_view, |
| 189 |
), |
| 190 |
], |
| 191 |
editable=False, |
| 192 |
selected=selected, |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
class _H5Tree(api.HasTraits): |
| 197 |
h5_tree = api.Instance(Hdf5FileNode) |
| 198 |
node = api.Any() |
| 199 |
path = api.Str() |
| 200 |
|
| 201 |
traits_view = ui.View( |
| 202 |
ui.Group( |
| 203 |
ui.Item( |
| 204 |
'h5_tree', |
| 205 |
editor=_hdf5_tree_editor(selected='node'), |
| 206 |
resizable=True, |
| 207 |
), |
| 208 |
ui.Item('path', label='Selected node'), |
| 209 |
orientation='vertical', |
| 210 |
), |
| 211 |
title='HDF5 Tree Example', |
| 212 |
buttons=['OK', 'Cancel'], |
| 213 |
resizable=True, |
| 214 |
width=0.3, |
| 215 |
height=0.3, |
| 216 |
) |
| 217 |
|
| 218 |
def _node_changed(self): |
| 219 |
self.path = self.node.path |
| 220 |
print(self.node.path) |
| 221 |
|
| 222 |
|
| 223 |
class _H5Trees(api.HasTraits): |
| 224 |
h5_trees = api.Instance(Hdf5FilesNode) |
| 225 |
node = api.Any() |
| 226 |
path = api.Str() |
| 227 |
|
| 228 |
traits_view = ui.View( |
| 229 |
ui.Group( |
| 230 |
ui.Item( |
| 231 |
'h5_trees', |
| 232 |
editor=_hdf5_tree_editor(selected='node'), |
| 233 |
resizable=True, |
| 234 |
), |
| 235 |
ui.Item('path', label='Selected node'), |
| 236 |
orientation='vertical', |
| 237 |
), |
| 238 |
title='Multiple HDF5 file Tree Example', |
| 239 |
buttons=['OK', 'Cancel'], |
| 240 |
resizable=True, |
| 241 |
width=0.3, |
| 242 |
height=0.3, |
| 243 |
) |
| 244 |
|
| 245 |
def _node_changed(self): |
| 246 |
self.path = self.node.path |
| 247 |
print(self.node.path) |
| 248 |
|
| 249 |
|
| 250 |
def hdf5_tree(filename): |
| 251 |
# if isinstance(filename, str): |
| 252 |
# return _H5Tree(h5_tree=_hdf5_tree(filename)) |
| 253 |
return _H5Trees(h5_trees=_hdf5_trees(filename)) |
| 254 |
|
| 255 |
|
| 256 |
def make_test_datasets(): |
| 257 |
"""Makes the test datasets and store it in the current folder""" |
| 258 |
import numpy as np |
| 259 |
import pandas as pd # pandas uses pytables to store datasets in hdf5 format. |
| 260 |
from random import randrange |
| 261 |
|
| 262 |
n = 100 |
| 263 |
|
| 264 |
df = pd.DataFrame( |
| 265 |
dict( |
| 266 |
[ |
| 267 |
("int{0}".format(i), np.random.randint(0, 10, size=n)) |
| 268 |
for i in range(5) |
| 269 |
] |
| 270 |
) |
| 271 |
) |
| 272 |
|
| 273 |
df['float'] = np.random.randn(n) |
| 274 |
|
| 275 |
for i in range(10): |
| 276 |
df["object_1_{0}".format(i)] = [ |
| 277 |
'%08x' % randrange(16 ** 8) for _ in range(n) |
| 278 |
] |
| 279 |
|
| 280 |
for i in range(7): |
| 281 |
df["object_2_{0}".format(i)] = [ |
| 282 |
'%15x' % randrange(16 ** 15) for _ in range(n) |
| 283 |
] |
| 284 |
|
| 285 |
df.info() |
| 286 |
df.to_hdf('test_fixed.h5', 'data', format='fixed') |
| 287 |
df.to_hdf('test_table_no_dc.h5', 'data', format='table') |
| 288 |
df.to_hdf('test_table_dc.h5', 'data', format='table', data_columns=True) |
| 289 |
df.to_hdf( |
| 290 |
'test_fixed_compressed.h5', |
| 291 |
'data', |
| 292 |
format='fixed', |
| 293 |
complib='blosc', |
| 294 |
complevel=9, |
| 295 |
) |
| 296 |
|
| 297 |
# h5py dataset |
| 298 |
time = np.arange(n) |
| 299 |
x = np.linspace(-7, 7, n) |
| 300 |
axes_latlon = [ |
| 301 |
('time', time), |
| 302 |
('coordinate', np.array(['lat', 'lon'], dtype='S3')), |
| 303 |
] |
| 304 |
axes_mag = [ |
| 305 |
('time', time), |
| 306 |
('direction', np.array(['x', 'y', 'z'], dtype='S1')), |
| 307 |
] |
| 308 |
latlon = np.vstack( |
| 309 |
(np.linspace(-0.0001, 0.00001, n) + 23.8, np.zeros(n) - 82.3) |
| 310 |
).T |
| 311 |
mag_data = np.vstack( |
| 312 |
( |
| 313 |
-(1 - np.tanh(x) ** 2) * np.sin(2 * x), |
| 314 |
-(1 - np.tanh(x) ** 2) * np.sin(2 * x), |
| 315 |
-(1 - np.tanh(x) ** 2), |
| 316 |
) |
| 317 |
).T |
| 318 |
datasets = ( |
| 319 |
axes_mag |
| 320 |
+ axes_latlon |
| 321 |
+ [('magnetic_3_axial', mag_data), ('latlon', latlon)] |
| 322 |
) |
| 323 |
with h5py.File(os.path.join(ROOT, 'test_h5pydata.h5'), "a") as h5file: |
| 324 |
h5group = h5file.require_group("run1_test1") |
| 325 |
for data_name, data in datasets: |
| 326 |
h5group.require_dataset( |
| 327 |
name=data_name, |
| 328 |
dtype=data.dtype, |
| 329 |
shape=data.shape, |
| 330 |
data=data, |
| 331 |
# **options |
| 332 |
) |
| 333 |
|
| 334 |
|
| 335 |
def main(): |
| 336 |
filenames = [ |
| 337 |
'test_fixed.h5', |
| 338 |
'test_table_no_dc.h5', |
| 339 |
'test_table_dc.h5', |
| 340 |
'test_fixed_compressed.h5', |
| 341 |
'test_h5pydata.h5', |
| 342 |
] |
| 343 |
fullfiles = [os.path.join(ROOT, fname) for fname in filenames] |
| 344 |
h5_trees = hdf5_tree(fullfiles) |
| 345 |
return h5_trees |
| 346 |
|
| 347 |
|
| 348 |
demo = main() |
| 349 |
|
| 350 |
|
| 351 |
if __name__ == '__main__': |
| 352 |
# make_test_datasets() |
| 353 |
ok_status = demo.configure_traits() |
| 354 |
print('The End', ok_status) |