-
Notifications
You must be signed in to change notification settings - Fork 4
/
om_viz.py
708 lines (655 loc) · 25.8 KB
/
om_viz.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/usr/bin/env python
##############################################################
# Author: Yasuko Matsubara
# Email: yasuko@sanken.osaka-u.ac.jp
# URL: https://www.dm.sanken.osaka-u.ac.jp/~yasuko/
# Date: 2020-01-01
#------------------------------------------------------------#
# Copyright (C) 2020 Yasuko Matsubara & Yasushi Sakurai
# OrbitMap is freely available for non-commercial purposes
##############################################################
import numpy as np
import tool as tl
import time
import graphviz as gv
import orbitmap as orbitmap
#------------------------------------------------#
DBG=tl.NO
PLOT_R=tl.YES
ANIMATION=tl.NO # plot animation (default: NO)
FULLSAVE=tl.NO # full plot & save
MULTI=tl.YES # multiprocessing (default: YES)
MSCALE_H=0 # # of scales (see _set_env)
SMTH=tl.YES # smoothing results
MINC=1 #2 # show result if c>=MINC
#------------------------------------------------#
#---------------------------------------------#
# convert (ImageMagick) :
# /etc/ImageMagick-6/policy.xml
# [ before ]
# policy domain="coder" rights="none" pattern="PDF"
# ->
# [after]
# policy domain="coder" rights="read|write" pattern="PDF"
#---------------------------------------------#
#--------------------------------#
def run_viz(cdir, animation=ANIMATION):
_set_env(cdir, animation)
tl.msg("om_viz (visualization), animation=%d, mscale=%d"%(animation, MSCALE_H))
if(MSCALE_H==0):_run_single(cdir, animation)
else: _run_mscale(cdir, animation)
#--------------------------------#
tl.msg('om_viz end.')
#--------------------------------#
#--------------------------------#
#--------------------------------#
def plotResultsEF(Snaps, mdb, outdir):
_plotResultsEF(Snaps, mdb, outdir)
def plotResultsE(Snaps, mdb, outdir):
_plotResultsE(Snaps, mdb, outdir)
def plotResultsF(Snaps, mdb, outdir):
_plotResultsF(Snaps, mdb, outdir)
def saveResults_txt(Snaps, mdb, outdir):
_saveResults_txt(Snaps, mdb, outdir)
#--------------------------------#
#--------------------------------#
def plotMD(mdb, outdir):
for i in mdb.MD:
md=mdb.MD[i]['medoid']
md.plot("%smd_%d"%(outdir,i))
md.save_mat("%smd_%d"%(outdir,i))
def plotCG(mdb,outdir):
mdb.plot_CG("%sout_CG"%outdir)
#--------------------------------#
#--------------------------------#
def _set_env(cdir, animation):
mscale=0
while(True):
dir_i='%s_Level%d'%(cdir,mscale)
if(not tl.os.path.isdir(dir_i)): break
mscale+=1
global MSCALE_H; MSCALE_H=mscale
global ANIMATION; ANIMATION=animation
tl.msg('om_viz (set_env): animation=%d, mscale=%d'%(animation, mscale))
#--------------------------------#
#--------------------------------#
def _run_single(cdir, animation=ANIMATION):
outdir="%s_viz/"%(cdir)
tl.mkdir("%s"%outdir)
#--------------------------------#
# (1) load modelDBs, Snaps
mdb=tl.load_obj('%sMDB.obj'%(cdir))
Snaps=tl.load_mat('%sSnaps.mat'%(cdir))
CG=tl.load_obj('%sCGs.obj'%(cdir))
# (2) save results
_save_all_L(Snaps, mdb, CG, outdir)
#--------------------------------#
#--------------------------------#
def _run_mscale(cdir, animation=ANIMATION):
outdir="%s_viz/"%(cdir)
tl.mkdir("%s"%outdir)
#--------------------------------#
# (1) load modelDBs, Snaps
mdbHs={}; SnapsHs={}; CGHs={}; outdirHs={}
for i in range(0,MSCALE_H):
outdirHs[i]="%s_Level%d/"%(outdir,i)
tl.mkdir("%s"%outdirHs[i])
mdbHs[i]=tl.load_obj('%s_Level%d/MDB.obj'%(cdir,i))
SnapsHs[i]=tl.load_mat('%s_Level%d/Snaps.mat'%(cdir,i))
CGHs[i]=tl.load_obj('%s_Level%d/CGs.obj'%(cdir,i))
#--------------------------------#
# (2) save multi-scale (LOCAL)
arg_list=[]
for i in range(0,MSCALE_H): arg_list.append([SnapsHs[i], mdbHs[i], CGHs[i], outdirHs[i]])
if(MULTI):
_multi_save_all_L(arg_list)
else:
for i in range(0,MSCALE_H): _save_all_L(SnapsHs[i], mdbHs[i], CGHs[i], outdirHs[i])
#--------------------------------#
# (3) save Xorg-cast (GLOBAL)
Xorg=tl.loadsq('%sXorg.txt'%cdir).T # load Xorg
Snaps=_save_all_G(Xorg, outdir, SnapsHs, mdbHs, outdir)
#--------------------------------#
#--------------------------------#
def _multi_save_all_L(arg_list):
n_proc=len(arg_list)
pool = tl.multiprocessing.Pool(processes=n_proc)
return pool.map(_wrapper_save_all_L, arg_list)
def _wrapper_save_all_L(arg_list):
return _save_all_L(*arg_list)
#--------------------------------#
#--------------------------------#
# save all figs (individual-level)
def _save_all_L(Snaps, mdb, CGs, outdir):
saveResults_txt(Snaps,mdb,outdir)
plotCG(mdb,outdir)
plotResultsEF(Snaps,mdb,outdir)
if(FULLSAVE): plotResultsF(Snaps,mdb,outdir)
if(FULLSAVE): plotResultsE(Snaps,mdb,outdir)
if(ANIMATION): _plotCG_SS(Snaps, mdb, CGs, outdir)
plotMD(mdb,outdir)
if(ANIMATION): _plotSS_L(Snaps, mdb, outdir)
#--------------------------------#
_save_html_L(Snaps, mdb, outdir)
#--------------------------------#
#--------------------------------#
# save full results
def _save_all_G(Xorg, cdir, SnapsHs, mdbHs, outdir):
# compute Snaps[G]
lstep=SnapsHs[0]['lstep']
multiscale_wd=1
Snaps=orbitmap.init_Snaps(Xorg, lstep, multiscale_wd)
Snaps['G']=tl.YES
Snaps['Ve_full']=SnapsHs[0]['Ve_full']
Snaps['Vf_full']=SnapsHs[0]['Vf_full']
for i in range(1,MSCALE_H):
Snaps['Ve_full']+=SnapsHs[i]['Ve_full']
Snaps['Vf_full']+=SnapsHs[i]['Vf_full']
Snaps['T_full']=np.nan*np.zeros((MSCALE_H,Snaps['n']))
for i in range(0,MSCALE_H):
Snaps['T_full'][i]=SnapsHs[i]['T_full']
# save all
_save_html_G(cdir, mdbHs)
Snaps=orbitmap.compute_Snaps_Errs(Snaps)
tl.save_mat(Snaps, "%sSnaps"%(outdir))
saveResults_txt(Snaps, None, outdir)
# plot overview (full)
_plotSS_t_G(Snaps['n'], Snaps, outdir, SnapsHs)
# (4) plot snaps (optional)
if(ANIMATION): _plotSS_G(Snaps, outdir, SnapsHs)
return Snaps
#--------------------------------#
def _save_html_G(outdir, mdbHs):
if(ANIMATION):
fp=tl.open_txt("%sresult_G_a.html"%(outdir))
else:
fp=tl.open_txt("%sresult_G.html"%(outdir))
tl.write_txt(fp,"<html>\n")
#
tl.write_txt(fp,"<h1> OrbitMap results</h1>\n")
if(not ANIMATION): tl.write_txt(fp,"(mode: no animation)<br>\n")
#
tl.write_txt(fp,"<h2> Overview </h1>\n")
tl.write_txt(fp,"<h3> Fitting result vs. original data stream </h3><br>\n")
tl.write_txt(fp,"<img src=\"result_G.png\", height=400>\n")
if(ANIMATION):
tl.write_txt(fp,"<h2> Animation</h1>\n")
tl.write_txt(fp,"<img src=\"SS_Vf_G.gif\", height=400>\n")
tl.write_txt(fp,"<embed src=\"Snaps_out.txt\", height=400><br>\n")
#
tl.write_txt(fp,"<h2> Multi-scale fitting & forecasting</h1>\n")
tl.write_txt(fp,"<h3> Fitting result vs. original data stream, Dynamic space transitions </h3><br>\n")
tl.write_txt(fp,"<table>\n")
for i in range(0,MSCALE_H):
if(mdbHs[i].get_c()>=MINC and mdbHs[i].CC==tl.YES):
tl.write_txt(fp,"<tr>\n")
tl.write_txt(fp,"<th>Level_%d</th>\n"%(i))
if(ANIMATION):
tl.write_txt(fp,"<th><img src=\"_Level%d/SS_Vf.gif\", height=300></th>\n"%(i))
tl.write_txt(fp,"<th><img src=\"_Level%d/SS_CG.gif\", height=300></th>\n"%(i))
else:
if(not FULLSAVE): tl.write_txt(fp,"<th><img src=\"_Level%d/out_Vef.png\", height=300></th>\n"%(i))
if(FULLSAVE): tl.write_txt(fp,"<th><img src=\"_Level%d/out_Ve.png\", height=300></th>\n"%(i))
if(FULLSAVE): tl.write_txt(fp,"<th><img src=\"_Level%d/out_Vf.png\", height=300></th>\n"%(i))
tl.write_txt(fp,"<th><img src=\"_Level%d/out_CG.png\", height=300></th>\n"%(i))
tl.write_txt(fp,"<th><embed src=\"_Level%d/Snaps_out.txt\", height=200></th>\n"%(i))
tl.write_txt(fp,"</tr>\n")
tl.write_txt(fp,"\n")
tl.write_txt(fp,"</table>\n")
#
tl.write_txt(fp,"<br>")
tl.write_txt(fp,"</html>")
tl.write_txt(fp,"")
tl.close_txt(fp)
#--------------------------------#
#--------------------------------#
def _save_html_L(Snaps, mdb, outdir):
if(ANIMATION):
fp=tl.open_txt("%sresult_L_a.html"%(outdir))
else:
fp=tl.open_txt("%sresult_L.html"%(outdir))
tl.write_txt(fp,"<html>\n")
tl.write_txt(fp,"<h1> OrbitMap result </h1><br>\n")
if(not ANIMATION): tl.write_txt(fp,"(mode: no animation)<br>\n")
#
tl.write_txt(fp,"<embed src=\"Snaps_out.txt\"><br>\n")
#
tl.write_txt(fp,"<h3> Fitting result vs. original data stream, Dynamic space transitions </h3><br>\n")
if(not FULLSAVE): tl.write_txt(fp,"<img src=\"out_Vef.png\", height=400>\n")
if(FULLSAVE): tl.write_txt(fp,"<img src=\"out_Ve.png\", height=400>\n")
if(FULLSAVE): tl.write_txt(fp,"<img src=\"out_Vf.png\", height=400>\n")
tl.write_txt(fp,"<img src=\"out_CG.png\", height=400>\n")
tl.write_txt(fp,"<br>\n")
tl.write_txt(fp,"<br>\n")
if(ANIMATION):
tl.write_txt(fp,"<img src=\"SS_Vf.gif\", height=400>\n")
tl.write_txt(fp,"<img src=\"SS_CG.gif\", height=400>\n")
tl.write_txt(fp,"<br>")
tl.write_txt(fp,"<br>")
#
tl.write_txt(fp,"<h3> Typical regimes </h3><br>\n")
for i in mdb.MD:
tl.write_txt(fp,"<embed src=\"md_%d_plot.pdf\", height=300, width=200>\n"%(i))
#
tl.write_txt(fp,"<br>")
tl.write_txt(fp,"</html>")
tl.write_txt(fp,"")
tl.close_txt(fp)
#--------------------------------#
def _plotCG_SS(Snaps, mdb, CGs, outdir):
CGraph_org=mdb.create_CGraph_rcds()
figfn_CG="%sSS_CG"%(outdir)
#--------------------------------#
nticks=len(Snaps['Re'])
lstep=Snaps['lstep']
pstep=Snaps['pstep']
if(mdb.CC is tl.NO): nticks=1 # if, no Orbit-Chain at this level, just ignore
for idx in range(0,nticks):
#------------------------------------------------------#
tb_st=Snaps['Te'][idx,0]; tb_ed=Snaps['Te'][idx,1]
tp_st=Snaps['Te'][idx,2]; tp_ed=Snaps['Te'][idx,3]
tc_st=Snaps['Te'][idx,4]; tc_ed=Snaps['Te'][idx,5]
tf_st=Snaps['Te'][idx,6]; tf_ed=Snaps['Te'][idx,7]
#if(DBG): tl.msg([tb_st, tb_ed, tp_st, tp_ed, tc_st, tc_ed, tf_st, tf_ed])
#SSResults=tl.YES # if, want to plot snapshot-fit (default: no)
SSResults=tl.NO # if, plot full-fit result, i.e., Re_full, (default: yes)
if(SSResults):
tb_ed-=tb_st;
tp_st-=tb_st; tp_ed-=tb_st;
tc_st-=tb_st; tc_ed-=tb_st;
tf_st-=tb_st; tf_ed-=tb_st;
tb_st=0;
if(DBG): tl.msg([tb_st, tb_ed, tp_st, tp_ed, tc_st, tc_ed, tf_st, tf_ed])
Re_t=Snaps['Re'][idx]
else:
Re_t=Snaps['Re_full']
#------------------------------------------------------#
#CGraph=tl.dcopy(CGraph_org) # if, static
CGraph=CGs[idx] # if, snapshots
#------------------------------------------------------#
Re_t[np.isnan(Re_t)]=-1
Re_t=Re_t.astype(int)
# Vb
nset=tl.collections.Counter(Re_t[tb_st:tb_ed])
if(SSResults):
del(nset[-1])
for nid in nset: _updateCG_node(CGraph, int(nid), 'powderblue')
# Vp
nset=tl.collections.Counter(Re_t[tp_st:tp_ed])
del(nset[-1])
for nid in nset: _updateCG_node(CGraph, int(nid), 'steelblue')
# Vf
nset=tl.collections.Counter(Re_t[tf_st:tf_ed])
del(nset[-1])
for nid in nset: _updateCG_node(CGraph, int(nid), 'lightpink')
# Vc
nset=tl.collections.Counter(Re_t[tc_st:tc_ed])
del(nset[-1])
for nid in nset: _updateCG_node(CGraph, int(nid), 'tomato')
else:
# Vc
(Rc,Rp,Rb)=_find_two_steps_prev(Re_t, tc_ed)
# Vb
#_updateCG_node(CGraph, Rb, 'powderblue')
# Vp
_updateCG_node(CGraph, Rp, 'powderblue') #'orange')
# Vc
_updateCG_node(CGraph, Rc, 'tomato')
#_updateCG_node(CGraph, Re_t[tc_ed], 'tomato')
#------------------------------------------------------#
gv.write_dot(CGraph,"%s_tmp"%(figfn_CG))
if(idx==0):
tl.os.system("pdfunite %s_tmp.pdf %s.pdf"%(figfn_CG,figfn_CG))
else:
tl.os.system("cp %s.pdf %s_cp.pdf"%(figfn_CG,figfn_CG))
tl.os.system("pdfunite %s_cp.pdf %s_tmp.pdf %s.pdf"%(figfn_CG,figfn_CG,figfn_CG))
#--------------------------------#
tl.os.system("convert -loop 1 -dispose previous -resize 500 -density 200 %s.pdf %s.gif"%(figfn_CG,figfn_CG))
#--------------------------------#
# create segID list
def _find_two_steps_prev(Re_full,tc):
n=len(Re_full)
Rc=Re_full[tc]; Rp=-1; Rb=-1;
for t in range(0,tc):
r_c=Re_full[tc-t]
if(Rp==-1 and r_c != Rc): Rp=r_c
if(Rp!=-1 and Rb==-1 and r_c!=Rp): Rb=r_c
return (Rc, Rp, Rb)
# update node style (color)
def _updateCG_node(CGraph, nid, col):
for node in CGraph['MD']:
if(node['id']==nid): node['col']=col; break
return CGraph
#--------------------------------#
def _plotSS_G(Snaps, outdir, SnapsHs):
figfn_SS="%sSS_Vf_G"%(outdir)
pp_SS=tl.pdfopen("%s.pdf"%(figfn_SS))
for idx in range(0,len(SnapsHs[0]['Xe'])):
tl.plt.clf()
tc=SnapsHs[0]['Te'][idx,5]
_plotSS_t_G(tc, Snaps, '', SnapsHs)
pp_SS.savefig(tl.plt.gcf())
pp_SS.close()
tl.os.system("convert -loop 1 -dispose previous %s.pdf %s.gif"%(figfn_SS,figfn_SS))
#--------------------------------#
def _plotSS_t_G(idx, Snaps, outdir, SnapsHs):
tc=idx
tf_st=tc+Snaps['lstep']
tf_ed=tf_st+Snaps['pstep']
n=Snaps['n']
Xorg=Snaps['Xorg']
mn=np.nanmin(Xorg.flatten()); mx=np.nanmax(Xorg.flatten());
if(DBG): tl.msg([tc, tf_st, tf_ed])
tl.plt.clf()
tl.plt.subplot(511)
tl.plt.plot(Xorg[0:idx,:]) #, 'black')
tl.plt.plot(range(idx,n), Xorg[idx:n,:], 'lightgrey')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Original')
tl.plt.xticks([])
_plot_lines([tc], mn, mx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.subplot(512)
tl.plt.plot(Xorg[0:idx,:], 'lightgrey')
tl.resetCol()
tl.plt.plot(Snaps['Ve_full'][0:idx]) #, 'royalblue')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Estimation')
tl.plt.xticks([])
_plot_lines([tc], mn, mx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.subplot(513)
tl.plt.plot(Xorg[0:tf_ed,:], 'lightgrey')
tl.resetCol()
if(SMTH): tl.plt.plot(tl.smoothWMAo(Snaps['Vf_full'][0:tf_ed], int(Snaps['pstep'])))
else: tl.plt.plot(Snaps['Vf_full'][0:tf_ed])
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Forecast')
tl.plt.xticks([])
_plot_lines([tc], mn, mx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.subplot(514)
tl.plt.plot(Snaps['Ef_full'][0:idx], 'yellowgreen')
tl.plt.plot(Snaps['Es_full'][0:idx], 'lightgrey')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylabel('RMSE')
tl.plt.xticks([])
emx=np.nanmax(Snaps['Es_full'].flatten())
_plot_lines([tc], 0, emx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], 0, emx, 0.5, 'r')
tl.plt.ylim([0,emx])
tl.plt.subplot(515)
tmx=-tl.INF; tmn=tl.INF
for i in range(0,MSCALE_H):
tl.plt.semilogy(SnapsHs[i]['T_full'][0:idx], '.', label='h=%d'%(i))
mn=np.nanmin(SnapsHs[i]['T_full']); mx=np.nanmax(SnapsHs[i]['T_full']);
if(tmx<mx): tmx=mx
if(tmn>mn): tmn=mn
tl.plt.xlim([0,len(Xorg)])
_plot_lines([tc], tmn, tmx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], tmn, tmx, 0.5, 'r')
tl.plt.ylim([tmn,tmx])
tl.plt.ylabel('Speed')
if(outdir!=''):
tl.savefig("%sresult_G"%(outdir),'pdf')
tl.savefig("%sresult_G"%(outdir),'png')
#--------------------------------#
def _plotSS_L(Snaps, mdb, outdir):
figfn_SS="%sSS_Vf"%(outdir)
pp_SS=tl.pdfopen("%s.pdf"%(figfn_SS))
for idx in range(0,len(Snaps['Xe'])):
tl.plt.clf()
_plotSS_t_L(idx, Snaps, mdb)
pp_SS.savefig(tl.plt.gcf())
pp_SS.close()
tl.os.system("convert -loop 1 -dispose previous %s.pdf %s.gif"%(figfn_SS,figfn_SS))
#--------------------------------#
def _plotSS_t_L(idx, Snaps, mdb):
#------------------------------------------------------#
tb_st=Snaps['Te'][idx,0]; tb_ed=Snaps['Te'][idx,1]
tp_st=Snaps['Te'][idx,2]; tp_ed=Snaps['Te'][idx,3]
tc_st=Snaps['Te'][idx,4]; tc_ed=Snaps['Te'][idx,5]
tf_st=Snaps['Te'][idx,6]; tf_ed=Snaps['Te'][idx,7]
#------------------------------------------------------#
if(DBG): tl.msg([tb_st, tb_ed, tp_st, tp_ed, tc_st, tc_ed, tf_st, tf_ed])
Xe_t=Snaps['Xe'][idx]
Ve_t=Snaps['Ve'][idx]
Re_t=Snaps['Re'][idx]
Xorg=Snaps['Xorg']
nduration=Snaps['n']
lstep=Snaps['lstep']
mn=np.nanmin(Xorg.flatten()); mx=np.nanmax(Xorg.flatten());
#-----------------------------------#
tl.plt.clf()
tl.plt.subplot(611)
tl.plt.plot(Xorg) #, 'black')
_plot_lines([tb_st, tb_ed, tp_st, tp_ed, tc_st, tc_ed], mn, mx, 0.5, 'b')
_plot_lines([tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.xlim([0,nduration])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Original')
tl.plt.xticks([])
tl.plt.subplot(612)
tl.plt.plot(Xorg, 'lightgrey', color=[0.8,0.8,0.8])
tl.resetCol()
if(SMTH): tl.plt.plot(range(0,tf_ed), tl.smoothWMAo(Snaps['Vf_full'][0:tf_ed], int(Snaps['pstep'])))
else: tl.plt.plot(range(0,tf_ed), Snaps['Vf_full'][0:tf_ed])
_plot_lines([tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.xlim([0,nduration])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Forecast')
tl.plt.xticks([])
tl.plt.subplot(613)
_plot_lines([tb_st, tb_ed, tp_st, tp_ed, tc_st, tc_ed], -1, 100, 0.5, 'b')
tl.plt.imshow(_createImap(mdb, Snaps['Re_full'][0:tc_ed]), cmap='gray', interpolation='nearest', aspect='auto')
tl.plt.xlim([0,nduration]) #; tl.plt.yticks([])
tl.plt.yticks(range(0,mdb.get_c()), range(1,mdb.get_c()+1))
tl.plt.xticks([])
tl.plt.ylabel('R-ID')
#tl.plt.subplot(12,1,6)
#_plot_lines([tf_st, tf_ed], -1, 100, 0.5, 'r')
#tl.plt.imshow(_createImap(mdb, Snaps['Rf_full'][0:tf_ed]), cmap='gray', interpolation='nearest', aspect='auto')
#tl.plt.xlim([0,nduration]) #; tl.plt.yticks([])
#tl.plt.yticks(range(0,mdb.get_c()), range(1,mdb.get_c()+1))
#-----------------------------------#
ss_st=int(max(0,tc_ed-lstep*4)); ss_ed=int(min(tf_ed+lstep/2, nduration))
#ss_st=int(max(0,tb_st-lstep*0)); ss_ed=int(min(tf_ed+lstep, nduration))
#tl.plt.subplot(223)
if(PLOT_R): tl.plt.subplot(234)
else: tl.plt.subplot(223)
#tl.plt.plot(range(ss_st,ss_ed), Snaps['Xorg'][ss_st:ss_ed]) #, 'black') #,:])
tl.plt.plot(range(ss_st,ss_ed), Snaps['Xorg'][ss_st:ss_ed], 'lightgrey')
tl.resetCol()
tl.plt.plot(range(ss_st,tc_ed), Snaps['Xorg'][ss_st:tc_ed]) #, 'black') #,:])
_plot_lines([tb_st, tb_ed, tp_st, tp_ed, tc_st], mn, mx, 0.5, 'b')
_plot_lines([tc_ed, tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.xlim(ss_st, ss_ed); tl.plt.ylim([mn, mx])
tl.plt.title('Org tc:%d, Vc [%d:%d]'%(tc_ed, tc_st,tc_ed))
if(PLOT_R): tl.plt.subplot(235)
else: tl.plt.subplot(224)
tl.plt.plot(range(ss_st,ss_ed), Snaps['Xorg'][ss_st:ss_ed], 'lightgrey')
tl.resetCol()
tl.plt.plot(range(tb_st,tf_ed), Ve_t) #, 'tomato')
_plot_lines([tb_st, tb_ed, tp_st, tp_ed, tc_st], mn, mx, 0.5, 'b')
_plot_lines([tc_ed, tf_st, tf_ed], mn, mx, 0.5, 'r')
tl.plt.xlim(ss_st, ss_ed); tl.plt.ylim([mn, mx])
tl.plt.title('Cast Vf [%d:%d]'%(tf_st, tf_ed))
if(PLOT_R):
tl.plt.subplot(236)
tl.plt.plot(range(tb_st,tf_ed), Re_t)
tl.plt.xlim(ss_st, ss_ed);
#-----------------------------------#
#--------------------------------#
#--------------------------------#
def _createImap(mdb, R):
n=len(R) # sequence length
if(mdb.CC is tl.NO): return np.zeros((n,1)) # if, no mdb, then return zero
c=mdb.get_c() # of cells
Rm=np.zeros((n,c)) # assigned ID list
cnt=0
for i in mdb.MD:
for t in range(0,n):
if(R[t]==i):
Rm[t,cnt]=1
cnt+=1
Rm=(-1)*Rm[:,0:cnt].T
return Rm
#--------------------------------#
def _plot_lines(Lines, mn, mx, wd, c):
for i in Lines:
tl.plt.plot([i,i],[mn,mx], '-', lw=wd, color=c)
#--------------------------------#
def _plotResultsEF(Snaps, mdb, outdir):
Xorg=Snaps['Xorg']
mn=np.nanmin(Xorg.flatten()); mx=np.nanmax(Xorg.flatten())
tl.plt.clf()
tl.plt.subplot(511)
tl.plt.plot(Xorg)
tl.resetCol()
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Original')
tl.plt.xticks([])
tl.plt.subplot(512)
tl.plt.plot(Xorg, 'lightgrey')
tl.resetCol()
tl.plt.plot(Snaps['Ve_full'])
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Estimation')
tl.plt.xticks([])
tl.plt.subplot(513)
tl.plt.plot(Xorg, 'lightgrey')
tl.resetCol()
if(SMTH): tl.plt.plot(tl.smoothWMAo(Snaps['Vf_full'],int(Snaps['pstep'])))
else: tl.plt.plot(Snaps['Vf_full'])
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Forecast')
tl.plt.xticks([])
tl.plt.subplot(514)
tl.plt.imshow(_createImap(mdb, Snaps['Re_full']), cmap='gray', interpolation='nearest', aspect='auto')
tl.plt.xlim([0,len(Xorg)]) #; tl.plt.yticks([])
tl.plt.yticks(range(0,mdb.get_c()), range(1,mdb.get_c()+1))
tl.plt.ylabel('R-ID (est)')
tl.plt.xticks([])
tl.plt.subplot(515)
tl.plt.semilogy(Snaps['T_full'], '.', color='yellowgreen')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylabel('Speed')
#tl.savefig("%sout_Vef"%(outdir),'pdf')
tl.savefig("%sout_Vef"%(outdir),'png')
tl.plt.close()
#--------------------------------#
def _plotResultsF(Snaps, mdb, outdir):
Xorg=Snaps['Xorg']
mn=np.nanmin(Xorg.flatten()); mx=np.nanmax(Xorg.flatten())
tl.plt.clf()
tl.plt.subplot(511)
tl.plt.plot(Xorg,) # 'black')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Original')
tl.plt.xticks([])
tl.plt.subplot(512)
tl.plt.plot(Xorg, 'lightgrey')
tl.resetCol()
if(SMTH): tl.plt.plot(tl.smoothWMAo(Snaps['Vf_full'],int(Snaps['pstep'])))
else: tl.plt.plot(Snaps['Vf_full'])
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Forecast')
tl.plt.xticks([])
tl.plt.subplot(513)
tl.plt.plot(Snaps['Es_full'], 'lightgrey')
tl.plt.plot(Snaps['Ef_full'], 'yellowgreen')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylabel('RMSE (cast)')
tl.plt.xticks([])
emx=np.nanmax(Snaps['Es_full'].flatten())
tl.plt.ylim([0,emx])
tl.plt.subplot(514)
#tl.plt.plot(Snaps['Rf_full'])
tl.plt.imshow(_createImap(mdb, Snaps['Rf_full']), cmap='gray', interpolation='nearest', aspect='auto')
tl.plt.xlim([0,len(Xorg)]) #; tl.plt.yticks([])
tl.plt.yticks(range(0,mdb.get_c()), range(1,mdb.get_c()+1))
tl.plt.ylabel('R-ID (cast)')
tl.plt.xticks([])
tl.plt.subplot(515)
tl.plt.semilogy(Snaps['T_full'], '.', color='yellowgreen')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylabel('Speed')
#tl.savefig("%sout_Vf"%(outdir),'pdf')
tl.savefig("%sout_Vf"%(outdir),'png')
tl.plt.close()
#--------------------------------#
def _plotResultsE(Snaps, mdb, outdir):
Xorg=Snaps['Xorg']
mn=np.nanmin(Xorg.flatten()); mx=np.nanmax(Xorg.flatten())
tl.plt.clf()
tl.plt.subplot(511)
tl.plt.plot(Xorg) #, 'black')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Original')
tl.plt.xticks([])
tl.plt.subplot(512)
tl.plt.plot(Xorg, 'lightgrey')
tl.resetCol()
tl.plt.plot(Snaps['Ve_full']) #, 'royalblue')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylim([mn, mx])
tl.plt.ylabel('Est')
tl.plt.xticks([])
tl.plt.subplot(513)
tl.plt.plot(Snaps['Ee_full'], 'yellowgreen')
tl.plt.xlim([0,len(Xorg)])
emx=np.nanmax(Snaps['Es_full'].flatten())
tl.plt.ylim([0,emx])
tl.plt.ylabel('RMSE (est)')
tl.plt.xticks([])
tl.plt.subplot(514)
#tl.plt.plot(Snaps['Re_full'])
tl.plt.imshow(_createImap(mdb, Snaps['Re_full']), cmap='gray', interpolation='nearest', aspect='auto')
tl.plt.xlim([0,len(Xorg)]) #; tl.plt.yticks([])
tl.plt.yticks(range(0,mdb.get_c()), range(1,mdb.get_c()+1))
tl.plt.ylabel('R-ID (est)')
tl.plt.xticks([])
tl.plt.subplot(515)
tl.plt.semilogy(Snaps['T_full'], '.', color='yellowgreen')
tl.plt.xlim([0,len(Xorg)])
tl.plt.ylabel('Speed')
#tl.savefig("%sout_Ve"%(outdir),'pdf')
tl.savefig("%sout_Ve"%(outdir),'png')
tl.plt.close()
#--------------------------------#
#--------------------------------#
def _saveResults_txt(Snaps, mdb, outdir):
fp=tl.open_txt("%sSnaps_out.txt"%(outdir))
tl.write_txt(fp,"fn: %s\n"%(outdir))
tl.write_txt(fp,"(n,d): (%d,%d)\n"%(Snaps['n'], Snaps['d']))
tl.write_txt(fp,"(lstep,pstep,wd_level): (%d, %d, %d)\n"%(Snaps['lstep'], Snaps['pstep'], Snaps['wd_level']))
if(mdb==None):
tl.write_txt(fp,"MDB (None)\n")
else:
tl.write_txt(fp,"MDB (c): %d (CC:%d)\n"%(mdb.get_c(), mdb.CC))
tl.write_txt(fp,"MDB (RE/RF/MS): %f/%f/%f\n"%(mdb.rho_RE, mdb.rho_RF, mdb.rho_MS))
tl.write_txt(fp,"errE: %f\n"%(Snaps['errE']))
tl.write_txt(fp,"errS: %f\n"%(Snaps['errS']))
tl.write_txt(fp,"errF: %f\n"%(Snaps['errF']))
tl.write_txt(fp,"errF (half): %f\n"%(Snaps['errF_half']))
if('G' in Snaps):
tl.write_txt(fp,"time: %f (max)\n"%(tl.mynanmean(np.amax(Snaps['T_full'],axis=0))))
tl.write_txt(fp,"time: %f (mean)\n"%(tl.mynanmean(Snaps['T_full'])))
else:
tl.write_txt(fp,"time: %f \n"%(tl.mynanmean(Snaps['T_full'])))
tl.close_txt(fp)
#--------------------------------#
#---------------#
# main #
#---------------#
if __name__ == "__main__":
tl.msg("OrbitMap (viz)")