-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaux_functions.py
executable file
·239 lines (165 loc) · 8.49 KB
/
aux_functions.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#! /usr/bin/env python3
# SCANPLOT - Um sistema de plotagem simples para o SCANTEC
# Copyright (C) 2020 INPE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
import pandas as pd
import skill_metrics as sm
from scipy.stats import t
from scipy.stats import ttest_ind
# Função proveniente de https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook
def isnotebook(shell):
try:
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
def concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,Var,series):
"""
concat_tables_and_loc
=====================
Esta função concatena um dicionário de tabelas do SCANTEC em um único dataframe e
retorna uma lista com as séries das variáveis e experimentos escolhidos.
Parâmetros de entrada
---------------------
dTable : objeto dicionário com uma ou mais tabelas do SCANTEC;
dataInicial : objeto datetime com a data inicial do experimento;
dataFinal : objeto datetime com a data final do experimento;
Exps : lista com os nomes das estatísticas a serem processadas;
Var : nome da variável na tabela de correlação de anomalia do SCANTEC;
series : valor Booleano para combinar as curvas dos experimentos em um só gráfico.
Resultado
---------
Lista com as séries das variáveis e experimentos escolhidos.
Uso
---
import scanplot
data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")
dataInicial = data_conf["Starting Time"]
dataFinal = data_conf["Ending Time"]
Vars = list(map(data_vars.get,[*data_vars.keys()]))
Var = Vars[0][0].lower()
Stats = ["ACOR", "RMSE", "VIES"]
Exps = list(data_conf["Experiments"].keys())
outDir = data_conf["Output directory"]
dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)
varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,Var,series=False)
"""
datai_fmt = dataInicial.strftime("%Y%m%d%H")
dataf_fmt = dataFinal.strftime("%Y%m%d%H")
cTable = pd.concat(dTable, axis=0, join='outer', ignore_index=False, keys=None, sort=True)
varlev_exps = []
if series:
for exp in Exps:
fname_exp_datai = 'ACOR' + str(exp) + '_' + datai_fmt + datai_fmt + 'T.scan'
fname_exp_dataf = 'ACOR' + str(exp) + '_' + dataf_fmt + dataf_fmt + 'T.scan'
varlev_dia_exp = cTable.sort_index(0).loc[fname_exp_datai:fname_exp_dataf, str(Var)]
varlev_exps.append(varlev_dia_exp)
else:
for exp in Exps:
fname_exp = 'ACOR' + str(exp) + '_' + datai_fmt + dataf_fmt + 'T.scan'
varlev_exp = cTable.loc[fname_exp, str(Var)]
varlev_exps.append(varlev_exp)
return varlev_exps
def df_fill_nan(varlev_exps,varlev_dia_exps):
"""
df_fill_nan
===========
Esta função completa os dataframes até um tamanho específico.
Parâmetros de entrada
---------------------
varlev_exps : lista de dataframes com as variáveis avaliadas para um período;
varlev_dia_exps : lista de dataframes com as variáveis avaliadas para todos os dias de um período.
Resultado
---------
Lista de dataframes completados com NaN até o tamanho do maior dataframe.
Uso
---
import scanplot
data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")
dataInicial = data_conf["Starting Time"]
dataFinal = data_conf["Ending Time"]
Vars = list(map(data_vars.get,[*data_vars.keys()]))
Stats = ["ACOR", "RMSE", "VIES"]
Exps = list(data_conf["Experiments"].keys())
outDir = data_conf["Output directory"]
dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)
varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,series=False)
lst_varlev_dia_exps_rsp = scanplot.df_fill_nan(varlev_exps,varlev_dia_exps)
"""
lst_shapes = []
for varlev_exp, varlev_dia_exp in zip(varlev_exps, varlev_dia_exps):
shape_exp = int(varlev_dia_exp.shape[0] / varlev_exp.shape[0]), int(varlev_exp.shape[0])
lst_shapes.append(shape_exp)
shape_exp_max = sorted(lst_shapes, key=lambda x: x[1], reverse=True)[0]
shape_exp_min = sorted(lst_shapes, key=lambda x: x[1], reverse=False)[0]
varlev_dia_exps_rsp = []
for varlev_dia_exp, shape_exp in zip(varlev_dia_exps, lst_shapes):
varlev_dia_exp_rsp = pd.DataFrame(varlev_dia_exp.values.reshape(shape_exp))
if shape_exp < shape_exp_max:
df_nan = pd.DataFrame(np.nan, index=np.arange(shape_exp_max[0]), columns=np.arange(shape_exp[1],shape_exp_max[1]))
varlev_dia_exp_rsp = varlev_dia_exp_rsp.join(df_nan)
varlev_dia_exps_rsp.append(varlev_dia_exp_rsp)
return varlev_dia_exps_rsp
def calc_tStudent(lst_varlev_dia_exps_rsp):
"""
calc_tStudent
===========
Esta função calcula o teste de significância t-Student com intervalo de confiânça de 95%.
Parâmetros de entrada
---------------------
lst_varlev_dia_exps_rsp : lista de dataframes com as variáveis avaliadas para um período.
Resultado
---------
Resultado do teste de significância e valores críticos para serem utilizados pela função
plot_lines_tStudent.
Uso
---
import scanplot
data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")
dataInicial = data_conf["Starting Time"]
dataFinal = data_conf["Ending Time"]
Vars = list(map(data_vars.get,[*data_vars.keys()]))
Stats = ["ACOR", "RMSE", "VIES"]
Exps = list(data_conf["Experiments"].keys())
outDir = data_conf["Output directory"]
dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)
varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,series=False)
lst_varlev_dia_exps_rsp = scanplot.df_fill_nan(varlev_exps,varlev_dia_exps)
ldrom_exp, ldrosup_exp, ldroinf_exp = scanplot.calc_tStudent(lst_varlev_dia_exps_rsp)
"""
lst_drom_exp = []
lst_drosup_exp = []
lst_droinf_exp = []
for varlev_dia_exp_rsp in lst_varlev_dia_exps_rsp[1:]:
dzm_exp = ((0.5 * np.log((1.0 + 0.5 * (lst_varlev_dia_exps_rsp[0] - varlev_dia_exp_rsp)) /
(1.0 - 0.5 * (lst_varlev_dia_exps_rsp[0] - varlev_dia_exp_rsp)))))
med_exp = dzm_exp.mean()
var_exp = dzm_exp.var()
stat, pval = ttest_ind(lst_varlev_dia_exps_rsp[0], varlev_dia_exp_rsp, equal_var=False)
dof_exp = lst_varlev_dia_exps_rsp[0].shape[0] + varlev_dia_exp_rsp.shape[0] - 2.0
texp = t.ppf(pval, dof_exp)
dzc_exp = texp * (np.sqrt(var_exp / dof_exp))
drom_exp = 2.0 * (np.exp(2.0 * med_exp) - 1.0) / (np.exp(2.0 * med_exp) + 1.0)
lst_drom_exp.append(drom_exp)
drosup_exp = 2.0 * (np.exp(2.0 * dzc_exp) - 1.0) / (np.exp(2.0 * dzc_exp) + 1.0)
lst_drosup_exp.append(drosup_exp)
droinf_exp = 2.0 * (np.exp(-2.0 * dzc_exp )- 1.0) / (np.exp(-2.0 * dzc_exp) + 1.0)
lst_droinf_exp.append(droinf_exp)
return lst_drom_exp, lst_drosup_exp, lst_droinf_exp