Skip to content

Table(ak.Array) doesn't preserve WaveformTables #210

@Yuan-Ru-Lin

Description

@Yuan-Ru-Lin
import numpy as np
from lgdo import Table, WaveformTable, Array

tbl = Table({
    "energy": Array([100, 200, 300]),
    "wf": WaveformTable(
        t0=np.array([0.0, 1.0, 2.0]),
        dt=np.array([0.01, 0.01, 0.01]),
        values=np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]),
    )
})

# Flatten for columnar storage
flat = tbl.flatten()
ak_arr = flat.view_as("ak")

# Reconstruct
tbl_back = Table(ak_arr)

assert "wf" not in tbl_back.keys()  # Lost! Only has wf__t0, wf__dt, wf__values

Note that a WaveformTable isn't reconstructed from the flattened arrays wf__{t0,dt,values}.

This issue is blocked by #208 because values needs to be reconstructed as an AOESA. After #208 is closed, this issue can be resolved by processing col_dict one more time here.

        if isinstance(col_dict, ak.Array):
            col_dict = _ak_to_lgdo_or_col_dict(col_dict)
+           col_dict = _reconstruct_waveform_tables(col_dict)

where _reconstruct_waveform_tables is defined as

def _reconstruct_waveform_tables(col_dict: dict) -> dict:
    """Detect {prefix}__t0, {prefix}__dt, {prefix}__values patterns and reconstruct WaveformTables."""
    # Find all prefixes that have the waveform pattern
    wf_prefixes = set()
    for key in col_dict:
        if key.endswith("__values"):
            prefix = key[:-8]  # remove "__values"
            if f"{prefix}__t0" in col_dict and f"{prefix}__dt" in col_dict:
                wf_prefixes.add(prefix)

    if not wf_prefixes:
        return col_dict

    # Reconstruct WaveformTables
    result = {}
    used_keys = set()
    for prefix in wf_prefixes:
        t0_key = f"{prefix}__t0"
        dt_key = f"{prefix}__dt"
        values_key = f"{prefix}__values"
        result[prefix] = lgdo.WaveformTable(
            t0=col_dict[t0_key],
            dt=col_dict[dt_key],
            values=col_dict[values_key],
        )
        used_keys.update([t0_key, dt_key, values_key])

    # Keep other columns unchanged
    for key, value in col_dict.items():
        if key not in used_keys:
            result[key] = value

    return result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions