-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashWEB_eng-darkmode.py
More file actions
1290 lines (1200 loc) · 50.9 KB
/
Copy pathdashWEB_eng-darkmode.py
File metadata and controls
1290 lines (1200 loc) · 50.9 KB
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
import dash, pickle, json, re, os
from dash import dcc
from dash import html
from dash import dash_table as dt
import numpy as np
from src import dashCLASSIFIER as cf
import collections as coll
stopWord = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with',
'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
def nestFunc():
return coll.defaultdict(list)
# calculate percentual probability from logarithm values
def probMap(probList, prior = False):
avgPerc = sum(probList)/3
if prior:
for ith, i in enumerate(probList): # calc % for positive values
probList[ith] = (i * 33.33) / avgPerc
else: # calc % for negative values
for ith, i in enumerate(probList):
probList[ith] = (((2*avgPerc) - i) * 33.33) / avgPerc
return probList
# creating axis properties for ternary graph
def makeAxis(title, minPerc):
return {
'min': minPerc,
'title': title,
'titlefont': { 'size': 15 },
'tickangle': 0,
'tickfont': { 'size': 15 },
'tickcolor': 'rgba(20,40,100,50)',
'ticklen': 5,
'showgrid': True,
'color': 'black',
'gridcolor': 'black',
'linecolor': 'black',
'tickcolor': 'black'
}
import base64
with open(os.path.join("src", "images", "NBclassifier.png"), "rb") as handle:
encoded_image = base64.b64encode(handle.read())
with open(os.path.join("src", "datasets", "dashjupy-likelihood"), "rb") as handle:
likelihood = pickle.loads(handle.read()) # likelihood[className][word] {counted}
with open(os.path.join("src", "datasets", "dashjupy-priors"), "rb") as handle:
priors = pickle.loads(handle.read()) # prior[className] {counted}
with open(os.path.join("src", "datasets", "dashjupy-content"), "rb") as handle:
content = pickle.loads(handle.read()) # content[className][sampleName][words(list)]
app = dash.Dash()
text_style = {
'font-family': 'Helvetica',
'color': 'white'
}
app.layout = html.Div([
# left side section (intro, classification, report)
html.Div([
dcc.Markdown('## Classification parameters and results',
style={
'font-family': 'Helvetica',
'color': 'white',
'text-align': 'center'
}
),
dcc.Markdown("""Used datasets (news, ohsu, revs) are divided into **training(75%)** and **testing(25%)** datasets.
**Choose three categories** from which we will pick training data for training and testing data for classification.""",
style={
'font-family': 'Helvetica',
'color': 'white'
}
),
dcc.Dropdown(
id='dropdown-categorySelection',
options= [{'label': str(category),'value': str(category)} for category in priors],
placeholder="Select three categories for classification",
value=['news-Graphics', 'news-Forsale', 'news-Baseball'],
multi=True,
style={
'backgroundColor': 'black'
}
),
# zero-fix selection
dcc.Markdown('### Zero-fix solution option',
style={
'font-family': 'Helvetica',
'color': 'white'
}
),
dcc.RadioItems(
id = "radio-zeroFixSelection",
options=[
{'label': 'Choose number', 'value': 'RTN'},
{'label': 'Laplace smoothing', 'value': 'LAP'},
],
value='RTN',
labelStyle={'display': 'inline-block'},
style={
'padding': '5px',
'display': 'inline-block',
'color': 'white'
}
),
# tooltip about zero fix selection
html.Abbr("[?]",
title="Choose number - pick number from the slider below to replace all zero word occuriences in categories\
\nLaplace smoothing - add one word occurence to all category word counts",
style={
'display': 'inline-block',
'marginLeft': '15px',
'cursor': 'help',
'text-decoration': 'none',
'font-family': 'Helvetica',
'color': 'white'
}
),
# range slider for rational number selection
dcc.RangeSlider(
id='rangeSlider-rationalNum',
marks={i: '1e-{}'.format(i) for i in range(4,13)},
max=12,
min=4,
value=[8],
dots=False,
step=0.01,
updatemode='drag'
),
html.Br(),
html.Br(),
# output for exact value of selected rational number
html.Div(id='div-outputZeroF',
style={
'display': 'inline-block',
'width': '200px',
'color': 'white'
}
),
html.Button('Classify',
id='button-classify',
style={
'font-size': '15px',
'cursor': 'pointer',
'color': 'black',
'width': '100px',
'height': '25px',
'display': 'inline-block',
'marginLeft': '50px'
}
),
html.Br(),
# classification report of 3 chosen categories
dcc.Markdown('### Classification report ',
style={
'display': 'inline-block',
'font-family': 'Helvetica',
'color': 'white'
}
),
# tooltip about score metrics
html.Abbr("[?]",
title="Precision - Probability that classified sample to this category was classified correctly \
\nRecall - Probability that sample assigned to this category was classified correctly\
\nAverage - (Precision + Recall) / 2\
\nF1-Score - 2 * (Precision * Recall) / (Precision + Recall)\
\nSupport - Sample amount",
style={
'display': 'inline-block',
'marginLeft': '15px',
'cursor': 'help',
'text-decoration': 'none',
'color': 'white'
}
),
html.Div([
dt.DataTable(
id='dataTable-scoreMetrics',
columns=[
{"name": "Category", "id": "category_id"},
{"name": "Precision", "id": "precision_id"},
{"name": "Recall", "id": "recall_id"},
{"name": "Average", "id": "average_id"},
{"name": "F1-score", "id": "f1-score_id"},
{"name": "Support", "id": "support_id"},
],
data=[],
style_header={
'fontWeight': 'bold',
'backgroundColor': 'rgb(35, 35, 35)',
'color': 'white'
},
style_cell={
'textAlign': 'left',
'font-family':'Helvetica',
'backgroundColor': 'black',
'color': 'white'
},
style_cell_conditional=[
{'if': {'column_id': 'category_id'}, 'width': '30%'},
{'if': {'column_id': 'precision_id'}, 'width': '16%'},
{'if': {'column_id': 'recall_id'}, 'width': '16%'},
{'if': {'column_id': 'average_id'}, 'width': '16%'},
{'if': {'column_id': 'f1-score_id'}, 'width': '16%'},
{'if': {'column_id': 'support_id'}, 'width': '16%'},
],
),
]),
# accuracy value output
html.Br(),
html.Div(id='div-accuracy',
style={
'font-size': '20px',
'white-space': 'pre',
'textAlign': 'center',
'color': 'white'
}
)],
style={
'padding': '25px',
'marginLeft': '10px',
'width': "45%",
'height': "800",
'boxShadow': '0px 0px 5px 5px rgba(204,204,204,0.4)',
'float': 'left'
}
),
# right side section for prior and word frequencies/likelihood graphs
html.Div([
dcc.Markdown('## Properties of training dataset',
style={
'text-align': 'center',
'color': 'white'
}
),
# bar graph of priors
dcc.Graph(
id='graph-prior',
figure={
'data': [{
'x': [str(category) for category in priors],
'y': [str(priors[category]) for category in priors],
'type': 'bar',
'name': 'Samples'
},{
'x': [str(category) for category in likelihood],
'y': [str(sum(likelihood[category].values())) for category in likelihood],
'type': 'bar',
'name': 'Words',
'visible': 'legendonly'
},{
'x': [str(category) for category in likelihood],
'y': [str(sum(likelihood[category].values())/priors[category]) for category in likelihood],
'type': 'bar',
'name': 'Avg words per sample',
'visible': 'legendonly'
}],
'layout': {
'title': 'Number of samples/words in each category',
'titlefont': {"size": 20},
'height': '550',
'legend': {
"font": {"size": 9}
},
'plot_bgcolor': 'black',
'paper_bgcolor': 'black',
# 'font_color': 'white',
# 'font-color': 'white',
# 'fontColor': 'white',
# 'color': 'white'
}
},
# style={
# 'font_color': 'white',
# 'font-color': 'white',
# 'fontColor': 'white',
# 'color': 'white'
# }
)],
style={
'padding': '25px', # space between div start and content inside
'marginLeft': '665px', # position placing for not getting overlayed
"width": "45%", # width of the block
"height": "800",
'boxShadow': '0px 0px 5px 5px rgba(204,204,204,0.4)',
}
),
html.Div(id='moreCatsSpace'),
# div for word freq/imp graph
html.Div([
dcc.Markdown('## Features of training dataset',
style={
'text-align': 'center',
'color': 'white'
}
),
# word freq/imp selecion
dcc.RadioItems(
id = 'radio-wordPreference',
options=[
{'label': 'Word frequencies', 'value': 'wFreq'},
{'label': 'Word probability', 'value': 'wImp'},
],
value='wFreq',
style={
'display': 'inline-block',
'marginLeft': '460px',
'color': 'white'
}
),
# tooltip about freq/imp selection
html.Abbr("[?]",
title="Word frequencies - counts word frequencies in chosen categories\
\nWord probability - calculates probability of a certain word regarding to total amount of words in category\
\n(particular word amount in training dataset category is divided by total amount of words in category)\
\nNote: Stopwords arent used in training set, but they are filtered in this graph as they dont give that much information",
style={
'display': 'inline-block',
'marginLeft': '15px',
'cursor': 'help',
'text-decoration': 'none',
'color': 'white'
},
),
html.Br(), html.Br(),
# selection of category with sorted frequent/importance words
dcc.Dropdown(
id='dropdown-categoryPreference',
options=[{'label': str(category), 'value': str(category)} for category in priors],
placeholder="Select a category in which you want to see the most frequent used words",
searchable=False,
style={
'backgroundColor': 'black'
}
),
# tab selection for different view of the graph
dcc.Tabs(
[
dcc.Tab(
label='Stacked up',
value="0",
),
dcc.Tab(
label='Comparison',
value="1",
),
],
value='0',
id='tabs-select'
),
# graph of word freqs/imps
html.Div(id='div-graph-bar'),
],
style={
'paddingLeft': '40px',
'paddingRight': '40px'
}
),
# section for ternaries, process, uniqs and sample text
html.Div([
dcc.Graph(id='graph-ternarySamples', style={'display': 'inline-block', 'marginLeft': '30px'}),
dcc.Graph(id='graph-ternaryWords', style={'display': 'inline-block', 'white-space': 'pre'}),
# range picker of words
html.Div([
dcc.RangeSlider(
min=0,
max=100,
id='slider-wordRange',
)],
),
# tooltip for word selection
html.Abbr("[?]",
title="Range selection of words in current selected sample\n" +
" - updates classification process\n" +
" - updates category prediction (Word probabilities graph color)",
style={
'display': 'inline-block',
'marginLeft': '25px',
'cursor': 'help',
'text-decoration': 'none',
'color': 'white'
}
),
# output with boundaries of selected word range
html.Div(id='div-wordRange',
style={
'textAlign': 'center',
'color': 'white'
}
),
html.Br(),
# range slider for reset point selection
html.Div([
dcc.Slider(
id='slider-chunkSum',
min=1,
max=100,
step=1,
value=25,
marks={
10: '10', 20: '20', 30: '30', 40: '40', 50: '50',
60: '60', 70: '70', 80: '80', 90: '90', 100: '100'
}
)],
),
# tooltip about reset points
html.Abbr("[?]",
title="The overall probability is less influenced by each word calculation,\n" +
"to display the flow throughout the whole process, set resetting\n" +
"probability point for division of overall probabilities",
style={
'display': 'inline-block',
'marginLeft': '25px',
'cursor': 'help',
'text-decoration': 'none',
'color': 'white'
}
),
html.Br(),
html.Br(),
# reset point output index
html.Div(id='div-sumRange',
style={
'textAlign': 'center',
'color': 'white'
}
),
# graph of probability computation process
dcc.Graph(id='graph-process'),
html.Br(),
# graph of uniqueness of words for each category
html.Div(id='div-wordImportances3', style={'boxShadow': '0px 0px 5px 5px rgba(204,204,204,0.4)'}),
dcc.Markdown('### Text of sample',
style={
'textAlign': 'center',
'color': 'white'
}
),
# text of selected sample also influenced with range selection
html.Div(id='div-sampleText',
style={
'padding': '30px',
'boxShadow': '0px 0px 5px 5px rgba(204,204,204,0.4)',
'text-align': 'justify',
'color': 'white'
}),
]),
# preprocessing hidden values
html.Div(id='intermediate-value', style={'display': 'none'}),
html.Div(id ='selected-sample', style={'display': 'none'}),
],
style={
'padding': '5px',
'width': '1300px',
'margin': 'auto',
'box-shadow': '0px 0px 20px #00070E',
'border-style': 'solid',
'border-width': '2px',
'backgroundColor': 'black'
}
)
################ CALLBACK FUNCTIONS ################
# updating category preference dropdown
@app.callback(
dash.dependencies.Output(component_id='dropdown-categoryPreference', component_property='options'),
[dash.dependencies.Input(component_id='dropdown-categorySelection', component_property='value')])
def updatePreference(selectedCategories):
return [{'label': category, 'value': category} for category in selectedCategories]
# displaying graph of word frequencies
@app.callback(
dash.dependencies.Output('div-graph-bar', 'children'),
[dash.dependencies.Input('tabs-select', 'value'),
dash.dependencies.Input('dropdown-categorySelection', 'value'),
dash.dependencies.Input('dropdown-categoryPreference', 'value'),
dash.dependencies.Input('radio-wordPreference', 'value')])
def displayFreqGraph(tabSelection, chosenCategories, preferedCategory, wordPref):
if len(chosenCategories) != 3:
return
secCats = [category for category in chosenCategories if category != preferedCategory]
if len(secCats) == 3: # changing chosenCategories doesnt change preferedCategory (color fix correction)
preferedCategory = secCats.pop()
newLikelihood1 = dict(likelihood[preferedCategory]) # assigning global list into new local one without id reference via list()
colors = [0, 0, 0]
for ith, i in enumerate(chosenCategories):
if i == preferedCategory:
colors[0] = ith
elif i == secCats[0]:
colors[1] = ith
else:
colors[2] = ith
for word in stopWord: # delete stopWords from new likelihood
newLikelihood1.pop(word, None)
sortedValues = sorted(newLikelihood1.items(), key=lambda x: x[1], reverse=True) # save number of word frequencies in sorted order
sortedWords = [word[0] for word in sortedValues[:150]] # save first 150 most frequent words
if wordPref == 'wImp':
newLikelihood2 = {}
newLikelihood3 = {}
n1 = float(sum(likelihood[preferedCategory].values())) # sum of all words
n2 = float(sum(likelihood[secCats[0]].values()))
n3 = float(sum(likelihood[secCats[1]].values()))
for i in sortedValues: # probability calculations
newLikelihood1[i[0]] = (newLikelihood1[i[0]] / n1)
newLikelihood2[i[0]] = (likelihood[secCats[0]][i[0]] / n2)
newLikelihood3[i[0]] = (likelihood[secCats[1]][i[0]] / n3)
tmpData = [{
'x': sortedWords,
'y': [newLikelihood1[x] for x in sortedWords],
'name': preferedCategory,
'marker': { 'color': ['red', 'green', 'blue'][colors[0]]},
'outlinewidth': 10,
'type': ['bar', 'scatter'][int(tabSelection) % 2]
},{
'x': sortedWords,
'y': [newLikelihood2[x] for x in sortedWords],
'name': secCats[0],
'marker': { 'color': ['red', 'green', 'blue'][colors[1]]},
'outlinewidth': 10,
'type': ['bar', 'bar'][int(tabSelection) % 2]
},{
'x': sortedWords,
'y': [newLikelihood3[x] for x in sortedWords],
'name': secCats[1],
'marker': {'color': ['red', 'green', 'blue'][colors[2]]},
'outlinewidth': 10,
'type': ['bar', 'bar'][int(tabSelection) % 2]
}]
else:
tmpData = [{
'x': sortedWords,
'y': [likelihood[preferedCategory][x] for x in sortedWords],
'name': preferedCategory,
'marker': { 'color': ['red', 'green', 'blue'][colors[0]]},
'type': ['bar', 'scatter'][int(tabSelection) % 2]
},{
'x': sortedWords,
'y': [likelihood[secCats[0]][x] for x in sortedWords],
'name': secCats[0],
'marker': { 'color': ['red', 'green', 'blue'][colors[1]]},
'type': ['bar', 'bar'][int(tabSelection) % 2]
},{
'x': sortedWords,
'y': [likelihood[secCats[1]][x] for x in sortedWords],
'name': secCats[1],
'marker': {'color': ['red', 'green', 'blue'][colors[2]]},
'type': ['bar', 'bar'][int(tabSelection) % 2]
}]
return dcc.Graph(
id='graph',
figure={
'data': tmpData,
'layout': {
'margin': {
'l': 30,
'r': 0,
'b': 30,
't': 0,
},
'barmode': ['stack', 'group'][int(tabSelection[-1]) % 2],
'legend': {'x': 0.9, 'y': 0.9},
'xaxis': {'rangeslider': {}},
'plot_bgcolor': 'black',
'paper_bgcolor': 'black',
}
}
),
# displaying exact zero fix rational number
@app.callback( #component_id, component_property
dash.dependencies.Output('div-outputZeroF', 'children'),
[dash.dependencies.Input('rangeSlider-rationalNum', 'value'),
dash.dependencies.Input('radio-zeroFixSelection', 'value')])
def displayZeroRTN(value, radio):
if radio == "RTN":
return 'Value: 1 / {}'.format(round(10 ** value[0]))
else:
return ""
# displaying score metrics table
@app.callback(
dash.dependencies.Output('dataTable-scoreMetrics', 'data'),
[dash.dependencies.Input('intermediate-value', 'children')])
def displayMetrics(intermediate):
data = json.loads(intermediate)
rows = [{
'category_id': category,
'precision_id': data[category]['report'][0],
'recall_id': data[category]['report'][1],
'average_id': data[category]['report'][2],
'f1-score_id': data[category]['report'][3],
'support_id': data[category]['report'][4]} for category in data if category != 'Avg/total' and category != 'accuracy'
]
rows.append({
'category_id': 'Avg/total',
'precision_id': data['Avg/total'][0],
'recall_id': data['Avg/total'][1],
'average_id': data['Avg/total'][2],
'f1-score_id': data['Avg/total'][3],
'support_id': data['Avg/total'][4]
})
return rows
# displaying ternary graph of samples (HARD)
@app.callback(
dash.dependencies.Output('graph-ternarySamples', 'figure'),
[dash.dependencies.Input('intermediate-value', 'children')])
def displaySampleGraph(intermediate):
data = json.loads(intermediate) # data[className]['testSampsProbs'] [sampleName][className][2d-row=cat, col=word, na konci vysl p, minP]
data.pop('Avg/total', None)
data.pop('accuracy', None)
# getting right data format for visualization
vizData = {} # vizData['class(sampleBelonging)] [0(list of samples)] [probs of 3 chosen categories, sampleLabel]
chosenCats = [] # discovering selected categories (no need another input for callback)
minZoom = [33, 33, 33] # min probability values for visualization with zoom
for i in data: # i actual className
chosenCats.append(i)
vizData[i] = []
for jth, j in enumerate(data[i]['testSampsProbs']): # j sample name
vizData[i].append({})
scProbs = [] # sample class probabilities
for k in data[i]['testSampsProbs'][j]: # k predicted className
scProbs.append(data[i]['testSampsProbs'][j][k][-1]) # -1th value is overall probability of class k
percProbs = probMap(scProbs) # mapping log probabilities of sample into percentual probabilities
for kth, k in enumerate(data[i]['testSampsProbs'][j]):
vizData[i][jth][k] = percProbs[kth]
if vizData[i][jth][k] < minZoom[kth]:
minZoom[kth] = vizData[i][jth][k]
vizData[i][jth]['label'] = j
vizData[i][jth]['size'] = len(data[i]['testSamps'][j]) # word count
figure = {
'data': [{
'type': 'scatterternary',
'mode': 'markers',
'a': [i for i in map(lambda x: x[chosenCats[0]], vizData[chosenCats[0]])],
'b': [i for i in map(lambda x: x[chosenCats[1]], vizData[chosenCats[0]])],
'c': [i for i in map(lambda x: x[chosenCats[2]], vizData[chosenCats[0]])],
'text': [i for i in map(lambda x: x['label'] + ", words: " + str(x['size']), vizData[chosenCats[0]])],
'marker': {'color': 'red',},
'name': chosenCats[0],
'customdata': [chosenCats[0] for i in range(0, len(vizData[chosenCats[0]]))]
},{
'type': 'scatterternary',
'mode': 'markers',
'a': [i for i in map(lambda x: x[chosenCats[0]], vizData[chosenCats[1]])],
'b': [i for i in map(lambda x: x[chosenCats[1]], vizData[chosenCats[1]])],
'c': [i for i in map(lambda x: x[chosenCats[2]], vizData[chosenCats[1]])],
'text': [i for i in map(lambda x: x['label'] + ", words: " + str(x['size']), vizData[chosenCats[1]])],
'marker': {'color': 'green',},
'name': chosenCats[1],
'customdata': [chosenCats[1] for i in range(0, len(vizData[chosenCats[1]]))]
},{
'type': 'scatterternary',
'mode': 'markers',
'a': [i for i in map(lambda x: x[chosenCats[0]], vizData[chosenCats[2]])],
'b': [i for i in map(lambda x: x[chosenCats[1]], vizData[chosenCats[2]])],
'c': [i for i in map(lambda x: x[chosenCats[2]], vizData[chosenCats[2]])],
'text': [i for i in map(lambda x: x['label'] + ", words: " + str(x['size']), vizData[chosenCats[2]])],
'marker': {'color': 'blue',},
'name': chosenCats[2],
'customdata': [chosenCats[2] for i in range(0, len(vizData[chosenCats[2]]))]
},{ # probability separation lines
'a': [0, 33],
'b': [50, 33],
'c': [50, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
},{
'a': [50, 33],
'b': [0, 33],
'c': [50, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
},{
'a': [50, 33],
'b': [50, 33],
'c': [0, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
}],
'layout': {
'title': "<b>Probability of samples</b><br>(<b>click</b> on sample to see classification process)",
'height': 600,
'width': 600,
'hoverdistance': 3,
'ternary': {
'sum': 100,
'aaxis': makeAxis(chosenCats[0], minZoom[0]),
'baxis': makeAxis(chosenCats[1], minZoom[1]),
'caxis': makeAxis(chosenCats[2], minZoom[2]),
},
'legend': {'x': 0.65,}, # 'y': 0.9 in SK version!!!!!
'plot_bgcolor': 'black',
'paper_bgcolor': 'black',
'bgcolor': 'black',
}
}
return figure
# sample data sender (HARD)
@app.callback(
dash.dependencies.Output('selected-sample', 'children'),
[dash.dependencies.Input('graph-ternarySamples', 'clickData')],
[dash.dependencies.State('intermediate-value', 'children')])
def getSelectedData(clickData, intermediate):
if not intermediate:
return
data = json.loads(intermediate)
# click -> {'points': [{'curveNumber': 2, 'pointNumber': 18, 'customdata': 'news-Baseball', 'a': 35.965673392081925, 'b': 29.978185948456115, 'c': 34.046140659461955, 'text': '104359'}]}
# wData -> wData[class][0(probList)], last classes are sampleText(list of words), sampleName
# # first value is prior, last is probability sum of logs
wData = data[clickData['points'][0]['customdata']]['testSampsProbs'][re.search(r'\d+', clickData['points'][0]['text']).group()] # probs (search for first number sample id)
wData['sampleText'] = data[clickData['points'][0]['customdata']]['testSamps'][re.search(r'\d+', clickData['points'][0]['text']).group()] # words
wData['sampleName'] = clickData['points'][0]['text']
return json.dumps(wData)
# update rangeslider of words based on selected sample
@app.callback(
dash.dependencies.Output('slider-wordRange', 'value'),
[dash.dependencies.Input('selected-sample', 'children')])
def getWordSum(wData):
if not wData:
return
data = json.loads(wData)
return [0, len(data['sampleText'])]
# rangeslider of words (setting max value because 100 is default)
@app.callback(
dash.dependencies.Output('slider-wordRange', 'max'),
[dash.dependencies.Input('selected-sample', 'children')])
def getWordSum(wData):
if not wData:
return
data = json.loads(wData)
return len(data['sampleText'])
# displaying ternary graph of words
@app.callback(
dash.dependencies.Output('graph-ternaryWords', 'figure'),
[dash.dependencies.Input('selected-sample', 'children'),
dash.dependencies.Input('slider-wordRange', 'value')])
def displayWordGraph(wData, wRange):
data = json.loads(wData) # ['class1', 'class2', 'class3', 'sampleText', 'sampleName']
# getting right data format for visualization
chosenCats = [] # discovering selected categories (no need another input for callback) and class with biggest probability for color setting
minZoom = [33, 33, 33] # min probability values for visualization with zoom
wRawProbs = [] # sum class log values
wPercProbs = [] # wPercProbs[0(prob list of words)][classProb]
for i in data: # changed 2.7... # SK version uses data
chosenCats.append(i) # chosenCats['class1', 'class2', 'class3']
for i in range(1+wRange[0], 1+wRange[1]): # +1 for prior ignore
wPercProbs.append(probMap([data[chosenCats[0]][i], data[chosenCats[1]][i], data[chosenCats[2]][i]])) # first value data[chosenCats[0]][i] is prior, last is probability sum of logs
wRawProbs.append( [data[chosenCats[0]][i], data[chosenCats[1]][i], data[chosenCats[2]][i]])
sumRawProbs = [0, 0, 0]
for i in range(0, len(wRawProbs)):
sumRawProbs[0] += wRawProbs[i][0]
sumRawProbs[1] += wRawProbs[i][1]
sumRawProbs[2] += wRawProbs[i][2]
priorPerc = probMap([data[chosenCats[0]][0], data[chosenCats[1]][0], data[chosenCats[2]][0]], prior = True)
nPriorProb = probMap(list(sumRawProbs))
yPriorProb = probMap([sumRawProbs[0] + data[chosenCats[0]][0],
sumRawProbs[1] + data[chosenCats[1]][0],
sumRawProbs[2] + data[chosenCats[2]][0]])
maxPiC = [-1E6, 0, ''] # prob, index, className
for i in range(0, 3): # finding max probability class for color index and name for legend
if yPriorProb[i] > maxPiC[0]:
maxPiC[0] = yPriorProb[i]
maxPiC[1] = i
maxPiC[2] = chosenCats[i]
for i in range(0, (wRange[1]-wRange[0])): # finding zoom word outliers
for j in range(0, 3):
if wPercProbs[i][j] < minZoom[j]:
minZoom[j] = wPercProbs[i][j]
for i in range(0, 3): # dont miss general predictions out of zoom
if priorPerc[i] < minZoom[i]:
minZoom[i] = priorPerc[i]
if nPriorProb[i] < minZoom[i]:
minZoom[i] = nPriorProb[i]
if yPriorProb[i] < minZoom[i]:
minZoom[i] = yPriorProb[i]
for ith, i in enumerate(data['sampleText'][wRange[0]:wRange[1]]): # word ordering in sample
data['sampleText'][ith+wRange[0]] = (str(wRange[0]+ith+1) + ". " + i)
figure = {
'data': [{
'type': 'scatterternary',
'mode': 'markers',
'a': [i for i in map(lambda x: x[0], wPercProbs)],
'b': [i for i in map(lambda x: x[1], wPercProbs)],
'c': [i for i in map(lambda x: x[2], wPercProbs)],
'text': [i for i in map(lambda x: x, data['sampleText'][wRange[0]:wRange[1]])],
'marker': {'color': ['red', 'green', 'blue'][maxPiC[1]]},
'name': maxPiC[2],
},{
'type': 'scatterternary',
'mode': 'markers',
'a': [priorPerc[0]],
'b': [priorPerc[1]],
'c': [priorPerc[2]],
'text': 'priors probability',
'marker': {
'color': 'black',
'size': 15,
'opacity': 0.5
},
'name': 'priors probability',
},{
'type': 'scatterternary',
'mode': 'markers',
'a': [nPriorProb[0]],
'b': [nPriorProb[1]],
'c': [nPriorProb[2]],
'text': 'resulting prob. without priors',
'marker': {
'color': 'orange',
'size': 15,
'opacity': 0.5
},
'name': 'resulting prob. without priors',
},{
'type': 'scatterternary',
'mode': 'markers',
'a': [yPriorProb[0]],
'b': [yPriorProb[1]],
'c': [yPriorProb[2]],
'text': 'resulting prob. with priors',
'marker': {
'color': 'purple',
'size': 15,
'opacity': 0.5
},
'name': 'probability with priors',
},{ # probability separation lines
'a': [0, 33],
'b': [50, 33],
'c': [50, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
},{
'a': [50, 33],
'b': [0, 33],
'c': [50, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
},{
'a': [50, 33],
'b': [50, 33],
'c': [0, 33],
'fillcolor': "#bebada",
'line': {"color": "black"},
'mode': 'lines',
'type': 'scatterternary',
'showlegend': False
}],
'layout': {
'title': "<b>Probability of words</b><br>" + "Sample name: " + data['sampleName'],
'height': 600,
'width': 600,
'hoverdistance': 3,
'ternary': {
'sum': 100,
'aaxis': makeAxis(chosenCats[0], minZoom[0]),
'baxis': makeAxis(chosenCats[1], minZoom[1]),
'caxis': makeAxis(chosenCats[2], minZoom[2])
},
'legend': {'x': 0.65}, # 'y': 0.9 in SK version
'plot_bgcolor': 'black',
'paper_bgcolor': 'black',
}
}
return figure
# displaying range selection of words
@app.callback(
dash.dependencies.Output('div-wordRange', 'children'),
[dash.dependencies.Input('slider-wordRange', 'value')])
def displayRangePerc(wRange):
if not wRange:
return
wRange[0] += 1
return 'Words selection {}'.format(wRange)
# display text according to range selection
@app.callback(
dash.dependencies.Output('div-sampleText', 'children'),
[dash.dependencies.Input('slider-wordRange', 'value'),
dash.dependencies.Input('selected-sample', 'children')])
def displayText(wRange, wData):
if not wData:
return
data = json.loads(wData)
wString = ''
for i in range(wRange[0], wRange[1]):
wString += data['sampleText'][i] + " "
return wString
# display probability calculation process with selected words
@app.callback(
dash.dependencies.Output('graph-process', 'figure'),
[dash.dependencies.Input('slider-wordRange', 'value'),
dash.dependencies.Input('selected-sample', 'children'),
dash.dependencies.Input('slider-chunkSum', 'value')])
def displayProcess(wRange, wData, chunk):
data = json.loads(wData) # ['class1', 'class2', 'class3', 'sampleText', 'sampleName']
chosenCats = [] # discovering selected categories (no need another input for callback) and class with biggest probability for color setting
wRawProbs = [] # sum class probabilities for visualization
wPercProbs = [] # wPercProbs[0(prob list of words)][classProb]
wSumProbs = []
dotSums = []
dotWords = []
for i in data:
chosenCats.append(i) # chosenCats['class1', 'class2', 'class3']
sumRawProbs = np.array([.0, .0, .0])
for ith, i in enumerate(range(1+wRange[0], 1+wRange[1])): # +1 for prior ignore
wPercProbs.append(probMap([data[chosenCats[0]][i], data[chosenCats[1]][i], data[chosenCats[2]][i]])) # first value data[chosenCats[0]][i] is prior, last is probability sum of logs
wRawProbs.append( [data[chosenCats[0]][i], data[chosenCats[1]][i], data[chosenCats[2]][i]])
sumRawProbs += wRawProbs[ith]
wSumProbs.append(probMap(np.copy(sumRawProbs)))
if ith % chunk == 0 and ith != 0: # we dont want first dot!
dotSums.append(wSumProbs[ith]) # need to save for big dots
dotWords.append(str(i) + ". " + data['sampleText'][i-1]) # saving words (x coords)
sumRawProbs = np.array([.0, .0, .0])
for ith, i in enumerate(data['sampleText']):
data['sampleText'][ith] = (str(ith+1) + ". " + i)
# need index, value
maxPiC = []
for cValues in dotSums:
maxVal = 0
for ith, value in enumerate(cValues):
if value > maxVal: