-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
167 lines (122 loc) · 5.34 KB
/
utils.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
import numpy as np
import pandas as pd
import plotly.express as px
from scipy.stats import zscore
from charts import scatter, line, radar, bar, box_plot
def remove_outliers(df):
z_scores = zscore(df)
abs_z_scores = np.abs(z_scores)
filtered_entries = (abs_z_scores < 3).all(axis=1)
return df[filtered_entries]
def select_data(df, tc, basins, methods):
df_select = []
for basin in basins:
for method in methods:
df_select.append([basin, method, tc[method][tc.index == basin].values[0]])
return pd.DataFrame(df_select, columns=['Bacias', 'Método',
'Tc (h)']).dropna()
def plot_data(df, x_axis, methods, opt1, basins):
df_plot = pd.DataFrame(())
for l in range(len(x_axis)):
for method in methods:
line = pd.Series([x_axis.values[l], method, df[method][l],
basins[l]])
line = pd.DataFrame([line])
df_plot = pd.concat([line, df_plot], ignore_index=True)
df_plot.columns = [opt1, 'método', 'tc (h)', 'BACIAS']
return df_plot
def bar_plot(df, tc, basins, methods, st):
try:
df_select = select_data(df, tc, basins, methods)
bar(df_select, methods, st)
except ValueError:
st.text("Não foi encontrado dados para a seleção feita. Tente novamente.")
def scatter_plot(df, tc, basins, methods, opt1, st):
try:
df_select = tc[methods][tc.index.isin(basins)]
df_select = remove_outliers(df_select)
df = df[df.BACIAS.isin(df_select.index.values[:])]
x_axis = df[df.BACIAS.isin(basins)][opt1]
try:
n = x_axis.astype(str)
x_axis = x_axis.str.replace(',', '.')
x_axis[x_axis == '\xa0'] = np.nan
x_axis = pd.to_numeric(x_axis)
except:
pass
df_plot = plot_data(df_select, x_axis, methods, opt1, basins)
scatter(df_plot, opt1, methods, st)
except ValueError:
st.text("Não foram encontrados dados para esta seleção. Tente novamente.")
def line_plot(df, tc, basins, methods, opt1, st):
try:
df_select = tc[methods][tc.index.isin(basins)]
df_select = remove_outliers(df_select)
df = df[df.BACIAS.isin(df_select.index.values[:])]
x_axis = df[df.BACIAS.isin(basins)][opt1]
try:
n = x_axis.astype(str)
x_axis = x_axis.str.replace(',', '.')
x_axis[x_axis == '\xa0'] = np.nan
x_axis = pd.to_numeric(x_axis)
except:
pass
df_plot = plot_data(df_select, x_axis, methods, opt1, basins)
df_plot = df_plot.sort_values(by=[opt1]).dropna()
line(df_plot, opt1, methods, st)
except ValueError:
st.text("Não foram encontrados dados para esta seleção. Tente novamente.")
def boxplot_plot(df, tc, basins, methods, st):
x_axis = st.radio('Eixo x', ['Método', 'Bacias'])
try:
df_select = select_data(df, tc, basins, methods)
box_plot(df_select, basins, methods, x_axis, st)
except ValueError:
st.text("Não foi encontrado dados para a seleção feita. Tente novamente.")
def radar_plot(df, tc, basins, methods, opt1, st):
try:
df_select = tc[methods][tc.index.isin(basins)]
x_axis = df[df.BACIAS.isin(basins)]
correl = pd.DataFrame((), columns=x_axis.columns[1:-1])
for x in correl.columns:
corr_method = []
for method in methods:
v1 = pd.DataFrame(np.array([df_select[method],x_axis[x]]).T)
v1 = v1.dropna()
corr_method.append(v1.corr()[0][1])
correl[x] = corr_method
correl.insert(0, 'Método', methods)
radar(correl, opt1, st)
except ValueError:
st.text("Não foram encontrados dados para esta seleção. Tente novamente.")
def heatmap_plot(df, tc, basins, methods, st, opt1=None, type=1):
try:
if type == 1:
tc = tc.loc[tc.index.isin(basins)][methods]
st.text("Matriz correlação")
st.dataframe(tc.corr())
st.subheader("Mapa de correlação")
fig = px.imshow(tc.corr(), color_continuous_scale='spectral')
st.plotly_chart(fig)
else:
df_select = tc[methods][tc.index.isin(basins)]
x_axis = df[df.BACIAS.isin(basins)]
correl = pd.DataFrame((), columns=x_axis.columns[1:-1])
for x in correl.columns:
corr_method = []
for method in methods:
v1 = pd.DataFrame(np.array([df_select[method],x_axis[x]]).T)
v1 = v1.dropna()
corr_method.append(v1.corr()[0][1])
correl[x] = corr_method
correl.insert(0, 'Método', methods)
correl = correl.set_index('Método')
st.text("Matriz correlação")
st.dataframe(correl)
st.subheader("Mapa de correlação")
fig = px.imshow(correl,
color_continuous_scale='spectral',
width=1000, height=800)
st.plotly_chart(fig)
except ValueError:
st.text("Não foi encontrado dados para a seleção feita. Tente novamente.")