Skip to content

Commit 2dc01b0

Browse files
committed
Fix code generation sandbox errors and add workspace utils template
- Fix database path in code_generation_assistant.py to use '/workspace/database/nasa_turbo.db' - Make utils import conditional and only for RUL transformations - Fix sys.path.append to use '/workspace' instead of '.' - Add utils_template folder with pre-built RUL transformation utilities - Update README with clear setup instructions for workspace utilities - Addresses customer issues: ModuleNotFoundError for utils and mysql modules Signed-off-by: Vineeth Kalluru <vikalluru@nvidia.com>
1 parent 61a393a commit 2dc01b0

File tree

4 files changed

+210
-33
lines changed

4 files changed

+210
-33
lines changed

industries/asset_lifecycle_management_agent/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,41 @@ This ensures that:
254254
- Output files are organized within your project
255255
- Configuration remains portable across different machines
256256
257+
#### Setting Up Workspace Utilities
258+
259+
**IMPORTANT**: The code generation assistant requires a `utils` folder inside your `output_data` directory for RUL transformation tasks.
260+
261+
**Setup Instructions:**
262+
263+
1. Create the output_data directory (if it doesn't exist):
264+
```bash
265+
mkdir -p output_data
266+
```
267+
268+
2. Copy the pre-built utility functions from the template:
269+
```bash
270+
cp -r utils_template output_data/utils
271+
```
272+
273+
3. Verify the setup:
274+
```bash
275+
ls output_data/utils/
276+
# You should see: __init__.py rul_utils.py
277+
```
278+
279+
**What's included:**
280+
- `apply_piecewise_rul_transformation(df, maxlife=100)` - Transforms RUL data to create realistic "knee" patterns
281+
- `show_utilities()` - Display available utility functions
282+
283+
These utilities are automatically available to the code generation assistant when running in the Docker sandbox (mapped as `/workspace/utils/`). The system will only import these utilities when specifically needed for RUL transformations, preventing unnecessary module loading errors (`ModuleNotFoundError: No module named 'utils'` will not occur).
284+
285+
**How It Works:**
286+
- When you start the sandbox with `output_data/` as the mount point, the `utils/` folder becomes accessible at `/workspace/utils/`
287+
- The code generation assistant only imports utils when performing RUL transformations
288+
- For regular tasks (data retrieval, plotting, etc.), utils are not imported, avoiding module errors
289+
290+
**Note**: If you move your `output_data` folder, make sure the `utils` subfolder comes with it, or copy it from `utils_template` again.
291+
257292
### 6. Vanna SQL Agent Training (Automatic)
258293
259294
**Important**: The Vanna SQL agent training happens automatically when you start the workflow server. The `vanna_training_data.yaml` file contains pre-configured domain-specific knowledge that will be loaded automatically during server startup.

industries/asset_lifecycle_management_agent/src/asset_lifecycle_management_agent/plotting/code_generation_assistant.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,45 +70,26 @@ async def _generate_and_execute_code(
7070
OUTPUT ONLY THE CODE. NO COMMENTS. NO DOCSTRINGS. NO EXPLANATIONS.
7171
Generate only the code needed. Your response must contain ONLY executable Python code which will be DIRECTLY EXECUTED IN A SANDBOX.
7272
73-
**UTILITIES AVAILABLE:**
74-
A 'utils' folder contains pre-built functions for Asset Lifecycle Management tasks (especially predictive maintenance):
75-
- utils.apply_piecewise_rul_transformation
76-
- DESCRIPTION: Takes an input pandas DataFrame with time series data and create realistic "knee" patterns on the provided RUL column.
77-
- INPUTS:
78-
- df: pandas DataFrame with time series data
79-
- maxlife: Maximum life threshold for the piecewise function (default: 100)
80-
- time_col: Name of the time/cycle column (default: 'time_in_cycles')
81-
- rul_col: Name of the RUL column to transform (default: 'RUL')
82-
OUTPUTS:
83-
- df - a pandas DataFrame with original data plus new 'transformed_RUL' column
73+
**DATABASE PATH:**
74+
For SQLite operations, the database file is located at: '/workspace/database/nasa_turbo.db'
75+
ALWAYS use this exact path when connecting to the database.
8476
85-
- utils.show_utilities(): Show all available utilities if you need to see them
77+
**UTILITIES (OPTIONAL - ONLY FOR RUL TRANSFORMATIONS):**
78+
ONLY IF the task involves piecewise RUL transformation, you may use:
79+
- utils.apply_piecewise_rul_transformation(df, maxlife=100, time_col='time_in_cycles', rul_col='RUL')
80+
Takes a pandas DataFrame and returns it with an added 'transformed_RUL' column using piecewise transformation.
8681
87-
**CRITICAL REQUIREMENT: ALWAYS USE UTILITIES when available instead of writing custom implementations.**
88-
This ensures reliable, tested functionality and consistent results.
89-
90-
To use utilities, start your code with:
82+
To use utilities (ONLY if needed for RUL transformation):
9183
```python
9284
import sys
93-
sys.path.append(".")
85+
sys.path.append("/workspace")
9486
import utils
9587
96-
<insert your code here>
97-
<insert your code here>
98-
<insert your code here>
99-
100-
# Saving files to the current working directory.)
101-
print("Original RUL column name: ", rul column name)
102-
print("Transformed RUL column name: ", 'transformed_RUL')
103-
print("File saved to: filename.json")
88+
# Your code using the utility
89+
df_transformed = utils.apply_piecewise_rul_transformation(df, maxlife=100)
10490
```
10591
106-
**UTILITY USAGE GUIDELINES:**
107-
- Check if your task can be accomplished using the utility function
108-
- For RUL transformations: ALWAYS use utils.apply_piecewise_rul_transformation() instead of custom logic
109-
- The utility handles all error checking and provides detailed success messages
110-
- Use maxlife parameter to control the knee threshold (default: 100)
111-
- Capture the DataFrame returned by the utility function for further processing
92+
DO NOT import utils unless specifically needed for RUL transformation tasks.
11293
11394
**CODE REQUIREMENTS:**
11495
1. Generate COMPLETE, SYNTACTICALLY CORRECT Python code
@@ -117,6 +98,7 @@ async def _generate_and_execute_code(
11798
4. NO comments, NO docstrings, NO explanations
11899
5. Use minimal variable names (df, fig, data, etc.)
119100
6. **CRITICAL FILE PATH RULE**: Use ONLY the filename directly (e.g., "filename.json"), NOT "output_data/filename.json"
101+
7. **DATABASE PATH RULE**: Use '/workspace/database/nasa_turbo.db' for SQLite connections
120102
8. **IF YOU STILL NEED TO SAVE FILES, THEN PRINT FILE NAMES TO STDOUT. (eg: print("Saved file to: filename.json"))**
121103
122104
GENERATE CODE ONLY. NO COMMENTS. NO EXPLANATIONS."""
@@ -225,17 +207,20 @@ def is_code_incomplete(code):
225207

226208
fix_prompt_text = f"""The previous code needs to be fixed. Please analyze the issue and generate corrected Python code.
227209
210+
ORIGINAL INSTRUCTIONS: {instructions}
211+
228212
PREVIOUS CODE:
229213
{code}
230214
231215
ISSUE TO FIX:
232216
{error_info}
233217
234218
Please generate corrected Python code that fixes the problem. Follow all requirements:
235-
- Use utilities when available
219+
- Use '/workspace/database/nasa_turbo.db' for database connections
220+
- Only import utils if doing RUL transformations (use sys.path.append("/workspace"))
236221
- Generate only executable Python code
237222
- No comments or explanations
238-
- Handle file paths correctly
223+
- Handle file paths correctly (use only filename, not paths)
239224
- Complete all code blocks properly
240225
- Ensure the code is complete and not truncated
241226
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Workspace utilities for Asset Lifecycle Management tasks.
3+
4+
These pre-built utility functions provide reliable, tested implementations
5+
for common data processing tasks, particularly for predictive maintenance workflows.
6+
"""
7+
8+
from .rul_utils import apply_piecewise_rul_transformation, show_utilities
9+
10+
__all__ = ['apply_piecewise_rul_transformation', 'show_utilities']
11+
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)