-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput process.py
238 lines (187 loc) · 5.31 KB
/
input process.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 29 16:08:36 2018
@author: Phoebe
"""
import csv
import numpy as np
#from decimal import *
#getcontext().prec = 8
'''
processing of input feature
'''
idpath = "/Users/Phoebe/Desktop/data/"
idfile = "idList.csv"
idf = open(idpath+idfile,'r')
ids = {}
skip = True
key = 0
for line in idf:
if skip:
skip = False
continue
id = int(line.split(',')[0])
ids[id] = key
key += 1
print(ids)
print(len(ids))
totaloutput = []
#path = "/Users/Phoebe/Desktop/data/selected/AUR02-CAR01/"
#path = "/Users/Phoebe/Desktop/data/selected/CHI01-AUR02/"
#path = "/Users/Phoebe/Desktop/data/selected/FRA01-SLO02/"
#path = "/Users/Phoebe/Desktop/data/selected/SEC10-TOR01/"
#path = "/Users/Phoebe/Desktop/data/selected/AUR02-CHI01/"
#path = "/Users/Phoebe/Desktop/data/selected/CAR01-AUR02/"
path = "/Users/Phoebe/Desktop/data/selected/SLO02-FRA01/"
drpath = "/Users/Phoebe/Desktop/data/"
#drfile = "AUR02-CAR01.csv"
#drfile = "CHI01-AUR02.csv"
#drfile = "FRA01-SLO02.csv"
#drfile = "SEC10-TOR01.csv"
#drfile = "AUR02-CHI01.csv"
#drfile = "CAR01-AUR02.csv"
drfile = "SLO02-FRA01.csv"
#fields for a single location
#temp temp_diff pressure humidity wind_speed clouds bagofweathers
for i in range(10):
filename = str(i) + ".csv"
f = open(path+filename, 'r')
reader = csv.reader(f)
first = True
output = []
dates = []
hours = []
for line in reader:
if first:
first = False
continue
[date, time] = line[0].split(' ')
hour = int(time.split(':')[0])
if len(dates) != 0:
if date == dates[-1]:
interval = hour - hours[-1]
else:
interval = 24 + hour - hours[-1]
if interval > 1:
print("other day!")#exam whether there are weather data missing between dates
if interval > 1:
prehour = hours[-1]
for i in range(interval-1):#assume there are not weather data missing between dates
dates.append(date)
hours.append(prehour+i+1)
row = []
for j in range(len(ids)+6):
row.append(None)
output.append(row)
temp_diff = float(line[5])-float(line[4])
weather = []
for i in range(len(ids)):
weather.append(0)
wtdata = line[9].split(')')
for i in range(len(wtdata)-1):
c = wtdata[i]
c = c.split(", '")[0]
wid = int(c.split('(')[1])
weather[ids[wid]] = 1
row = []
row.append(float(line[1]))
row.append(temp_diff)
row.append(float(line[2]))
row.append(float(line[3]))
row.append(float(line[6]))
row.append(float(line[8]))
for i in range(len(weather)):
row.append(weather[i])
dates.append(date)
hours.append(hour)
output.append(row)
print(len(dates), len(hours), len(output))
print(len(output[0]))
totaloutput.append(output)
print(len(totaloutput))
f.close()
totaloutput = np.hstack(totaloutput)
print(totaloutput.shape)
'''
processing of label
drop rate label correlation
0% 0
(0%,10%] 1
(10%,100%) 2
100% 3
'''
drfirst = True
drf = open(drpath+drfile, 'r')
drdate = []
drhour = []
dr = []
for line in drf:
if drfirst:
drfirst = False
continue
r = line.split(',')
[date,time] = r[1].split('T')
hour = int(time.split(':')[0])
drdate.append(date)
drhour.append(hour)
dr.append(float(r[2]))
drf.close()
#transfer from linear dr to catrgorical dr
def transfer(droprate):
if droprate == 0:
label = 0
elif droprate <= 0.1:
label = 1
elif droprate < 1:
label = 2
elif droprate == 1:
label = 3
return label
#compute max, min, mean droprate
fdate = []
fhour = []
fdr = []
s = 0
total = dr[0]
maximum = dr[0]
minimum = dr[0]
for i in range(len(drhour)):
if drhour[i] == drhour[i-1]:
total += dr[i]
maximum = np.max([maximum, dr[i]])
minimum = np.min([minimum, dr[i]])
if drhour[i] != drhour[i-1] and i != 0:
fdate.append(drdate[i])
fhour.append(drhour[i])
meanlab = transfer(total/(i-s))
maxlab = transfer(maximum)
minlab = transfer(minimum)
# fdr.append([total/(i-s), maximum, minimum])
fdr.append([maxlab, meanlab, minlab])
s = i
total = dr[i]
maximum = dr[i]
minimum = dr[i]
print("length of hourly drop rate: ")
print(len(fdate), len(fhour), len(fdr))
#print(fdr[3])
'''
concate of weather data and drop rate data
'''
matched = []
index = 0
for i in range(len(dates)):
if dates[i] == fdate[index] and hours[i] == fhour[index]:
# print("matched!")
# print(dates[i], hours[i])
if None not in totaloutput[i]:
row = np.append(totaloutput[i], fdr[index])
matched.append(row)
index = index + 1
if index == len(fdr)-1:
break
print("matched data shape: ")
print(len(matched), len(matched[0]))
print(len(dates), len(fdate), len(matched))
np.savetxt("/Users/Phoebe/Desktop/data/input/"+drfile, matched, delimiter=',', fmt = "%s")