-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassessOriginalModel.py
304 lines (244 loc) · 14.9 KB
/
assessOriginalModel.py
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import sys
import os
import shutil
import time
import pandas
import sklearn.ensemble
import sklearn.metrics
import mlutilities.types as mltypes
import mlutilities.dataTransformation as mldata
import mlutilities.modeling as mlmodel
import mlutilities.utilities as mlutils
import thesisFunctions
import constants
def setUpFiles(basePath):
"""
Creates folder structure in OriginalModelAssessment folder and copies all years training data + test set
:return:
"""
allMonthsPath = 'AllMonthsDryHalf/'
regions = ['IntMnt', 'Xeric']
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
subFolders = ['CurrentFoldData', 'Output', 'Prediction']
for region in regions:
for month in months:
print('Processing:', region, month.capitalize())
# Create folder for each month & region in original model folder
newFolderPath = basePath + region + '/' + month
if not os.path.exists(newFolderPath):
os.makedirs(newFolderPath)
# Create subfolders used in model pipeline
for subFolder in subFolders:
newSubFolderPath = newFolderPath + '/' + subFolder
if not os.path.exists(newSubFolderPath):
os.makedirs(newSubFolderPath)
# Copy all years training sets and test sets
for i in range(5):
# Build paths
trainFileName = '{}_{}_all_{}_train.csv'.format(month, region, i)
testFileName = '{}_{}_{}_test.csv'.format(month, region, i)
sourceTrainFilePath = allMonthsPath + region + '/' + month + '/' + trainFileName
newTrainFilePath = newFolderPath + '/' + trainFileName
sourceTestFilePath = allMonthsPath + region + '/' + month + '/' + testFileName
newTestFilePath = newFolderPath + '/' + testFileName
# Copy files
shutil.copyfile(sourceTrainFilePath, newTrainFilePath)
shutil.copyfile(sourceTestFilePath, newTestFilePath)
# Add in full dataset and Sacramento data for prediction
fullFileName = '{}_{}_all.csv'.format(month, region)
sourceFullFilePath = allMonthsPath + region + '/' + month + '/' + fullFileName
newFullFilePath = newFolderPath + '/Prediction/' + fullFileName
shutil.copyfile(sourceFullFilePath, newFullFilePath)
sacData = thesisFunctions.prepSacramentoData(month,
region)
predictionFilePath = newFolderPath + '/Prediction/sacramentoData.csv'
sacData.to_csv(predictionFilePath, index=False)
def getMonthVars(basepath, month, region):
"""
Gets the list of variables chosen for that month and region from a text file
"""
# Find month number
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
monthNumber = months.index(month) + 1
monthNumber = str(monthNumber)
if len(monthNumber) < 2:
monthNumber = '0' + monthNumber
# Get month variables from file
monthVarPath = basepath + region + '/Vars/mvar' + str(monthNumber) + '.txt'
with open(monthVarPath) as monthVarFile:
monthVarString = '[' + monthVarFile.read() + ']'
# Get rid of TOPWET because I don't have that data
monthVarString = monthVarString.replace(',"TOPWET"', '')
monthVars = eval(monthVarString)
# Drop the first 5, because they are ID variables and the label
monthVars = monthVars[5:]
return monthVars
def runModels(basePath, performanceEstimation=True, prediction=False):
randomSeed = constants.randomSeed
myFeaturesIndex = 6
myLabelIndex = 5
kFolds = 5
regions = ['IntMnt', 'Xeric']
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
r2Method = mltypes.ModelScoreMethod('R Squared', sklearn.metrics.r2_score)
meanOEMethod = mltypes.ModelScoreMethod('Mean O/E', mlmodel.meanObservedExpectedScore)
sdOEMethod = mltypes.ModelScoreMethod('Standard Deviation O/E', mlmodel.sdObservedExpectedScore)
mseMethod = mltypes.ModelScoreMethod('Mean Squared Error (cfs)', sklearn.metrics.mean_squared_error)
testScoreMethods = [r2Method, meanOEMethod, sdOEMethod, mseMethod]
randomForestParameters = {'n_estimators': 2000,
'max_features': .333,
'random_state': randomSeed,
'n_jobs': -1}
randomForestMethod = mltypes.ModellingMethod(constants.randomForest,
sklearn.ensemble.RandomForestRegressor)
for region in regions:
for month in months:
print('Processing:', region, month.capitalize())
# Get expert features from text files
selectedFeatures = getMonthVars(basePath, month, region)
expertSelectedConfig = mltypes.FeatureEngineeringConfiguration('Expert Selection',
'selection',
mltypes.ExtractSpecificFeatures,
{'featureList': selectedFeatures})
modelFolder = basePath + region + '/' + month + '/'
# Run model once on each fold to get estimates of test metrics
if performanceEstimation:
allFoldScoreModelResultsDFs = []
for fold in range(kFolds):
# Get dataset info
foldTestFilePath = modelFolder + '{}_{}_{}_test.csv'.format(month, region, fold)
foldTrainFilePath = modelFolder + '{}_{}_all_{}_train.csv'.format(month, region, fold)
testDescription = month.capitalize() + ' ' + region + ' Test'
trainDescription = month.capitalize() + ' ' + region + ' Train'
# Copy to CurrentFoldDataFolder
testFilePath = modelFolder + 'CurrentFoldData/' + '{}_{}_test.csv'.format(month, region)
trainFilePath = modelFolder + 'CurrentFoldData/' + '{}_{}_all_train.csv'.format(month, region)
shutil.copyfile(foldTestFilePath, testFilePath)
shutil.copyfile(foldTrainFilePath, trainFilePath)
# Get datasets
fullTestDataSet = mltypes.DataSet(testDescription,
testFilePath,
featuresIndex=myFeaturesIndex,
labelIndex=myLabelIndex)
fullTrainDataSet = mltypes.DataSet(trainDescription,
trainFilePath,
featuresIndex=myFeaturesIndex,
labelIndex=myLabelIndex)
fullTrainDataSet = makeLabelRunoffPerDrainageUnit(fullTrainDataSet, 'labeled')
fullTestDataSet = makeLabelRunoffPerDrainageUnit(fullTestDataSet, 'labeled')
# Select features
trainDataSet, transformer = mldata.engineerFeaturesForDataSet(fullTrainDataSet,
expertSelectedConfig)
testDataSet = mldata.engineerFeaturesByTransformer(fullTestDataSet,
transformer)
# Apply model
applyRFModelConfig = mltypes.ApplyModelConfiguration('Apply ' + constants.randomForest,
randomForestMethod,
randomForestParameters,
trainDataSet,
testDataSet)
randomForestResult = mlmodel.applyModel(applyRFModelConfig)
applyModelResults = [randomForestResult]
# Score model and convert results to data frame
scoreModelResults = mlmodel.scoreModels(applyModelResults, testScoreMethods)
scoreModelResultsDF = mlutils.createScoreDataFrame(scoreModelResults)
# Add RMSE, then add to list of results for this month
scoreModelResultsDF['RMSE (cfs)'] = scoreModelResultsDF['Mean Squared Error (cfs)'].map(lambda x: x ** (1/2))
allFoldScoreModelResultsDFs.append(scoreModelResultsDF)
print(region, month, fold, 'processed')
# Aggregate results into a single DataFrame
allResultsDF = pandas.DataFrame()
for fold in allFoldScoreModelResultsDFs:
allResultsDF = allResultsDF.append(fold, ignore_index=True)
allResultsDF.to_csv(modelFolder + 'Output/scoreModelResults_all.csv', index=False)
# Group by unique model & dataset combinations to average
averageResultsDF = allResultsDF.groupby(['Base DataSet', 'Model Method']).mean().reset_index()
sortedAverageResultsDF = averageResultsDF.sort(columns='R Squared', ascending=False)
sortedAverageResultsDF.to_csv(modelFolder + 'Output/scoreModelResults_average.csv', index=False)
# Prediction
if prediction:
predictionFolder = modelFolder + 'Prediction/'
# Get data
fullTrainDataSet = mltypes.DataSet(month.capitalize() + ' Training Data',
predictionFolder + '{}_{}_all.csv'.format(month, region),
featuresIndex=myFeaturesIndex,
labelIndex=myLabelIndex)
fullPredictionDataSet = mltypes.DataSet(month.capitalize() + ' Prediction Data',
predictionFolder + 'sacramentoData.csv',
featuresIndex=3,
labelIndex=None)
# Get scaled label (runoff/drainage unit)
fullTrainDataSet = makeLabelRunoffPerDrainageUnit(fullTrainDataSet, 'labeled')
fullPredictionDataSet = makeLabelRunoffPerDrainageUnit(fullPredictionDataSet, 'prediction')
# Select features
trainDataSet, transformer = mldata.engineerFeaturesForDataSet(fullTrainDataSet,
expertSelectedConfig)
predictionDataSet = mldata.engineerFeaturesByTransformer(fullPredictionDataSet,
transformer)
# Train model and predict for the Sacramento region
applyRFModelConfig = mltypes.ApplyModelConfiguration('Apply ' + constants.randomForest,
randomForestMethod,
randomForestParameters,
trainDataSet,
predictionDataSet)
applyRFModelResult = mlmodel.applyModel(applyRFModelConfig)
rescalePredictions(applyRFModelResult, predictionDataSet)
predictionOutputPath = predictionFolder + 'sacramentoPredictions.csv'
thesisFunctions.outputPredictions(applyRFModelResult, predictionOutputPath)
if prediction:
print('Aggregating predictions.')
aggregateFile = thesisFunctions.aggregateSacPredictions([basePath],
'Output/',
'RandomForestData.csv',
months,
regions)
waterYear = 1977
thesisFunctions.formatWaterYearPredictions(waterYear, aggregateFile)
def makeLabelRunoffPerDrainageUnit(dataSet, dataSetType):
if dataSetType == 'labeled':
dataSet.dataFrame['RunoffPerDrainageUnit'] = dataSet.dataFrame.qmeas / dataSet.dataFrame.DRAIN_SQKM
columns = dataSet.dataFrame.columns.tolist()
newColumns = columns[:6] + columns[48:49] + columns[216:] + columns[6:48] + columns[49:216]
orderedFullTrainDF = dataSet.dataFrame[newColumns]
newDataSet = mltypes.DataSet(dataSet.description,
dataSet.path[:-4] + '_ratio.csv',
mode='w',
dataFrame=orderedFullTrainDF,
featuresIndex=8,
labelIndex=7)
elif dataSetType == 'prediction':
columns = dataSet.dataFrame.columns.tolist()
newColumns = columns[:3] + columns[45:46] + columns[3:45] + columns[46:]
orderedFullTrainDF = dataSet.dataFrame[newColumns]
newDataSet = mltypes.DataSet(dataSet.description,
dataSet.path[:-4] + '_ratio.csv',
mode='w',
dataFrame=orderedFullTrainDF,
featuresIndex=4,
labelIndex=None)
else:
raise Exception('dataSetType not recognized.')
return newDataSet
def rescalePredictions(applyModelResult, predictionDataSet):
predictions = applyModelResult.testPredictions
predictionsDataFrame = pandas.DataFrame(predictions, columns=['RunoffPerDrainageUnit'])
drainageArea = predictionDataSet.dataFrame.DRAIN_SQKM
combinedDataFrame = pandas.concat([predictionsDataFrame, drainageArea], axis=1)
combinedDataFrame['qpred'] = combinedDataFrame['RunoffPerDrainageUnit'] * combinedDataFrame['DRAIN_SQKM']
scaledPredictions = combinedDataFrame.qpred.tolist()
applyModelResult.testPredictions = scaledPredictions
return
# Initial setup
startSecond = time.time()
startTime = time.strftime('%a, %d %b %Y %X')
originalModelPath = 'ThesisAnalysis/OriginalModelAssessment/'
# Modeling process
# setUpFiles(originalModelPath)
runModels(originalModelPath, performanceEstimation=True, prediction=False)
# Report finish
endSecond = time.time()
endTime = time.strftime('%a, %d %b %Y %X')
totalSeconds = endSecond - startSecond
print('Start time:', startTime)
print('End time:', endTime)
print('Total: {} minutes and {} seconds'.format(int(totalSeconds // 60), round(totalSeconds % 60)))