-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
183 lines (155 loc) · 8.19 KB
/
run.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
import json
import time
from models.LSTM import *
from models.biLSTM import *
from models.biLSTM_global import *
from models.naive import *
from models.linear import *
from models.mlp import *
# from utils.tract import *
import utils.preprocess as pp
import utils.tract as tr
from utils.eval import cv_mean_absolute_error
import config
if __name__ == '__main__':
startTime = time.time()
# 0.0 config
features = config.features
target_buildingLevel = config.target_buildingLevel
lag = config.lag
modelName = config.modelName
tuneTrail = config.tuneTrail
maxEpoch = config.maxEpoch
target_tractLevel = config.target_tractLevel
saveFolderHead = config.saveFolderHead
randomSeed = config.randomSeed
dirTargetYear = config.dirTargetYear
dayOfWeekJan1 = config.dayOfWeekJan1
testDataPer = config.testDataPer
dir_buildingMeta = './data/building_metadata/building_metadata.csv'
dir_energyData = './data/hourly_heat_energy/sim_result_ann_WRF_2018_csv'
dir_weatherData = './data/weather input/2018'
dir_typicalData = './data/testrun'
dirList = [dir_energyData, dir_weatherData, dir_typicalData]
dir_trueTractData = './data/hourly_heat_energy/annual_2018_tract.csv'
if dirTargetYear != None:
dirList.append(dirTargetYear[0])
dirList.append(dirTargetYear[1])
dirList.append(dirTargetYear[2])
dir_trueTractData = dirTargetYear[3]
np.random.seed(randomSeed)
# 0.1 create a dir for saving results
currentTime = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
experimentLabel = saveFolderHead + '_' + str(currentTime)
dirName = './saved/estimates_tracts/' + saveFolderHead + '_' + str(currentTime)
if not os.path.exists(dirName):
os.makedirs(dirName)
# 0.2 save config file
with open("./config.py", 'rb') as src_file:
with open(dirName + '/' + "./config.py", 'wb') as dst_file:
dst_file.write(src_file.read())
# 1.0 set training and testing set
# Option 1: split data using microclimates
# pairListTrain, pairListTest = tr.splitBuildingWeatherPair_byWeather(dir_buildingMeta, 0.85)
# if dirTargetYear != None:
# pairListTrain = pairListTrain + pairListTest
# pairListTest = pairListTrain
# Option 2: split data using microclimates for each prototype
pairListTrain, pairListTest = tr.splitBuildingWeatherPair_byProto(dir_buildingMeta, testDataPer)
# Option 3: only for debugging, it can limit the train and test to selected prototype-weather pairs
# pairListTrain = pairListTrain[0:1]
# pairListTest = pairListTest[0:1]
# pairListTrain = [i for i in pairListTrain if i[0] == 'SingleFamily-2004']
# pairListTest = [i for i in pairListTest if i[0] == 'SingleFamily-2004']
with open(dirName + '/pairListTrain.json', 'w') as f:
json.dump(pairListTrain, f)
with open(dirName + '/pairListTest.json', 'w') as f:
json.dump(pairListTest, f)
# 1.1 train
if modelName == 'naive':
prediction_tract = train_tract_naive(dirList,
pairListTrain, pairListTest,
target = target_buildingLevel,
dayOfWeekJan1 =dayOfWeekJan1,
)
if modelName == 'LSTM':
prediction_tract = train_tract_LSTM(dirList,
pairListTrain, pairListTest,
features,
target = target_buildingLevel,
lag = lag,
tuneTrail=tuneTrail,
maxEpoch=maxEpoch,
dayOfWeekJan1 = dayOfWeekJan1
)
if modelName == 'biLSTM':
prediction_tract = train_tract_biRNN(dirList,
pairListTrain, pairListTest,
features,
target = target_buildingLevel,
lag = lag,
tuneTrail = tuneTrail,
maxEpoch = maxEpoch,
dayOfWeekJan1 = dayOfWeekJan1,
)
if modelName == 'linear':
prediction_tract = train_tract_linear(dirList,
pairListTrain, pairListTest,
features,
target = target_buildingLevel,
lag = lag,
dayOfWeekJan1 = dayOfWeekJan1,
)
if modelName == 'mlp':
prediction_tract = train_tract_mlp(dirList,
pairListTrain, pairListTest,
features,
target = target_buildingLevel,
lag = lag,
dayOfWeekJan1 = dayOfWeekJan1,
)
if modelName == 'biLSTM_global':
prediction_tract = train_tract_biRNN_global(pp.getAllPrototype(dir_energyData),
pairListTrain, pairListTest,
features,
target = target_buildingLevel,
lag = lag,
tuneTrail = tuneTrail,
maxEpoch = maxEpoch,
metaDataDir = dir_buildingMeta,
randomSeed = randomSeed,
dayOfWeekJan1 = dayOfWeekJan1,
)
if not os.path.exists(dirName + '/buildingLevel'):
os.makedirs(dirName + '/buildingLevel')
for key, df in prediction_tract.items():
df.to_csv(dirName + '/buildingLevel/' + f'{key}.csv')
# 1.2 norm the prediction
# prototypeArea = getBuildingArea_prototype('C:/Users/xiyu/Downloads/output_data/output_data/EP_output/result_ann_WRF_2018', verbose = 0)
# used for generate the prototype areas, do not have to do it again
with open('./data/building_metadata/prototypeArea.json', 'r') as f:
prototypeArea = json.load(f)
prediction_tract_norm = tr.normalize_perM2(prediction_tract, pairListTest, prototypeArea)
# 2.0 get metadata of tracts and remove tracts with the weather not in the test pairs
tractBuildingMeta = tr.getBuildingArea_tracts(dir_buildingMeta, pairListTest)
# tractNameRemove = tr.getTracts_remove(dir_buildingMeta, tractBuildingMeta)
# tractBuildingMeta = tractBuildingMeta[~tractBuildingMeta['id.tract'].isin(tractNameRemove)]
# 2.1 scaling up
estimate_tract = tr.predict_tracts(prediction_tract_norm, tractBuildingMeta)
endTime = time.time()
executionTime = endTime - startTime
# 3 get true data and remove tracts with weather that is not in the test pairs
true_tract = tr.filterTractData(tr.loadTractData(dir_trueTractData, target_tractLevel),
estimate_tract)
# true_tract = true_tract[~true_tract.geoid.isin(tractNameRemove)]
# 4 eval
tractsDf = tr.combineEstimateTrue(true_tract, estimate_tract, target_tractLevel)
tractsDf.to_csv(dirName + '/' + 'tractsDF.csv')
tr.getTractLevelMetrics(tractsDf,
'./saved/estimates_tracts/' + experimentLabel,
executionTime,
)
tr.plotPrototypeLevelMetrics(prediction_tract,
'./saved/estimates_tracts/' + experimentLabel,
cv_mean_absolute_error_wAbs,
'CVMAE_wAbs')