aokitools commited on
Commit
e0c7f01
·
1 Parent(s): 54f47d8
Files changed (1) hide show
  1. app.py +50 -6
app.py CHANGED
@@ -545,7 +545,7 @@ def f_googlesentenceencoder(e_text1, e_checkbox1):
545
  pass
546
  # // ---------------------------------------------- // #
547
 
548
- def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
549
  result = {}
550
  result["output1"] = e_text1
551
  result["output2"] = None
@@ -567,6 +567,7 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
567
 
568
  list_vectors = []
569
  list_labels = []
 
570
 
571
  list1 = []
572
  index1 = 0
@@ -601,6 +602,19 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
601
  s2 = " ".join(ss1[1:])
602
  pass
603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
604
  import io
605
  buf = io.StringIO(s2)
606
 
@@ -609,6 +623,7 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
609
 
610
  list_labels.append(s1)
611
  list_vectors.append(l1)
 
612
 
613
  index1 += 1
614
  pass
@@ -639,6 +654,18 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
639
  ).fit_transform(X)
640
  pass
641
 
 
 
 
 
 
 
 
 
 
 
 
 
642
  list_vectors2 = []
643
 
644
  if n_dimension == 3:
@@ -667,8 +694,6 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
667
  x = tsne[:,0]
668
  y = tsne[:,1]
669
 
670
-
671
-
672
  l1 = []
673
  for i1 in range(len(x)):
674
  s1 = "{0}\t{1:.3f}\t{2:.3f}".format(list_labels[i1], x[i1], y[i1])
@@ -760,11 +785,16 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
760
  )
761
  pass
762
  else:
 
 
 
 
 
763
  trace1 = go.Scatter(
764
  x=tsne[:,0],
765
  y=tsne[:,1],
766
  text=list_labels,
767
- mode="markers",
768
  marker=dict(
769
  sizemode="diameter",
770
  color = preprocessing.LabelEncoder().fit_transform(clusters),
@@ -774,9 +804,22 @@ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
774
  size=10
775
  )
776
  )
 
777
  pass
 
778
  data=[trace1]
779
- fig = go.Figure(data=data)
 
 
 
 
 
 
 
 
 
 
 
780
  result["output2"] = fig
781
  pass
782
  except Exception as e:
@@ -976,6 +1019,7 @@ with gr.Blocks() as sample01:
976
 
977
  e_text1 = gr.TextArea(s1, show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
978
  e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
 
979
  e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
980
  e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
981
 
@@ -986,7 +1030,7 @@ with gr.Blocks() as sample01:
986
  e_text2 = gr.TextArea(show_label=True, label="次元削減後のベクトル")
987
  e_text3 = gr.TextArea(show_label=True, label="クラスタリング結果")
988
 
989
- e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2], outputs=[e_text1, e_plot1, e_text2, e_text3, e_plot2])
990
  pass
991
 
992
 
 
545
  pass
546
  # // ---------------------------------------------- // #
547
 
548
+ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2, e_checkbox2):
549
  result = {}
550
  result["output1"] = e_text1
551
  result["output2"] = None
 
567
 
568
  list_vectors = []
569
  list_labels = []
570
+ list_annotations = []
571
 
572
  list1 = []
573
  index1 = 0
 
602
  s2 = " ".join(ss1[1:])
603
  pass
604
 
605
+ d2 = {
606
+ "showarrow": False,
607
+ "x": 0,
608
+ "y": 0,
609
+ "z": 0,
610
+ "text": s1,
611
+ "xanchor": "left",
612
+ "xshift": 10,
613
+ "opacity": 0.7
614
+
615
+ }
616
+
617
+
618
  import io
619
  buf = io.StringIO(s2)
620
 
 
623
 
624
  list_labels.append(s1)
625
  list_vectors.append(l1)
626
+ list_annotations.append(d2)
627
 
628
  index1 += 1
629
  pass
 
654
  ).fit_transform(X)
655
  pass
656
 
657
+
658
+ index1 = 0
659
+ for i in tsne:
660
+ list_annotations[index1]["x"] = i[0]
661
+ list_annotations[index1]["y"] = i[1]
662
+ if len(i) == 3:
663
+ list_annotations[index1]["z"] = i[2]
664
+ pass
665
+ index1 += 1
666
+ pass
667
+
668
+
669
  list_vectors2 = []
670
 
671
  if n_dimension == 3:
 
694
  x = tsne[:,0]
695
  y = tsne[:,1]
696
 
 
 
697
  l1 = []
698
  for i1 in range(len(x)):
699
  s1 = "{0}\t{1:.3f}\t{2:.3f}".format(list_labels[i1], x[i1], y[i1])
 
785
  )
786
  pass
787
  else:
788
+ mode = "markers"
789
+ if e_checkbox2:
790
+ mode = "markers+text"
791
+ pass
792
+
793
  trace1 = go.Scatter(
794
  x=tsne[:,0],
795
  y=tsne[:,1],
796
  text=list_labels,
797
+ mode=mode,
798
  marker=dict(
799
  sizemode="diameter",
800
  color = preprocessing.LabelEncoder().fit_transform(clusters),
 
804
  size=10
805
  )
806
  )
807
+
808
  pass
809
+
810
  data=[trace1]
811
+ layout=None
812
+
813
+ if e_checkbox2 and n_dimension == 3:
814
+ layout={
815
+ "scene": {
816
+ "annotations": list_annotations
817
+ }
818
+ }
819
+ pass
820
+
821
+ fig = go.Figure(data=data, layout=layout)
822
+
823
  result["output2"] = fig
824
  pass
825
  except Exception as e:
 
1019
 
1020
  e_text1 = gr.TextArea(s1, show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
1021
  e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
1022
+ e_checkbox2 = gr.Checkbox(label="ラベルをプロットする")
1023
  e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
1024
  e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
1025
 
 
1030
  e_text2 = gr.TextArea(show_label=True, label="次元削減後のベクトル")
1031
  e_text3 = gr.TextArea(show_label=True, label="クラスタリング結果")
1032
 
1033
+ e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2, e_checkbox2], outputs=[e_text1, e_plot1, e_text2, e_text3, e_plot2])
1034
  pass
1035
 
1036