Coverage for rta_to_lstchain_formator/utils/pytables.py: 12%
14 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-25 05:21 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-25 05:21 +0000
1import tables
4# Copied from https://github.com/cta-observatory/cta-lstchain/blob/v0.10.7/lstchain/io/io.py#L828
5# to remove explicit dependnecy on entire lstchain
6def add_column_table(table, ColClass, col_label, values):
7 """
8 Add a column to an pytable Table
10 Parameters
11 ----------
12 table: `tables.table.Table`
13 ColClass: `tables.atom.MetaAtom`
14 col_label: str
15 values: list or `numpy.ndarray`
17 Returns
18 -------
19 `tables.table.Table`
20 """
21 # Step 1: Adjust table description
22 d = table.description._v_colobjects.copy() # original description
23 d[col_label] = ColClass() # add column
25 # Step 2: Create new temporary table:
26 newtable = tables.Table(table._v_file.root, "_temp_table", d, filters=table.filters) # new table
27 table.attrs._f_copy(newtable) # copy attributes
28 # Copy table rows, also add new column values:
29 for row, value in zip(table, values):
30 newtable.append([tuple(list(row[:]) + [value])])
31 newtable.flush()
33 # Step 3: Move temporary table to original location:
34 parent = table._v_parent # original table location
35 name = table._v_name # original table name
36 table.remove() # remove original table
37 newtable.move(parent, name) # move temporary table to original location
39 return newtable