File size: 8,316 Bytes
25853a2
 
 
 
 
 
 
 
 
 
dc610d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
title: ZenVolt AI Trust Engine
emoji: 🔋
colorFrom: green
colorTo: blue
sdk: docker
app_port: 7860
pinned: false
---

# Battery State of Health (SOH) Prediction - Advanced ML Models

## Overview

This project implements an advanced machine learning pipeline for predicting Battery State of Health (SOH) using multiple models (Random Forest, XGBoost, LightGBM) with hyperparameter tuning, uncertainty quantification, and End-of-Life (EOL) prediction.

**SOH Definition**: `current_capacity / initial_capacity`

## Features

### 1. **Data Cleaning & Preprocessing**
- Missing value handling (forward fill, backward fill, median imputation)
- Outlier detection and capping using IQR method
- Duplicate removal

### 2. **Advanced Feature Engineering**
- **Statistical Features**: Mean, std, min, max, range, median per cycle for voltage, current, temperature
- **Rolling Features**: 5-cycle rolling averages and standard deviations
- **Temporal Features**: Cycles from start, cycles to end (supports timestamp data if available)
- **Physics-Informed Features**:
  - Energy throughput per cycle
  - Charge/discharge asymmetry
  - Power (voltage × current)
  - Temperature stress indicators
  - Capacity fade rate and acceleration
- **Degradation Features**: Capacity degradation rates, cumulative capacity loss

### 3. **Multiple ML Models with Hyperparameter Tuning**
- **Random Forest**: Grid search over n_estimators, max_depth, min_samples_split, min_samples_leaf
- **XGBoost**: Grid search over n_estimators, max_depth, learning_rate, subsample, colsample_bytree
- **LightGBM**: Grid search over n_estimators, max_depth, learning_rate, subsample, colsample_bytree, num_leaves

### 4. **Uncertainty Quantification**
- Prediction intervals (95% confidence) using Random Forest tree predictions
- Standard deviation of predictions across trees

### 5. **Early Stopping Detection (EOL Prediction)**
- Predicts when battery reaches 80% SOH (industry standard End-of-Life)
- Remaining Useful Life (RUL) prediction for each cycle
- Linear extrapolation from recent predictions

### 6. **Model Evaluation**
- R² Score, MAE, RMSE
- 5-fold cross-validation
- Model comparison table with R² improvements

### 7. **Visualizations**
- Model comparison: Predicted vs Actual SOH
- SOH vs Cycle Index (with uncertainty bands)
- Feature importance plots
- Residuals analysis
- EOL prediction visualization
- RUL prediction plots

## Dataset Requirements

### Expected CSV Format

Columns required:
- `cycle_index`: Cycle number
- `voltage`: Battery voltage (V)
- `current`: Battery current (A, positive for charge, negative for discharge)
- `temperature`: Battery temperature (°C)
- `capacity`: Battery capacity (Ah)

Optional columns (for enhanced temporal features):
- `timestamp` or `date`: Timestamp for each measurement

### Sample Data Format

```
cycle_index,voltage,current,temperature,capacity
1,4.2,1.5,25.0,2.0
1,4.1,1.5,25.1,2.0
...
2,4.2,1.5,25.0,1.98
...
```

## Public Battery Datasets

### NASA PCoE Battery Datasets

**Official Source**:
- NASA Open Data Portal: https://data.nasa.gov/dataset/Li-ion-Battery-Aging-Datasets/uj5r-zjdb/data
- Datasets: B0005, B0006, B0007, B0018 (run-to-failure cycling data)
- Format: `.mat` files (MATLAB format)

**CSV Conversions**:
- GitHub: `bnarms/NASA-Battery-Dataset` - Contains original .mat files and conversion scripts
- GitHub: `RyuMyunggi/NASA-battery-dataset-eda` - EDA and CSV conversion examples
- GitHub: `huzaifi18/RUL_prediction` - Pre-processed CSV files

**Conversion Script** (if using .mat files):
```python
import scipy.io
import pandas as pd

mat_data = scipy.io.loadmat('B0005.mat')
# Extract data and convert to CSV format
# Refer to NASA dataset documentation for structure
```

### CALCE Battery Datasets

**Official Sources**:
- Main page: https://calce.umd.edu/battery-data
- Data index: https://calce.umd.edu/data
- Format: Text/CSV files per experiment

**Direct Access**:
- University of Michigan IoFT mirror: https://ioft-data.engin.umich.edu/calce-battery-datasets/
- Provides CSV-ready cycling data

### Stanford Battery Datasets

**Sources**:
- BEEP Project: https://tri-amdd.github.io/beep/ (includes Stanford aging datasets)
- GitHub: `petermattia/revisit-severson-et-al` - Conversion scripts
- Format: MATLAB/HDF5 (with CSV export tools)

**Access**:
1. Download from BEEP project
2. Use BEEP tools to export to CSV
3. Or use conversion scripts from the GitHub repo

## Usage

### Quick Start (Sample Data)

For testing, you can generate sample data:

```python
from download_battery_datasets import create_sample_battery_data
file_path = create_sample_battery_data('./data/sample_battery_data.csv', n_cycles=100)
```

### Google Colab

1. **Upload both files** to Google Colab:
   - `battery_soh_prediction.ipynb` (notebook)
   - `download_battery_datasets.py` (dataset downloader)

2. **Install dependencies**:
   ```python
   !pip install xgboost lightgbm scipy requests
   ```

3. **Choose a data source** (see Step 1 in the notebook):
   - **Option A**: Download public datasets
     ```python
     from download_battery_datasets import download_nasa_dataset
     file_path = download_nasa_dataset('B0005')
     ```
   - **Option B**: Generate sample data
     ```python
     from download_battery_datasets import create_sample_battery_data
     file_path = create_sample_battery_data('./data/sample_battery_data.csv')
     ```
   - **Option C**: Upload your CSV file
     ```python
     from google.colab import files
     uploaded = files.upload()
     file_path = list(uploaded.keys())[0]
     ```

4. **Run all cells** in the notebook

### Local Python Environment

1. **Install dependencies**:
   ```bash
   pip install pandas numpy scikit-learn matplotlib seaborn xgboost lightgbm scipy requests
   ```

2. **Download datasets** (optional):
   ```python
   from download_battery_datasets import download_nasa_dataset, create_sample_battery_data
   
   # Download NASA dataset
   file_path = download_nasa_dataset('B0005')
   
   # Or create sample data
   file_path = create_sample_battery_data('./data/sample_battery_data.csv')
   ```

3. **Run the Python script**:
   ```bash
   python battery_soh_prediction.py
   ```
   
   **Or use Jupyter**:
   ```bash
   jupyter notebook battery_soh_prediction.ipynb
   ```

## Output

The pipeline generates:

1. **Model Comparison Table**: R², MAE, RMSE for all models
2. **Best Model Selection**: Automatically selects model with highest Test R²
3. **Prediction Intervals**: 95% confidence intervals (Random Forest)
4. **EOL Prediction**: Cycle when battery reaches 80% SOH
5. **Visualizations**: 6 comprehensive plots saved as `battery_soh_prediction_results.png`

## Model Comparison

The code automatically compares:
- **Random Forest** (baseline)
- **XGBoost** (if available)
- **LightGBM** (if available)

Results include:
- Train R², Test R²
- Cross-validation R² (mean ± std)
- Test MAE, Test RMSE
- R² Improvement over baseline

## Notes

- **Time Series Splitting**: The 80/20 train-test split is done without shuffling to preserve time series order
- **Cross-Validation**: 5-fold CV is used for model evaluation
- **Hyperparameter Tuning**: Uses GridSearchCV with 3-fold CV
- **Feature Engineering**: All features are created at the cycle level (aggregated per cycle)
- **Uncertainty**: Currently implemented for Random Forest only (using tree variance)
- **EOL Threshold**: Set to 80% SOH (industry standard), can be modified in code

## Troubleshooting

### XGBoost/LightGBM Not Available
- Install: `pip install xgboost lightgbm`
- The code will automatically skip unavailable models

### Missing Columns Error
- Ensure your CSV has at least: `cycle_index`, `voltage`, `current`, `temperature`, `capacity`
- Column names are case-sensitive

### Out of Memory
- Reduce hyperparameter grid search space
- Use smaller dataset or sample data
- Reduce number of estimators in models

## Citation

If using public datasets, please cite the original sources:
- **NASA**: "Li-ion Battery Aging Datasets" - NASA Prognostics Center of Excellence
- **CALCE**: University of Maryland Center for Advanced Life Cycle Engineering
- **Stanford**: Severson et al. cycle-life prediction datasets

## License

This code is provided as-is for research and educational purposes.