1 from __future__
import print_function
7 Turns out it is useful for hdf5 groups to be self-describing for the 8 PandAna framework. This is done by adding an HDF5 attribute to each group 9 that specifies the columns within that group that should be used as indices 10 when constructing a pandas DataFrame. 12 HDF5Maker was updated to produces these attributes at the time of file creation 13 For those files that were created before HDF5Maker was updated, 14 this script adds the attributes by hand 16 author: Derek.Doyle@colostate.edu 26 required_indices = [
'run',
'subrun',
'cycle',
'evt']
27 other_indices = [
'subevt']
31 dsets = h5group.keys()
32 for required
in required_indices:
33 if required
in dsets: index_cols.append(required)
34 for other
in other_indices:
35 if other
in dsets: index_cols.append(other)
37 for dset
in h5group.keys():
38 if dset.endswith(
'_idx'): index_cols.append(dset)
42 return [
','.join(index_cols)]
51 if '.' not in group_name:
54 return [
'.'.join(group_name.split(
'.')[:-1])]
66 existing_index_cols = g.attrs[
'index_cols']
68 existing_index_cols =
'None' 71 print(
'\t|index_cols: {} ---> {}'.
format(existing_index_cols, attr_index_cols))
73 if not dry_run: h5group.attrs[
'index_cols' ] = attr_index_cols
79 existing_parent_table = g.attrs[
'parent_table']
81 existing_parent_table =
'None' 83 print(
'\t|parent_table: {} ---> {}'.
format(existing_parent_table, attr_parent_table))
85 if not dry_run: h5group.attrs[
'parent_table'] = attr_parent_table
90 def insert(astring, substr, pos, overwrite=True):
92 return astring[:pos] + substr + astring[pos + len(substr):]
94 return astring[:pos] + substr + astring[pos:]
97 invert_open =
'\033[F\033[7m[' 98 invert_close =
'\033[0m' 100 print(message,
'....')
105 percent_str =
'{:.1f}%'.
format(percent*100)
107 bar = progbar.invert_open +
' ' * (self.
_nfields - 1) +
']' 108 bar =
insert(bar, percent_str , pos=len(progbar.invert_open) , overwrite=
True)
109 bar =
insert(bar, progbar.invert_close, pos=len(progbar.invert_open)+current, overwrite=
False)
113 parser = argparse.ArgumentParser(
'Add index columns to the input h5 file(s)')
114 parser.add_argument(
'files', nargs=
'*', help=
'Input file(s). Wildcard allowed')
115 parser.add_argument(
'--dry_run',
'-d', action=
'store_true', help=
'Don\'t actually add the attributes yet')
116 parser.add_argument(
'--verbose',
'-v', action=
'store_true', help=
'Print out old and new attributes')
118 args = parser.parse_args()
122 if not args.verbose: prog =
progbar(
'Adding attributes')
123 for i, fname
in enumerate(files):
124 with h5py.File(fname,
'r+')
as h5file:
125 for group_name
in h5file.keys():
126 if group_name !=
'MetaData':
127 g = h5file.get(group_name)
130 verbose=args.verbose,
131 dry_run=args.dry_run)
133 if not args.verbose:
prog(i / len(files))
def parent_table(group_name)
def write_attributes(h5group, verbose=False, dry_run=True)
def determine_index_cols(h5group)
def encode_index_cols(index_cols)
def __init__(self, message, nfields=50)
std::string format(const int32_t &value, const int &ndigits=8)
def insert(astring, substr, pos, overwrite=True)