-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1408 lines (1102 loc) · 47.9 KB
/
main.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from matplotlib import cm
import re
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from bokeh.io import curdoc, show
from bokeh.layouts import row, column, layout, gridplot
from bokeh.models.callbacks import CustomJS
from bokeh.models import Legend, LegendItem
from bokeh.models import ColumnDataSource, LabelSet, HoverTool, TapTool, OpenURL
from bokeh.models import Circle, Div, Paragraph, PreText
from bokeh.models import CDSView, IndexFilter, BoxAnnotation
from bokeh.models.widgets import Slider, TextInput, MultiSelect, Select, Button, AutocompleteInput
from bokeh.models.widgets import CheckboxGroup, RadioButtonGroup, RadioGroup
from bokeh.plotting import figure, output_file
from bokeh.transform import factor_mark, factor_cmap
from bokeh.colors import RGB
from bokeh import __version__
from scipy.special import softmax
from matplotlib.colors import Normalize
from bokeh.models import LinearColorMapper, ColorBar, FixedTicker, CustomJSTickFormatter
#from encodings import circularEnc, linearEnc
# Definition of useful functions
import numpy as np
import pandas as pd
# Definimos un encoding circular adecuado
def circularEnc(list_vals, keyString):
"""
DESCRIPTION
Produces a dataframe E, containing N=len(list_vals) positions equally distributed
in a circle.
The N positions are associated to the N classes of a given category, respectively.
It is assumed that:
- The classes in list_values are exhaustive (the encoding includes all the possible classes in the category)
- The classes are defined by an integer that can be sorted
INPUTS
list_vals: list with values for all the categories
keyString: String identifier for the encoding (ej. "weekday")
OUTPUTS
E: Encoding (dataframe with the class number and the position)
"""
list_vals.sort()
N=len(list_vals)
x = np.arange(N)
pos = np.vstack((np.cos(2 * np.pi * x / N), np.sin(2 * np.pi * x / N))).T
E = pd.DataFrame(pos, list_vals, columns=['posx', 'posy'])
E[keyString] = list_vals
return E
# Definimos un encoding lineal adecuado para el valor de una columna del dataframe de datos.
# Esta función debe ser global para poder usarla cuando cambiemos las cajas de texto con las
# variables utilizadas.
# Recibe el dataframe (df) el nombre de la columna a usar (col) y el tipo ('ver'=vertical),
# por defecto horizontal
def linearEnc(df, col, tipo='ver'):
from sklearn import preprocessing
mms = preprocessing.MinMaxScaler(feature_range=(-1, 1))
if tipo == 'ver':
pos = np.hstack((0*df[col].values[:, None],
mms.fit_transform(df[col].values[:, None])))
else:
pos = np.hstack(
(mms.fit_transform(df[col].values[:, None]), 0*df[col].values[:, None]))
return pos
# Definimos una función auxiliar para devolver un mapa de colores para una variable
# determinada.
def getColorMap(df, variable):
vals=df[variable].values
if len(df[variable].unique())>20:
[min,max]=df[variable].quantile([0.05,0.85])
min=np.min(vals[np.nonzero(vals)])
else:
max=np.max(vals)
min=np.min(vals)
# META: Buscar un mapa de color adecuado, seleccionarlo automáticamente o
# como parámetro.
if(len(df[variable].unique()) <= 20):
# Suppose categorical
print("Supuesto colormap categórico")
colores = [RGB(*cm.turbo(i, bytes=True)[:4]) for i in Normalize(
vmin=min, vmax=max) (df[variable].values)]
palette = "Turbo256"
else:
print("Supuesto colormap continuo")
colores = [RGB(*cm.viridis(i, bytes=True)[:4]) for i in Normalize(
vmin=min, vmax=max) (df[variable].values)]
palette = "Viridis256"
# print("Supuesto colormap continuo")
# colores = [RGB(*cm.turbo(i, bytes=True)[:4]) for i in Normalize(
# vmin=min, vmax=max) (df[variable].values)]
# palette = "Turbo256"
color_mapper = LinearColorMapper(palette = palette,
low = df[variable].min(),
high = df[variable].max())
return colores, color_mapper
#######################################################
# VAMOS CON UNAS FUNCIONES PARA CALCULAR DISTANCIAS
# ENTRE LOS GRUPOS SELECCIONADOS POR EL USUARIO
#######################################################
# CalculaRegresionLogistica(s1,s2)
# Calcula el hiperplano que mejor separa los grupos
# de datos de los conjuntos de índices s1 y s2
# mediante regresión logística, y devuelve el
# vector director procesado para usar como criterio
# de ordenación.
def CalculaRegresionLogistica(s1, s2, lista):
import sklearn as sk
from sklearn.linear_model import LogisticRegression
x = df.iloc[s1, :]
if (pd.__version__ < '2.0'):
x = x.append(df.iloc[s2, :])
else:
x = pd.concat([x, df.iloc[s2, :]], ignore_index=True)
x = x[x.columns.intersection(lista)]
y = pd.DataFrame([0]*len(s1) + [1]*len(s2), columns=list('T'))
print(f'. numpy: {np.__version__}')
print(f'scikit-learn: {sk.__version__}')
print('Realizando regresión...')
LR = LogisticRegression(
random_state=0, solver='liblinear', multi_class='ovr').fit(x, y.iloc[:, 0])
print('Listo.')
d = LR.coef_
return d
# CalculaVectorOrdenado: recibe dos vectores con índices de elementos
# correspondientes a dos grupos seleccionados y una lista con nombres
# de columnas y realiza las siguientes acciones:
# 1/ Realiza una regresión logística y devuelve el vector director
# del hiperplano que separa ambos grupos.
# 3/ Ordena la lista de variables utilizada de mayor a menor por
# el módulo de su componente asociada del vector director
# como aproximación a aquellos que tienen más influencia en que ambos
# grupos estén separados
# Devuelve un dataframe con la lista de variables y las distancias ordenado
def CalculaVectorOrdenado(s1, s2, lista_cols):
# Tomamos los datos seleccionados de
# ambos patches del data frame
print("Calculando distancia entre grupos...")
d = CalculaRegresionLogistica(s1, s2, lista_cols)
print("Normalizando...")
d = np.abs(d)
# Normalizamos para que el máximo valga 1 y el resto como fracción de él.
d = d/np.max(np.max(d))
print("Creando nuevo dataframe...")
# Y creamos un dataframe con ellas y las columnas del dataframe
# global df, que contienen la lista de variables
tdf = pd.DataFrame(d.T, index=df.columns.intersection(
lista_cols).T, columns=list(['Distance']))
# Ordenamos el dataframe por 'Distance'
tdf.sort_values(by='Distance', inplace=True, ascending=False)
return tdf
##########################################################
# Leemos y procesamos los datos de configuración
##########################################################
import json
f = open('mpconfig.json', encoding='utf-8')
conf = json.load(f)
f.close()
print("Leyendo archivo de configuración...")
print(conf["comment"]["text"])
print("Versión:")
print(conf["comment"]["version"])
# Datos
datapath = conf["data"]["datapath"]
filename = conf["data"]["filename"]
# Orden y coloreado
sortbyColumn = conf["defaults"]["sortbyColumn"]
colorbyColumn = conf["defaults"]["colorbyColumn"]
# Cajas de selección
vbleEncodingHor = conf["defaults"]["box1"]
vbleEncodingVer = conf["defaults"]["box2"]
autocomplete = (conf["defaults"]["autocomplete"] != 'No')
# Columnas a usar, si vacía usamos todas
columnas = conf["columns"]["columnsToUse"]
# Columna con identificador unívoco del dato
idData = conf["columns"]["idColumn"]
# Separamos variables de encodings (supervisadas) del resto
lista_variables_encodings = conf["columns"]["encodings"]
resto_variables = conf["columns"]["rest"]
# META: Mejor que resto sean el resto, por si hay muchas. También, si hay muchas, sustituir
# luego desplegable por autocompletar (o configuración)
# Cuáles son categóricas y necesitan generar columnas numéricas asociadas?
# Si ya vienen en el csv como <nombre>#, no indicar aquí.
list_cl_str = conf["columns"]["categorical"]
# Títulos para hover, app y figura
appTitle = conf["appearance"]["appTitle"]
hoverTitle = conf["appearance"]["hoverTitle"]
figTitle = conf["appearance"]["figTitle"]
# Variables en hover
listaHover = conf["columns"]["hover"]
# Datos con posiciones precalculadas (ej: tSNE)
precalculatedPositionsFiles = conf["precalcEncodings"]["files"]
precalculatedPositionsNames = conf["precalcEncodings"]["names"]
##########################################################
# Donde se guardan los datos para descargarlos al cliente
save_path = 'static/export/'
save_request = 'MP/uniovi-gsdpi-bokeh-mp-traccion/static/export/'
# Leemos los datos principales
df = pd.read_csv(datapath + filename, index_col=0)
# Si hay columna para ordenar, ordenamos
if(sortbyColumn != ''):
df.sort_values(by=[sortbyColumn],ignore_index=True, inplace=True)
# Si no tenemos lista con las variables resto (porque sean muchas, por ejemplo),
# tratar de obtenerla automáticamente.
if resto_variables == []:
ll=df.columns.to_list()
resto_variables = [i for i in ll if i not in lista_variables_encodings]
resto_variables = [i for i in resto_variables if '#' not in i]
if idData in resto_variables:
resto_variables.remove(idData)
# Generamos variables numéricas para las categóricas
for i in list_cl_str:
clave = set(df[i])
diccionario = dict(np.column_stack(
(list(sorted(clave)), np.array(range(len(clave))).astype(object))))
df[i+'#'] = [diccionario[j] for j in df[i]]
ll=df.columns.to_list()
list_cl_strnum = [i for i in ll if '#' in i]
# Añadimos los diferentes encodings
E = []
encoding_name = []
# Añadimos codificaciones pre-calculadas
i=0
for file in precalculatedPositionsFiles:
pos =np.load(datapath + file)
# Normalizamos, por si acaso no vienen...
pos = pos/np.max(np.abs(pos))
E.append(pos)
encoding_name.append(precalculatedPositionsNames[i])
i = i+1
# Generamos ahora encodings para las variables supervisadas. Circular para las categóricas, lineal para
# las numéricas (horizontal)
for i in lista_variables_encodings:
print('generando codificaciones espaciales para variable ' + i + '...')
if (i+'#' in list_cl_strnum):
pos = df[[i+'#']].merge(circularEnc(df[i+'#'].unique(), i+'#'),
on=i + '#', how='left')[['posx', 'posy']].values
else:
pos = linearEnc(df, i, 'hor')
encoding_name.append(i)
E.append(pos)
# Ahora las cajas que permiten añadir encodings para el resto de variables
print('Creando cajas...')
encoding_name.append(vbleEncodingHor)
E.append(linearEnc(df, vbleEncodingHor,'hor'))
encoding_name.append(vbleEncodingVer)
E.append(linearEnc(df, vbleEncodingVer, 'ver'))
if (columnas == []):
columnas = df.columns.tolist()
print('Creando source...')
# Preparamos el source
aux = pd.concat([
df[columnas]],
axis=1)
# fuente de datos
source = ColumnDataSource(aux)
# Utilizamos la vista, para reudcir el número de datos procesados
from bokeh.models import CDSView, BooleanFilter
if(__version__ < '3.0'):
myView = CDSView(source = source, filters=[])
else:
myView = CDSView()
#myView.filter = BooleanFilter(booleans)
print('Inicializando cosas...')
# Inicializaciones necesarias
selGA = []
selGB = []
################
# colores
###############
source.data['color'], TheColorMapper= getColorMap(df, colorbyColumn)
##############
# añadimos encodings
##############
print('Añadiendo encodings al source...')
# añadimos los encodings al source
# los tenemos que apilar en forma de matriz (n_samples,2*num_encodings)
source.data['E'] = np.concatenate(E, axis=1).tolist()
def ResetEncodings(which=0, quantity=0.5):
# nota: asignamos al encoding base E[which] un peso de quantity
z = np.zeros(len(encoding_name))
z[which] = quantity
a = softmax(10*z)
Epos = np.zeros([len(E[0]), 2])
for i in range(len(encoding_name)):
print('Encoding: '+ encoding_name[i] +'...')
Epos = Epos + a[i]*E[i]
source.data['pos_x'] = Epos[:, 0]
source.data['pos_y'] = Epos[:, 1]
ResetEncodings(0,0.5) # Resetea los encodings, poniendo el 0 al 50%
##########################################
#######################################################
# FIGURAS BOKEH
#######################################################
##########################################
# Caja de coloreado
##########################################
var_color = CheckboxGroup(
labels=["usar mapa de color:"], active=[])
def update_coloring(attrname, old, new):
global plot
from matplotlib import cm
if(len(new) == 0):
variable = colorbyColumn
plot.legend.visible=True
bar.visible = False
else:
variable = select_coloreado.value
plot.legend.visible=False
bar.visible = True
colores, TheColorMapper = getColorMap(df,variable)
bar.color_mapper = TheColorMapper
# ticker = FixedTicker(ticks=[TheColorMapper.low, TheColorMapper.high])
# formatter = CustomJSTickFormatter(code="""
# var data = { %d: '%s', %d: '%s' }
# return data[tick]
# """ %(TheColorMapper.low, str(TheColorMapper.low), TheColorMapper.high, str(TheColorMapper.high)) )
# bar.formatter = formatter
# bar.ticker = ticker
source.data['color'] = colores
return
var_color.on_change('active', update_coloring)
##########################################
# Caja para el parámetro de coloreado
##########################################
def update_color_var(attrname, old, new):
var_color.active = [0]
update_coloring(None, None, [0])
select_coloreado = Select(value=vbleEncodingHor, options=resto_variables, title="color:", width=180)
select_coloreado.on_change('value', update_color_var)
##########################################
# Actualiza el contenido del tooltip
# para el hover
#########################################
def actualiza_hover():
str = '<style>.bk-tooltip>div:not(:first-child){display:none;}</style><b>%s: </b> @{%s} <br>'%(hoverTitle,hoverTitle)
#lista_hover = lista_variables_encodings[1:] + \
# [resto_variables[0]] + resto_variables[2:11] + [resto_variables[-1]]
listaHoverAux = listaHover.copy()
if (vbleEncodingHor not in listaHoverAux):
listaHoverAux.append(vbleEncodingHor)
if (vbleEncodingVer not in listaHoverAux):
listaHoverAux.append(vbleEncodingVer)
for i in listaHoverAux:
str = str + '<b>{}:</b> @{}<br>'.format(i, i)
custom_hover.tooltips = str
###########################################
# FIGURA PRINCIPAL: PLOT DE LAS MUESTRAS
###########################################
if(__version__ >= '3.1.0'):
plot = figure(name='mainplot', height=800, width=1000, title=figTitle,
tools="crosshair,pan,reset,save,wheel_zoom,box_select,lasso_select",
toolbar_location="above",
output_backend='webgl',match_aspect=True)
else:
plot = figure(name='mainplot', plot_height=800, plot_width=1000, title=figTitle,
tools="crosshair,pan,reset,save,wheel_zoom,box_select,lasso_select",
toolbar_location="above",
output_backend='webgl',match_aspect=True)
#plt = plot.circle(x='pos_x', y='pos_y', color='color',
# source=source, view = myView,
# size=6, #radius=0.5,
# alpha=0.7, nonselection_alpha=0.1, legend_group=colorbyColumn,
# line_color='black', line_width=0.5)
plt = plot.scatter('pos_x', 'pos_y', color='color',
source=source, view=myView,
# legend_group=colorbyColumn,
size=6, alpha=0.7,
selection_color='red',
nonselection_alpha=0.1,
line_color='#000000',
# line_width=0.5
line_width=0.0
)
# añadir colorbar a la figura
# ticker = FixedTicker(ticks=[TheColorMapper.low, TheColorMapper.high])
# formatter = CustomJSTickFormatter(code="""
# var data = { %d: '%s', %d: '%s' }
# return data[tick]
# """%(TheColorMapper.low, str(TheColorMapper.low), TheColorMapper.high, str(TheColorMapper.high)) )
# bar = ColorBar(color_mapper = TheColorMapper, formatter = formatter, ticker = ticker, location=(0,0))
bar = ColorBar(color_mapper = TheColorMapper, location=(0,0))
plot.add_layout(bar, "left")
# META: Un poco chapuza para quitar la barra si es categórico...
if len(df[colorbyColumn].unique())<=20:
bar.visible = False
# Vamos a crear una herramienta hover customizada para que se comporte como queremos
# mostrando los valores de las variables de encodings.
custom_hover = HoverTool()
actualiza_hover()
plot.tools.append(custom_hover)
# Prueba para ajustar el source al rango:
def event_callback(event):
global myView
# Algo pasa que usa el source en el servidor, no el del cliente
# y, por tanto, los pos_x y pos_y del primer encoding, sin
# actualizar
# Nos quedamos solo con los puntos visibles. Para eso
# modificamo el filtro de la vista
booleans = [True if ((x >= event.x0) and (x <= event.x1) and (y>=event.y0) and (y<=event.y1)) else False
for [x,y] in zip(source.data['pos_x'], source.data['pos_y'])]
print(f"De {len(booleans)} Nos quedamos con {sum(booleans)}")
""" if(__version__ < '3.0'):
myView.filters = [BooleanFilter(booleans)]
else:
myView.filter = BooleanFilter(booleans) """
from bokeh.events import *
#plot.on_event(RangesUpdate, event_callback)
##############################################
# SPINNER
# Todo esto es para implementar el spinner, la
# rueda que gira cuando tenemos que realizar un
# cálculo que lleva tiempo.
##############################################
# Definimos el div con el gráfico animado
spinner_text = """
<!-- https://www.w3schools.com/howto/howto_css_loader.asp -->
<div class="loader">
<style scoped>
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</div>
"""
div_spinner = Div(text="", width=300, height=120)
# Y ahora dos funciones para mostrarlo y ocultarlo
def show_spinner():
div_spinner.text = spinner_text
def hide_spinner():
div_spinner.text = ""
def SelectionCallback(attrname, old, new):
global source
ss = df.iloc[new]
if len(ss) > 0:
# Informamos del número de elementos copiados
tt = 'Seleccionados %d registros de %d datos diferentes.' % (
len(ss), len(ss[hoverTitle].unique()))
else:
tt = ''
div_spinner.text = tt
# Esto ha cambiado en la versión 1.3.4!!!!
# así que chequeamos la versión y usamos la llamada
# adecuada para instalar la callback
if(__version__ >= '1.3.4'):
plt.data_source.selected.on_change('indices', SelectionCallback)
else:
plt.data_source.on_change('selected', SelectionCallback)
#######################################################
# SLIDERS
#######################################################
sliders = []
for i in encoding_name:
sliders.append(Slider(title=i, value=0, start=0,
end=1, step=0.01, width=200, height=39))
sliders[0].value = 0.5
# Callback para los sliders que realiza el morphing mediate la función Softmax.
# Implementación en javascript para que se ejecute localmente.
# De este modo es mucho más eficiente y la visualización es más
# suave.
# Recibe la fuente de datos, el mapa de encodings y una lista con los
# widgets slider para tratar.
CodigoJS = """
// pesos para los encodings definidos en la interfaz
var z = s.map(x=>x.value)
// aplicamos coeficiente de sensibilidad de morphing (sens = 10)
z = z.map(x => x*10)
// calculamos función softmax para obtener los pesos "a"
var ez = z.map(x => Math.exp(x)) // exponentes de z_i
var suma_ez = ez.reduce( (x,v) => x + v) // suma de los exponentes de z_i
var a = ez.map( x => x/suma_ez) // exponentes de z_i / suma de exponentes de z_i
// No sé por qué razón así parece ir mucho más rápido.
a = a.map(x => Math.round((x + Number.EPSILON) * 1000) / 1000)
// calculamos encodings x pesos
var Epos = []
var data = source.data
var N = data['E'].length
for (var j=0; j< N; j++ )
{
Epos[j] = [0,0]
for (var i=0; i< a.length; i++)
{
Epos[j][0] += a[i]*data['E'][j][2*i]
Epos[j][1] += a[i]*data['E'][j][2*i+1]
}
}
// actualizadmos coordenadas x,y en source
data['pos_x'] = Epos.map(x => x[0])
data['pos_y'] = Epos.map(x => x[1])
source.data = data
source.change.emit()
"""
# Instalamos la callback anterior
update_data = CustomJS(args=dict(source=source, s=sliders), code=CodigoJS)
for s in sliders:
s.js_on_change('value', update_data)
################################################################
# Cajas autocompletables o desplegables para elegir variable
################################################################
if autocomplete:
select1 = AutocompleteInput(
value=vbleEncodingHor, completions=resto_variables, title="hor:", width=180)
select2 = AutocompleteInput(
value=vbleEncodingVer, completions=resto_variables, title="ver:", width=180)
else:
select1 = Select(
value=vbleEncodingHor, options=resto_variables, title="hor:", width=180)
select2 = Select(
value=vbleEncodingVer, options=resto_variables, title="ver:", width=180)
# Función que actualiza los encodings cuando se modifican las variables
# que tienen encodings lineales horizontal y vertical.
# Esto toma tiempo, de forma que se hace en python y en dos partes.
# El proceso es un poco complejo, para que se muestren los spinners.
# Se realiza una callback normal para el control que:
# 1- muestra el spinner
# 2- instala una callback para el siguiente tick de reloj
# que realiza el trabajo
# 3- esa callback realiza el trabajo y esconde el spinner
# Además se hacen otras cosas, como calcular los nuevos encodings,
# o deshabilitar los sliders para evitar interacciones del usuario mientras
# se realiza el trabajo.
# Función que actualiza los encodings en la fuente de datos en python.
def actualiza_encodings():
import sys
z = np.array([s.value for s in sliders])
a = softmax(10*z)
a = list(map(lambda x: round(x,3), a))
Epos = np.zeros([len(E[0]), 2])
for i in range(len(encoding_name)):
Epos = Epos + a[i]*E[i]
p1 = [i for i in zip(range(Epos.shape[0]), Epos[:, 0])]
p2 = [i for i in zip(range(Epos.shape[0]), Epos[:, 1])]
source.patch(dict(pos_x=p1, pos_y=p2))
return
# Función auxiliar que deshabilita los sliders y las cajas de texto
def disable_sliders(b):
for s in sliders:
s.disabled = b
select1.disabled = b
select2.disabled = b
# Función que realiza el trabajo para el paso 3
def actualiza_worker():
source.data['E'] = np.concatenate(E, axis=1).tolist()
actualiza_encodings()
actualiza_hover()
disable_sliders(False)
hide_spinner()
# Callbacks de los controles
def update_select1(attrname, old, new):
encoding_name[-2] = new
E[-2] = (linearEnc(df, new, 'hor'))
sliders[-2].title = new
global vbleEncodingHor
vbleEncodingHor = new
disable_sliders(True)
show_spinner()
curdoc().add_next_tick_callback(actualiza_worker)
def update_select2(attrname, old, new):
encoding_name[-1] = new
E[-1] = (linearEnc(df, new, 'ver'))
sliders[-1].title = new
global vbleEncodingVer
vbleEncodingVer = new
disable_sliders(True)
show_spinner()
curdoc().add_next_tick_callback(actualiza_worker)
# Asignamos las callbacks
select1.on_change('value', update_select1)
select2.on_change('value', update_select2)
##########################################
# Botones para tipo de encoding de los
# sliders
##########################################
tipo_encoding = []
for i in encoding_name:
if (i + '#' in list_cl_strnum):
sactive = 0
else:
sactive = 1
tipo_encoding.append(RadioButtonGroup(
labels=["circ", "hor", "ver"], active=sactive, orientation="horizontal", width=150, height=39)
)
# Deshabilitar los que son de tSNE (no tiene sentido otra que circular)
for i in range(len(precalculatedPositionsNames)):
tipo_encoding[i].disabled = True
# Poner la correspondiente para las dos últimas (cajas de variables no supervisadas)
# y deshabilitar
tipo_encoding[-1].active = 2
tipo_encoding[-1].disabled = True
tipo_encoding[-2].active = 1
tipo_encoding[-2].disabled = True
# Ahora la callback para todos
# Una función auxiliar para recalcular los encodings...
def recalcula_encodings(E):
k = len(precalculatedPositionsNames) # Tras los tSNE
for i in lista_variables_encodings:
print('recalculando encoding para ' + i + '...')
tipo = tipo_encoding[k].active
if (i + '#' in list_cl_strnum):
name = i + '#'
else:
name = i
if (tipo == 0):
#pos = df[[name]].merge(circularEnc(max(df[name])+1, name),
# on=name, how='left')[['posx', 'posy']].values
pos = df[[name]].merge(circularEnc(df[name].unique(), name),
on=name, how='left')[['posx', 'posy']].values
print('Circular')
elif (tipo == 1):
pos = linearEnc(df, name, 'hor')
print('Horizontal')
else:
pos = linearEnc(df, name, 'ver')
print('Vertical')
E[k] = pos
k = k+1
return
def update_tipo_encoding(attrname, old, new):
global source
global E
global df
disable_sliders(True)
show_spinner()
recalcula_encodings(E)
curdoc().add_next_tick_callback(actualiza_worker)
return
for i in tipo_encoding:
i.on_change('active', update_tipo_encoding)
#######################################################
# DESCARGA DE DATOS
#######################################################
############################################
# Enviar resultados guardados al cliente
#############################################
# Esto necesita una callback en javascript
JScode_fetch = """
debugger
var filename = t.text;
console.log('Entering saving code..')
if (filename != '') {
console.log('Saving..'+filename)
try {
fetch('/'+'%s'+filename, {
cache: 'no-store',
method: 'GET',
headers: {
// 'Authorization': 'Bearer ' + sessionStorage.getItem('token')
},
}).then(
data => {
return data.blob();
}
).then(
response => {
console.log(response.type);
const dataType = response.type;
const binaryData = [];
binaryData.push(response);
const downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
downloadLink.setAttribute('download', filename);
document.body.appendChild(downloadLink);
downloadLink.click();
downloadLink.remove();
}
)
} catch (e) {
addToast('Error inesperado.', {appearance: 'error', autoDismiss: true});
}
}
t.text=''
console.log('Exiting saving code..')
""" %(save_request)
# y un control dummy para asociar esa función
div_dummy = Div(text="", width=300, height=120, visible=False)
div_dummy.js_on_change('text', CustomJS(args=dict(t=div_dummy),
code=JScode_fetch))
##########################################
# Botón para calcular influencias
# de variables en separación de grupos A y B
#########################################
# Función que calcula las influencias de las variables (pasados en lista_diff)
# en el agrupamiento tSNE utilizando la regresión logística
# Esto toma tiempo, de forma que se hace en dos partes.
# El proceso es un poco complejo, para que se muestren los spinners.
# Se realiza una callback normal para el control que:
# 1- comprueba que ambos grupos tengan datos muestra el spinner
# 2- instala una callback para el siguiente tick de reloj
# que realiza el trabajo
# 3- esa callback realiza el trabajo y esconde el spinner
def influencias_worker():
global selGA
global selGB
global lista_diff
undf = CalculaVectorOrdenado(selGA, selGB, lista_diff)
# Vamos a imprimir las primeras variables, según la ordenación anterior
l = undf.index
tabla = '''<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:11px;padding:0px 0px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:11px;font-weight:normal;padding:0px 0px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-fila{border-color:#ffffff;text-align:center;vertical-align:top}
.tg .tg-tit{font-weight:bold;border-color:#ffffff;text-align:center;vertical-align:top}
</style>
'''
tabla += '<table class="tg"><tr><th class="tg-tit">Var</th><th class="tg-tit">A (μ ± σ)</th><th class="tg-tit">B (μ ± σ)</th><th class="tg-tit">Relevance</th></tr>'
#k = 0
#while (undf.iloc[k, 0] >= 0.1 and k < 100):
# tabla += '<tr><td class="tg-fila">%s</td><td class="tg-fila">%2.3g ± %2.3g</td><td class="tg-fila">%2.3g ± %2.3g</td><td class="tg-fila">%2.3g</td class="tg-fila"></tr>' % (l[k],
# df.iloc[selGA, :][l[k]].mean(
# axis=0), df.iloc[selGA, :][l[k]].std(axis=0),
# df.iloc[selGB, :][l[k]].mean(
# axis=0), df.iloc[selGB, :][l[k]].std(axis=0),
# undf.iloc[k, 0])
# k = k+1
k = 0
while k < len(l) and k < 100:
tabla += '<tr><td class="tg-fila">%s</td><td class="tg-fila">%2.3g ± %2.2g</td><td class="tg-fila">%2.3g ± %2.2g</td><td class="tg-fila">%2.2g</td class="tg-fila"></tr>' % (l[k],
df.iloc[selGA, :][l[k]].mean(
axis=0), df.iloc[selGA, :][l[k]].std(axis=0),
df.iloc[selGB, :][l[k]].mean(
axis=0), df.iloc[selGB, :][l[k]].std(axis=0),
undf.iloc[k, 0])
k = k+1
tabla += '</table>'
notifications.text = tabla
disable_sliders(False)
hide_spinner()
def InfluenciasCallback(event):
global selGA
global selGB
global lista_diff
lista_diff = resto_variables
if (len(selGA) == 0):
notifications.text = 'Group A is empty'
return
if (len(selGB) == 0):
notifications.text = 'Group B is empty'
return
show_spinner()
disable_sliders(True)
curdoc().add_next_tick_callback(influencias_worker)
return
boton_influencias = Button(
label="Diff A ► B ", button_type="success", sizing_mode = "scale_width")
boton_influencias.on_click(InfluenciasCallback)
#############################################################
# OTROS CONTROLES
############################################################
#########################################
# Notificaciones
#########################################
if(__version__ >= '3.1.0'):
notifications = Div(text='', styles={'title': 'Salida.', 'border': 'double', 'overflow-y': 'scroll',
'width': '300px', 'height': '300px'})
else:
notifications = Div(text='', style={'title': 'Salida.', 'border': 'double', 'overflow-y': 'scroll',
'width': '300px', 'height': '300px'})
##########################################
# Botón para asignar la selección
# actual al grupo A
#########################################
def SeleccionGACallback(event):
global selGA
selGA = source.selected.indices
if len(selGA) > 0:
notifications.text = '%d registers selected to group A' % (
len(selGA))
source.selected.indices = []
boton_gA.label = 'Selection to group A(%d)' % (len(selGA))