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
| Name | Description |
|---|
CycleDataProcessor | A class to process cycle-based data and values with optimized performance. |
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
Parameters
| Name | Type | Description | Default |
|---|
dataframe_cycles | DataFrame | DataFrame containing columns 'cycle_start', 'cycle_end', and 'cycle_uuid' (formerly cycles_df). | required |
dataframe_values | DataFrame | DataFrame containing the values and timestamps in the 'systime' column (formerly values_df). | required |
cycle_uuid_column | str | Name of the column representing cycle UUIDs. cycle_uuid_col is the deprecated name. | 'cycle_uuid' |
time_column | str | Name of the column representing the timestamps for the values. systime_col is the deprecated name. | 'systime' |
Raises
| Type | Description |
|---|
ColumnNotFoundError | If a cycle column or the time column is missing. |
TypeError | If one of cycles and values is tz-aware and the other naive. |
Deprecated name of :attr:cycle_uuid_column.
Deprecated name of :attr:time_column.
split_by_cycle() -> dict[str, pd.DataFrame]
Splits the values DataFrame by cycles defined in the cycles DataFrame.
Returns
| Type | Description |
|---|
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() -> pd.DataFrame
Merges the values DataFrame with the cycles DataFrame based on the cycle time intervals.
Returns
| Type | Description |
|---|
DataFrame | The 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(
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
| Name | Type | Description | Default |
|---|
dataframe | DataFrame | None | DataFrame to be grouped by cycle_uuid. If None, uses merge_dataframes_by_cycle(). | None |
Returns
| Type | Description |
|---|
list[DataFrame] | List of DataFrames, one per cycle uuid, in order of first appearance. |
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
| Name | Type | Description | Default |
|---|
dfs | list[DataFrame] | List of DataFrames to be split. | required |
group_column | str | Column name to group by. | required |
Returns
| Type | Description |
|---|
list[DataFrame] | List of DataFrames, each corresponding to a group in the original DataFrames. Rows whose group_column is missing are left out. |
Raises
| Type | Description |
|---|
ColumnNotFoundError | If one of dfs has no group_column (it was skipped with a log message before). |
compute_cycle_statistics() -> pd.DataFrame
Compute statistics for each cycle.
Returns
| Type | Description |
|---|
DataFrame | One 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(
reference_cycle_uuid: str, value_column: str = "value_double"
) -> pd.DataFrame
Compare all cycles against a reference cycle.
Parameters
| Name | Type | Description | Default |
|---|
reference_cycle_uuid | str | UUID of the reference cycle | required |
value_column | str | Column holding the values to compare. metric is the deprecated name. | 'value_double' |
Returns
| Type | Description |
|---|
DataFrame | One 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
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
| Name | Type | Description | Default |
|---|
value_column | str | Column holding the values to evaluate. metric is the deprecated name. | 'value_double' |
method | str | 'low_variability' (smallest std / | mean |
top_n | int | Number of golden cycles to identify | 5 |
target | float | None | Target value, required for 'target_value'. | None |
Returns
| Type | Description |
|---|
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
| Type | Description |
|---|
ValueError | For an unknown method, or 'target_value' without target. |
ColumnNotFoundError | If value_column is not a column of dataframe_values. |