forked from Dylan987/windatachallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_aggregate.py
101 lines (79 loc) · 3.36 KB
/
day_aggregate.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from basic_tests import dorchester_arr, malden_arr, totten_arr
import pandas_bokeh
plt.close('all')
pd.set_option('plotting.backend', 'pandas_bokeh')
def plot_one(og, d, title):
plt.close('all')
graph_title = title
if (d==0):
df = og.iloc[:, :6]
else:
df = og.iloc[:, np.r_[0, d*6+1:d*6+6]]
df_grouped = df.groupby([df.index.hour, df.index.minute]).mean()
df_grouped.plot(title=graph_title)
ax = plt.axes()
ax.set_xlabel("Time of day (hour, minute)")
ax.set_ylabel("# of vehicles")
plt.show()
# arr = array of dfs representing an intersection
# d = direction (N = 0, E = 1, S = 2, W = 3)
# i = index of arr with the df you want to graph
def plot_intersection(arr, d, i, graph_title):
plt.close('all')
if (d==0):
df = arr[i].iloc[:, :6]
else:
df = arr[i].iloc[:, np.r_[0, d*6+1:d*6+4]]
df_grouped = df.groupby([df.index.hour, df.index.minute]).mean()
df_grouped.plot(title=graph_title, xlabel='Time of day (hour, minute)', ylabel='Number of Vehicles')
plt.show()
# plot_intersection(dorchester_arr,3, 5)
intersections = [dorchester_arr, malden_arr, totten_arr]
# intersection: 0 = dorch, 1 = malden, 2 = totten
def plot_any(intersection, d, i):
intersection_dict = {0: 'Huron Church & Dorchester', 1: 'Huron Church & Malden', 2: 'Huron Church & Totten'}
d_dict = {0: 'Southbound', 1: 'Westbound', 2: 'Northbound', 3: 'Eastbound'}
i_dict = {0: 'General Traffic', 1: 'Single-Unit Trucks', 2: 'Articulated Trucks', 3: 'Buses',
4: 'Work Vans', 5: 'Bicycles', 6: 'Pedestrians'}
graph_title = d_dict[d] + " " + i_dict[i] + " on " + intersection_dict[intersection]
plot_intersection(intersections[intersection], d, i, graph_title)
# plot_any(1, 0, 0)
# for i in range(3):
# for j in range(4):
# for k in range(6):
# print(str(i) + str(j) + str(k))
# f = open("static/views/graph" + str(i) + str(j) + str(k) + ".html", "wb")
# pandas_bokeh.output_file("static/views/graph" + str(i) + str(j) + str(k) + ".html")
# plot_any(i, j, k)
# f.close()
##### MOST OF BELOW CAN NOW BE COMPLETED WITH PLOT_ANY #####
### Setup
# d_lights_n = dorchester_arr[0].iloc[:, :6]
# dlnr = d_lights_n.groupby([d_lights_n.index.hour, d_lights_n.index.minute]).mean()
# plt.figure()
# dlnr.plot()
# d_lights_n['Start Time'] = pd.to_datetime(d_lights_n['Start Time'])
# d_lights_n.set_index(['Start Time'], inplace=True)
# d_sut_n = dorchester_arr[1].iloc[:,0:6]
# print(d_sut_n)
# d_lights_e = dorchester_arr[0].iloc[:, np.r_[0, 7:12]]
# d_sut_n['Start Time'] = pd.to_datetime(d_sut_n['Start Time'])
# d_sut_n.set_index(['Start Time'], inplace=True)
## Filter by day (monday = 0)
# d_lights_n_monday = d_lights_n[d_lights_n.index.weekday == 0]
## Resample: split data at different intervals (ex. every day or every 30 min instead of 15min)
# dlnr = d_lights_n_monday.resample('D').mean()
## Group by day (view average data for one day)
# dlnr = d_lights_n.groupby(lambda x: x.hour+x.minute, axis=0).mean() # doesn't work
# dlnr = d_sut_n.groupby([d_sut_n.index.hour, d_sut_n.index.minute]).mean()
#
# print(dlnr)
# plot_data(dlnr)
# plt.show()
# dler = d_lights_e.groupby([d_lights_e.index.hour, d_lights_e.index.minute]).mean()
# print(dler.info)
# plot_data(dler)
# plt.show()