| 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 PyTables to walk the |
| 12 |
hierarchy of an HDF5 file. This only picks out tables, arrays and groups, but could |
| 13 |
easily be extended to other structures. |
| 14 |
|
| 15 |
In the demo, the path to the selected item is printed whenever the selection |
| 16 |
changes. An example HDF5 file is provided here, but you could easily change |
| 17 |
the path given at the bottom of this file to a path to your own HDF5 file. |
| 18 |
|
| 19 |
To run this demonstration successfully, you must have the following packages |
| 20 |
installed: |
| 21 |
|
| 22 |
- **PyTables** (``tables``) |
| 23 |
- **HDF5 for Python** (``h5py``) |
| 24 |
|
| 25 |
Note that PyTables can't read HDF5 files created with h5py, |
| 26 |
but h5py can read HDF5 files created with PyTables. See HDF5_tree_demo2 for |
| 27 |
an example using h5py. |
| 28 |
""" |
| 29 |
import os |
| 30 |
import tables as tb |
| 31 |
|
| 32 |
from traits.api import HasTraits, Str, List, Instance, Any |
| 33 |
from traitsui.api import TreeEditor, TreeNode, View, Item, Group |
| 34 |
|
| 35 |
ROOT = os.path.dirname(__file__) |
| 36 |
|
| 37 |
# View for objects that aren't edited |
| 38 |
no_view = View() |
| 39 |
|
| 40 |
|
| 41 |
# HDF5 Nodes in the tree |
| 42 |
class Hdf5ArrayNode(HasTraits): |
| 43 |
name = Str('<unknown>') |
| 44 |
path = Str('<unknown>') |
| 45 |
parent_path = Str('<unknown>') |
| 46 |
|
| 47 |
|
| 48 |
class Hdf5GroupNode(HasTraits): |
| 49 |
name = Str('<unknown>') |
| 50 |
path = Str('<unknown>') |
| 51 |
parent_path = Str('<unknown>') |
| 52 |
# Can't have recursive traits? Really? |
| 53 |
# groups = List(Hdf5GroupNode) |
| 54 |
groups = List() |
| 55 |
arrays = List(Hdf5ArrayNode) |
| 56 |
groups_and_arrays = List() |
| 57 |
|
| 58 |
|
| 59 |
class Hdf5FileNode(HasTraits): |
| 60 |
name = Str('<unknown>') |
| 61 |
path = Str('/') |
| 62 |
groups = List(Hdf5GroupNode) |
| 63 |
arrays = List(Hdf5ArrayNode) |
| 64 |
groups_and_arrays = List() |
| 65 |
|
| 66 |
|
| 67 |
# Recursively build tree, there is probably a better way of doing this. |
| 68 |
|
| 69 |
|
| 70 |
def _get_sub_arrays(group, h5file): |
| 71 |
"""Return a list of all arrays immediately below a group in an HDF5 file.""" |
| 72 |
return [ |
| 73 |
Hdf5ArrayNode( |
| 74 |
name=array._v_name, |
| 75 |
path=array._v_pathname, |
| 76 |
parent_path=array._v_parent._v_pathname, |
| 77 |
) |
| 78 |
for array in group |
| 79 |
if isinstance(array, (tb.Array, tb.Table)) |
| 80 |
] # More pythonic |
| 81 |
# for array in h5file.iter_nodes(group, classname='Array')] # Old call |
| 82 |
|
| 83 |
|
| 84 |
def _get_sub_groups(group, h5file): |
| 85 |
"""Return a list of all groups and arrays immediately below a group in an HDF5 file.""" |
| 86 |
subgroups = [] |
| 87 |
|
| 88 |
for subgroup in h5file.iter_nodes(group, classname='Group'): |
| 89 |
subsubgroups = _get_sub_groups(subgroup, h5file) |
| 90 |
subsubarrays = _get_sub_arrays(subgroup, h5file) |
| 91 |
subgroups.append( |
| 92 |
Hdf5GroupNode( |
| 93 |
name=subgroup._v_name, |
| 94 |
path=subgroup._v_pathname, |
| 95 |
parent_path=subgroup._v_parent._v_pathname, |
| 96 |
groups=subsubgroups, |
| 97 |
arrays=subsubarrays, |
| 98 |
groups_and_arrays=subsubgroups + subsubarrays, |
| 99 |
) |
| 100 |
) |
| 101 |
return subgroups |
| 102 |
|
| 103 |
|
| 104 |
def _hdf5_tree(filename): |
| 105 |
"""Return a list of all groups and arrays below the root group of an HDF5 file.""" |
| 106 |
|
| 107 |
with tb.open_file(filename, 'r') as h5file: |
| 108 |
subgroups = _get_sub_groups(h5file.root, h5file) |
| 109 |
subarrays = _get_sub_arrays(h5file.root, h5file) |
| 110 |
h5_tree = Hdf5FileNode( |
| 111 |
name=filename, |
| 112 |
groups=subgroups, |
| 113 |
arrays=subarrays, |
| 114 |
groups_and_arrays=subgroups + subarrays, |
| 115 |
) |
| 116 |
|
| 117 |
return h5_tree |
| 118 |
|
| 119 |
|
| 120 |
# Get a tree editor |
| 121 |
|
| 122 |
|
| 123 |
def _hdf5_tree_editor(selected=''): |
| 124 |
"""Return a TreeEditor specifically for HDF5 file trees.""" |
| 125 |
return TreeEditor( |
| 126 |
nodes=[ |
| 127 |
TreeNode( |
| 128 |
node_for=[Hdf5FileNode], |
| 129 |
auto_open=True, |
| 130 |
children='groups_and_arrays', |
| 131 |
label='name', |
| 132 |
view=no_view, |
| 133 |
), |
| 134 |
TreeNode( |
| 135 |
node_for=[Hdf5GroupNode], |
| 136 |
auto_open=False, |
| 137 |
children='groups_and_arrays', |
| 138 |
label='name', |
| 139 |
view=no_view, |
| 140 |
), |
| 141 |
TreeNode( |
| 142 |
node_for=[Hdf5ArrayNode], |
| 143 |
auto_open=False, |
| 144 |
children='', |
| 145 |
label='name', |
| 146 |
view=no_view, |
| 147 |
), |
| 148 |
], |
| 149 |
editable=False, |
| 150 |
selected=selected, |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
class ATree(HasTraits): |
| 155 |
h5_tree = Instance(Hdf5FileNode) |
| 156 |
node = Any() |
| 157 |
|
| 158 |
traits_view = View( |
| 159 |
Group( |
| 160 |
Item( |
| 161 |
'h5_tree', |
| 162 |
editor=_hdf5_tree_editor(selected='node'), |
| 163 |
resizable=True, |
| 164 |
show_label=False, |
| 165 |
), |
| 166 |
orientation='vertical', |
| 167 |
), |
| 168 |
title='HDF5 Tree Example', |
| 169 |
buttons=['Undo', 'OK', 'Cancel'], |
| 170 |
resizable=True, |
| 171 |
width=0.3, |
| 172 |
height=0.3, |
| 173 |
) |
| 174 |
|
| 175 |
def _node_changed(self): |
| 176 |
print(self.node.path) |
| 177 |
|
| 178 |
|
| 179 |
def make_test_datasets(): |
| 180 |
"""Makes the test datasets and store it in the current folder""" |
| 181 |
import h5py |
| 182 |
import numpy as np |
| 183 |
import pandas as pd # pandas uses pytables to store datasets in hdf5 format. |
| 184 |
from random import randrange |
| 185 |
|
| 186 |
n = 100 |
| 187 |
|
| 188 |
df = pd.DataFrame( |
| 189 |
dict( |
| 190 |
[ |
| 191 |
("int{0}".format(i), np.random.randint(0, 10, size=n)) |
| 192 |
for i in range(5) |
| 193 |
] |
| 194 |
) |
| 195 |
) |
| 196 |
|
| 197 |
df['float'] = np.random.randn(n) |
| 198 |
|
| 199 |
for i in range(10): |
| 200 |
df["object_1_{0}".format(i)] = [ |
| 201 |
'%08x' % randrange(16 ** 8) for _ in range(n) |
| 202 |
] |
| 203 |
|
| 204 |
for i in range(7): |
| 205 |
df["object_2_{0}".format(i)] = [ |
| 206 |
'%15x' % randrange(16 ** 15) for _ in range(n) |
| 207 |
] |
| 208 |
|
| 209 |
df.info() |
| 210 |
df.to_hdf('test_fixed.h5', 'data', format='fixed') |
| 211 |
df.to_hdf('test_table_no_dc.h5', 'data', format='table') |
| 212 |
df.to_hdf('test_table_dc.h5', 'data', format='table', data_columns=True) |
| 213 |
df.to_hdf( |
| 214 |
'test_fixed_compressed.h5', |
| 215 |
'data', |
| 216 |
format='fixed', |
| 217 |
complib='blosc', |
| 218 |
complevel=9, |
| 219 |
) |
| 220 |
|
| 221 |
# h5py dataset |
| 222 |
time = np.arange(n) |
| 223 |
x = np.linspace(-7, 7, n) |
| 224 |
axes_latlon = [ |
| 225 |
('time', time), |
| 226 |
('coordinate', np.array(['lat', 'lon'], dtype='S3')), |
| 227 |
] |
| 228 |
axes_mag = [ |
| 229 |
('time', time), |
| 230 |
('direction', np.array(['x', 'y', 'z'], dtype='S1')), |
| 231 |
] |
| 232 |
latlon = np.vstack( |
| 233 |
(np.linspace(-0.0001, 0.00001, n) + 23.8, np.zeros(n) - 82.3) |
| 234 |
).T |
| 235 |
mag_data = np.vstack( |
| 236 |
( |
| 237 |
-(1 - np.tanh(x) ** 2) * np.sin(2 * x), |
| 238 |
-(1 - np.tanh(x) ** 2) * np.sin(2 * x), |
| 239 |
-(1 - np.tanh(x) ** 2), |
| 240 |
) |
| 241 |
).T |
| 242 |
datasets = ( |
| 243 |
axes_mag |
| 244 |
+ axes_latlon |
| 245 |
+ [('magnetic_3_axial', mag_data), ('latlon', latlon)] |
| 246 |
) |
| 247 |
with h5py.File(os.path.join(ROOT, 'test_h5pydata.h5'), "a") as h5file: |
| 248 |
h5group = h5file.require_group("run1_test1") |
| 249 |
for data_name, data in datasets: |
| 250 |
h5group.require_dataset( |
| 251 |
name=data_name, |
| 252 |
dtype=data.dtype, |
| 253 |
shape=data.shape, |
| 254 |
data=data, |
| 255 |
# **options |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
def main(): |
| 260 |
import sys |
| 261 |
|
| 262 |
filename = os.path.join(ROOT, 'test_fixed.h5') |
| 263 |
filename = os.path.join(ROOT, 'test_table_no_dc.h5') |
| 264 |
if len(sys.argv) > 1: |
| 265 |
filename = sys.argv[1] |
| 266 |
a_tree = ATree(h5_tree=_hdf5_tree(filename)) |
| 267 |
return a_tree |
| 268 |
|
| 269 |
|
| 270 |
demo = main() |
| 271 |
|
| 272 |
|
| 273 |
if __name__ == '__main__': |
| 274 |
# make_test_datasets() |
| 275 |
demo.configure_traits() |