-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
159 lines (124 loc) · 6.1 KB
/
processing.py
File metadata and controls
159 lines (124 loc) · 6.1 KB
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
import numpy as np
import pandas as pd
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from visualization import plotPredictions, plotTimeSeries
def loadData(filePath, includeCycleNum=True):
"""
loads and prepares battery data from a csv file.
input:
filepath (str): name of the csv file in the data directory
includecyclenum (bool): whether to include cycle number as a feature (default: true)
output:
x (numpy.ndarray): feature matrix with shape (n_samples, n_features)
capacity (numpy.ndarray): target capacity values with shape (n_samples, 1)
time (numpy.ndarray): time values with shape (n_samples, 1)
function:
reads battery cycling data from csv, extracts time, voltage, current, temperature,
and capacity columns. optionally includes cycle number as an additional feature.
returns preprocessed feature matrix and target values.
"""
data = pd.read_csv("data/" + filePath)
# extract relevant columns
# time_s: time since beginning of experiment in seconds
time = data.iloc[:, 0].values.reshape(-1, 1)
# ecell_v: cell voltage
voltage = data.iloc[:, 1].values.reshape(-1, 1)
# i_ma: cell current in milliamperes
current = data.iloc[:, 2].values.reshape(-1, 1)
# temperature__c: cell surface temperature in degrees celsius
temperature = data.iloc[:, 7].values.reshape(-1, 1)
# qdischarge_ma_h: charge extracted from cell during discharge in milliampere-hours
capacity = data.iloc[:, 6].values.reshape(-1, 1)
if includeCycleNum:
# cyclenumber: cycle number as recorded by the cell tester
cycleNumber = data.iloc[:, 8].values.reshape(-1, 1)
X = np.concatenate((time, voltage, current, temperature, cycleNumber), axis=1)
else:
X = np.concatenate((time, voltage, current, temperature), axis=1)
return X, capacity, time
def trainIndividualFiles(filePaths, includeCycleNum, testSize, numEstimators, seed):
"""
trains random forest models on individual battery files with train/test split.
input:
filepaths (list): list of csv file names to process
includecyclenum (bool): whether to include cycle number as a feature
testsize (float): proportion of data to use for testing (0.0 to 1.0)
numestimators (int): number of trees in the random forest
seed (int): random seed for reproducibility
output:
none (prints metrics and displays plots for each file)
function:
for each file, loads data, splits into train and test sets, trains a random forest
regressor, makes predictions, calculates mse and r^2 metrics, and generates
visualization plots showing prediction accuracy and time series degradation.
"""
for filePath in tqdm(filePaths, desc="processing files"):
# load data
X, y, time = loadData(filePath, includeCycleNum)
# split the data into training and testing sets
indices = np.arange(len(y))
xTrain, xTest, yTrain, yTest, indicesTrain, indicesTest = train_test_split(
X, y, indices, test_size=testSize, random_state=seed
)
# create and train the model
rfRegressor = RandomForestRegressor(n_estimators=numEstimators, random_state=seed)
rfRegressor.fit(xTrain, yTrain.ravel())
# make predictions
yPred = rfRegressor.predict(xTest)
# get evaluation metrics
mse = mean_squared_error(yTest, yPred)
r2 = r2_score(yTest, yPred)
print(f'for {filePath}:')
print(f'mse: {mse}')
print(f'r^2 score: {r2}')
# plot results
plotPredictions(yTest, yPred, filePath, r2, mse, numEstimators,
seed, includeCycleNum, testSize=testSize)
plotTimeSeries(time, indicesTest, yTest, yPred, filePath, testSize)
def trainCombinedFiles(filePaths, testFile, includeCycleNum, numEstimators, seed):
"""
trains a random forest model on combined battery files and tests on a held-out file.
input:
filepaths (list): list of all csv file names available
testfile (str): name of the file to use for testing (excluded from training)
includecyclenum (bool): whether to include cycle number as a feature
numestimators (int): number of trees in the random forest
seed (int): random seed for reproducibility
output:
none (prints metrics and displays plots)
function:
combines data from all training files into a single dataset, trains a random forest
model on the combined data, evaluates performance on the held-out test file,
calculates mse and r^2 metrics, and generates visualization plots. tests
cross-battery generalization capability.
"""
trainFiles = [f for f in filePaths if f != testFile]
# determine number of features
numFeatures = 5 if includeCycleNum else 4
# placeholder for combined training data
combinedXTrain = np.empty((0, numFeatures))
combinedYTrain = np.empty((0, 1))
# combine the training data from multiple files
for filePath in tqdm(trainFiles, desc="processing files"):
X, y, _ = loadData(filePath, includeCycleNum)
combinedXTrain = np.vstack((combinedXTrain, X))
combinedYTrain = np.vstack((combinedYTrain, y))
# train the model using the combined training data
rfRegressor = RandomForestRegressor(n_estimators=numEstimators, random_state=seed)
rfRegressor.fit(combinedXTrain, combinedYTrain.ravel())
# read the test data
xTest, yTest, _ = loadData(testFile, includeCycleNum)
# make predictions on the test set
yPred = rfRegressor.predict(xTest)
# evaluate the model's performance on the test set
mse = mean_squared_error(yTest, yPred)
r2 = r2_score(yTest, yPred)
print(f'test results for {testFile}:')
print(f'mse: {mse}')
print(f'r^2 score: {r2}')
# plot results
plotPredictions(yTest, yPred, testFile, r2, mse, numEstimators,
seed, includeCycleNum, trainFiles=trainFiles)