-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiod.py
executable file
·188 lines (144 loc) · 4.98 KB
/
period.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 26 17:21:29 2022
@author: pzaninelli
"""
import pandas as pd
from abc import abstractmethod
class PeriodDF:
def __init__(self, df, is_preproc = False):
self._df = df
self._is_preproc = is_preproc
def preproc(self):
self._df["start"] = pd.to_datetime(self._df["start"], errors = 'coerce')
self._df["end"] = pd.to_datetime(self._df["end"], errors = 'coerce')
self._df["year"] = self._df.start.dt.year
self._df["month"] = self._df.start.dt.month
self._df["season"] = self._df["month"]%12 // 3 + 1
self._df["duration"] = self._df.end-self._df.start
self._is_preproc = True
@abstractmethod
def hwf(self, days):
pass
@abstractmethod
def hwd(self, days):
pass
@abstractmethod
def hwa(self, days):
pass
@abstractmethod
def hwm(self, days):
pass
@property
def df(self):
return self._df
@df.setter
def df(self, df):
self._df = df
@property
def is_preproc(self):
return self._is_preproc
@is_preproc.setter
def is_preproc(self,is_o_preproc):
self._is_preproc = is_o_preproc
@classmethod
def from_file(cls, filename):
df = pd.read_csv(filename)
df = df.dropna()
df = df.drop("Unnamed: 0",axis=1)
return cls(df, False)
@classmethod
def from_obj(cls, obj):
return cls(obj.df, obj.is_preproc)
class Monthly(PeriodDF):
def __init__(self, df, is_preproc):
super().__init__(df, is_preproc)
def monthly(self):
if not super().is_preproc:
super().preproc()
return self.df.groupby(["longitude","latitude","month"])
def hwf(self, days = None):
if not super().is_preproc:
super().preproc()
return self.monthly()["mean"].count()
def hwd(self, days = None):
if not super().is_preproc:
super().preproc()
return self.monthly()['duration'].max()
def hwa(self, days = None):
if not super().is_preproc:
super().preproc()
return self.monthly()["max_ex"].max()
def hwm(self, days = None):
if not super().is_preproc:
super().preproc()
return self.monthly()["sum_ex"].sum()
class Annual(PeriodDF):
def __init__(self,df, is_preproc):
super().__init__(df, is_preproc)
def annual(self):
if not super().is_preproc:
super().preproc()
return self.df.groupby(["longitude","latitude","year"])
def hwf(self, days = None):
if not super().is_preproc:
super().preproc()
return self.annual()["mean"].count()
def hwd(self, days = None):
if not super().is_preproc:
super().preproc()
return self.annual()['duration'].max()
def hwa(self, days = None):
if not super().is_preproc:
super().preproc()
return self.annual()["max_ex"].max()
def hwm(self, days = None):
if not super().is_preproc:
super().preproc()
return self.annual()["sum_ex"].sum()
class Seasonal(PeriodDF):
def __init__(self,df, is_preproc):
super().__init__(df, is_preproc)
def seasonal(self):
if not super().is_preproc:
super().preproc()
return self.df.groupby(["longitude","latitude","season"])
def hwf(self, days = None):
if not super().is_preproc:
super().preproc()
return self.seasonal()["mean"].count()
def hwd(self, days = None):
if not super().is_preproc:
super().preproc()
return self.seasonal()['duration'].max()
def hwa(self, days = None):
if not super().is_preproc:
super().preproc()
return self.seasonal()["max_ex"].max()
def hwm(self, days = None):
if not super().is_preproc:
super().preproc()
return self.seasonal()["sum_ex"].sum()
class UserPeriod(PeriodDF):
def __init__(self,df, is_preproc):
super().__init__(df, is_preproc)
def groupdays(self, days):
assert days > 0, "days must be greater than zero"
return self.df.set_index("start").groupby(["longitude","latitude",pd.Grouper(freq=(f"{days}D"))])
def hwf(self, days):
if not super().is_preproc:
super().preproc()
return self.groupdays(days)["mean"].count()
def hwd(self,days):
if not super().is_preproc:
super().preproc()
return self.groupdays(days)['duration'].max()
def hwa(self, days):
if not super().is_preproc:
super().preproc()
return self.groupdays(days)["max_ex"].max()
def hwm(self, days):
if not super().is_preproc:
super().preproc()
return self.groupdays(days)["sum_ex"].sum()