legend-exp / pygama

Python package for data processing and analysis
https://pygama.readthedocs.io
GNU General Public License v3.0
18 stars 56 forks source link

Enhancement: ability to append columns to existing tables in lh5 files using `store.write_object` #455

Closed SamuelBorden closed 1 year ago

SamuelBorden commented 1 year ago

This addresses the enhancement part of issue #176. Users can now append new columns to an existing lgdo.Table in an lh5 file by using the append_column argument of the wo_mode parameter.

If the user tries to append_column with a table that has any of the same fields as the existing table, or if the new table is a different size than the existing table, then the function will raise an error.

>>> import pygama.lgdo as lh5 
>>> import numpy as np 
>>> tb1 = lh5.Table(col_dict={'dset1': lh5.Array(np.zeros(10))})
>>> tb2 = lh5.Table(col_dict={'dset2': lh5.Array(np.ones(10))})  # second table with new field name
>>> store = lh5.LH5Store()
>>> store.write_object(tb1, name='my_table', lh5_file="my_output.lh5")
>>> store.write_object(tb2, name='my_table', lh5_file="my_output.lh5", wo_mode="append_column")
>>> tb_dat, _ = store.read_object("my_table", "/tmp/my_output.lh5")
>>> tb_dat['dset1'].nda
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
>>> tb_dat['dset2'].nda
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
gipert commented 1 year ago

Great! Thanks a lot @SamuelBorden.