forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncertainty.py
4739 lines (4275 loc) · 150 KB
/
uncertainty.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
# -*- coding: utf-8 -*-
import scipy
from manimlib.imports import *
from old_projects.fourier import *
import warnings
warnings.warn("""
Warning: This file makes use of
ContinualAnimation, which has since
been deprecated
""")
FREQUENCY_COLOR = RED
USE_ALMOST_FOURIER_BY_DEFAULT = False
class GaussianDistributionWrapper(Line):
"""
This is meant to encode a 2d normal distribution as
a mobject (so as to be able to have it be interpolated
during animations). It is a line whose center is the mean
mu of a distribution, and whose radial vector (center to end)
is the distribution's standard deviation
"""
CONFIG = {
"stroke_width" : 0,
"mu" : ORIGIN,
"sigma" : RIGHT,
}
def __init__(self, **kwargs):
Line.__init__(self, ORIGIN, RIGHT, **kwargs)
self.change_parameters(self.mu, self.sigma)
def change_parameters(self, mu = None, sigma = None):
curr_mu, curr_sigma = self.get_parameters()
mu = mu if mu is not None else curr_mu
sigma = sigma if sigma is not None else curr_sigma
self.put_start_and_end_on(mu - sigma, mu + sigma)
return self
def get_parameters(self):
""" Return mu_x, mu_y, sigma_x, sigma_y"""
center, end = self.get_center(), self.get_end()
return center, end-center
def get_random_points(self, size = 1):
mu, sigma = self.get_parameters()
return np.array([
np.array([
np.random.normal(mu_coord, sigma_coord)
for mu_coord, sigma_coord in zip(mu, sigma)
])
for x in range(size)
])
class ProbabalisticMobjectCloud(ContinualAnimation):
CONFIG = {
"fill_opacity" : 0.25,
"n_copies" : 100,
"gaussian_distribution_wrapper_config" : {},
"time_per_change" : 1./60,
"start_up_time" : 0,
}
def __init__(self, prototype, **kwargs):
digest_config(self, kwargs)
fill_opacity = self.fill_opacity or prototype.get_fill_opacity()
if "mu" not in self.gaussian_distribution_wrapper_config:
self.gaussian_distribution_wrapper_config["mu"] = prototype.get_center()
self.gaussian_distribution_wrapper = GaussianDistributionWrapper(
**self.gaussian_distribution_wrapper_config
)
self.time_since_last_change = np.inf
group = VGroup(*[
prototype.copy().set_fill(opacity = fill_opacity)
for x in range(self.n_copies)
])
ContinualAnimation.__init__(self, group, **kwargs)
self.update_mobject(0)
def update_mobject(self, dt):
self.time_since_last_change += dt
if self.time_since_last_change < self.time_per_change:
return
self.time_since_last_change = 0
group = self.mobject
points = self.gaussian_distribution_wrapper.get_random_points(len(group))
for mob, point in zip(group, points):
self.update_mobject_by_point(mob, point)
return self
def update_mobject_by_point(self, mobject, point):
mobject.move_to(point)
return self
class ProbabalisticDotCloud(ProbabalisticMobjectCloud):
CONFIG = {
"color" : BLUE,
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
dot = Dot(color = self.color)
ProbabalisticMobjectCloud.__init__(self, dot)
class ProbabalisticVectorCloud(ProbabalisticMobjectCloud):
CONFIG = {
"color" : RED,
"n_copies" : 20,
"fill_opacity" : 0.5,
"center_func" : lambda : ORIGIN,
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
vector = Vector(
RIGHT, color = self.color,
max_tip_length_to_length_ratio = 1,
)
ProbabalisticMobjectCloud.__init__(self, vector)
def update_mobject_by_point(self, vector, point):
vector.put_start_and_end_on(
self.center_func(),
point
)
class RadarDish(SVGMobject):
CONFIG = {
"file_name" : "radar_dish",
"fill_color" : LIGHT_GREY,
"stroke_color" : WHITE,
"stroke_width" : 1,
"height" : 1,
}
class Plane(SVGMobject):
CONFIG = {
"file_name" : "plane",
"color" : LIGHT_GREY,
"height" : 1,
}
def __init__(self, **kwargs):
SVGMobject.__init__(self, **kwargs)
self.rotate(-TAU/4)
class FalconHeavy(SVGMobject):
CONFIG = {
"file_name" : "falcon_heavy",
"color" : WHITE,
"logo_color" : BLUE_E,
"height" : 1.5,
}
def __init__(self, **kwargs):
SVGMobject.__init__(self, **kwargs)
self.logo = self[-9:]
self.logo.set_color(self.logo_color)
class RadarPulseSingleton(ContinualAnimation):
CONFIG = {
"speed" : 3.0,
"direction" : RIGHT,
"start_up_time" : 0,
"fade_in_time" : 0.5,
"color" : WHITE,
"stroke_width" : 3,
}
def __init__(self, radar_dish, target, **kwargs):
digest_config(self, kwargs)
self.direction = self.direction/get_norm(self.direction)
self.radar_dish = radar_dish
self.target = target
self.reflection_distance = None
self.arc = Arc(
start_angle = -30*DEGREES,
angle = 60*DEGREES,
)
self.arc.set_height(0.75*radar_dish.get_height())
self.arc.move_to(radar_dish, UP+RIGHT)
self.start_points = np.array(self.arc.points)
self.start_center = self.arc.get_center()
self.finished = False
ContinualAnimation.__init__(self, self.arc, **kwargs)
def update_mobject(self, dt):
arc = self.arc
total_distance = self.speed*self.internal_time
arc.points = np.array(self.start_points)
arc.shift(total_distance*self.direction)
if self.internal_time < self.fade_in_time:
alpha = np.clip(self.internal_time/self.fade_in_time, 0, 1)
arc.set_stroke(self.color, alpha*self.stroke_width)
if self.reflection_distance is None:
#Check if reflection is happening
arc_point = arc.get_edge_center(self.direction)
target_point = self.target.get_edge_center(-self.direction)
arc_distance = np.dot(arc_point, self.direction)
target_distance = np.dot(target_point, self.direction)
if arc_distance > target_distance:
self.reflection_distance = target_distance
#Don't use elif in case the above code creates reflection_distance
if self.reflection_distance is not None:
delta_distance = total_distance - self.reflection_distance
point_distances = np.dot(self.direction, arc.points.T)
diffs = point_distances - self.reflection_distance
shift_vals = np.outer(-2*np.maximum(diffs, 0), self.direction)
arc.points += shift_vals
#Check if done
arc_point = arc.get_edge_center(-self.direction)
if np.dot(arc_point, self.direction) < np.dot(self.start_center, self.direction):
self.finished = True
self.arc.fade(1)
def is_finished(self):
return self.finished
class RadarPulse(ContinualAnimation):
CONFIG = {
"n_pulse_singletons" : 8,
"frequency" : 0.05,
"colors" : [BLUE, YELLOW]
}
def __init__(self, *args, **kwargs):
digest_config(self, kwargs)
colors = color_gradient(self.colors, self.n_pulse_singletons)
self.pulse_singletons = [
RadarPulseSingleton(*args, color = color, **kwargs)
for color in colors
]
pluse_mobjects = VGroup(*[ps.mobject for ps in self.pulse_singletons])
ContinualAnimation.__init__(self, pluse_mobjects, **kwargs)
def update_mobject(self, dt):
for i, ps in enumerate(self.pulse_singletons):
ps.internal_time = self.internal_time - i*self.frequency
ps.update_mobject(dt)
def is_finished(self):
return all([ps.is_finished() for ps in self.pulse_singletons])
class MultipleFlashes(Succession):
CONFIG = {
"run_time_per_flash" : 1.0,
"num_flashes" : 3,
}
def __init__(self, *args, **kwargs):
digest_config(self, kwargs)
kwargs["run_time"] = self.run_time_per_flash
Succession.__init__(self, *[
Flash(*args, **kwargs)
for x in range(self.num_flashes)
])
class TrafficLight(SVGMobject):
CONFIG = {
"file_name" : "traffic_light",
"height" : 0.7,
"post_height" : 2,
"post_width" : 0.05,
}
def __init__(self, **kwargs):
SVGMobject.__init__(self, **kwargs)
post = Rectangle(
height = self.post_height,
width = self.post_width,
stroke_width = 0,
fill_color = WHITE,
fill_opacity = 1,
)
self.move_to(post.get_top(), DOWN)
self.add_to_back(post)
###################
class MentionUncertaintyPrinciple(TeacherStudentsScene):
def construct(self):
title = TextMobject("Heisenberg Uncertainty Principle")
title.to_edge(UP)
dot_cloud = ProbabalisticDotCloud()
vector_cloud = ProbabalisticVectorCloud(
gaussian_distribution_wrapper_config = {"sigma_x" : 0.2},
center_func = lambda : dot_cloud.gaussian_distribution_wrapper.get_parameters()[0],
)
for cloud in dot_cloud, vector_cloud:
cloud.gaussian_distribution_wrapper.next_to(
title, DOWN, 2*LARGE_BUFF
)
vector_cloud.gaussian_distribution_wrapper.shift(3*RIGHT)
def get_brace_text_group_update(gdw, vect, text, color):
brace = Brace(gdw, vect)
text = brace.get_tex("2\\sigma_{\\text{%s}}"%text, buff = SMALL_BUFF)
group = VGroup(brace, text)
def update_group(group):
brace, text = group
brace.match_width(gdw, stretch = True)
brace.next_to(gdw, vect)
text.next_to(brace, vect, buff = SMALL_BUFF)
group.set_color(color)
return Mobject.add_updater(group, update_group)
dot_brace_anim = get_brace_text_group_update(
dot_cloud.gaussian_distribution_wrapper,
DOWN, "position", dot_cloud.color
)
vector_brace_anim = get_brace_text_group_update(
vector_cloud.gaussian_distribution_wrapper,
UP, "momentum", vector_cloud.color
)
self.add(title)
self.add(dot_cloud)
self.play(
Write(title),
self.teacher.change, "raise_right_hand",
self.get_student_changes(*["pondering"]*3)
)
self.play(
Write(dot_brace_anim.mobject, run_time = 1)
)
self.add(dot_brace_anim)
self.wait()
# self.wait(2)
self.play(
dot_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : 0.1*RIGHT},
run_time = 2,
)
self.wait()
self.add(vector_cloud)
self.play(
FadeIn(vector_brace_anim.mobject)
)
self.add(vector_brace_anim)
self.play(
vector_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : RIGHT},
self.get_student_changes(*3*["confused"]),
run_time = 3,
)
#Back and forth
for x in range(2):
self.play(
dot_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : 2*RIGHT},
vector_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : 0.1*RIGHT},
run_time = 3,
)
self.change_student_modes("thinking", "erm", "sassy")
self.play(
dot_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : 0.1*RIGHT},
vector_cloud.gaussian_distribution_wrapper.change_parameters,
{"sigma" : 1*RIGHT},
run_time = 3,
)
self.wait()
class FourierTradeoff(Scene):
CONFIG = {
"show_text" : True,
"complex_to_real_func" : lambda z : z.real,
"widths" : [6, 0.02, 1],
}
def construct(self):
#Setup axes
time_mean = 4
time_axes = Axes(
x_min = 0,
x_max = 2*time_mean,
x_axis_config = {"unit_size" : 1.5},
y_min = -2,
y_max = 2,
y_axis_config = {"unit_size" : 0.5}
)
time_label = TextMobject("Time")
time_label.scale(1.5)
time_label.next_to(
time_axes.x_axis.get_right(), UP+LEFT,
buff = MED_SMALL_BUFF,
)
time_axes.add(time_label)
time_axes.center().to_edge(UP)
time_axes.x_axis.add_numbers(*list(range(1, 2*time_mean)))
frequency_axes = Axes(
x_min = 0,
x_max = 8,
x_axis_config = {"unit_size" : 1.5},
y_min = -0.025,
y_max = 0.075,
y_axis_config = {
"unit_size" : 30,
"tick_frequency" : 0.025,
},
color = TEAL,
)
frequency_label = TextMobject("Frequency")
frequency_label.scale(1.5)
frequency_label.next_to(
frequency_axes.x_axis.get_right(), UP+LEFT,
buff = MED_SMALL_BUFF,
)
frequency_label.set_color(FREQUENCY_COLOR)
frequency_axes.add(frequency_label)
frequency_axes.move_to(time_axes, LEFT)
frequency_axes.to_edge(DOWN, buff = LARGE_BUFF)
frequency_axes.x_axis.add_numbers()
# Graph information
#x-coordinate of this point determines width of wave_packet graph
width_tracker = ExponentialValueTracker(0.5)
get_width = width_tracker.get_value
def get_wave_packet_function():
factor = 1./get_width()
return lambda t : (factor**0.25)*np.cos(4*TAU*t)*np.exp(-factor*(t-time_mean)**2)
def get_wave_packet():
graph = time_axes.get_graph(
get_wave_packet_function(),
num_graph_points = 200,
)
graph.set_color(YELLOW)
return graph
time_radius = 10
def get_wave_packet_fourier_transform():
return get_fourier_graph(
frequency_axes,
get_wave_packet_function(),
t_min = time_mean - time_radius,
t_max = time_mean + time_radius,
n_samples = 2*time_radius*17,
complex_to_real_func = self.complex_to_real_func,
color = FREQUENCY_COLOR,
)
wave_packet = get_wave_packet()
wave_packet_update = UpdateFromFunc(
wave_packet,
lambda g : Transform(g, get_wave_packet()).update(1)
)
fourier_graph = get_wave_packet_fourier_transform()
fourier_graph_update = UpdateFromFunc(
fourier_graph,
lambda g : Transform(g, get_wave_packet_fourier_transform()).update(1)
)
arrow = Arrow(
wave_packet, frequency_axes.coords_to_point(
4, frequency_axes.y_max/2,
),
color = FREQUENCY_COLOR,
)
fourier_words = TextMobject("Fourier Transform")
fourier_words.next_to(arrow, LEFT, buff = MED_LARGE_BUFF)
sub_words = TextMobject("(To be explained shortly)")
sub_words.set_color(BLUE)
sub_words.scale(0.75)
sub_words.next_to(fourier_words, DOWN)
#Draw items
self.add(time_axes, frequency_axes)
self.play(ShowCreation(wave_packet, rate_func = double_smooth))
anims = [ReplacementTransform(
wave_packet.copy(), fourier_graph
)]
if self.show_text:
anims += [
GrowArrow(arrow),
Write(fourier_words, run_time = 1)
]
self.play(*anims)
# self.play(FadeOut(arrow))
self.wait()
for width in self.widths:
self.play(
width_tracker.set_value, width,
wave_packet_update,
fourier_graph_update,
run_time = 3
)
if sub_words not in self.mobjects and self.show_text:
self.play(FadeIn(sub_words))
else:
self.wait()
self.wait()
class ShowPlan(PiCreatureScene):
def construct(self):
self.add_title()
words = self.get_words()
self.play_sound_anims(words[0])
self.play_doppler_anims(words[1])
self.play_quantum_anims(words[2])
def add_title(self):
title = TextMobject("The plan")
title.scale(1.5)
title.to_edge(UP)
h_line = Line(LEFT, RIGHT).scale(FRAME_X_RADIUS)
h_line.next_to(title, DOWN)
self.add(title, h_line)
def get_words(self):
trips = [
("sound waves", "(time vs. frequency)", YELLOW),
("Doppler radar", "(distance vs. velocity)", GREEN),
("quantum particles", "(position vs. momentum)", BLUE),
]
words = VGroup()
for topic, tradeoff, color in trips:
word = TextMobject("Uncertainty for", topic, tradeoff)
word[1:].set_color(color)
word[2].scale(0.75)
word[2].next_to(word[1], DOWN, buff = 1.5*SMALL_BUFF)
words.add(word)
words.arrange(DOWN, aligned_edge = LEFT, buff = MED_LARGE_BUFF)
words.to_edge(LEFT)
return words
def play_sound_anims(self, word):
morty = self.pi_creature
wave = FunctionGraph(
lambda x : 0.3*np.sin(15*x)*np.sin(0.5*x),
x_min = 0, x_max = 30,
step_size = 0.001,
)
wave.next_to(word, RIGHT)
rect = BackgroundRectangle(wave, fill_opacity = 1)
rect.stretch(2, 1)
rect.next_to(wave, LEFT, buff = 0)
always_shift(wave, direction=LEFT, rate=5)
wave_fader = UpdateFromAlphaFunc(
wave,
lambda w, a : w.set_stroke(width = 3*a)
)
checkmark = self.get_checkmark(word)
self.add(wave)
self.add_foreground_mobjects(rect, word)
self.play(
Animation(word),
wave_fader,
morty.change, "raise_right_hand", word
)
self.wait(2)
wave_fader.rate_func = lambda a : 1-smooth(a)
self.add_foreground_mobjects(checkmark)
self.play(
Write(checkmark),
morty.change, "happy",
wave_fader,
)
self.remove_foreground_mobjects(rect, word)
self.add(word)
self.wait()
def play_doppler_anims(self, word):
morty = self.pi_creature
radar_dish = RadarDish()
radar_dish.next_to(word, DOWN, aligned_edge = LEFT)
target = Plane()
# target.match_height(radar_dish)
target.next_to(radar_dish, RIGHT, buff = LARGE_BUFF)
always_shift(target, direction = RIGHT, rate = 1.25)
pulse = RadarPulse(radar_dish, target)
checkmark = self.get_checkmark(word)
self.add(target)
self.play(
Write(word),
DrawBorderThenFill(radar_dish),
UpdateFromAlphaFunc(
target, lambda m, a : m.set_fill(opacity = a)
),
morty.change, "pondering",
run_time = 1
)
self.add(pulse)
count = it.count() #TODO, this is not a great hack...
while not pulse.is_finished() and next(count) < 15:
self.play(
morty.look_at, pulse.mobject,
run_time = 0.5
)
self.play(
Write(checkmark),
UpdateFromAlphaFunc(
target, lambda m, a : m.set_fill(opacity = 1-a)
),
FadeOut(radar_dish),
morty.change, "happy"
)
self.wait()
def play_quantum_anims(self, word):
morty = self.pi_creature
dot_cloud = ProbabalisticDotCloud()
gdw = dot_cloud.gaussian_distribution_wrapper
gdw.next_to(word, DOWN, MED_LARGE_BUFF)
gdw.rotate(5*DEGREES)
gdw.save_state()
gdw.scale(0)
checkmark = self.get_checkmark(word)
ish = TextMobject("$\\dots$ish")
ish.next_to(checkmark, RIGHT, -SMALL_BUFF, DOWN)
self.add(dot_cloud)
self.play(
Write(word),
FadeIn(dot_cloud.mobject),
morty.change, "confused",
)
self.play(gdw.restore, run_time = 2)
self.play(Write(checkmark))
self.wait()
self.play(
Write(ish),
morty.change, 'maybe'
)
self.wait(6)
##
def get_checkmark(self, word):
checkmark = TexMobject("\\checkmark")
checkmark.set_color(GREEN)
checkmark.scale(1.25)
checkmark.next_to(word[1], UP+RIGHT, buff = 0)
return checkmark
class StartWithIntuition(TeacherStudentsScene):
def construct(self):
self.teacher_says(
"You already \\\\ have this \\\\ intuition",
bubble_kwargs = {
"height" : 3.5,
"width" : 3,
},
)
self.change_student_modes("pondering", "erm", "maybe")
self.look_at(VectorizedPoint(4*LEFT + 2*UP))
self.wait(5)
class TwoCarsAtRedLight(Scene):
CONFIG = {
"text_scale_val" : 0.75,
}
def construct(self):
self.pull_up_behind()
self.flash_in_sync_short_time()
self.show_low_confidence()
self.flash_in_sync_long_time()
self.show_high_confidence()
def pull_up_behind(self):
#Setup Traffic light
traffic_light = TrafficLight()
traffic_light.move_to(6*RIGHT + 2.5*DOWN, DOWN)
source_point = VectorizedPoint(
traffic_light[2].get_right()
)
screen = Line(ORIGIN, UP)
screen.next_to(source_point, RIGHT, LARGE_BUFF)
red_light = Spotlight(
color = RED,
source_point = source_point,
radius = 0.5,
screen = screen,
num_levels = 20,
opacity_function = lambda r : 1/(10*r**2+1)
)
red_light.fade(0.5)
red_light.rotate(TAU/2, about_edge = LEFT)
self.add(red_light, traffic_light)
#Setup cars
car1, car2 = cars = self.cars = VGroup(*[
Car() for x in range(2)
])
cars.arrange(RIGHT, buff = LARGE_BUFF)
cars.next_to(
traffic_light, LEFT,
buff = LARGE_BUFF, aligned_edge = DOWN
)
car2.pi_creature.set_color(GREY_BROWN)
car1.start_point = car1.get_corner(DOWN+RIGHT)
car1.shift(FRAME_X_RADIUS*LEFT)
#Pull up car
self.add(cars)
self.play(
SwitchOn(
red_light,
rate_func = squish_rate_func(smooth, 0, 0.3),
),
Animation(traffic_light),
self.get_flashes(car2, num_flashes = 3),
MoveCar(
car1, car1.start_point,
run_time = 3,
rate_func = rush_from,
)
)
def flash_in_sync_short_time(self):
car1, car2 = cars = self.cars
#Setup axes
axes = Axes(
x_min = 0,
x_max = 5,
y_min = 0,
y_max = 2,
y_axis_config = {
"tick_frequency" : 0.5,
},
)
axes.x_axis.add_numbers(1, 2, 3)
time_label = TextMobject("Time")
time_label.scale(self.text_scale_val)
time_label.next_to(axes.x_axis.get_right(), DOWN)
y_title = TextMobject("Signal")
y_title.scale(self.text_scale_val)
y_title.next_to(axes.y_axis, UP, SMALL_BUFF)
axes.add(time_label, y_title)
axes.to_corner(UP+LEFT, buff = MED_SMALL_BUFF)
graph = axes.get_graph(
self.get_multispike_function(list(range(1, 4))),
x_min = 0.8,
x_max = 3.8,
)
graph.set_color(YELLOW)
#Label short duration
brace = Brace(Line(
axes.input_to_graph_point(1, graph),
axes.input_to_graph_point(3, graph),
), UP)
text = TextMobject("Short duration observation")
text.scale(self.text_scale_val)
text.next_to(brace, UP, SMALL_BUFF)
text.align_to(
axes.coords_to_point(0.25, 0), LEFT
)
self.play(
self.get_flashes(car1, num_flashes = 2),
self.get_flashes(car2, num_flashes = 2),
LaggedStartMap(FadeIn, VGroup(
axes, time_label, y_title,
))
)
self.play(
self.get_flashes(car1, num_flashes = 3),
self.get_flashes(car2, num_flashes = 3),
ShowCreation(graph, rate_func=linear, run_time = 3)
)
self.play(
self.get_flashes(car1, num_flashes = 10),
self.get_flashes(car2, num_flashes = 10, run_time_per_flash = 0.98),
GrowFromCenter(brace),
Write(text),
)
self.time_axes = axes
self.time_graph = graph
self.time_graph_label = VGroup(
brace, text
)
def show_low_confidence(self):
car1, car2 = cars = self.cars
time_axes = self.time_axes
#Setup axes
frequency_axes = Axes(
x_min = 0,
x_max = 3,
y_min = 0,
y_max = 1.5,
y_axis_config = {
"tick_frequency" : 0.5,
}
)
frequency_axes.next_to(time_axes, DOWN, LARGE_BUFF)
frequency_axes.set_color(LIGHT_GREY)
frequency_label = TextMobject("Frequency")
frequency_label.scale(self.text_scale_val)
frequency_label.next_to(frequency_axes.x_axis.get_right(), DOWN)
frequency_axes.add(
frequency_label,
VectorizedPoint(frequency_axes.y_axis.get_top())
)
frequency_axes.x_axis.add_numbers(1, 2)
frequency_graph = frequency_axes.get_graph(
lambda x : np.exp(-4*(x-1)**2),
x_min = 0,
x_max = 2,
)
frequency_graph.set_color(RED)
peak_point = frequency_axes.input_to_graph_point(
1, frequency_graph
)
#Setup label
label = TextMobject("Low confidence")
label.scale(self.text_scale_val)
label.move_to(peak_point + UP+RIGHT, DOWN)
label.match_color(frequency_graph)
arrow = Arrow(label.get_bottom(), peak_point, buff = 2*SMALL_BUFF)
arrow.match_color(frequency_graph)
self.play(
ReplacementTransform(
self.time_axes.copy(), frequency_axes
),
ReplacementTransform(
self.time_graph.copy(), frequency_graph
),
)
self.play(
Write(label),
GrowArrow(arrow)
)
self.wait()
self.frequency_axes = frequency_axes
self.frequency_graph = frequency_graph
self.frequency_graph_label = VGroup(
label, arrow
)
def flash_in_sync_long_time(self):
time_graph = self.time_graph
time_axes = self.time_axes
frequency_graph = self.frequency_graph
frequency_axes = self.frequency_axes
n_spikes = 12
new_time_graph = time_axes.get_graph(
self.get_multispike_function(list(range(1, n_spikes+1))),
x_min = 0.8,
x_max = n_spikes + 0.8,
)
new_time_graph.match_color(time_graph)
new_frequency_graph = frequency_axes.get_graph(
lambda x : np.exp(-500*(x-1)**2),
x_min = 0,
x_max = 2,
num_anchors = 500,
)
new_frequency_graph.match_color(self.frequency_graph)
def pin_freq_graph_end_points(freq_graph):
freq_graph.points[0] = frequency_axes.coords_to_point(0, 0)
freq_graph.points[-1] = frequency_axes.coords_to_point(2, 0)
self.play(LaggedStartMap(
FadeOut, VGroup(
self.time_graph_label,
self.frequency_graph_label,
self.time_graph,
)
))
self.play(
ApplyMethod(
self.time_axes.x_axis.stretch, 2.5, 0,
{"about_edge" : LEFT},
run_time = 4,
rate_func = squish_rate_func(smooth, 0.3, 0.6),
),
UpdateFromFunc(
self.time_axes.x_axis.tip,
lambda m : m.move_to(
self.time_axes.x_axis.get_right(),
LEFT
)
),
ShowCreation(
new_time_graph,
run_time = n_spikes,
rate_func=linear,
),
ApplyMethod(
frequency_graph.stretch, 0.1, 0,
run_time = n_spikes,
),
UpdateFromFunc(frequency_graph, pin_freq_graph_end_points),
*[
self.get_flashes(car, num_flashes = n_spikes)
for car in self.cars
]
)
self.new_time_graph = new_time_graph
self.new_frequency_graph = new_frequency_graph
def show_high_confidence(self):
#Frequency stuff
arrow = self.frequency_graph_label[1]
label = TextMobject("High confidence")
label.scale(self.text_scale_val)
label.next_to(arrow.get_start(), UP, SMALL_BUFF)
label.match_color(arrow)
frequency_axes = self.frequency_axes
#Time stuff
new_time_graph = self.new_time_graph
brace = Brace(new_time_graph, UP, buff = SMALL_BUFF)
text = TextMobject("Long duration observation")
text.scale(self.text_scale_val)
text.next_to(brace, UP, buff = SMALL_BUFF)
self.play(
FadeIn(label),
GrowArrow(arrow),
*list(map(self.get_flashes, self.cars))
)
self.play(
GrowFromCenter(brace),
Write(text, run_time = 1),
*list(map(self.get_flashes, self.cars))
)
self.play(*[
self.get_flashes(car, num_flashes = 10)
for car in self.cars
])
###
def get_flashes(self, car, colors = [YELLOW, RED], num_flashes = 1, **kwargs):
return AnimationGroup(*[
MultipleFlashes(light, color, num_flashes = num_flashes, **kwargs)
for light, color in zip(car.get_lights(), colors)
])
def get_multispike_function(self, spike_times):
return lambda x : sum([
1.25*np.exp(-100*(x-m)**2)
for m in spike_times
])
class VariousMusicalNotes(Scene):
def construct(self):
freq = 20
# x-coordinate of this point represents log(a)
# where the bell curve component of the signal
# is exp(-a*(x**2))
graph_width_tracker = ExponentialValueTracker(1)
def get_graph():
a = graph_width_tracker.get_value()
return FunctionGraph(
lambda x : np.exp(-a*x**2)*np.sin(freq*x)-0.5,
step_size = 0.001,
)
graph = get_graph()
def graph_update(graph):
graph.points = get_graph().points
graph_update_anim = UpdateFromFunc(graph, graph_update)
def change_width_anim(width, **kwargs):
a = 2.0/(width**2)
return AnimationGroup(
ApplyMethod(graph_width_tracker.set_value, a),
graph_update_anim,
**kwargs
)
change_width_anim(FRAME_X_RADIUS).update(1)
graph_update_anim.update(0)
phrases = [
TextMobject(*words.split(" "))
for words in [
"Very clear frequency",
"Less clear frequency",
"Extremely unclear frequency",
]
]
#Show graphs and phrases
widths = [FRAME_X_RADIUS, 1, 0.2]
for width, phrase in zip(widths, phrases):