-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpyfunc.py
220 lines (177 loc) · 6.95 KB
/
pyfunc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
This module contains functions for parsing and processing uploaded data,
generating summary statistics for rainfall data,
transforming table data into a pandas DataFrame,
and calculating the cumulative sum of a DataFrame.
"""
import base64
import io
import pandas as pd
from dash import html
import numpy as np
from hidrokit.contrib.taruma import statistic_summary
def parse_upload_data(content, filename, filedate):
"""
Parse and process uploaded data.
Args:
content (str): The content of the uploaded file.
filename (str): The name of the uploaded file.
filedate (str): The date of the uploaded file.
Returns:
tuple: A tuple containing the processed data and an HTML element.
The processed data is a pandas DataFrame if the file is in CSV format.
If the file is in XLSX or XLS format, an HTML element with a warning message
is returned.
If the file is in any other format, an HTML element with an error message
is returned.
"""
_ = filedate # unused variable
_, content_string = content.split(",")
decoded = base64.b64decode(content_string)
try:
if filename.endswith(".csv"):
dataframe = pd.read_csv(
io.StringIO(decoded.decode("utf-8")), index_col=0, parse_dates=True
)
elif filename.endswith(".xlsx") or filename.endswith(".xls"):
return (
html.Div(
[
"Fitur pembacaan berkas excel masih dalam tahap pengembangan.",
" Template yang akan digunakan adalah hidrokit excel template.",
],
className="text-center bg-danger text-white fs-4",
),
None,
)
else:
return (
html.Div(
["Hanya dapat membaca format .csv (tiap kolom merupakan stasiun)"],
className="text-center bg-danger text-white fs-4",
),
None,
)
except UnicodeDecodeError as e:
print(e)
return html.Div([f"File is not valid UTF-8. {e}"]), None
except pd.errors.ParserError as e:
print(e)
return html.Div([f"CSV file is not well-formed. {e}"]), None
except ValueError as e:
print(e)
return html.Div([f"Content string is not valid base64. {e}"]), None
return html.Div(["File Diterima"]), dataframe
def generate_summary_single(dataframe, n_days="1MS"):
"""
Generate a summary of rainfall data for a single location.
Args:
dataframe (pandas.DataFrame): The input dataframe containing rainfall data.
n_days (str, optional): The number of days to consider for the summary.
Defaults to "1MS".
Returns:
pandas.DataFrame: The summary dataframe containing various statistics of
the rainfall data.
"""
def days(vector):
return len(vector)
def vector_sum(vector):
return vector.sum().round(3)
def n_rain(vector):
return (vector > 0).sum()
def n_dry(vector):
return np.logical_or(vector.isna(), vector == 0).sum()
def max_date(vector):
if vector.any():
return vector.idxmax().date()
return pd.NaT
def vector_max(vector):
return vector.max()
ufunc = [days, vector_max, vector_sum, n_rain, n_dry, max_date]
ufunc_col = ["days", "max", "sum", "n_rain", "n_dry", "max_date"]
summary = statistic_summary.summary_all(
dataframe, ufunc=ufunc, ufunc_col=ufunc_col, n_days=n_days
)
return summary.infer_objects()
def generate_summary_all(dataframe, n_days: list = None):
"""
Generate summary statistics for multiple time periods.
Args:
dataframe (pandas.DataFrame): The input dataframe containing the data.
n_days (list, optional): A list of time periods to calculate
the summary statistics for.
If not provided, the default time periods ["16D", "1MS", "1YS"] will be used.
Returns:
list: A list of summary statistics for each time period.
"""
n_days = ["16D", "1MS", "1YS"] if n_days is None else n_days
summary_all = []
for n_day in n_days:
summary_all.append(generate_summary_single(dataframe, n_days=n_day))
return summary_all
def transform_to_dataframe(
table_data,
table_columns,
multiindex: bool = False,
apply_numeric: bool = True,
parse_dates: list = None,
):
"""
Transform table data into a pandas DataFrame.
Args:
table_data (list): The data to be transformed into a DataFrame.
table_columns (list): The column names of the table data.
multiindex (bool, optional): Whether to create a multi-index DataFrame.
Defaults to False.
apply_numeric (bool, optional): Whether to apply numeric conversion to the DataFrame.
Defaults to True.
parse_dates (list, optional): The column names to parse as dates.
Defaults to None.
Returns:
pandas.DataFrame: The transformed DataFrame.
"""
if multiindex is True:
dataframe = pd.DataFrame(table_data)
dataframe.columns = pd.MultiIndex.from_tuples(
[item["name"] for item in table_columns]
)
else:
columns = pd.Index([item["name"] for item in table_columns])
dataframe = pd.DataFrame(table_data, columns=columns)
dataframe["DATE"] = pd.to_datetime(dataframe.DATE)
dataframe = dataframe.set_index("DATE").sort_index()
if multiindex is True:
# removing date (index.name) from top level multiindex
dataframe.columns = pd.MultiIndex.from_tuples(dataframe.columns.to_flat_index())
if apply_numeric is True:
dataframe = dataframe.apply(pd.to_numeric, errors="coerce")
else:
dataframe = dataframe.infer_objects()
if parse_dates is not None:
if multiindex:
for col_dates in parse_dates:
col_parsing = [
col_tuple
for col_tuple in dataframe.columns
if col_dates in col_tuple
]
for col_dates in col_parsing:
dataframe[col_dates] = pd.to_datetime(
dataframe[col_dates], errors="coerce"
)
else:
for col_dates in parse_dates:
dataframe[col_dates] = pd.to_datetime(
dataframe[col_dates], errors="coerce"
)
return dataframe
def calculate_cumulative_sum(dataframe):
"""
Calculate the cumulative sum of a DataFrame by resampling it on a yearly basis.
Parameters:
dataframe (pandas.DataFrame): The input DataFrame containing the data.
Returns:
pandas.DataFrame: The DataFrame with the cumulative sum rounded to the nearest integer.
"""
consistency = dataframe.resample("YS").sum().cumsum()
return consistency.round()