-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
1560 lines (1408 loc) · 59.7 KB
/
classes.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Contains all the classes used to obtain the
animations
'''
from pylab import *
from manim import *
import networkx as nx
from manimnx import manimnx as mnx
from functions import *
#################################################################################################################
'''
Animation 1 : LDPC decoding
'''
#################################################################################################################
class ErasureLDPC(Scene) :
'''
Tanner graph of LDPC code passed through
a binary erasure channel
'''
def construct(self) :
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z = 2 #No. of cnxns leaving from CN
#Tanner graph initialization
CNnames = ['A','B','C','D']
VNnames = ['0', '1', '2', '3', '4', '5']
QNnames = ['G','H','I','J','K','L']
CNlabels = ['.','.','.','.']
VNlabels = ['1','.','0','.','.','.']
QNlabels = ['1','0','0','1','0','1']
CNVNedgecxns = "0-3,2-4,3-5,1-4"
tg = tanner_graph_LDPC(CNnames,len(VNnames),CNVNedgecxns,QNnames,CNlabels,VNlabels,QNlabels)
xycoord = {'yCN3' : np.arange(2,-3,-2),
'yVN9' : np.arange(8/3,-3,-2/3),
'yCN6' : np.arange(2.5,-3,-1),
'yVN12' : np.arange(3.3,-3.4,-3/5),
'yCN4' : np.arange(1.8,-2,-1.2),
'yVN6' : np.arange(2.5,-3,-1)
}
#Give coordinates based on CN-VN length
if len(CNnames) == 6 and len(VNnames) == 12 :
yCN = '6'
yVN = '12'
elif len(CNnames) == 3 and len(VNnames) == 9 :
yCN = '3'
yVN = '9'
elif len(CNnames) == 4 and len(VNnames) == 6 :
yCN = '4'
yVN = '6'
#CN coordinates
xCN = -4
pos = np.zeros((len(CNnames), 2))
pos[:, 0] = xCN
pos[:, 1] = xycoord['yCN'+yCN]
mnx.map_attr('pos', CNnames, pos, tg)
#VN coordinates
xVN = 1
pos = np.zeros((len(VNnames), 2))
pos[:, 0] = xVN
pos[:, 1] = xycoord['yVN'+yVN]
mnx.map_attr('pos',VNnames , pos, tg)
#QN coordinates
xQN = 5
pos = np.zeros((len(VNnames), 2))
pos[:, 0] = xQN
pos[:, 1] = xycoord['yVN'+yVN]
mnx.map_attr('pos',QNnames , pos, tg)
#define the manim graph
mtg = mnx.ManimGraph(tg,tg_node_LDPC,tg_edge)
#Add to make edges go behind variables
self.add_foreground_mobjects(*mtg.nodes.values())
#Begin fade animation
CNVNgroups = []
#Obtain group for animation along edge and reverse
edgeCNVNgroups = []
revCNVNgroups = []
CNgroups = []
VNgroups = []
NZcumsum = [0]
#Create all node groups for box
allQNgroups = VGroup()
allVNgroups = VGroup()
allCNgroups = VGroup()
#Create groups to handle together
for i in range(len(QNnames)) :
allQNgroups.add(mtg.nodes[tg.nodes[QNnames[i]]['mob_id']])
allVNgroups.add(mtg.nodes[tg.nodes[VNnames[i]]['mob_id']])
for i in range(len(CNnames)) :
allCNgroups.add(mtg.nodes[tg.nodes[CNnames[i]]['mob_id']])
#Fade all nodes
allNodes = VGroup()
for i in range(len(mtg.nodes)) :
allNodes.add(mtg.nodes[i])
allNodes.set_opacity(0)
#Add Q-VN edges
QVNgroups = VGroup()
for qnd in QNnames :
for ng in tg.neighbors(qnd) :
if tg.nodes[ng]['type'] == 'variablenode' :
e = (qnd,ng,0)
QVNgroups.add(mtg.edges[tg.edges[e]['mob_id']])
self.add(QVNgroups.set_opacity(1))
#Make all QNs, VNs visible
allQNgroups.set_opacity(1)
#Add channel
rectChannel = Rectangle(height=6, width=2, fill_color=YELLOW,fill_opacity=1, stroke_color=YELLOW)
txtCh = Text(" Binary\n Erasure\n Channel",color=BLACK).scale(0.4)
rectChannel.move_to(3*RIGHT)
txtCh.move_to(3*RIGHT)
self.add_foreground_mobjects(txtCh)
self.add(rectChannel)
self.add(txtCh)
#Add signal label
txtTx = Text("Transmitted bits",color=BLACK).scale(0.4)
txtTx.move_to(5*RIGHT+3.75*UP)
self.add(txtTx)
txtRx = Text("Received bits",color=BLACK).scale(0.4)
txtRx.move_to(1*RIGHT+3.75*UP)
self.add(txtRx)
txtSPC = Text("SPC constraints",color=BLACK).scale(0.4)
txtSPC.move_to(-4*RIGHT+3.75*UP)
self.add(txtSPC)
#Pass the message from VN to Q
msg = []
for j in range(len(QNnames)) :
msg.append(Dot().set_color(YELLOW))
#Send message
self.play(*[MoveAlongPath(msg[j],QVNgroups[j]) for j in range(len(QNnames))],run_time=2)
self.play(*[Transform(msg[j],allVNgroups[j].set_opacity(1)) for j in range(len(VNnames))])
self.wait(1)
for j in range(len(QNnames)) :
msg[j].set_opacity(0)
#Make QN and their edges fade
QVNgroups.set_opacity(0)
#Parity check values of CN
txtCNbit = []
CNbit = ["1","0","1","0"]
for i in range(len(CNnames)) :
#Add initial and final value
txtCNbit.append([MathTex(CNbit[i],color=YELLOW).set_opacity(0).move_to(xCN*RIGHT + xycoord['yCN'+yCN][i]*UP), MathTex("0",color=YELLOW).set_opacity(0).move_to(xCN*RIGHT+xycoord['yCN'+yCN][i]*UP)])
self.add_foreground_mobjects(*[txtCNbit[i][0] for i in range(len(CNbit))])
self.add_foreground_mobjects(*[txtCNbit[i][1] for i in range(len(CNbit))])
#Parity check values of VN
txtVNbit = []
VNbit = ["1","0","1","0"]
VNpos = [3,4,5,1]
for i in range(len(CNnames)) :
#Add VN value
txtVNbit.append(MathTex(VNbit[i],color=YELLOW).set_opacity(0).move_to(xVN*RIGHT + xycoord['yVN'+yVN][VNpos[i]]*UP))
self.add_foreground_mobjects(*[txtVNbit[i][0] for i in range(len(VNbit))])
#Create VN-CN groups for message passing
for cnidx in range(len(CNnames)) :
#Create all 3 (CN-VN-e) and edge only groups
CNVNcxns = VGroup()
edgeCNVNcxns = VGroup()
revCNVNcxns = VGroup()
#Add for highlight/indication
CNonly = VGroup()
VNonly = VGroup()
#Add neighbouring VNs
cnnd = CNnames[cnidx]
#Add CNs
CNVNcxns.add(mtg.nodes[tg.nodes[cnnd]['mob_id']])
CNonly.add(mtg.nodes[tg.nodes[cnnd]['mob_id']])
#Loop over neighbours
for ng in tg[cnnd] :
CNVNcxns.add(mtg.nodes[tg.nodes[ng]['mob_id']])
VNonly.add(mtg.nodes[tg.nodes[ng]['mob_id']])
#Obtained by printing the e in tg.edges format
e = (cnnd,ng,0)
#Reverse direction of above edge (VN to CN)
erev = (ng,cnnd,0)
#Add edge
CNVNcxns.add(mtg.edges[tg.edges[e]['mob_id']])
edgeCNVNcxns.add(mtg.edges[tg.edges[e]['mob_id']])
#Add reverse edge from rev edge in same graph
revCNVNcxns.add(mtg.edges[tg.edges[erev]['mob_id']])
#Add groups into loop
CNVNgroups.append(CNVNcxns)
edgeCNVNgroups.append(edgeCNVNcxns)
revCNVNgroups.append(revCNVNcxns)
CNgroups.append(CNonly)
VNgroups.append(VNonly)
#Fade all
self.add(*[CNVNgroups[i].set_opacity(0.15) for i in range(len(CNVNgroups))])
#Order of CN message passing
CNorder = [0,1,2,3]
#Deduced VNs which need to be ON
VNded = [None,None,0,1]
for i in CNorder :
CNVNgroups[i].set_opacity(1)
if VNded[i] != None :
txtVNbit[VNded[i]].set_opacity(1)
#Pass the message from VN to CN
msg = []
for j in range(Z) :
msg.append(Dot().set_color(PURPLE_B))
#Change direction
self.play(*[MoveAlongPath(msg[j],revCNVNgroups[i][j]) for j in range(Z)],rate_func=slow_into)
#Parity-check bit VN to CN
self.play(Indicate(CNgroups[i],color=PURPLE_B),
Transform(msg[0],txtCNbit[i][0].set_opacity(1)))
for j in range(Z) :
msg[j].set_opacity(0)
#Pass from CN to VN
msg = []
for j in range(Z) :
msg.append(Dot().set_color(GREEN))
#Change direction
self.play(*[MoveAlongPath(msg[j],edgeCNVNgroups[i][j]) for j in range(Z)],rate_func=slow_into)
for j in range(Z) :
msg[j].set_opacity(0)
#Msg to be transformed
msgtrans = [1,1,1,0]
#Parity-check bit CN to VN
self.play(Transform(txtCNbit[i][0],txtCNbit[i][1].set_opacity(1)),
Transform(msg[msgtrans[i]],txtVNbit[i].set_opacity(1)),
Indicate(VNgroups[i],color=GREEN),
scale_factor=1.1)
self.wait(0.5)
txtCNbit[i][0].set_opacity(0)
self.play(FadeOut(txtCNbit[i][1]))
txtVNbit[i].set_opacity(0.05)
CNVNgroups[i].set_opacity(0.15)
#Show Tx-Rx bits
allNodes.set_opacity(0)
self.add(*[CNVNgroups[i].set_opacity(0) for i in range(len(CNVNgroups))])
allQNgroups.set_opacity(1)
allVNgroups.set_opacity(1)
for i in range(len(txtVNbit)) :
txtVNbit[i].set_opacity(1)
self.wait(2.5)
#################################################################################################################
'''
Animation 2 : Effect of Z on H
'''
#################################################################################################################
class BGtoH(Scene) :
'''
Using same BG show variation in H due to
varying value of Z (3,5)
'''
def construct(self) :
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z0 = 384
Z1 = 2
Z2 = 4
shiftval0 = [106,-1,269,-1,-1,135,-1,212]
shiftval1 = [0,-1,1,-1,-1,1,-1,0]
shiftval2 = [2,-1,1,-1,-1,3,-1,0]
BGmat0 = np.array([[106,-1,269,-1],[-1,135,-1,212]])
BGmat1 = np.array([[0,-1,1,-1],[-1,1,-1,0]])
BGmat2 = np.array([[2,-1,1,-1],[-1,3,-1,0]])
#Create manim BG matrix
manimBG0 = (Matrix((BGmat0.astype("int")).astype("str"),width=0.2,height=0.2)
.scale(0.5)
)
manimBG1 = (Matrix((BGmat1.astype("int")).astype("str"),width=0.2,height=0.2)
.scale(0.5)
)
manimBG2 = (Matrix((BGmat2.astype("int")).astype("str"),width=0.2,height=0.2)
.scale(0.5)
)
manimBG0 = manimBG0.set_color(BLACK)
manimBG1 = manimBG1.set_color(BLACK)
manimBG2 = manimBG2.set_color(BLACK)
#Text declarations of BG_Z
txtBG0 = MathTex("{PM}_{Z=384} =",color=BLACK,height=0.4,width=1.5)
txtBG1 = MathTex("{PM}_{Z=2} =",color=BLACK,height=0.4,width=1.3)
txtBG2 = MathTex("{PM}_{Z=4} =",color=BLACK,height=0.4,width=1.3)
txtmod1 = MathTex("{PM}_{Z=384}\pmod{2} \equiv",color=BLACK,height=0.4,width=2.5)
txtmod2 = MathTex("{PM}_{Z=384}\pmod{4} \equiv",color=BLACK,height=0.4,width=2.5)
txtH1 = MathTex("H_{Z=2} =",color=BLACK,height=0.4,width=1.3)
txtH2 = MathTex("H_{Z=4} =",color=BLACK,height=0.4,width=1.3)
#Text and BG_Z matrix animations
txtBG0.move_to(-5*RIGHT)
txtBG1.move_to(2.5*RIGHT + 2*UP)
txtBG2.move_to(2.5*RIGHT + -2*UP)
txtmod1.move_to(0.5*RIGHT + 2*UP)
txtmod2.move_to(0.5*RIGHT + -2*UP)
manimBG0.move_to(-2.5*RIGHT)
manimBG1.move_to(5*RIGHT+ 2*UP)
manimBG2.move_to(5*RIGHT+ -2*UP)
self.play(FadeIn(txtBG0),run_time=1)
self.play(FadeIn(manimBG0),run_time=1)
self.play(FadeIn(txtmod1),
FadeIn(txtmod2),run_time=1)
self.play(FadeIn(txtBG1),
FadeIn(txtBG2),run_time=1)
self.play(FadeIn(manimBG1),
FadeIn(manimBG2),run_time=1)
self.wait(1)
#Fade others and slide the BG_Z1
eqnBG1 = VGroup()
eqnBG1.add(txtBG1)
eqnBG1.add(manimBG1)
eqnBG1.generate_target()
eqnBG1.target.move_to(2*UP)
self.play(FadeOut(txtBG0),
FadeOut(manimBG0),
FadeOut(txtmod1),
FadeOut(txtmod2),
FadeOut(txtBG2),
FadeOut(manimBG2))
self.play(MoveToTarget(eqnBG1))
#Create H animation
#Create matrices from shift values
shiftIM1 = []
shiftIM2 = []
for i in range(len(shiftval1)) :
#For Z1
mnm1 = genshiftedIM(shiftval1[i],N=Z1)
mnm1 = mnm1.set_color(BLACK)
brack1 = mnm1.get_brackets()
brack1[0].set_color(WHITE)
brack1[1].set_color(WHITE)
shiftIM1.append(mnm1)
#Positions of matrices
x1,y1 = 1,-1
#Z1
x1off0 = 0.6
x1off1 = 1.8
shiftIM1[0].move_to((x1-x1off1)*RIGHT + (y1+1)*UP)
shiftIM1[1].move_to((x1-x1off0)*RIGHT + (y1+1)*UP)
shiftIM1[2].move_to((x1+x1off0)*RIGHT + (y1+1)*UP)
shiftIM1[3].move_to((x1+x1off1)*RIGHT + (y1+1)*UP)
#Row 2
shiftIM1[4].move_to((x1-x1off1)*RIGHT + (y1)*UP)
shiftIM1[5].move_to((x1-x1off0)*RIGHT + (y1)*UP)
shiftIM1[6].move_to((x1+x1off0)*RIGHT + (y1)*UP)
shiftIM1[7].move_to((x1+x1off1)*RIGHT + (y1)*UP)
#Get manim indices of BG
mnmidx = manimBG1.get_entries()
#Create VGroups for H
Hz1 = VGroup()
#Obtain H from BG for Z1
for i in range(len(shiftval1)) :
#Obtain the index of BG
self.play(
Indicate(mnmidx[i],color=RED),
TransformFromCopy(mnmidx[i],shiftIM1[i])
)
#Add to VGroup
Hz1.add(shiftIM1[i])
#Add H_z1 text and brackets
txtH1.move_to(-3*RIGHT+ -0.5*UP)
mnmbrack1 = add_brack(Hz1,Zval=2)
Hbrack1 = VGroup()
Hbrack1.add(txtH1)
Hbrack1.add(mnmbrack1)
self.play(FadeIn(Hbrack1))
self.wait(1)
#Fade all
self.play(FadeOut(Hbrack1),
FadeOut(eqnBG1),
FadeOut(Hz1))
################ Similar animations for Z2 ##################
#Fade in Z2 equation
eqnBG2 = VGroup()
eqnBG2.add(txtBG2)
eqnBG2.add(manimBG2)
eqnBG2.move_to(2*UP)
self.play(FadeIn(eqnBG2))
x2,y2 = 1,1.5
shiftIM2 = []
for i in range(len(shiftval2)) :
#For Z2
mnm2 = genshiftedIM(shiftval2[i],N=Z2)
mnm2 = mnm2.set_color(BLACK)
brack2 = mnm2.get_brackets()
brack2[0].set_color(WHITE)
brack2[1].set_color(WHITE)
shiftIM2.append(mnm2)
#Z2
x2off0 = 1.25
x2off1 = 3.75
y2off0 = -1.5
y2off1 = -3.2
shiftIM2[0].move_to((x2-x2off1)*RIGHT + (y2+y2off0)*UP)
shiftIM2[1].move_to((x2-x2off0)*RIGHT + (y2+y2off0)*UP)
shiftIM2[2].move_to((x2+x2off0)*RIGHT + (y2+y2off0)*UP)
shiftIM2[3].move_to((x2+x2off1)*RIGHT + (y2+y2off0)*UP)
#Row 2
shiftIM2[4].move_to((x2-x2off1)*RIGHT + (y2+y2off1)*UP)
shiftIM2[5].move_to((x2-x2off0)*RIGHT + (y2+y2off1)*UP)
shiftIM2[6].move_to((x2+x2off0)*RIGHT + (y2+y2off1)*UP)
shiftIM2[7].move_to((x2+x2off1)*RIGHT + (y2+y2off1)*UP)
#Get manim indices of BG
mnmidx = manimBG2.get_entries()
#Create VGroups for H
Hz2 = VGroup()
#Obtain H from BG for Z2
for i in range(len(shiftval2)) :
#Obtain the index of BG
self.play(
Indicate(mnmidx[i],color=RED),
TransformFromCopy(mnmidx[i],shiftIM2[i])
)
#Add to VGroup
Hz2.add(shiftIM2[i])
#Add H_z2 text and brackets
txtH2.move_to(-5.5*RIGHT+ -1*UP)
mnmbrack2 = add_brack(Hz2,Zval=4)
Hbrack2 = VGroup()
Hbrack2.add(txtH2)
Hbrack2.add(mnmbrack2)
self.play(FadeIn(Hbrack2))
self.wait(1)
#################################################################################################################
'''
Animation 3 : Obtain Tanner graph from H
'''
#################################################################################################################
class mattoTG(Scene) :
'''
Each highlighted row of a matrix is converted
to a set of nodes and edges of the Tanner Graph
'''
def construct(self) :
'''
To construct the animation
'''
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z = 3
Nlayers = 2
shiftval = [1,-1,2,-1,-1,0,-1,2]
cshift = []
shiftIM = []
arrIM = []
H = VGroup()
#Create the shiftvalues and matrices
for i in range(len(shiftval)) :
cshift.append(Tex(str(shiftval[i]),color=BLACK))
mnm,npa = genshiftedIM(shiftval[i],N=Z)
mnm = mnm.set_color(BLACK)
brack = mnm.get_brackets()
brack[0].set_color(WHITE)
brack[1].set_color(WHITE)
shiftIM.append(mnm)
H.add(shiftIM[i])
arrIM.append(npa)
#Define the shift/matrices position
x,y = 2,3
#Matrix positions
xm,ym = 3,2
#Row 1
cshift[0].move_to(x*RIGHT + y*UP)
cshift[1].move_to((x+1)*RIGHT + y*UP)
cshift[2].move_to((x+2)*RIGHT + y*UP)
cshift[3].move_to((x+3)*RIGHT + y*UP)
#Row 2
cshift[4].move_to(x*RIGHT + (y-1)*UP)
cshift[5].move_to((x+1)*RIGHT + (y-1)*UP)
cshift[6].move_to((x+2)*RIGHT + (y-1)*UP)
cshift[7].move_to((x+3)*RIGHT + (y-1)*UP)
#Tanner graph initialization
CNnames = ['A','B','C','D','E','F']
VNnames = ['0', '1', '2', '3', '4', '5','6','7','8','9','10','11']
tg = tanner_graph(CNnames,len(VNnames),VNstoCN(array(arrIM[:4]))+","+VNstoCN(array(arrIM[4:])))
xycoord = {'yCN3' : np.arange(2,-3,-2),
'yVN9' : np.arange(8/3,-3,-2/3),
'yCN6' : np.arange(2.5,-3,-1),
'yVN12' : np.arange(3.3,-3.4,-3/5)
}
#Give coordinates based on CN-VN length
if len(CNnames) == 6 and len(VNnames) == 12 :
yCN = '6'
yVN = '12'
elif len(CNnames) == 3 and len(VNnames) == 9 :
yCN = '3'
yVN = '9'
#CN coordinates
x1 = -6
pos = np.zeros((len(CNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yCN'+yCN]
mnx.map_attr('pos', CNnames, pos, tg)
#VN coordinates
x1 = -2
pos = np.zeros((len(VNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yVN'+yVN]
mnx.map_attr('pos',VNnames , pos, tg)
#define the manim graph
mtg = mnx.ManimGraph(tg,tg_node,tg_edge)
#Begin animation
#Create VGroups for PM
PM = VGroup()
#Entrance of shift values
for i in range(len(shiftval)) :
self.play(FadeIn(cshift[i]))
PM.add(cshift[i])
#Add PM text and brackets
txtPM = MathTex("PM_{Z=3} =",color=BLACK)
txtPM.move_to((x-2)*RIGHT+ (y-0.5)*UP)
mnmbrack = add_brack(PM,Zval=2)
PMbrack = VGroup()
PMbrack.add(txtPM)
PMbrack.add(mnmbrack)
self.play(FadeIn(PMbrack))
self.wait(1)
#Fix matrix locations
#Row 1
shiftIM[0].move_to((xm-3)*RIGHT + (ym-2)*UP)
shiftIM[1].move_to((xm-1)*RIGHT + (ym-2)*UP)
shiftIM[2].move_to((xm+1)*RIGHT + (ym-2)*UP)
shiftIM[3].move_to((xm+3)*RIGHT + (ym-2)*UP)
#Row 2
shiftIM[4].move_to((xm-3)*RIGHT + (ym-3.5)*UP)
shiftIM[5].move_to((xm-1)*RIGHT + (ym-3.5)*UP)
shiftIM[6].move_to((xm+1)*RIGHT + (ym-3.5)*UP)
shiftIM[7].move_to((xm+3)*RIGHT + (ym-3.5)*UP)
#Animate the matrices (highlight shift + transform)
for i in range(len(shiftval)) :
self.play(
Indicate(cshift[i],color=RED),
TransformFromCopy(cshift[i],shiftIM[i])
)
#Add box
boxH = SurroundingRectangle(H,color=BLACK)
self.play(FadeIn(boxH))
#Labels around the matrices
CNlabel = []
VNlabel = []
x2 = xm-4.35
y2 = ym-1.6
for i in range(len(CNnames)) :
CNlabel.append(MathTex(r"C_{",CNnames[i],"}",height=0.2, color=BLACK))
if i < len(CNnames)/Nlayers :
CNlabel[i].move_to((x2*RIGHT + (y2-0.4*i)*UP))
else :
CNlabel[i].move_to((x2*RIGHT + (y2-0.3-0.4*i)*UP))
x3 = xm-3.6
y3 = ym-1
for i in range(len(VNnames)) :
VNlabel.append(MathTex("V_{",VNnames[i],"}",height=0.2, color=BLACK))
VNlabel[i].move_to(((x3+0.65*i+0.05*(i//Z))*RIGHT + y3*UP))
#Tanner graph animation
self.play(*[FadeIn(mtg.nodes[tg.nodes[nd]['mob_id']])
for nd in CNnames],
*[FadeIn(CNlabel[i]) for i in range(len(CNnames))]
)
self.play(*[FadeIn(mtg.nodes[tg.nodes[nd]['mob_id']])
for nd in VNnames],
*[FadeIn(VNlabel[i]) for i in range(len(VNnames))]
)
self.add_foreground_mobjects(*mtg.nodes.values())
self.wait(1)
#Add CN-VN labels along matrix?
'''
If h/w implemented, then use mod to get
non-zero values
'''
#Loop across number of shiftvalues
nz = []
for i in range(len(shiftval)) :
#Get non-zero indices in each expansion
nz.append(np.where(arrIM[i]!=0))
#Loop across rows/CNs
redidx = np.zeros(len(shiftval))
#Fading group
edgefade = VGroup()
for k in range(Nlayers) :
for i in range(Z) :
#Take nz values of each row across shifts
rowvg = VGroup()
onesvg = VGroup()
for j in range(int(len(shiftval)/Nlayers)) :
#Shift-index based on layer value
jlayer = int(k*int(len(shiftval)/Nlayers))+ j
#Skip zero blocks
if len(nz[jlayer][1]) == 0 :
continue
#Make nz of each expansion red
colnz = nz[jlayer][1][i] #Choose nz column of ith row/CN
#This is done since manimmatrix.get_entries() has
#linear/flat indices which go from 0-9 for a 3x3
#matrix like in matlab
nzred = np.ravel_multi_index([[i],[colnz]],(Z,Z)) #Obtain flat index value
#This is done since the VNlabels are flat
#from 0 to len(shiftval)
labelred = np.ravel_multi_index([[j],[colnz]],(int(len(shiftval)/Nlayers),Z)) #Obtain flat label index
mnmidx = shiftIM[jlayer].get_entries()
#Create row group
rowvg.add(shiftIM[jlayer].get_rows()[i])
#Make nz ith row indices highlight
onesvg.add(mnmidx[nzred[0]])
onesvg.add(VNlabel[labelred[0]])
onesvg.add(CNlabel[int(k*Z)+i])
self.play(Indicate(onesvg,color=RED))
self.wait(1)
#Fade the older edges
edgefade.set_opacity(0.15)
#Make edges red initially
self.play(*[TransformFromCopy(rowvg,mtg.edges[tg.edges[e]['mob_id']])
for e in tg.edges(CNnames[int(k*Z)+i],keys=True)])
#Add older edges to fade
for e in tg.edges(CNnames[k*Z+i],keys=True) :
edgefade.add(mtg.edges[tg.edges[e]['mob_id']])
self.wait(1)
#Unfade all edges
edgefade.set_opacity(1)
#################################################################################################################
'''
Animation 4 : Layered v/s Flooding
'''
#################################################################################################################
class FloodvsLayer(Scene) :
'''
Move circle through/over edge from VN to CN
and square through edge from CN to VN
'''
def construct(self) :
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z = 3
shiftval = [1,-1,2,-1,-1,0,-1,2]
#Total edges = NZ*Z (since each NZ entry upon
#expansion has Z non-zero entries => Z edges)
Nedges = Z*len(np.where(np.array(shiftval)!=-1)[0])
Niter = 1
arrIM = []
#Create the shiftvalues and matrices
for i in range(len(shiftval)) :
_,npa = genshiftedIM(shiftval[i],N=Z)
arrIM.append(npa)
#Tanner graph initialization
CNnames = ['A','B','C','D','E','F']
VNnames = ['0', '1', '2', '3', '4', '5','6','7','8','9','10','11']
tg = tanner_graph(CNnames,len(VNnames),VNstoCN(array(arrIM[:4]))+","+VNstoCN(array(arrIM[4:])))
xycoord = {'yCN3' : np.arange(2,-3,-2),
'yVN9' : np.arange(8/3,-3,-2/3),
'yCN6' : np.arange(2.5,-3,-1),
'yVN12' : np.arange(3.3,-3.4,-3/5)
}
#Give coordinates based on CN-VN length
if len(CNnames) == 6 and len(VNnames) == 12 :
yCN = '6'
yVN = '12'
elif len(CNnames) == 3 and len(VNnames) == 9 :
yCN = '3'
yVN = '9'
#CN coordinates
x1 = -3
pos = np.zeros((len(CNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yCN'+yCN]
mnx.map_attr('pos', CNnames, pos, tg)
#VN coordinates
x1 = 2
pos = np.zeros((len(VNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yVN'+yVN]
mnx.map_attr('pos',VNnames , pos, tg)
#define the manim graph
mtg = mnx.ManimGraph(tg,tg_node,tg_edge)
#Add to make edges go behind variables
self.add_foreground_mobjects(*mtg.nodes.values())
#Begin fade animation
CNVNgroups = []
########################################### FLOODING ###################################################
#Text heading
txtFlood = Text("Flooding schedule",color=BLACK).scale(0.5)
self.play(FadeIn(txtFlood.move_to(4*LEFT+3.5*UP)),run_time=1)
#Add for highlight/indication
CNonly = VGroup()
VNonly = VGroup()
#Add edge and their reverse in groups
edgeCNVNcxns = VGroup()
revCNVNcxns = VGroup()
#Add all VNs
for vnnd in VNnames :
VNonly.add(mtg.nodes[tg.nodes[vnnd]['mob_id']])
#Add all CNs
for cnnd in CNnames :
CNonly.add(mtg.nodes[tg.nodes[cnnd]['mob_id']])
#Add edges and revedge
for ng in tg[cnnd] :
e = (cnnd,ng,0)
erev = (ng,cnnd,0)
edgeCNVNcxns.add(mtg.edges[tg.edges[e]['mob_id']])
revCNVNcxns.add(mtg.edges[tg.edges[erev]['mob_id']])
#Make edges visible
self.add(edgeCNVNcxns.set_opacity(1))
#Perform MP
for iters in range(Niter) :
#Send messages from VN to CN
msg = []
for j in range(Nedges) :
msg.append(Dot().set_color(PURPLE_B))
self.play(*[MoveAlongPath(msg[j],revCNVNcxns[j]) for j in range(Nedges)],rate_func=slow_into)
for j in range(Nedges) :
msg[j].set_opacity(0)
self.play(Indicate(CNonly,color=PURPLE_B),scale_factor=1.1)
#Send messages from CN to VN
msg = []
for j in range(Nedges) :
msg.append(Dot().set_color(GREEN))
self.play(*[MoveAlongPath(msg[j],edgeCNVNcxns[j]) for j in range(Nedges)],rate_func=slow_into)
for j in range(Nedges) :
msg[j].set_opacity(0)
self.play(Indicate(VNonly,color=GREEN),scale_factor=1.05)
self.play(FadeOut(txtFlood))
self.wait(1)
########################################### LAYERED #####################################################
#Text heading
txtLayer = Text("Layered schedule",color=BLACK).scale(0.5)
self.play(FadeIn(txtLayer.move_to(4*LEFT+3.5*UP)),run_time=1)
#Obtain group for animation along edge and reverse
edgeCNVNgroups = []
revCNVNgroups = []
CNgroups = []
VNgroups = []
for cn in range(len(CNnames)) :
#Add for highlight/indication
CNonly = VGroup()
VNonly = VGroup()
#Add edge and their reverse in groups
edgeCNVNcxns = VGroup()
revCNVNcxns = VGroup()
#Do MP
cnnd = CNnames[cn]
CNonly.add(mtg.nodes[tg.nodes[cnnd]['mob_id']])
#Use neighbors to add connected VNs/edges
for ng in tg[cnnd] :
VNonly.add(mtg.nodes[tg.nodes[ng]['mob_id']])
e = (cnnd,ng,0)
erev = (ng,cnnd,0)
edgeCNVNcxns.add(mtg.edges[tg.edges[e]['mob_id']])
revCNVNcxns.add(mtg.edges[tg.edges[erev]['mob_id']])
#Add groups into list
CNgroups.append(CNonly)
VNgroups.append(VNonly)
edgeCNVNgroups.append(edgeCNVNcxns)
revCNVNgroups.append(revCNVNcxns)
#Make edges visible
self.add(*[CNgroups[i].set_opacity(0.15) for i in range(len(CNgroups))],
*[VNgroups[i].set_opacity(0.15) for i in range(len(VNgroups))],
*[edgeCNVNgroups[i].set_opacity(0.15) for i in range(len(edgeCNVNgroups))])
#Perform MP
for iters in range(Niter) :
for cn in range(len(CNnames)) :
#Send messages from VN to CN
#Darken
CNgroups[cn].set_opacity(1)
VNgroups[cn].set_opacity(1)
edgeCNVNgroups[cn].set_opacity(1)
msg = []
for j in range(len(revCNVNgroups[cn])) :
msg.append(Dot().set_color(PURPLE_B))
self.play(*[MoveAlongPath(msg[j],revCNVNgroups[cn][j]) for j in range(len(revCNVNgroups[cn]))],rate_func=slow_into)
#Fade the message ball
for j in range(len(revCNVNgroups[cn])) :
msg[j].set_opacity(0)
self.play(Indicate(CNgroups[cn],color=PURPLE_B))
#Send messages from CN to VN
msg = []
for j in range(len(revCNVNgroups[cn])) :
msg.append(Dot().set_color(GREEN))
self.play(*[MoveAlongPath(msg[j],edgeCNVNgroups[cn][j]) for j in range(len(revCNVNgroups[cn]))],rate_func=slow_into)
#Fade the message ball
for j in range(len(revCNVNgroups[cn])) :
msg[j].set_opacity(0)
self.play(Indicate(VNgroups[cn],color=GREEN),scale_factor=1.1)
#Lighten
CNgroups[cn].set_opacity(0.15)
VNgroups[cn].set_opacity(0.15)
edgeCNVNgroups[cn].set_opacity(0.15)
#################################################################################################################
'''
Animation 5 : Algorithm flow with time
'''
#################################################################################################################
class BGandTG(Scene) :
'''
Simultaneously show side-by-side functioning
of base graph and tanner graph
'''
def construct(self) :
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z = 3
shiftval = [1,0,1,1,0,-1,0,0]
arrIM = []
#Create the shiftvalues and matrices
for i in range(len(shiftval)) :
_,npa = genshiftedIM(shiftval[i],N=Z)
arrIM.append(npa)
#Tanner graph initialization
CNnames = ['A','B','C','D','E','F']
VNnames = ['0', '1', '2', '3', '4', '5','6','7','8','9','10','11']
tg = tanner_graph(CNnames,len(VNnames),VNstoCN(array(arrIM[:4]))+","+VNstoCN(array(arrIM[4:])))
xycoord = {'yCN3' : np.arange(2,-3,-2),
'yVN9' : np.arange(8/3,-3,-2/3),
'yCN6' : np.arange(2.5,-3,-1),
'yVN12' : np.arange(3.3,-3.4,-3/5)
}
#Give coordinates based on CN-VN length
if len(CNnames) == 6 and len(VNnames) == 12 :
yCN = '6'
yVN = '12'
elif len(CNnames) == 3 and len(VNnames) == 9 :
yCN = '3'
yVN = '9'
#CN coordinates
x1 = -3
pos = np.zeros((len(CNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yCN'+yCN]
mnx.map_attr('pos', CNnames, pos, tg)
#VN coordinates
x1 = 2
pos = np.zeros((len(VNnames), 2))
pos[:, 0] = x1
pos[:, 1] = xycoord['yVN'+yVN]
mnx.map_attr('pos',VNnames , pos, tg)
#define the manim graph
mtg = mnx.ManimGraph(tg,tg_node,tg_edge)
#Add to make edges go behind variables
self.add_foreground_mobjects(*mtg.nodes.values())
#Begin fade animation
CNVNgroups = []
for BGrows in range(int(len(CNnames)/Z)) :
#Need to include Z CNs in all BG columns
for BGcol in range(int(len(VNnames)/Z)) :
#Remove group if shiftval is -1
if shiftval[int(BGrows*(len(VNnames)/Z))+BGcol] == -1 :
continue
CNVNcxns = VGroup()
#Add all Z CNs in group
for nd in range(Z) :
#Obtain CN id
cnnd = CNnames[int(Z*BGrows)+nd]
#Add Z CNs
CNVNcxns.add(mtg.nodes[tg.nodes[cnnd]['mob_id']])
#Loop over neighbours
for ng in tg.neighbors(cnnd) :
#Add if neighbour is in the BGcol
lowbound = BGcol*Z
upbound = (BGcol+1)*Z
ngint = int(ng)
if ngint >= lowbound and ngint < upbound :
#Add VN to group
CNVNcxns.add(mtg.nodes[tg.nodes[ng]['mob_id']])
#Obtained by printing the e in tg.edges format
e = (cnnd,ng,0)
#Add edge
CNVNcxns.add(mtg.edges[tg.edges[e]['mob_id']])
break
#Add groups into loop
CNVNgroups.append(CNVNcxns)
#Fade all
self.add(*[CNVNgroups[i].set_opacity(0.15) for i in range(len(CNVNgroups))])
#Fade others
for i in range(len(CNVNgroups)) :
CNVNgroups[i].set_opacity(1)
self.wait(2)
CNVNgroups[i].set_opacity(0.15)
#################################################################################################################
'''
Animation 6 : Outlook on decoding
'''
#################################################################################################################
class QandTG(Scene) :
'''
Move circle through/over edge from VN to CN
and square through edge from CN to VN
'''
def construct(self) :
#Set background color
self.camera.background_color = WHITE
#Constants and variables
Z = 3
#shiftval = [0,2,-1,1,1,2,2,1]
shiftval = [1,-1,2,-1,-1,0,-1,2]
arrIM = []
#Create the shiftvalues and matrices
for i in range(len(shiftval)) :
_,npa = genshiftedIM(shiftval[i],N=Z)
arrIM.append(npa)
#Tanner graph initialization
CNnames = ['A','B','C','D','E','F']
#CNnames = ['12','13','14','15','16','17']
VNnames = ['0', '1', '2', '3', '4', '5','6','7','8','9','10','11']
#QNnames = []
QNnames = ['G','H','I','J','K','L','M','N','O','P','Q','R']
tg = tanner_graph_OUTLOOK(CNnames,len(VNnames),VNstoCN(array(arrIM[:4])) + ","+VNstoCN(array(arrIM[4:])),QNnames)
xycoord = {'yCN3' : np.arange(2,-3,-2),
'yVN9' : np.arange(8/3,-3,-2/3),
'yCN6' : np.arange(2.5,-3,-1),
'yVN12' : np.arange(3.3,-3.4,-3/5)
}
#Give coordinates based on CN-VN length
if len(CNnames) == 6 and len(VNnames) == 12 :
yCN = '6'
yVN = '12'
elif len(CNnames) == 3 and len(VNnames) == 9 :
yCN = '3'
yVN = '9'