Skip to main content
Version: Next

ts_shape.features.cycles.cycle_processor

Assign sample data to cycles and compute per-cycle results.

Boundary rule (used by every method): a sample belongs to a cycle when cycle_start <= systime < cycle_end. The interval is half-open, so with back-to-back cycles a sample at the shared timestamp belongs to the later cycle only, and every sample is counted once. A cycle without an end (cycle_end NaT) gets no samples. Samples with a missing timestamp belong to no cycle. Samples are matched on time only: pass the values of the machine the cycles belong to.

Classes
NameDescription
CycleDataProcessorA class to process cycle-based data and values with optimized performance.

CycleDataProcessor​

classsource
CycleDataProcessor(
dataframe_cycles: pd.DataFrame,
dataframe_values: pd.DataFrame,
cycle_uuid_column: str = "cycle_uuid",
time_column: str = "systime",
) -> None

Bases: Base

A class to process cycle-based data and values with optimized performance. Samples are matched to cycles with a sorted search (no per-cycle scans).

Functions
NameDescription
split_by_cycleSplits the values DataFrame by cycles defined in the cycles DataFrame.
merge_dataframes_by_cycleMerges the values DataFrame with the cycles DataFrame based on the cycle time intervals.
group_by_cycle_uuidGroup the DataFrame by the cycle_uuid column, resulting in a list of DataFrames, each containing data for one cycle.
split_dataframes_by_groupSplits a list of DataFrames by groups based on a specified column.
compute_cycle_statisticsCompute statistics for each cycle.
compare_cyclesCompare all cycles against a reference cycle.
identify_golden_cyclesIdentify the best performing cycles (golden cycles).
Parameters
NameTypeDescriptionDefault
dataframe_cyclesDataFrameDataFrame containing columns 'cycle_start', 'cycle_end', and 'cycle_uuid' (formerly cycles_df).required
dataframe_valuesDataFrameDataFrame containing the values and timestamps in the 'systime' column (formerly values_df).required
cycle_uuid_columnstrName of the column representing cycle UUIDs. cycle_uuid_col is the deprecated name.'cycle_uuid'
time_columnstrName of the column representing the timestamps for the values. systime_col is the deprecated name.'systime'
Raises
TypeDescription
ColumnNotFoundErrorIf a cycle column or the time column is missing.
TypeErrorIf one of cycles and values is tz-aware and the other naive.

cycle_uuid_col​

propertysource
cycle_uuid_col: str

Deprecated name of :attr:cycle_uuid_column.

systime_col​

propertysource
systime_col: str

Deprecated name of :attr:time_column.

split_by_cycle​

methodsource
split_by_cycle() -> dict[str, pd.DataFrame]

Splits the values DataFrame by cycles defined in the cycles DataFrame.

Returns
TypeDescription
dict[str, DataFrame]Dictionary where keys are cycle_uuids and values are DataFrames with the corresponding cycle data. Keys are in chronological cycle order; cycles without samples are left out. Rows keep their dataframe_values index and are sorted by time.

merge_dataframes_by_cycle​

methodsource
merge_dataframes_by_cycle() -> pd.DataFrame

Merges the values DataFrame with the cycles DataFrame based on the cycle time intervals.

Returns
TypeDescription
DataFrameThe samples that fall in a cycle, with an added cycle_uuid column, sorted by time (original index kept). Samples in no cycle are dropped. A sample inside two overlapping cycles appears once per cycle. No match: an empty frame with the same columns.

group_by_cycle_uuid​

methodsource
group_by_cycle_uuid(
dataframe: pd.DataFrame | None = None,
) -> list[pd.DataFrame]

Group the DataFrame by the cycle_uuid column, resulting in a list of DataFrames, each containing data for one cycle.

Parameters
NameTypeDescriptionDefault
dataframeDataFrame | NoneDataFrame to be grouped by cycle_uuid. If None, uses merge_dataframes_by_cycle().None
Returns
TypeDescription
list[DataFrame]List of DataFrames, one per cycle uuid, in order of first appearance.

split_dataframes_by_group​

methodsource
split_dataframes_by_group(
dfs: list[pd.DataFrame], group_column: str
) -> list[pd.DataFrame]

Splits a list of DataFrames by groups based on a specified column. This function performs a groupby operation on each DataFrame in the list and then flattens the result.

Parameters
NameTypeDescriptionDefault
dfslist[DataFrame]List of DataFrames to be split.required
group_columnstrColumn name to group by.required
Returns
TypeDescription
list[DataFrame]List of DataFrames, each corresponding to a group in the original DataFrames. Rows whose group_column is missing are left out.
Raises
TypeDescription
ColumnNotFoundErrorIf one of dfs has no group_column (it was skipped with a log message before).

compute_cycle_statistics​

methodsource
compute_cycle_statistics() -> pd.DataFrame

Compute statistics for each cycle.

Returns
TypeDescription
DataFrameOne row per cycle in dataframe_cycles order: cycle_uuid, cycle_start, cycle_end, duration_seconds, value_count (samples in the cycle), unique_uuids, mean_value_double / std_value_double (when dataframe_values has value_double), and the canonical start, end, sample_count. An open cycle has NaN duration and 0 samples.

compare_cycles​

methodsource
compare_cycles(
reference_cycle_uuid: str, value_column: str = "value_double"
) -> pd.DataFrame

Compare all cycles against a reference cycle.

Parameters
NameTypeDescriptionDefault
reference_cycle_uuidstrUUID of the reference cyclerequired
value_columnstrColumn holding the values to compare. metric is the deprecated name.'value_double'
Returns
TypeDescription
DataFrameOne row per cycle with data for value_column (chronological order): cycle_uuid, is_reference, mean_value, std_value, deviation_from_ref (mean minus reference mean), deviation_pct (relative to the reference mean, NaN when it is 0) and variability_ratio (std over reference std, NaN when that is 0 or undefined). Empty (full columns) with a DataQualityWarning when the reference cycle is unknown or has no data.
Raises
TypeDescription
ColumnNotFoundErrorIf value_column is not a column of dataframe_values.

identify_golden_cycles​

methodsource
identify_golden_cycles(
value_column: str = "value_double",
method: str = "low_variability",
top_n: int = 5,
target: float | None = None,
) -> list[str]

Identify the best performing cycles (golden cycles).

Parameters
NameTypeDescriptionDefault
value_columnstrColumn holding the values to evaluate. metric is the deprecated name.'value_double'
methodstr'low_variability' (smallest std /mean
top_nintNumber of golden cycles to identify5
targetfloat | NoneTarget value, required for 'target_value'.None
Returns
TypeDescription
list[str]Up to top_n cycle UUIDs, best first; ties keep chronological order. Cycles without value_column data, or whose score is undefined (a single sample has no std), are not ranked.
Raises
TypeDescription
ValueErrorFor an unknown method, or 'target_value' without target.
ColumnNotFoundErrorIf value_column is not a column of dataframe_values.