-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.js
177 lines (146 loc) · 4.49 KB
/
view.js
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
console.log("view.js");
window.onload = async () => {
const start_button = document.getElementById("start_button");
start_button.onclick = async () => {
const loader_msg = document.getElementById("loader_msg");
loader_msg.innerText = "Loading Pyodide";
const pyodide = await load_pyodide();
await launch_app(pyodide);
start_button.remove();
}
}
async function launch_app(pyodide) {
// スライダー0の設定
const index_slider_x = document.getElementById("index0");
const index_res_x = document.getElementById("slider_res_0");
index_slider_x.disabled = false;
index_slider_x.oninput = () => {
index_res_x.innerText = index_slider_x.value;
plotData(
pyodide,
index_res_x.innerText,
index_res_y.innerText);
};
//スライダー1の設定
const index_slider_y = document.getElementById("index1");
const index_res_y = document.getElementById("slider_res_1");
index_slider_y.disabled = false;
index_slider_y.oninput = () => {
index_res_y.innerText = index_slider_y.value;
plotData(
pyodide,
index_res_x.innerText,
index_res_y.innerText);
};
// t-SNEボタンの設定
const tSNE_button = document.getElementById("tsne_button");
tSNE_button.onclick = () => {
plotTSNE(pyodide);
}
// ローダーの削除
const loader = document.getElementById("loader_wrap");
console.log(loader);
loader.remove();
// python実行
pyodide.runPython(`
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.manifold import TSNE
all_data = load_breast_cancer()
`);
document.getElementById("warning_area").remove();
plotData(
pyodide,
index_res_x.innerText,
index_res_y.innerText);
}
async function load_pyodide() {
const loader_instance = document.getElementById("loader_instance");
loader_instance.className = "loader";
const pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/",
});
const loader_msg = document.getElementById("loader_msg");
loader_msg.innerText = "Loading NumPy";
await pyodide.loadPackage("numpy");
loader_msg.innerText = "Loading Scikit-learn";
await pyodide.loadPackage("scikit-learn");
loader_msg.innerText = "pyodide is ready";
return pyodide;
}
async function plotData(pyodide, index_x, index_y) {
pyodide.runPython(`
xx = all_data.data[all_data.target == 0]
print(xx.shape)
xx_neg = all_data.data[all_data.target == 0][:,${index_x}].T
xx_pos = all_data.data[all_data.target == 1][:,${index_x}].T
yy_neg = all_data.data[all_data.target == 0][:,${index_y}].T
yy_pos = all_data.data[all_data.target == 1][:,${index_y}].T
`);
const xx_neg = pyodide.globals.get("xx_neg").toJs();
const xx_pos = pyodide.globals.get("xx_pos").toJs();
const yy_neg = pyodide.globals.get("yy_neg").toJs();
const yy_pos = pyodide.globals.get("yy_pos").toJs();
Plotly.newPlot("graph_area", [{
name: "Negative",
x: xx_neg,
y: yy_neg,
mode: "markers",
type: "scatter",
marker: {
color: "blue",
}
},
{
name: "Positive",
x: xx_pos,
y: yy_pos,
mode: "markers",
type: "scatter",
marker: {
color: "red",
}
}]);
}
async function plotTSNE(pyodide) {
pyodide.runPython(`
tsne = TSNE(n_components=2,learning_rate=50)
x_embed = tsne.fit_transform(all_data.data)
xx_neg = x_embed[all_data.target == 0].T
xx_pos = x_embed[all_data.target == 1].T
`);
const xx_neg = pyodide.globals.get("xx_neg").toJs();
const xx_pos = pyodide.globals.get("xx_pos").toJs();
console.log(xx_neg);
Plotly.newPlot("graph_area", [{
name: "Negative",
x: xx_neg[0],
y: xx_neg[1],
mode: "markers",
type: "scatter",
marker: {
color: "blue",
}
},
{
name: "Positive",
x: xx_pos[0],
y: xx_pos[1],
mode: "markers",
type: "scatter",
marker: {
color: "red",
}
}]);
}
// まずばbreast_cancer_dataset
async function test_plot_data() {
const xx = [1, 2, 3, 4, 5];
const yy = [2, 10, 15, 10, 21];
Plotly.newPlot("graph_area", [{
x: xx,
y: yy,
mode: "markers",
type: "scatter",
}]);
}