|
| 1 | +""" |
| 2 | +RUL (Remaining Useful Life) transformation utilities. |
| 3 | +
|
| 4 | +Provides pre-built functions for transforming RUL data to create realistic patterns |
| 5 | +for Asset Lifecycle Management and predictive maintenance tasks. |
| 6 | +""" |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import logging |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def apply_piecewise_rul_transformation( |
| 15 | + df: pd.DataFrame, |
| 16 | + maxlife: int = 100, |
| 17 | + time_col: str = 'time_in_cycles', |
| 18 | + rul_col: str = 'RUL' |
| 19 | +) -> pd.DataFrame: |
| 20 | + """ |
| 21 | + Transform RUL data to create realistic "knee" patterns. |
| 22 | + |
| 23 | + This function applies a piecewise transformation to RUL (Remaining Useful Life) values |
| 24 | + to create a more realistic degradation pattern commonly seen in predictive maintenance: |
| 25 | + - RUL stays constant at MAXLIFE until the remaining cycles drop below the threshold |
| 26 | + - Then RUL decreases linearly to 0 as the equipment approaches failure |
| 27 | + |
| 28 | + This creates the characteristic "knee" pattern seen in actual equipment degradation. |
| 29 | + |
| 30 | + Args: |
| 31 | + df: pandas DataFrame with time series data containing RUL values |
| 32 | + maxlife: Maximum life threshold for the piecewise function (default: 100) |
| 33 | + RUL values above this will be capped at maxlife |
| 34 | + time_col: Name of the time/cycle column (default: 'time_in_cycles') |
| 35 | + rul_col: Name of the RUL column to transform (default: 'RUL') |
| 36 | + |
| 37 | + Returns: |
| 38 | + pandas DataFrame with original data plus new 'transformed_RUL' column |
| 39 | + |
| 40 | + Raises: |
| 41 | + ValueError: If required columns are missing from the DataFrame |
| 42 | + |
| 43 | + Example: |
| 44 | + >>> df = pd.DataFrame({'time_in_cycles': [1, 2, 3], 'RUL': [150, 100, 50]}) |
| 45 | + >>> df_transformed = apply_piecewise_rul_transformation(df, maxlife=100) |
| 46 | + >>> print(df_transformed['transformed_RUL']) |
| 47 | + 0 100 |
| 48 | + 1 100 |
| 49 | + 2 50 |
| 50 | + Name: transformed_RUL, dtype: int64 |
| 51 | + """ |
| 52 | + # Validate inputs |
| 53 | + if not isinstance(df, pd.DataFrame): |
| 54 | + raise ValueError(f"Expected pandas DataFrame, got {type(df)}") |
| 55 | + |
| 56 | + if rul_col not in df.columns: |
| 57 | + raise ValueError( |
| 58 | + f"RUL column '{rul_col}' not found in DataFrame. " |
| 59 | + f"Available columns: {list(df.columns)}" |
| 60 | + ) |
| 61 | + |
| 62 | + if time_col not in df.columns: |
| 63 | + logger.warning( |
| 64 | + f"Time column '{time_col}' not found in DataFrame, but continuing anyway. " |
| 65 | + f"Available columns: {list(df.columns)}" |
| 66 | + ) |
| 67 | + |
| 68 | + # Create a copy to avoid modifying the original |
| 69 | + df_copy = df.copy() |
| 70 | + |
| 71 | + logger.info(f"Applying piecewise RUL transformation with maxlife={maxlife}") |
| 72 | + logger.debug(f"Input RUL range: [{df_copy[rul_col].min()}, {df_copy[rul_col].max()}]") |
| 73 | + |
| 74 | + # Apply piecewise transformation |
| 75 | + def transform_rul(rul_value): |
| 76 | + """Apply the piecewise transformation to a single RUL value.""" |
| 77 | + if pd.isna(rul_value): |
| 78 | + return rul_value # Keep NaN values as NaN |
| 79 | + if rul_value > maxlife: |
| 80 | + return maxlife |
| 81 | + return rul_value |
| 82 | + |
| 83 | + # Apply transformation to create new column |
| 84 | + df_copy['transformed_RUL'] = df_copy[rul_col].apply(transform_rul) |
| 85 | + |
| 86 | + logger.info( |
| 87 | + f"✅ Transformation complete! Added 'transformed_RUL' column. " |
| 88 | + f"Output range: [{df_copy['transformed_RUL'].min()}, {df_copy['transformed_RUL'].max()}]" |
| 89 | + ) |
| 90 | + logger.debug(f"Total rows processed: {len(df_copy)}") |
| 91 | + |
| 92 | + return df_copy |
| 93 | + |
| 94 | + |
| 95 | +def show_utilities(): |
| 96 | + """ |
| 97 | + Display available utility functions and their usage. |
| 98 | + |
| 99 | + Prints a formatted list of all available utilities in this workspace, |
| 100 | + including descriptions and example usage. |
| 101 | + """ |
| 102 | + utilities_info = """ |
| 103 | + ================================================================================ |
| 104 | + WORKSPACE UTILITIES - Asset Lifecycle Management |
| 105 | + ================================================================================ |
| 106 | + |
| 107 | + Available utility functions: |
| 108 | + |
| 109 | + 1. apply_piecewise_rul_transformation(df, maxlife=100, time_col='time_in_cycles', rul_col='RUL') |
| 110 | + |
| 111 | + Description: |
| 112 | + Transforms RUL (Remaining Useful Life) data to create realistic "knee" patterns |
| 113 | + commonly seen in predictive maintenance scenarios. |
| 114 | + |
| 115 | + Parameters: |
| 116 | + - df: pandas DataFrame with time series data |
| 117 | + - maxlife: Maximum life threshold (default: 100) |
| 118 | + - time_col: Name of time/cycle column (default: 'time_in_cycles') |
| 119 | + - rul_col: Name of RUL column to transform (default: 'RUL') |
| 120 | + |
| 121 | + Returns: |
| 122 | + DataFrame with original data plus new 'transformed_RUL' column |
| 123 | + |
| 124 | + Example: |
| 125 | + df_transformed = utils.apply_piecewise_rul_transformation(df, maxlife=100) |
| 126 | + print(df_transformed[['time_in_cycles', 'RUL', 'transformed_RUL']]) |
| 127 | + |
| 128 | + 2. show_utilities() |
| 129 | + |
| 130 | + Description: |
| 131 | + Displays this help message with all available utilities. |
| 132 | + |
| 133 | + Example: |
| 134 | + utils.show_utilities() |
| 135 | + |
| 136 | + ================================================================================ |
| 137 | + """ |
| 138 | + print(utilities_info) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + # Simple test |
| 143 | + print("RUL Utilities Module") |
| 144 | + print("=" * 50) |
| 145 | + show_utilities() |
| 146 | + |
0 commit comments