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 | import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import * as d3 from "d3"
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
//mount Vuex
Vue.use(Vuex)
//create VueX
const store = new Vuex.Store({
state:{
// server_url: 'http://localhost:20211',
server_url: 'http://thuvis.org:20211',
// action trail
history: [],
image_num: 0,
step: 0,
name_edit_history: {},
label_consistency: 0,
symmetrical_consistency: 0,
current_id: 0,
word_tsne: [],
tree: {},
use_treecut: true,
image_cluster_list: [],
fetched_image_result: [],
mismatch: [],
vis_image_per_cluster: {},
expand_tree: true,
expand_set_id: -1,
grid_data: [],
grid_image_info: [],
nav_id: [],
one_image_boxes_threshold: 0.5,
label_layout_mode: null,
cluster_association_mat: [],
focus_node: null,
selected_node: {
node_ids: [],
nodes: [],
curr_full_name: ''
},
all_sets: [],
words: [],
selected_images: [],
focus_word: null,
focus_image: null,
text_list: [],
focus_text: null,
image_list: [],
selected_flag: [],
f1_score_selected: false,
tooltip: {
top: 0,
left: 0,
show: false,
width: 0,
content: '',
},
word_cloud_recycled: {
},
classNames:[]
},
getters: {
label_consistency: (state) => {
return state.label_consistency;
},
symmetrical_consistency: (state) => {
return state.symmetrical_consistency;
}
},
mutations:{
edit(state){ // for DEBUG
state.name = "hello 2";
},
set_name_edit_history(state, data){
state.name_edit_history[data.id] = data.name;
},
set_manifest_data(state, manifest_data){
console.log("set manifest data");
state.step = manifest_data.real_step;
state.image_num = manifest_data.image_num;
state.classNames = manifest_data.class_name;
state.label_consistency = manifest_data.label_consistency;
state.symmetrical_consistency = manifest_data.symmetrical_consistency;
},
set_selected_flag(state, tree){
console.log("set tree");
state.tree = tree;
state.selected_flag = state.tree.all_descendants.map(d => d.selected_flag);
},
set_hypergraph_data(state, hypergraph_data){
console.log("set hypergraph data");
console.log("hypergraph_data", hypergraph_data);
console.log("state", state);
this.commit("set_word_tsne", hypergraph_data.word_tsne);
this.commit("set_text_tree_data", hypergraph_data.text_tree);
this.commit("set_image_cluster_list_data", hypergraph_data.image_cluster_list);
this.commit("set_cluster_association_mat", hypergraph_data.cluster_association_matrix);
this.commit("set_mismatch", hypergraph_data.mismatch);
this.commit("set_vis_image_per_cluster", hypergraph_data.vis_image_per_cluster);
},
set_word_tsne(state, word_tsne){
state.word_tsne = word_tsne;
},
set_text_tree_data(state, text_tree){
// process tree
state.tree = d3.hierarchy(text_tree,
function(d){
let children = d.children;
return children
});
state.tree.eachAfter(element => {
element.id = element.data.id;
element.full_name = element.data.name;
element.name = element.full_name;
// // combining the names of all children as the name of their father
// if (element.children && element.name !== "root"){
// element.name = element.children.map(d=>d.name + " ").join("");
// }
if (state.name_edit_history[element.id]){
element.full_name = state.name_edit_history[element.id];
element.name = element.full_name;
}
// all_children: all children
// children: children that are visible
// _children: children that are invisible
element.all_children = element.children;
if(element.children) element.children.forEach((d,i) => d.siblings_id = i);
element._children = [];
element._total_width = 0;
if (!element.data.precision){
let s = element.all_children.map(d=>d.data.precision);
if (s) element.data.precision = s.reduce((a,c)=>{return a+c}, 0) / s.length;
}
if (!element.data.recall){
let s = element.all_children.map(d=>d.data.recall);
if (s) element.data.recall = s.reduce((a,c)=>{return a+c}, 0) / s.length;
}
if (!element.data.mismatch){
let s = element.all_children.map(d=>d.data.mismatch);
if (s) element.data.mismatch = s.reduce((a,c)=>{return a+c}, 0);
}
if (!element.data.descendants_idx){
if (!element.all_children){
element.data.descendants_idx = [element.data.id];
}
else{
let idxs = [];
for (let i = 0; i < element.all_children.length; i++){
idxs = idxs.concat(element.all_children[i].data.descendants_idx);
}
element.data.descendants_idx = idxs;
}
}
element.f1_api = 2 - (element.data.precision * 2 + element.data.recall) / 2;
element.mm_api = element.data.mismatch / 20000;
element.api = state.f1_score_selected ? element.f1_api : element.mm_api;
// element.api = element.mm_api;
});
state.tree.eachBefore((d, i) => d.order = i);
state.tree.all_descendants = state.tree.descendants();
state.tree.all_descendants.forEach(d => d.children = []);
console.log("api", state.tree.all_descendants.map(d => d.api));
console.log("api", state.tree.all_descendants.map(d => d.mm_api));
// let temp_history = {};
// state.tree.all_descendants.filter(d => d.all_children)
// .forEach(d => {
// temp_history[d.id] = d.name;
// })
// state.name_edit_history = temp_history;
console.log("state.tree", state.tree)
},
set_image_cluster_list_data(state, image_cluster_list){
state.image_cluster_list = image_cluster_list;
state.image_cluster_list.forEach(d => d.collapse = 0);
// state.image_cluster_list[1].collapse = 1;
console.log("state.image_cluser_list", state.image_cluster_list);
},
set_cluster_association_mat(state, cluster_association_mat){
state.cluster_association_mat = cluster_association_mat;
console.log("cluster_association_mat:", state.cluster_association_mat);
},
set_mismatch(state, mismatch){
state.mismatch = mismatch;
console.log("cluster_association_mat:", state.mismatch);
},
set_history_data(state, history_data) {
console.log("set history data");
state.history = history_data;
},
set_focus_node(state, nodes) {
console.log("set focus node");
state.focus_node = nodes;
},
set_focus_image(state, image){
console.log("set focus image");
state.focus_image = image;
console.log(image);
},
set_selected_node(state, node) {
console.log("set selected node");
let index = state.selected_node.node_ids.indexOf(node.id);
if (index === -1) {
state.selected_node.node_ids.push(node.id);
state.selected_node.nodes.push(node);
d3.selectAll(`#id-${node.id}`)
.selectAll(".background").style('stroke', "#ffa953")
.style("stroke-width", 2);
}
else {
d3.selectAll(`#id-${node.id}`).style('stroke', '');
state.selected_node.node_ids.splice(index, 1);
state.selected_node.nodes.splice(index, 1);
}
let new_full_names = [];
state.selected_node.nodes.forEach(node=>{
new_full_names.push(node.full_name);
});
state.selected_node.curr_full_name = new_full_names.join('&');
},
set_expand_tree(state, node){
console.log("set expand tree");
state.expand_tree = node;
},
set_expand_set_id(state, id){
console.log("set expand set id", id);
state.expand_set_id = id;
},
set_words(state, words){
console.log("set words");
state.words = words.map(d => {
let res = {};
res.text = d[0];
res.value = d[1];
// res.id = element.data.id;
return res;
});
state.words = state.words.slice(0, 20);
},
set_selected_images(state, images){
console.log("set images");
state.selected_images = images.map((d, i) => {
let res = {};
res.id = d.idx;
res.index = i;
res.data = d;
return res;
});
// state.images = state.images.slice(0, 20);
},
set_focus_word(state, word){
console.log("set focus word");
state.focus_word = word;
},
set_text_list(state, text_list){
console.log("set text list");
state.text_list = text_list;
},
set_focus_text(state, text){
console.log("set focus text");
state.focus_text = text;
},
set_vis_image_per_cluster(state, res){
console.log("set_vis_image_per_cluster", res);
for(let i in res){
state.vis_image_per_cluster[i] = res[i];
}
},
set_use_treecut(state, use_treecut){
state.use_treecut = use_treecut;
},
set_f1_score_selected(state, f1_score_selected){
state.f1_score_selected = f1_score_selected;
},
set_grid_layout_data(state, data){
console.log("set grid layout data", data);
state.grid_data = data.layout;
state.grid_image_info = data.image_info;
state.nav_id = data.id;
state.grid_data.forEach((d, i) => {
d.d = state.grid_image_info[i].d;
})
},
set_one_image_boxes_threshold(state, data){
console.log("set one_image_boxes_threshold", data);
state.one_image_boxes_threshold = data - 0.2;
},
showTooltip(state, { top, left, width, content }) {
state.tooltip.top = top
state.tooltip.left = left
state.tooltip.content = content
state.tooltip.show = true
state.tooltip.width = width
},
hideTooltip(state) {
state.tooltip.show = false
}
},
actions:{
async fetch_manifest({commit, state}, key){
console.log("fetch_manifest");
const resp = await axios.post(`${state.server_url}/detection/GetManifest`, key,
{headers: {
"Content-Type":"application/json",
"Access-Control-Allow-Origin": "*",
}});
console.log("get manifest", resp);
commit("set_manifest_data", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_hypergraph({commit, state}, key){
console.log("fetch_hypergraph");
const resp = await axios.post(`${state.server_url}/detection/HyperGraph`, {word: key},
{headers:
{"Access-Control-Allow-Origin": "*"}});
commit("set_hypergraph_data", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_history({ commit, state }, key){
console.log("fetch_history");
const resp = await axios.post(`${state.server_url}/history/GetHistory`, {word: key}, {headers: {"Access-Control-Allow-Origin": "*"}});
// console.log(resp);
commit("set_history_data", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_text_by_word_and_cat_ids({commit, state}, key){
console.log("fetch_text", key);
let query = {
"cat_id": key.cat_id,
"word": key.text,
"rules": key.rules
}
const resp = await axios.post(`${state.server_url}/text/GetTextByWord`, {query}, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_text_list", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_text_by_ids({commit, state}, ids){
console.log("fetch_text_by_ids", ids);
let query = {
"ids": ids,
}
const resp = await axios.post(`${state.server_url}/text/GetText`, {query}, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_text_list", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_word({commit, state}){
console.log("fetch_word");
let query = {
tree_node_ids: state.selected_node.node_ids,
match_type: "p",
};
const resp = await axios.post(`${state.server_url}/text/GetWord`, {query}, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_words", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_image({commit, state}){
console.log("fetch_image");
let query = {
tree_node_ids: state.selected_node.node_ids,
match_type: "p",
};
const resp = await axios.post(`${state.server_url}/text/GetImage`, {query}, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_selected_images", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_images({commit, state}, image_cluster_ids){
console.log("fetch_images", image_cluster_ids);
const resp = await axios.post(`${state.server_url}/detection/Rank`, image_cluster_ids, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_vis_image_per_cluster", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_single_image_detection({commit, state}, query){
console.log("fetch_single_image_detection", query);
const resp = await axios.post(`${state.server_url}/image/SingleImageDetection`, query, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_focus_image", JSON.parse(JSON.stringify(resp.data)));
},
async fetch_single_image_detection_for_focus_text({commit, state}, query){
console.log("fetch_single_image_detection_for_focus_text", query);
if (state.fetched_image_result[query.image_id] !== undefined){
console.log("use buffer");
}
else{
query.conf = 0.1;
const resp = await axios.post(`${state.server_url}/image/SingleImageDetection`, query, {headers: {"Access-Control-Allow-Origin": "*"}});
state.fetched_image_result[query.image_id] = JSON.parse(JSON.stringify(resp.data));
}
commit("set_focus_text", state.fetched_image_result[query.image_id]);
},
async fetch_grid_layout({commit, state}, query){
const resp = await axios.post(`${state.server_url}/detection/GridLayout`, query, {headers: {"Access-Control-Allow-Origin": "*"}});
commit("set_grid_layout_data", JSON.parse(JSON.stringify(resp.data)));
}
},
// computed: {
// selected_flag(){
// return this.$store.state.tree.all_descendants.map(d => !! d.selected_flag);
// }
// },
modules:{
// empty
}
})
export default store
|