-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoep-filter.py
124 lines (74 loc) · 2.68 KB
/
hoep-filter.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
#!usr/bin/python3.5
import pandas
import numpy as np
import matplotlib.pyplot as plt
def main():
hoep_file = pandas.read_csv("hoep_data.csv", parse_dates=['Date'])
hoep_filtered = hoep_file.copy()
hoep_filtered['HOEP'] = filter(hoep_filtered['HOEP'])
# plt.plot(hoep_file['HOEP'])
# plt.plot(hoep_filtered['HOEP'])
# plt.show(block=True)
daily = day_profile(hoep_file)
daily_filtered = day_profile(hoep_filtered)
# #
# print(daily)
# print(daily_filtered)
# plt.plot(daily)
# plt.plot(daily_filtered)
#make a histogram of the output
# n, bins, patchs = plt.hist(hoep_file['HOEP'], 100)
daily = hoep_file.pivot(index='Date', columns='Hour', values='HOEP')
mask = ((daily > daily.stack().quantile(0.01)) & (daily < daily.stack().quantile(0.99))).all(axis=1)
print(mask)
pctTrue = len([i for i in mask if i])/len(mask)
print("{:.2f}%".format(pctTrue*100))
# print(daily[mask])
plt.plot(daily[mask].transpose())
plt.plot(daily[mask].mean(axis=0), linewidth=3)
# for profile, toPlot in zip(daily, mask):
# if toPlot:
# plt.plot(profile)
plt.show(block=True)
# mask = (hoep_file['HOEP'] > hoep_file['HOEP'].quantile(0.025)) & (hoep_file['HOEP'] < hoep_file['HOEP'].quantile(0.975))
# print(mask)
# plt.show(block=True)
#
def day_profile(df):
daily = df.pivot(index='Date', columns='Hour', values='HOEP')
return daily.mean(axis=0)
def filter(hoep, show=False):
#sample rate is 1 sample per hour
T = 1*60*60
f_samp = 1/(T)
f_ny = f_samp / 2
#filter between 3 and 10 days
short_days, long_days = 3, 30
f_high= 1/(2 * 24*60*60)/f_samp
f_low= 1/(30 * 24*60*60)/f_samp
b = 1/(13 * 24*60*60)/f_samp
N = int(np.ceil(12/b))
if not N%2: N+= 1
n = np.arange(N)
# Compute a low-pass filter with cutoff frequency fL.
hlpf = np.sinc(2 * f_low * (n - (N - 1) / 2.))
hlpf *= np.blackman(N)
hlpf /= np.sum(hlpf)
# Compute a high-pass filter with cutoff frequency fH.
hhpf = np.sinc(2 * f_high * (n - (N - 1) / 2.))
hhpf *= np.blackman(N)
hhpf /= np.sum(hhpf)
hhpf = -hhpf
hhpf[(N - 1) / 2] += 1
# Add both filters.
h = hlpf + hhpf
# h = hhpf
hoep_f = np.convolve(hoep, h, mode="same")
if show:
orig, = plt.plot(hoep)
filt, = plt.plot(hoep_f)
plt.legend([orig, filt], 'HOEP', 'weather-adjusted')
plt.show(block=True)
return hoep_f
if __name__ == "__main__":
main()