-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic_tools.py
359 lines (298 loc) · 10.4 KB
/
graphic_tools.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 18:22:03 2018
@author: Javier Fumanal Idocin
"""
import pandas
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
from ggplot import ggplot, geom_line, aes, xlab,ylab, ggtitle, geom_point, theme, element_text
from matplotlib import colors as mcolors
from sklearn.decomposition import PCA
from clustering import minmax_norm, filter_numerical
def plot_line(X,y,title=None,labelx=None,labely=None,save=False, colors=None):
'''
Show on screen a line plot. Can save to a .pdf file too if specified.
X,y -
'''
df = pandas.DataFrame()
if (title!=None):
img_title = title.replace(" ","").replace(".","-") + ".pdf"
df['X'] = X
for i in range(y.shape[1]):
df[str(i)] = y.iloc[:,i].values
if colors is None:
colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())
df = df.iloc[0:df.shape[0]-1, :]
p = ggplot(df, aes(x='X'))
for i in range(y.shape[1]):
if colors not in X.columns.values:
p = p + geom_line(aes(y=str(i),color = colors[i]))
else:
p = p + geom_point(aes(y=str(i),color = colors))
p = p + xlab(labelx) + ylab(labely) + ggtitle(title)
if(save):
p.save(img_title)
else:
return p
def plotly_line_plot(df, columna):
'''
A simple plotly plot thay creates a line plot with a variable from a data frame
'''
data = [
go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df[columna]
)
]
return py.plot(data, filename=df['ticker'][0].replace('/', "_")+ columna)
def full_plot(df, segmentos, clusters=[]):
'''
A plotly plot thay creates a line plot with a variable from a data frame
'''
df=df.drop(['var','vvar'],axis=1,errors='ignore')
lineas_divisorias = []
if len(clusters) == 0:
escala = max(df.drop(['volumen','vcierre','vapertura','vmaximo','vminimo','vvolumen'],axis=1,errors='ignore').select_dtypes(include=[np.number]).max())
else:
escala = max(df.drop(['volumen','vcierre','cluster','vapertura','vmaximo','vminimo','vvolumen'],axis=1,errors='ignore').select_dtypes(include=[np.number]).max())
for i in segmentos:
coordenada = i[1]
# Line Vertical
linea = {
'type': 'line',
'x0': df.index[coordenada],
'y0': 0,
'x1': df.index[coordenada],
'y1': escala,
'line': {
'color': 'rgb(255, 0, 0)',
'width': 3,
'dash':'dashdot'
},
}
lineas_divisorias.append(linea)
data = []
invisibles = [True] * int(len(list(df))/2)
botones = []
tobutton = dict(label = 'Todos',
method = 'update',
args = [{'visible': [True] * int(len(list(df))/2)+ invisibles},
{'title': 'Todos los campos'}])
botones.append(tobutton)
try:
cierre = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['cierre'],
name = "Cierre",
mode='line'
)
data.append(cierre)
visuals = [False] * int(len(list(df))/2)
visuals[len(data)-1] = True
cbutton = dict(label = 'Precio de Cierre',
method = 'update',
args = [{'visible': visuals+ invisibles},
{'title': 'Precio de Cierre'}])
botones.append(cbutton)
except KeyError:
pass
try:
apertura = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['apertura'],
name = "Apertura",
mode='line'
)
data.append(apertura)
visuals = [False] * int(len(list(df))/2)
visuals[len(data)-1] = True
abutton = dict(label = 'Precio de Apertura',
method = 'update',
args = [{'visible': visuals+ invisibles},
{'title': 'Precio de Apertura'}])
botones.append(abutton)
except KeyError:
pass
try:
minimo = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['minimo'],
name = "Mínimo",
mode='line'
)
data.append(minimo)
visuals = [False] * int(len(list(df))/2)
visuals[len(data)-1] = True
mibutton = dict(label = 'Precio Mínimo',
method = 'update',
args = [{'visible': visuals+ invisibles},
{'title': 'Precio Mínimo'}])
botones.append(mibutton)
except KeyError:
pass
try:
maximo = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['maximo'],
name = "Máximo",
mode='line'
)
data.append(maximo)
visuals = [False] * int(len(list(df))/2)
visuals[len(data)-1] = True
mabutton= dict(label = 'Precio Máximo',
method = 'update',
args = [{'visible': visuals+ invisibles},
{'title': 'Precio Máximo'}])
botones.append(mabutton)
except KeyError:
pass
try:
volumen = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['volumen']/np.max(df['volumen']) * escala,
name = "Volumen",
mode='line'
)
data.append(volumen)
visuals = [False] * int(len(list(df))/2)
visuals[len(data)-1] = True
vtitle = 'Volumen (Factor de escala: ' + str(escala / np.max(df['volumen']))+ ')'
vbutton = dict(label = 'Volumen',
method = 'update',
args = [{'visible': visuals + invisibles},
{'title': vtitle}])
botones.append(vbutton)
except KeyError:
pass
try:
vcierre = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['vcierre'],
visible = "legendonly",
name = "Ev. Cierre",
opacity = 0
)
data.append(vcierre)
except KeyError:
pass
try:
vapertura = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['vapertura'],
visible = "legendonly",
opacity = 0,
name = "Ev. Apertura"
)
data.append(vapertura)
except KeyError:
pass
try:
vminimo = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['vminimo'],
visible = "legendonly",
opacity = 0,
name = "Ev. Mínimo"
)
data.append(vminimo)
except KeyError:
pass
try:
vmaximo = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['vmaximo'],
visible = "legendonly",
opacity = 0,
name = "Ev. Máximo"
)
data.append(vmaximo)
except KeyError:
pass
try:
vvolumen = go.Scatter(
x=df['fecha'], # assign x as the dataframe column 'x'
y=df['vvolumen'],
visible = "legendonly",
opacity = 0,
name = "Ev. Volumen"
)
data.append(vvolumen)
except KeyError:
pass
updatemenus = list([
dict(type="buttons",
active=-1,
buttons=list(botones),
)
])
nombre_grafo = 'Registro Financiero de ' + df['ticker'][0][df['ticker'][0].find("/")+1:]
layout = dict(title=nombre_grafo, showlegend=False,
updatemenus=updatemenus,
shapes=lineas_divisorias)
fig = dict(data=data, layout=layout)
return py.plot(fig, filename=df['ticker'][0].replace('/', "_"))
def visualize_clusters(X, var, color = 'cluster'):
'''
Prints with ggplot a visualization of the different clusters.
'''
aux = pandas.DataFrame()
aux['fecha'] = X.index
aux.index = X.index
aux[var] = X[var]
aux['Cluster'] = X[color]
return ggplot(aes(x='fecha', y=var, color='Cluster'), aux) + geom_point() + xlab(var) + ylab("Valor") + ggtitle("Clustering de la variable \"" + var + "\"") + theme(axis_text_x = element_text(color=[0,0,0,0]))
def visualize_segmentation(X, var):
'''
Prints with ggplot a visualization of the different segments.
'''
aux = pandas.DataFrame(index = X.index)
aux['fecha'] = X.index.values
aux[var] = X[var]
aux['Segmento'] = X['segmento'].astype(str)
return ggplot(aes(x="fecha", y=var, color="Segmento"), aux) + geom_point() + xlab("Fecha") + ylab(var) + ggtitle("Segmentacion de la variable \"" + var + "\"") + theme(axis_text_x = element_text(color=[0,0,0,0]))
def biplot(X, color='cluster'):
'''
Prints a biplot with ggplot. Requires color variable: "cluster" in the dataframe.
'''
pca = PCA(n_components=2)
res = pca.fit_transform(filter_numerical(X))
df = pandas.DataFrame(res)
df.columns = ["x", "y"]
if color == 'cluster':
df['Cluster'] = X[color].values
color = 'Cluster'
else:
c = X[color].values
c[c=="1"] = "Normal"
c[c=="-1"] = "Anomalia"
df['Detectado como:'] = c
color = 'Detectado como:'
return ggplot(aes("x","y", color=color),df) + geom_point(aes(size=40))
def plotly_biplot(X):
'''
Prints a biplot with plotly. Requires color variable: "cluster" in the dataframe.
'''
pca = PCA(n_components=2)
try:
res = pca.fit_transform(minmax_norm(X).drop(['fecha','ticker'], axis=1))
except ValueError:
res = pca.fit_transform(minmax_norm(X))
df = pandas.DataFrame(res)
df.columns = ["x", "y"]
df['cluster'] = X['cluster'].values
cierre = go.Scatter(
x=df['x'], # assign x as the dataframe column 'x'
y=df['y'],
mode='markers',
marker=dict(
color = df['cluster'],
colorscale = 'Pastel2',
)
)
data = [cierre]
layout = dict(title="Biplot", showlegend=False)
fig = dict(data=data, layout=layout)
return py.plot(fig, filename="Biplot")