-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_graph.py
94 lines (73 loc) · 2.77 KB
/
test_graph.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
import matplotlib.pyplot as plt
import pandas as pd
import datetime
import calendar
def findDay(date):
"""retrieves the day of the week given a date
Parameters
----------
date : str
An string given in Y:m:d specifying the date
"""
# retrieve the int day of the week
day = datetime.datetime.strptime(date, '%Y:%m:%d').weekday()
# return the day name of the week
return calendar.day_name[day]
def load_graph(path):
"""load and update the graph plots of visitor flows
Parameters
----------
path : string
An string of the output path to save and load graph plots
"""
# load data from path
data = pd.read_csv(path)
# setup dictionary with each day each hour
avg_days = {}
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in days: # every day
avg_days[day] = {}
for j in range(24): # every hour
if j < 10:
avg_days[day]['0' + str(j)] = []
else:
avg_days[day][str(j)] = []
# retrieve each record from data and add the count to the respective day and hour of the data dictionary
for dat in data['date'].unique():
day = findDay(str(dat))
for i in range(len(data)):
hr = str(data.iloc[i]['time'][0:2])
# where date matches each other
if str(data.iloc[i]['date']) == str(dat):
avg_days[day][hr].append(
data.iloc[i]['count'])
# append the average of the counts from data dictionary
for dat in data['date'].unique():
day = findDay(str(dat))
for time in avg_days[day].keys():
# where data is in correct format
if isinstance(avg_days[day][time], list):
if len(avg_days[day][time]) > 0:
avg_days[day][time] = sum(
avg_days[day][time])/len(avg_days[day][time])
else:
avg_days[day][time] = 0
# for each day create a plot of average visitor flow
for dat in data['date'].unique():
# print(dat)
day = findDay(str(dat))
fig, ax = plt.subplots()
ax.bar(avg_days[day].keys(),
avg_days[day].values())
ax.tick_params(axis='x', which='major', labelsize=5)
ax.tick_params(axis='y', which='minor', labelsize=8)
plt.suptitle(day+' Visitor Flow')
plt.xlabel('Hour of Day')
plt.ylabel('Average Occurence of People')
plt.grid(True)
# save to the respective folder paths
plt.savefig('runs/tests/'+day+'4_plot.pdf', bbox_inches='tight')
print('saved to ' + 'runs/tests/'+day+'4_plot.pdf')
if __name__ == '__main__':
load_graph('runs/tests/case4.csv')