-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
269 lines (247 loc) Β· 10.3 KB
/
functions.py
File metadata and controls
269 lines (247 loc) Β· 10.3 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
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
def maindef(month, day, time, subwayStop, direction):
# λͺνΈμ μΈμ§ νλ³νκΈ° μν λ³μ (etc.κ΅°μμμ κ²½μ° 5νΈμ , 7νΈμ λμ μ‘΄μ¬)
global status
line = 0
week_day = ""
direc_dict = {
"up": "μμ ",
"down": "νμ ",
"in": "λ΄μ ",
"out": "μΈμ "
}
if day == "HOL":
week_day = "곡ν΄μΌ"
elif day == "MON" or day == "TUE" or day == "WED" or day == "THU" or day == "FRI":
week_day = "νμΌ"
elif day == "SAT":
week_day = "ν μμΌ"
direction = direc_dict.get(direction)
if subwayStop == "μ΄λ¦°μ΄λ곡μμ 7νΈμ ":
line = 7.0
if direction == "μμ ":
subwayStop = "κ΅°μ"
if direction == "νμ ":
subwayStop = "건λμ
ꡬ"
elif subwayStop == "건λμ
ꡬμ 7νΈμ ":
line = 7.0
if direction == "μμ ":
subwayStop = "μ΄λ¦°μ΄λ곡μ"
if direction == "νμ ":
subwayStop = "λμ¬μ μμ§"
elif subwayStop == "건λμ
ꡬμ 2νΈμ ":
line = 2.0
if direction == "λ΄μ ":
subwayStop = "ꡬμ"
if direction == "μΈμ ":
subwayStop = "λμ¬"
# print(day, direction, subwayStop, line)
# μ²λ¦¬ν λ°μ΄ν° νλ³
# CSV νμΌ μ½κΈ°
# print(f"File path: {file_path}")
data = pd.read_csv("dataset/Congestion.csv")
# # νκΈ ν°νΈ μ€μ
# rc('font', family='AppleGothic')
# 'μλ²νΈ'κ° 1κ³Ό 2μ΄κ³ 'μνꡬλΆ'μ΄ 'μΈμ 'μΈ λ°μ΄ν° νν°λ§
filtered_data = data[(data['μΆλ°μ'] == subwayStop) & (data['μνꡬλΆ'] == direction) & (data['νΈμ '] == line)].copy()
# print(filtered_data)
# 05μ 30λΆ, 00μ 30λΆμ λ°μ΄ν°μ μΈλ±μ€ μμ
filtered_data = filtered_data.drop(['5μ30λΆ', '00μ30λΆ', '00μ00λΆ'], axis=1)
# μκ°μ΄ μ ν
time_columns = filtered_data.columns[6:]
# λ μ§λ³λ‘ λ°μ΄ν° λΆλ₯νκΈ° (νμΌ, ν μμΌ, 곡ν΄μΌ)
weekday_data = filtered_data[filtered_data['μμΌκ΅¬λΆ'] == 'νμΌ']
saturday_data = filtered_data[filtered_data['μμΌκ΅¬λΆ'] == 'ν μμΌ']
holiday_data = filtered_data[filtered_data['μμΌκ΅¬λΆ'] == '곡ν΄μΌ']
# μκ°μ΄μ 30λΆ κ°κ²©μΌλ‘ λ¬Άμ΄ νκ· κ³μ°
average_weekday = []
average_saturday = []
average_holiday = []
average_time_list = [time.split('μ')[0] + 'μ' for time in time_columns if time.endswith('00λΆ')]
for i in range(0, len(time_columns), 2):
start_index = filtered_data.columns.get_loc(time_columns[i])
end_index = filtered_data.columns.get_loc(time_columns[i + 1]) + 1
average_weekday.append(weekday_data.iloc[:, start_index:end_index].mean(axis=1).values[0])
average_saturday.append(saturday_data.iloc[:, start_index:end_index].mean(axis=1).values[0])
average_holiday.append(holiday_data.iloc[:, start_index:end_index].mean(axis=1).values[0])
if week_day == "곡ν΄μΌ":
# # μκ°λλ³ λ°μ΄ν° μκ°ν
# plt.figure(figsize=(12, 6))
# plt.plot(range(0, len(time_columns), 2), average_holiday, label='곡ν΄μΌ')
# plt.title(f'{subwayStop}μ {direction} νΉμ μκ°λλ³ νκ· μΉκ° μ')
# plt.xlabel('μκ°')
# plt.ylabel('νκ· μΉκ° μ')
# plt.legend()
# plt.xticks(range(0, len(time_columns), 2), [time_columns[i] for i in range(0, len(time_columns), 2)],
# rotation=45)
# plt.grid(True)
# # κ° νμ
# for i in range(len(average_holiday)):
# plt.text(i * 2, average_holiday[i], f'{average_holiday[i]:.2f}', ha='right', va='bottom', fontsize=8,
# color='blue')
# plt.show()
status = get_status_time(average_holiday, average_time_list, time, month, day)
elif week_day == "νμΌ":
# # μκ°λλ³ λ°μ΄ν° μκ°ν
# plt.figure(figsize=(12, 6))
# plt.plot(range(0, len(time_columns), 2), average_weekday, label='νμΌ')
# plt.title(f'{subwayStop}μ {direction} νΉμ μκ°λλ³ νκ· μΉκ° μ')
# plt.xlabel('μκ°')
# plt.ylabel('νκ· μΉκ° μ')
# plt.legend()
# plt.xticks(range(0, len(time_columns), 2), [time_columns[i] for i in range(0, len(time_columns), 2)],
# rotation=45)
# plt.grid(True)
# # κ° νμ
# for i in range(len(average_weekday)):
# plt.text(i * 2, average_weekday[i], f'{average_weekday[i]:.2f}', ha='right', va='bottom', fontsize=8,
# color='blue')
# plt.show()
status = get_status_time(average_weekday, average_time_list, time, month, day)
elif week_day == "ν μμΌ":
# # μκ°λλ³ λ°μ΄ν° μκ°ν
# plt.figure(figsize=(12, 6))
# plt.plot(range(0, len(time_columns), 2), average_saturday, label='ν μμΌ')
# plt.title(f'{subwayStop}μ {direction} νΉμ μκ°λλ³ νκ· μΉκ° μ')
# plt.xlabel('μκ°')
# plt.ylabel('νκ· μΉκ° μ')
# plt.legend()
# plt.xticks(range(0, len(time_columns), 2), [time_columns[i] for i in range(0, len(time_columns), 2)],
# rotation=45)
# plt.grid(True)
# # κ° νμ
# for i in range(len(average_saturday)):
# plt.text(i * 2, average_saturday[i], f'{average_saturday[i]:.2f}', ha='right', va='bottom', fontsize=8,
# color='blue')
# plt.show()
status = get_status_time(average_saturday, average_time_list, time, month, day)
return status
# μκ°μΌλ‘ λΆλ₯
def get_status_time(data, time_list, time, month, day):
status_dict = {
"6μ": [],
"7μ": [],
"8μ": [],
"9μ": [],
"10μ": [],
"11μ": [],
"12μ": [],
"13μ": [],
"14μ": [],
"15μ": [],
"16μ": [],
"17μ": [],
"18μ": [],
"19μ": [],
"20μ": [],
"21μ": [],
"22μ": [],
"23μ": []
}
# print(len(time_columns))
for i in range(len(data)):
data_value = float(data[i])
time_key = time_list[i]
# print(data_value)
if data_value <= 45.0:
status_dict[time_key].append(("good", 8))
if data_value < 30.0:
status_dict[time_key].pop()
status_dict[time_key].append(("good", 9))
if data_value < 15.0:
status_dict[time_key].pop()
status_dict[time_key].append(("good", 10))
elif data_value <= 90.0:
status_dict[time_key].append(("soso", 5))
if data_value < 75.0:
status_dict[time_key].pop()
status_dict[time_key].append(("soso", 6))
if data_value < 60.0:
status_dict[time_key].pop()
status_dict[time_key].append(("soso", 7))
elif data_value <= 150.0:
status_dict[time_key].append(("bad", 1))
if data_value < 135.0:
status_dict[time_key].pop()
status_dict[time_key].append(("bad", 2))
if data_value < 120.0:
status_dict[time_key].pop()
status_dict[time_key].append(("bad", 3))
if data_value < 105.0:
status_dict[time_key].pop()
status_dict[time_key].append(("bad", 4))
time = time.split(":")[0] + "μ"
time_index = str(int(str(time.split('μ')[0]))) + "μ"
MON_index = ["9μ", "22μ", "23μ"]
FRI_index = ["16μ", "17μ", "18μ", "19μ", "20μ", "21μ", "22μ", "23μ"]
# μ, μμΌ, μκ°λλ³ κ°μ€μΉ μ μ©
if day=="MON":
for i in MON_index:
temp = list(status_dict[i][0])
if (temp[1] < 10):
temp[1] += 1
temp = tuple(temp)
status_dict[i].pop()
status_dict[i].append(temp)
if status_dict[i][0][1] >= 8:
temp2 = list(status_dict[i][0])
temp2[0] = "good"
temp2 = tuple(temp2)
status_dict[i].pop()
status_dict[i].append(temp2)
elif status_dict[i][0][1] >= 4:
temp3 = list(status_dict[i][0])
temp3[0] = "soso"
temp3 = tuple(temp3)
status_dict[i].pop()
status_dict[i].append(temp3)
elif status_dict[i][0][1] >= 4:
temp4 = list(status_dict[i][0])
temp4[0] = "bad"
temp4 = tuple(temp4)
status_dict[i].pop()
status_dict[i].append(temp4)
elif day == "FRI":
for i in FRI_index:
temp = list(status_dict[i][0])
if (temp[1] < 10):
temp[1] -= 1
temp = tuple(temp)
status_dict[i].pop()
status_dict[i].append(temp)
if status_dict[i][0][1] >= 8:
temp2 = list(status_dict[i][0])
temp2[0] = "good"
temp2 = tuple(temp2)
status_dict[i].pop()
status_dict[i].append(temp2)
elif status_dict[i][0][1] >= 4:
temp3 = list(status_dict[i][0])
temp3[0] = "soso"
temp3 = tuple(temp3)
status_dict[i].pop()
status_dict[i].append(temp3)
elif status_dict[i][0][1] >= 4:
temp4 = list(status_dict[i][0])
temp4[0] = "bad"
temp4 = tuple(temp4)
status_dict[i].pop()
status_dict[i].append(temp4)
status = status_dict[time_index][0][0]
data = []
now = int(str(time.split('μ')[0]))
#print(now)
if now == 6 or now == 7:
for i in range(6, 11):
data.append((str(i), status_dict.get(str(i) + "μ")[0][1]))
elif now == 22 or now == 23:
for i in range(19, 24):
data.append((str(i), status_dict.get(str(i) + "μ")[0][1]))
else:
for i in range(now - 2, now + 3):
data.append((str(i), status_dict.get(str(i) + "μ")[0][1]))
# print(time)
# print(status)
return status, data