-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (83 loc) · 3.4 KB
/
main.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import timedelta
from datetime import time as datetime_time
plt.style.use('seaborn-whitegrid')
# In[2]:
# = pd.DataFrame(columns=['date', 'washers', 'dryers'])
#raw_data.read_csv("./both", header=None)
headers = ['datetime', 'wash', 'dry']
dtypes = {'datetime': 'str', 'wash': 'int', 'dry': 'int'}
parse_dates = ['datetime']
raw_data = pd.read_csv("./data", sep=',', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates, date_parser=pd.to_datetime)
#print(raw_data.dtypes)
#print(raw_data)
# In[3]:
def shift_forward(data):
local_data=data.copy()
local_data['datetime'] = [time + timedelta(hours=4) for time in local_data['datetime']]
return local_data
def shift_back(data):
local_data=data.copy()
local_data['datetime'] = [time + timedelta(hours=-4) for time in local_data['datetime']]
return local_data
shifted_data = shift_forward(shift_back(raw_data))
#print(shifted_data)
# In[4]:
def get_by_months(data):
return[g.reset_index() for n, g in data.set_index('datetime').groupby(pd.Grouper(freq='M'))]
months = get_by_months(raw_data)
#print(pd.concat(months[1:3]))
# In[5]:
def get_weekdays(data):
return data[data.set_index('datetime').index.dayofweek < 5]
def get_weekends(data):
return data[data.set_index('datetime').index.dayofweek >= 5]
#print(get_weekdays(months[1]))
#print(get_weekends(months[1]))
# In[6]:
def average_range(data):
local_data=data.copy()
local_data['datetime'] = [time.floor("5min").time() for time in local_data['datetime']]
return local_data.set_index('datetime').groupby('datetime').mean().reset_index()
#print(average_range(months[1]))
# In[7]:
def shift_time_forward(data):
local_data=data.copy()
local_data['datetime'] = [datetime_time((time.hour + 4)%24, time.minute) for time in local_data['datetime']]
return local_data
def make_pretty(data_ref):
data = data_ref.copy()
data.names = ["Time", "Washers Available", "Dryers Available"]
def plot_data(data_ref, title, filename):
data = data_ref.copy()
data['int_index'] = range(len(data))
ticks=[time.time() for time in pd.date_range(start='1/1/2018 4:00', end='1/3/2018', freq='4H')]
tickss=[tick.strftime("%H:%M") for tick in ticks]
ax = data.plot(x="datetime", y=["wash", "dry"], figsize=(12, 6), xticks=ticks, title=title)
ax.set_xticklabels(tickss)
ax.legend(["Washers Available", "Dryers Available"])
ax.set_xlabel("Time")
fig = ax.get_figure()
fig.savefig("plots/"+filename)
plt.close(fig)
#plot_data(average_range(get_weekdays(get_by_months(shift_back(raw_data))[1])), "Weekdays Jan 2019")
# In[8]:
def process_range(data, label, prefix):
plot_data(average_range(get_weekdays(data)), "Weekdays " + label, prefix + "_weekday_" + label.replace(" ", "_").lower() + ".png")
plot_data(average_range(get_weekends(data)), "Weekend " + label, prefix + "_weekend_" + label.replace(" ", "_").lower() + ".png")
# In[9]:
months = get_by_months(shift_back(raw_data))[1:]
# In[10]:
for month in months:
title = month['datetime'][0].strftime("%B %Y")
m = month['datetime'][0].month
y = month['datetime'][0].year
pref = str(y) + "." + str(m).zfill(2)
process_range(month, title, pref)
# In[11]:
process_range(shift_back(raw_data), "all time Average", "0.0")