-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_topic.m
More file actions
executable file
·257 lines (235 loc) · 8.41 KB
/
Copy pathsub_topic.m
File metadata and controls
executable file
·257 lines (235 loc) · 8.41 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
%% running standard nmf of subset
% function [mappedX_sub, cl_idx_sub, Wtopk_idx_sub] = sub_topic(idx, cl_idx)
addpath('./library/nmf');
addpath('./library/ramkis');
addpath('./library/peripheral');
addpath('./library/discnmf');
addpath('./library/tSNE_matlab');
%% importing term-doc matrix
% in_txt_dir = 'text_files';
% out_fname = 'out.txt';
%
% is_stemming = 1;
%
% % merge multiple text files in a particular directory to a single file
% %[raw_txt,fnames] = merge_to_single_text(in_txt_dir,out_fname);
%
% if ~exist('tmp1','dir')
% mkdir('tmp1');
% end
%
% delete(['tmp1' filesep '*.*']);
%
% % run python function to generate a term-document matrix
% load('vis_paper_ver201508.mat');
% fid = fopen('out.txt', 'w');
% for i=1:size(PaperTitle,1)
% fprintf(fid, '%s %s\n', PaperTitle{i}, Abstract{i});
% end
% fclose(fid);
% dos(sprintf('python txt2mtx_fast.py %s',out_fname));
%
% % importing dictionary and term-document matrix
% dict = import_dictionary(['tmp1' filesep 'vocabulary.txt']);
% tdm = import_tdm(['tmp1' filesep 'tmp.mtx']);
% A = sparse(tdm(:,1),tdm(:,2),tdm(:,3),max(tdm(:,1)),max(tdm(:,2)));
% clear tdm;
%
% if is_stemming
% tmp = cell(size(dict));
% tmp{i}=cell(size(dict));
% for i=1:length(dict)
% tmp{i} = porterStemmer(dict{i});
% end
% [dict_stemmed,ia,ic] = unique(tmp);
% A_stemmed = sparse([],[],[],length(dict_stemmed),size(A,2));
% A_stemmed = A_stemmed';
% A_transpose = A';
% for i=1:length(dict_stemmed)
% A_stemmed(:,i) = sum(A_transpose(:,ic==i),2);
% end
% A_stemmed = A_stemmed';
%
% A = A_stemmed;
% dict = dict_stemmed;
% clear A_transpose A_stemmed dict_stemmed;
% end
%
% % [A w] = tfidf2(A);
%
% %% additional stopwords
% addl_stopwords = {'visualization','visual','information','analysis','analysi','data','approach','process','based','technique','techniques','paper'};
%
% idxs =[];
%
% for i=1:length(addl_stopwords)
% idxs = [idxs find(strcmp(dict,addl_stopwords{i}))];
% end
%
% idxs = setdiff(1:size(A,1),idxs);
% A = A(idxs,:);
% dict = dict(idxs);
%% sub_A set
%% load tdm;
if iscell(idx)
idx = cell2mat(idx);
end
A_sub = A(:,idx);
cluster_idx=unique(cl_idx(idx));
% disp(cluster_idx);
sub_k=length(cluster_idx);
% no of topics
k_sub = max(min([floor(length(idx)/20) 5]), sub_k);
if (k_sub<2)
k_sub = 2;
end
% no of top keywords
topk_sub = 5;
% normalization
A_norm_sub = bsxfun(@rdivide,A_sub,sqrt(sum(A_sub.^2)));
% choosing one among different preprocessings
target_A_sub = A_norm_sub; % replaced by below code (9/10) <- original
% target_A = A_norm;
%% hier8_neat1 (initial run)
cnt = 0;
if(cnt == 0)
if ~exist('params', 'var')
trial_allowance = 3;
unbalanced = 0.1;
vec_norm = 2.0;
normW = true;
anls_alg = @anls_entry_rank2_precompute;
tol = 1e-4;
maxiter = 10000;
else
if isfield(params, 'trial_allowance')
trial_allowance = params.trial_allowance;
else
trial_allowance = 3;
end
if isfield(params, 'unbalanced')
unbalanced = params.unbalanced;
else
unbalanced = 0.1;
end
if isfield(params, 'vec_norm')
vec_norm = params.vec_norm;
else
vec_norm = 2.0;
end
if isfield(params, 'normW')
normW = params.normW;
else
normW = true;
end
if isfield(params, 'anls_alg')
anls_alg = params.anls_alg;
else
anls_alg = @anls_entry_rank2_precompute;
end
if isfield(params, 'tol')
tol = params.tol;
else
tol = 1e-4;
end
if isfield(params, 'maxiter')
maxiter = params.maxiter;
else
maxiter = 10000;
end
end
params = [];
params.vec_norm = vec_norm;
params.normW = normW;
params.anls_alg = anls_alg;
params.tol = tol;
params.maxiter = maxiter;
[m, n] = size(target_A_sub);
cluster_idx=unique(cl_idx(idx)); %
sub_k=length(cluster_idx); % number of cluster selected
ctrary = zeros(size(target_A_sub,1),sub_k); %
timings_sub = zeros(1, k_sub-sub_k);
clusters_sub = cell(1, 2*(k_sub-sub_k));
Ws_sub = cell(1, 2*(k_sub-sub_k));
W_buffer = cell(1, 2*(k_sub-sub_k));
H_buffer = cell(1, 2*(k_sub-sub_k));
priorities_sub = zeros(1, 2*(k_sub-sub_k));
is_leaf_sub = -1 * ones(1, 2*(k_sub-sub_k));
tree_sub = zeros(2, 2*(k_sub-sub_k));
splits_sub = -1 * ones(1, k_sub-sub_k);
min_priority = 1e308;
for i=1:sub_k
subidx = find(cl_idx(idx)==cluster_idx(i));
ctrary(:,i) = mean(target_A_sub(:, subidx),2);
Ws_sub{i} = ctrary(:,i);
is_leaf_sub(i) = 1;
[~, W_buffer_one, H_buffer_one, priority_one]...
= trial_split(trial_allowance, unbalanced, min_priority, target_A_sub, subidx, Ws_sub{i}, params);
clusters_sub{i} = subidx;
W_buffer{i} = W_buffer_one;
H_buffer{i} = H_buffer_one;
priorities_sub(i) = priority_one;
end
cnt = 1;
result_used = sub_k;
end
%% hier8_neat2 (run interatively)
if k_sub == sub_k
W_sub = cell2mat(Ws_sub(find(is_leaf_sub)));
[H_sub,temp,suc_H,numChol_H,numEq_H] = nnlsm_blockpivot(W_sub'*W_sub,W_sub'*target_A_sub,1,rand(k_sub,size(target_A_sub,2)));
% else k_sub > sub_k
% for i = 1:k_sub-sub_k
% leaves = find(is_leaf_sub == 1);
% temp_priority = priorities_sub(leaves);
% min_priority = min(temp_priority(temp_priority > 0));
% [max_priority, split_node] = max(temp_priority);
% if max_priority < 0
% fprintf('Cannot generate all %d leaf clusters_sub\n', k);
% return;
% end
% split_node = leaves(split_node);
% is_leaf_sub(split_node) = 0;
% W_sub = W_buffer{split_node};
% H_sub = H_buffer{split_node};
% split_subset = clusters_sub{split_node};
% new_nodes = [result_used+1 result_used+2];
% tree_sub(1, split_node) = new_nodes(1);
% tree_sub(2, split_node) = new_nodes(2);
% result_used = result_used + 2;
% [max_val, cluster_subset] = max(H_sub);
% clusters_sub{new_nodes(1)} = split_subset(cluster_subset == 1);
% clusters_sub{new_nodes(2)} = split_subset(cluster_subset == 2);
% Ws_sub{new_nodes(1)} = W_sub(:, 1);
% Ws_sub{new_nodes(2)} = W_sub(:, 2);
% splits_sub(i) = split_node;
% is_leaf_sub(new_nodes) = 1;
% subset = clusters_sub{new_nodes(1)};
% [subset, W_buffer_one, H_buffer_one, priority_one] = trial_split(trial_allowance, unbalanced, min_priority, target_A_sub, subset, W_sub(:, 1), params);
% clusters_sub{new_nodes(1)} = subset;
% W_buffer{new_nodes(1)} = W_buffer_one;
% H_buffer{new_nodes(1)} = H_buffer_one;
% priorities_sub(new_nodes(1)) = priority_one;
% subset = clusters_sub{new_nodes(2)};
% [subset, W_buffer_one, H_buffer_one, priority_one] = trial_split(trial_allowance, unbalanced, min_priority, target_A_sub, subset, W_sub(:, 2), params);
% clusters_sub{new_nodes(2)} = subset;
% W_buffer{new_nodes(2)} = W_buffer_one;
% H_buffer{new_nodes(2)} = H_buffer_one;
% priorities_sub(new_nodes(2)) = priority_one;
% W_sub = cell2mat(Ws_sub(find(is_leaf_sub)));
% [H_sub,temp,suc_H,numChol_H,numEq_H] = nnlsm_blockpivot(W_sub'*W_sub,W_sub'*target_A_sub,1,rand(i+sub_k,size(target_A_sub,2)));
% end
end
% displaying top keywords for each topic
% [Wtopk_sub,Htopk_sub,DocTopk_sub,Wtopk_idx_sub] = parsenmf(W_sub,H_sub,dict,topk_sub);
% Wtopk_idx_sub = Wtopk_idx_sub';
% [~,cl_idx_sub] = max(H_sub);
%% t-sne visualization
% Run t-SNE
% no_dims=2;
% initial_dims_sub=min([30, size(A_sub,2)]);
% shrink_factor = .3;
% perplexity = 30;
% mappedX_sub = tsne_sup(target_A_sub', cl_idx_sub, shrink_factor, no_dims, initial_dims_sub, perplexity);
% Run t?SNE
% mappedX = tsne(target_A', cl_idx, no_dims, initial_dims, perplexity);
%mappedX_sub = tsne(target_A_sub', cl_idx_sub, no_dims, initial_dims, perplexity);