Skip to main content
Version: 0.0.98

ts_shape.transform.harmonization

Classes
NameDescription
DataHarmonizerData Harmonization for multi-signal timeseries.

DataHarmonizer​

classsource
DataHarmonizer(
dataframe: pd.DataFrame,
time_column: str = "systime",
uuid_column: str = "uuid",
value_column: str = "value_double",
) -> None

Bases: Base

Data Harmonization for multi-signal timeseries.

Provides utilities to pivot, resample, align, and fill gaps across multiple UUID-keyed signals stored in long (stacked) format.

Methods:

  • pivot_to_wide: Pivot long-format to wide-format (one column per UUID).
  • resample_to_uniform: Resample to a uniform time grid with interpolation.
  • detect_gaps: Identify time gaps per UUID exceeding a threshold.
  • fill_gaps: Fill detected gaps using various strategies.
  • align_asof: Align two UUID signals using merge_asof.
  • merge_multi_signals: End-to-end harmonization pipeline.
Functions
NameDescription
pivot_to_widePivot long-format DataFrame to wide-format with one column per UUID.
resample_to_uniformResample to a uniform time grid with interpolation.
detect_gapsIdentify time gaps per UUID exceeding the threshold.
fill_gapsFill gaps in the wide-format data using the specified strategy.
align_asofAlign two UUID signals using merge_asof with configurable tolerance.
merge_multi_signalsEnd-to-end harmonization: pivot, filter, resample, interpolate.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameLong-format input.required
time_columnstrTimestamp column. Text and pyarrow timestamps are converted; an empty untyped column becomes datetime64.'systime'
uuid_columnstrSignal identifier column.'uuid'
value_columnstrColumn holding the values to harmonize.'value_double'
Raises
TypeDescription
ColumnNotFoundErrorIf one of the three columns is missing.

pivot_to_wide​

methodsource
pivot_to_wide(aggfunc: str = 'first') -> pd.DataFrame

Pivot long-format DataFrame to wide-format with one column per UUID.

Memory: the wide frame is dense, one row per distinct timestamp of any signal and one column per UUID, so it holds timestamps x signals cells (8 bytes each for float values) however sparse the long frame is. Signals sampled at their own instants barely share rows: 100 such signals of 100,000 samples each (10 million long rows, about 0.5 GB) pivot to 10 million x 100 cells, 8 GB, mostly NaN. Select the signals you need and resample or round the timestamps onto a common grid before pivoting large data (:meth:resample_to_uniform pivots too, then resamples).

Parameters
NameTypeDescriptionDefault
aggfuncstrAggregation function for duplicate timestamps ('first', 'mean', 'last').'first'
Returns
TypeDescription
DataFrameDataFrame with systime as index and one column per UUID. A signal without any value is kept as an all-NaN column.

resample_to_uniform​

methodsource
resample_to_uniform(
window: str,
method: str = "linear",
fill_limit: int | None = None,
*,
sampling: Sampling = "first"
) -> pd.DataFrame

Resample to a uniform time grid with interpolation.

Pivots first, so the full wide frame of :meth:pivot_to_wide (one row per distinct timestamp, see its memory note) is built before it is reduced to the grid.

The grid is the resample(window) bin starts. sampling sets what a grid row holds:

  • "first" (default): the first sample in the bin t, t + window), labeled with the bin start t. The value is not the signal at t: a sample at t + 0.5 s in a 1 s bin is reported at t. Empty bins are then interpolated with method.
  • "interpolate": the signal's value at t, interpolated between the raw samples on either side (linear in time for method="linear").

Either way a signal is only filled between its samples (limit_area="inside"): grid rows before its first or after its last sample stay NaN rather than repeat the edge value.

Parameters
NameTypeDescriptionDefault
window[strPandas frequency string (e.g. '1s', '100ms', '1min').required
methodstrInterpolation method ('linear', 'time', 'nearest', 'quadratic', 'cubic').'linear'
fill_limitint | NoneMaximum number of consecutive NaNs to fill.None
samplingSampling"first" (default) or "interpolate", see above.'first'
Returns
TypeDescription
DataFrameDataFrame with uniform DatetimeIndex.

detect_gaps​

methodsource
detect_gaps(threshold: str = '10s') -> pd.DataFrame

Identify time gaps per UUID exceeding the threshold.

Parameters
NameTypeDescriptionDefault
thresholdstrMinimum gap duration as a pandas Timedelta string.'10s'
Returns
TypeDescription
DataFrameDataFrame with one row per gap: uuid, gap_start, gap_end, gap_duration (Timedelta) and the canonical start, end, duration_seconds (the same gap). Empty with these columns, typed, when there is no gap.

fill_gaps​

methodsource
fill_gaps(
strategy: str = "interpolate",
max_gap: str | None = None,
fill_value: float | None = None,
) -> pd.DataFrame

Fill gaps in the wide-format data using the specified strategy.

Parameters
NameTypeDescriptionDefault
strategystrOne of 'interpolate', 'ffill', 'bfill', 'constant'.'interpolate'
max_gapstr | NoneMaximum gap size to fill (pandas Timedelta string), measured from the valid sample before a run of missing values to the valid sample after it. Larger gaps remain NaN. None (default) fills every gap the strategy can fill, however long.None
fill_valuefloat | NoneValue used when strategy='constant'.None
Returns
TypeDescription
DataFrameWide-format DataFrame with gaps filled.

align_asof​

methodsource
align_asof(
left_uuid: str,
right_uuid: str,
tolerance: str = "1s",
direction: str = "nearest",
) -> pd.DataFrame

Align two UUID signals using merge_asof with configurable tolerance.

Parameters
NameTypeDescriptionDefault
left_uuidstrUUID of the left (reference) signal.required
right_uuidstrUUID of the right signal.required
tolerancestrMaximum time difference for matching.'1s'
directionstrOne of 'nearest', 'backward', 'forward'.'nearest'
Returns
TypeDescription
DataFrameDataFrame with systime, value_left, value_right.
Warns
TypeDescription
DataQualityWarningWhen a uuid is not in the (non-empty) frame; the result then has no matches for it.

merge_multi_signals​

methodsource
merge_multi_signals(
uuids: list[str] | None = None,
window: str | None = None,
method: str = "linear",
*,
sampling: Sampling = "first"
) -> pd.DataFrame

End-to-end harmonization: pivot, filter, resample, interpolate.

The selected signals are pivoted before resampling: the memory of :meth:pivot_to_wide applies to them (pass uuids to bound it).

Parameters
NameTypeDescriptionDefault
uuidslist[str] | NoneOptional list of UUIDs to include. None means all.None
windowstr | NoneOptional resample frequency. None means no resampling.None
methodstrInterpolation method for resampling.'linear'
samplingSamplingWith window, "first" (default: the first sample in each bin, labeled with the bin start) or "interpolate" (the value at each grid timestamp); see :meth:resample_to_uniform. A signal is not extended before its first or after its last sample.'first'
Returns
TypeDescription
DataFrameWide-format DataFrame ready for cross-signal analytics.