-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAstrea.py
More file actions
275 lines (219 loc) · 8.63 KB
/
Astrea.py
File metadata and controls
275 lines (219 loc) · 8.63 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
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
import time,logging,os, numpy as np
from logging import handlers as loghds
from sklearn import preprocessing
#Module logging
logger = logging.getLogger("Astrea")
logger.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s')
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
class Astrea():
idxTS = None
idxName = None
data2keep = None
def __init__(self,idxTS,idxName,data2keep,logFolder="./logs"):
if not os.path.exists(logFolder):
os.makedirs(logFolder)
logFile = logFolder + "/Astrea.log"
hdlr = loghds.TimedRotatingFileHandler(logFile,
when="H",
interval=1,
backupCount=30)
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
self.idxTS = idxTS
self.idxName = idxName
self.data2keep = data2keep
def kFoldWithDegradetion(self,healthly,degraded,degradationPerc,k):
tt = time.clock()
logger.debug("kFoldWithDegradetion - start")
logger.debug("There are %d batteries to distribute in %d fold" % (len(healthly),k))
batteris4fold = int( len(healthly) / k )
logger.debug("Batteries for fold: %d" % batteris4fold)
episodesInDataset = 0
for battery in healthly:
totalEpisodeInBattery = 0
for episodeInMonth in battery:
totalEpisodeInBattery += len(episodeInMonth)
episodesInDataset += totalEpisodeInBattery
batteryName = self.__getBatteryName(battery)
logger.debug("There are %d episode in battery %s" % (totalEpisodeInBattery,batteryName))
logger.debug("There are %d episode in dataset." % (episodesInDataset))
indexes,datas = self.__foldSplitDegradation(healthly,degraded,degradationPerc,episodesInDataset,k)
logger.debug("kfoldByKind - end - %f" % (time.clock() - tt))
return indexes,datas
def __foldSplitDegradation(self,batteries,degraded,degradationPerc,episodesInDataset,k):
tt = time.clock()
logger.debug("__foldSplit - start")
print(episodesInDataset)
maxEpisodesForFold = int( episodesInDataset / k )
logger.debug("Max episodes for fold %d" % maxEpisodesForFold)
currentFold = 0
foldIndex = []
foldIndex.append([])
foldData = []
foldData.append([])
#np.random.seed(1710)
np.random.seed(4988)
permutedIdx = np.random.permutation(len(batteries))
assigned = 0
degradedCount = 0
for idx in permutedIdx:
# iteration over batteries
battery = batteries[idx]
batteryName = self.__getBatteryName(battery)
totalEpisodeInBattery = 0
batteryIndex = []
batteryData = []
for monthIdx in range(0,len(battery)):
# iteration over months in battery
episodeInMonth = battery[monthIdx]
totalEpisodeInBattery += len(episodeInMonth)
for epIdx in range(0,len(episodeInMonth)):
# iteration over episodes in month
episode = episodeInMonth[epIdx]
degProb = np.random.uniform(0, 1)
for degIdx in range(len(degradationPerc)):
percCurretn = degradationPerc[degIdx]
if(degProb < percCurretn):
degradedCount +=1
episode = degraded[degIdx][idx][monthIdx][epIdx]
break
startTS = episode.values[:, self.idxTS][0]
indexRecord = (batteryName,startTS)
batteryIndex.append(indexRecord)
batteryData.append(episode[self.data2keep])
# check how many episodes are in the fold, it there are more than max, then switch fold
episodeInFold = len(foldIndex[currentFold])
if((episodeInFold + totalEpisodeInBattery) > maxEpisodesForFold and currentFold < (k - 1)):
assigned += episodeInFold
logger.debug("End of fold %d, dimension %d" % (currentFold,len(foldIndex[currentFold])))
currentFold += 1
foldIndex.append([])
foldData.append([])
foldIndex[currentFold] += batteryIndex
foldData[currentFold] += batteryData
episodeInFold = len(foldIndex[currentFold])
logger.debug("Last fold has %d episode" % (episodesInDataset - assigned))
logger.info("Degraded %d" % degradedCount)
logger.debug("__foldSplit - end - %f" % (time.clock() - tt))
return foldIndex, foldData
def kfoldByKind(self,batteries,k,printFold=False):
"""
Build K fold for the input. It is granted that every episode of a battery are all in the
same fold.
Output:
index: array that contains the battery label and for every episode the starting TS
data: array with episodes divided in K fold
"""
tt = time.clock()
logger.debug("kfoldByKind - start")
logger.debug("There are %d batteries to distribute in %d fold" % (len(batteries),k))
batteris4fold = int( len(batteries) / k )
logger.debug("Batteries for fold: %d" % batteris4fold)
episodesInDataset = 0
for battery in batteries:
totalEpisodeInBattery = 0
for episodeInMonth in battery:
totalEpisodeInBattery += len(episodeInMonth)
episodesInDataset += totalEpisodeInBattery
batteryName = self.__getBatteryName(battery)
logger.debug("There are %d episode in battery %s" % (totalEpisodeInBattery,batteryName))
logger.debug("There are %d episode in dataset." % (episodesInDataset))
indexes,datas = self.__foldSplit(batteries,episodesInDataset,k,printFold)
logger.debug("kfoldByKind - end - %f" % (time.clock() - tt))
return indexes,datas
def getScaler(self,foldedData):
data2dimension = []
for fold in foldedData:
for episode in fold:
for t in range(0,episode.shape[0]):
data2dimension.append(episode.values[t])
data2dimension.append([0, 22.0]) #should get this automatically
data2dimension.append([0, 36.0]) #should get this automatically
data2dimension = np.asarray(data2dimension)
scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
scaler.fit(data2dimension)
return scaler
def foldAs3DArray(self,fold,scaler = None):
"""
Convert the dataset list structure to numpy 3D array
batteries: 3 layer list of dataframe [battery][month][episode] = dataframe
if scaler are specified, data will be transformed
"""
tt = time.clock()
logger.debug("foldAs3DArray - start")
tmpData = []
#for fold in folds:
for e in fold:
x = e.values
if(scaler is not None):
x = scaler.transform(x)
tmpData.append( x )
outData = np.asarray(tmpData)
logger.debug("foldAs3DArray - end - %f" % (time.clock() - tt) )
return outData
def leaveOneFoldOut(self,k):
train = [[ j for j in range(k) if j != i ] for i in range(k)]
test = [[ j for j in range(k) if j == i ] for i in range(k)]
return train,test
def __foldSplit(self,batteries,episodesInDataset,k,printFold=False):
tt = time.clock()
logger.debug("__foldSplit - start")
maxEpisodesForFold = int( episodesInDataset / k )
logger.debug("Max episodes for fold %d" % maxEpisodesForFold)
currentFold = 0
foldIndex = []
foldIndex.append([])
foldData = []
foldData.append([])
#np.random.seed(1710)
np.random.seed(20091017)
permutedIdx = np.random.permutation(len(batteries))
assigned = 0
for idx in permutedIdx:
# iteration over batteries
battery = batteries[idx]
batteryName = self.__getBatteryName(battery)
totalEpisodeInBattery = 0
batteryIndex = []
batteryData = []
for episodeInMonth in battery:
# iteration over months in battery
totalEpisodeInBattery += len(episodeInMonth)
for episode in episodeInMonth:
# iteration over episodes in month
startTS = episode.values[:, self.idxTS][0]
indexRecord = (batteryName,startTS)
batteryIndex.append(indexRecord)
batteryData.append(episode[self.data2keep])
# check how many episodes are in the fold, it there are more than max, then switch fold
episodeInFold = len(foldIndex[currentFold])
if((episodeInFold + totalEpisodeInBattery) > maxEpisodesForFold and currentFold < (k - 1)):
assigned += episodeInFold
logger.debug("End of fold %d, dimension %d" % (currentFold,len(foldIndex[currentFold])))
currentFold += 1
foldIndex.append([])
foldData.append([])
if(printFold):
logger.debug("Battery #%s is is in fold %d" % (idx,currentFold))
foldIndex[currentFold] += batteryIndex
foldData[currentFold] += batteryData
episodeInFold = len(foldIndex[currentFold])
logger.debug("Last fold has %d episode" % (episodesInDataset - assigned))
logger.debug("__foldSplit - end - %f" % (time.clock() - tt))
return foldIndex, foldData
def __getBatteryName(self,battery):
batteryName = None
for episodeInMonth in battery:
if(len(episodeInMonth) > 0):
batteryName = episodeInMonth[0].values[:, self.idxName][0]
return batteryName
#####
#minAgeChargeScale = 50
#maxAgeChargeScale = 105
#step = 5
#dataRange(minerva,astrea,K,minAgeChargeScale,maxAgeChargeScale,step)
#return
#####