-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots_results.m
executable file
·323 lines (269 loc) · 8.54 KB
/
plots_results.m
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
function [result_f] = plots_results( dir, varargin )
%% maximum # of sequences
seq_max = 4;
need_legend = false;
plot_seg = false;
%% load original data and segmenst information
fid = fopen([dir, 'input']);
fn = textscan(fid, '%s');
fclose(fid);
seq_length = length(fn{1});
if seq_length > seq_max
seq_length = seq_max;
end
seq_list = (0:seq_length-1);
%% load segment information
datfn=[dir, 'segment.'];
fid = fopen([datfn, 'labels']);
labels = textscan(fid, '%s %s %s %s %s');
n_regime = size(labels{1},1); % number of regimes
fclose(fid);
%% main figure
result_f = figure('Position', [0 0 800 400]);
%% plot result for each sequence
regime_list = zeros(1, n_regime);
%% load original data
orgfn=fn{1}{1};
org = load(orgfn);
for i_seq = 2:seq_length
seq_id = seq_list(i_seq);
%% select single sequence
orgfn=fn{1}{seq_id+1};
%% load orginal data
org(:,:,i_seq) = load(orgfn);
end
%% normalization
[n,d,w] = size(org);
hoge = permute(org, [2,1,3]); % d n w
hoge = reshape(hoge, d, [])';
hoge = zscore(hoge); % d * nw
org = reshape(hoge', d,n,w);
org = permute(org, [2,1,3]);
st=1;
ed=length(org)-1;
miny=min(min(min(org)));
maxy=max(max(max(org)));
% plot each sequence
for i_seq = 1:seq_length
seq_id = seq_list(i_seq);
figure(result_f);
%% plot original data
if seq_length == i_seq
ax = subplot(seq_length+1, 1, i_seq, 'Position', [ 0.4-i_seq*0.2/seq_length 0.9-i_seq*0.5/seq_length 0.5 1/seq_length]);
else
ax = subplot(seq_length+1, 1, i_seq, 'Position', [ 0.4-i_seq*0.2/seq_length 0.9-i_seq*0.5/seq_length 0.5 1/seq_length]);
end
%% plot segments
if plot_seg
new_regime_list = plot_segments(datfn, n_regime, st, ed, miny, maxy, seq_id, seq_length, i_seq, org(:,:,i_seq), regime_list);
regime_list = new_regime_list;
end
%% plot single sequence
plot(org(:,:,i_seq), 'LineWidth', 1.0);
xlim([st, ed])
ylim([miny*1.1 maxy*1.1])
yticklabels([]);
end
if need_legend
%% make legend title
title_list{1, n_regime} = [];
for i = 1:n_regime
mnum = num2str(i);
title_list{1,i} = strcat('\theta', mnum);
end
%% show legend
ax = subplot(seq_length+1, 1, seq_length+1);
set(ax, 'Visible', 'off');
legend(ax, regime_list, 'DisplayName', title_list, 'Orientation', 'horizontal', 'Location', 'southeast');
end
%% plot on google map
if nargin ~= 1
map_data = load(fullfile(varargin{1}));
tmp.d = mat2dataset(map_data.X,'VarNames',map_data.md.col_names);
map_f = plot_gmap(datfn, n_regime, tmp.d, seq_length);
end
end
function [result_f map_f] = plots_each(dir, varargin)
%% maximum # of sequences
seq_max = 32;
%% load original data and segmenst information
fid = fopen([dir, 'input']);
fn = textscan(fid, '%s');
fclose(fid);
seq_length = length(fn{1});
if seq_length > seq_max
seq_length = seq_max;
end
seq_list = (0:seq_length-1);
%% load segment information
datfn=[dir, 'segment.'];
fid = fopen([datfn, 'labels']);
labels = textscan(fid, '%s %s %s %s %s');
n_regime = size(labels{1},1); % number of regimes
fclose(fid);
%% main figure
result_f = figure('Position', [0 0 800 400]);
%% plot result for each sequence
regime_list = zeros(1, n_regime);
%% load original data
orgfn=fn{1}{1};
org = load(orgfn);
org_unfold = load(orgfn);
for i_seq = 2:seq_length
seq_id = seq_list(i_seq);
%% select single sequence
orgfn=fn{1}{seq_id+1};
%% load orginal data
org(:,:,i_seq) = load(orgfn);
org_unfold = [org_unfold;load(orgfn)];
end
%% normalization
[n,d,w] = size(org);
hoge = permute(org, [2,1,3]); % d n w
hoge = reshape(hoge, d, [])';
hoge = zscore(hoge); % d * nw
org = reshape(hoge', d,n,w);
org = permute(org, [2,1,3]);
st=1;
ed=length(org)-1;
% miny=min(min(min(org(st:500))));
% maxy=max(max(max(org(st:500))));
miny=min(min(min(org)));
maxy=max(max(max(org)));
for i_seq = 1:seq_length
seq_id = seq_list(i_seq);
%% plot segments
figure(result_f);
[new_regime_list, ax] = plot_segments(datfn, n_regime, st, ed, miny, maxy, seq_id, seq_length, i_seq, org(:,:,i_seq), regime_list);
regime_list = new_regime_list;
end
%% make legend title
title_list{1, n_regime} = [];
for i = 1:n_regime
mnum = num2str(i);
title_list{1,i} = strcat('\theta', mnum);
end
%% show legend
ax = subplot(seq_length+1, 1, seq_length+1);
set(ax, 'Visible', 'off');
legend(ax, regime_list, 'DisplayName', title_list, 'Orientation', 'horizontal', 'Location', 'southeast');
%% plot on google map
if nargin ~= 1
map_data = load(fullfile(varargin{1}));
tmp.d = mat2dataset(map_data.X,'VarNames',map_data.md.col_names);
map_f = plot_gmap(datfn, n_regime, tmp.d, seq_length);
end
end
function mapf = plot_gmap(datfn, n_regime, d, seq_length)
%% regime color and transparency setting
alpha = 0.6*1/seq_length;
% alpha = 0.05
max_colors = 8;
if max_colors > n_regime
max_colors = n_regime;
end
orgf=gcf;
mapf=figure;
xlim([min(d.lon) max(d.lon)]);
ylim([min(d.lat) max(d.lat)]);
plot_google_map('language','ja','MapType','satellite');
for i=0: n_regime-1
figure(orgf);
%% regime color setting
cols = colormap(jet(max_colors));
col=cols(mod(i, size(cols, 1)) + 1,:);
% if mod(i, 2) == 0
% col = cols(i/2 + 1,:);
% else
% col = cols(size(cols, 1) - ceil(i/2) + 1, :);
% end
col = [col, alpha];
%% load data: "segment.[i]"
fid = fopen([datfn, num2str(i)]);
tline = fgets(fid);
X = [ ];
while ischar(tline)
dline = str2num(tline);
% if dline(3) == seq_id
if 1
X = [X; dline];
end
tline = fgets(fid);
end
fclose(fid);
figure(mapf);
for j=1:size(X,1)
p = plot(d.lon(X(j,1)+1:X(j,2)+1), d.lat(X(j,1)+1:X(j,2)+1), 'Color', col, 'LineWidth',5);
end
end
end
function [new_regime_list, ax] = plot_segments(datfn, n_regime, st, ed, miny, maxy, seq_id, seq_n,i_seq, org, old_regime_list)
new_regime_list = old_regime_list;
%% regime color and transparency setting
alpha = 0.4;
max_colors = 32;
if max_colors > n_regime
max_colors = n_regime;
end
%% plot segments in G regimes
for i=0: n_regime-1
%% regime color setting
% jet hsv colorcube parula
cols = colormap(jet(max_colors));
col=cols(mod(i, size(cols, 1)) + 1,:);
%% load data: "segment.[i]"
fid = fopen([datfn, num2str(i)]);
tline = fgets(fid);
X = [ ];
while ischar(tline)
dline = str2num(tline);
if dline(3) == seq_id
X = [X; dline];
end
tline = fgets(fid);
end
fclose(fid);
%% if regime #i is notthing in the sequence, ignore
if size(X,1) == 0
continue;
end
%% plot segments in the regime #i
new_regime_list(i+1) = plot_box(X, col, alpha, miny, maxy);
plot_lines(X(:,1));
box on
set(gca,'YTickLabel',{''})
end
end
function p = plot_box( X, color, alpha, miny, maxy)
n=size(X,1);
for i=1:n
hold on;
p = plot_box_aux(X(i,1), X(i,2), color, alpha, miny, maxy);
hold off;
end
end
function p = plot_box_aux( st, ed, color, alpha ,miny, maxy)
btm=miny*1.1;
top=maxy*1.1;
p=patch([st, ed, ed, st],[btm, btm, top, top], color);
set(p,'EdgeColor','none')
set(p,'FaceAlpha',alpha);
ylim([btm, top])
end
function [ ] = plot_lines( X )
btm=-1000;
top=1000;
hold on;
for i=1: length(X)
% plot([X(i), X(i)], [btm, top], 'black-')
end
%ylim([btm, top])
end
function [ Xfull ] = normalize( X )
Xfull=X;
for i=1: size(Xfull,2)
X=Xfull(:,i);
Xn=(X-min(X))/(max(X)-min(X));
Xfull(:,i) = Xn;
end
end