-
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