aboutsummaryrefslogtreecommitdiffstats
path: root/scenes/v12_simpson.py
blob: 6092924c1a08df91ca2a086303ed99630e5e2a77 (plain)
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
# -*- coding: utf-8 -*-
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from manim import *
from tiktok import *

# datos reales (Charig et al., 1986): tratamiento A vs B para calculos renales
LEV = (("A", 81, 87), ("B", 234, 270))
GRA = (("A", 192, 263), ("B", 55, 80))
TOT = (("A", 81 + 192, 87 + 263), ("B", 234 + 55, 270 + 80))

BARW = 4.05         # ancho de barra al 100%
XL = -1.55          # borde izquierdo de las barras


class Simpson(TikTok):
    NAME = "simpson"

    def barra(self, letra, num, den, y, gana):
        pct = 100.0 * num / den
        col = VERDE if gana else GRIS
        lab = Text(letra, font=FONT, font_size=50, weight=BOLD,
                   color=CELESTE if letra == "A" else ROSA)
        lab.move_to([-3.65, y, 0])
        track = RoundedRectangle(corner_radius=0.12, width=BARW, height=0.62,
                                 stroke_width=0, fill_color="#1B2233", fill_opacity=1)
        track.move_to([XL, y, 0], LEFT)
        fill = RoundedRectangle(corner_radius=0.12, width=max(pct / 100 * BARW, 0.2),
                                height=0.62, stroke_width=0, fill_color=col, fill_opacity=1)
        fill.move_to([XL, y, 0], LEFT)
        val = Text(f"{pct:.0f}%", font=FONT, font_size=42, weight=BOLD, color=col)
        val.next_to(track, RIGHT, buff=0.22)
        return VGroup(lab, track, fill, val)

    def bloque(self, titulo, datos, ytop, ganador):
        t = Text(titulo, font=FONT, font_size=40, weight=BOLD, color=WHITE)
        t.move_to([-3.85, ytop, 0], LEFT)
        b1 = self.barra(datos[0][0], datos[0][1], datos[0][2], ytop - 0.85,
                        datos[0][0] == ganador)
        b2 = self.barra(datos[1][0], datos[1][1], datos[1][2], ytop - 1.70,
                        datos[1][0] == ganador)
        return VGroup(t, b1, b2)

    def construct(self):
        self.prepare()
        chip = self.chip("Estadística que engaña", ROSA)

        with self.beat(0) as b:
            b.play(FadeIn(chip, shift=DOWN * 0.3), run_time=0.45)
            cab = VGroup(
                self.fit(Text("dos tratamientos", font=FONT, font_size=52,
                              weight=BOLD, color=WHITE), 8.2),
                VGroup(
                    Text("A", font=FONT, font_size=90, weight=BOLD, color=CELESTE),
                    Text("vs", font=FONT, font_size=44, color=GRIS),
                    Text("B", font=FONT, font_size=90, weight=BOLD, color=ROSA),
                ).arrange(RIGHT, buff=0.7),
            ).arrange(DOWN, buff=0.45).move_to(UP * 3.0)
            b.play(FadeIn(cab[0], shift=UP * 0.15), run_time=0.6)
            b.play(FadeIn(cab[1][0], scale=0.6), run_time=0.35)
            b.play(FadeIn(cab[1][1]), run_time=0.2)
            b.play(FadeIn(cab[1][2], scale=0.6), run_time=0.35)

        lev = self.bloque("casos LEVES", LEV, 2.55, "A")
        with self.beat(1) as b:
            b.play(cab.animate.scale(0.5).move_to(UP * 5.4), run_time=0.5)
            b.play(FadeIn(lev[0], shift=RIGHT * 0.2), run_time=0.4)
            for bb in (lev[1], lev[2]):
                b.play(FadeIn(bb[0]), FadeIn(bb[1]),
                       GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.55)

        gra = self.bloque("casos GRAVES", GRA, -0.35, "A")
        with self.beat(2) as b:
            b.play(FadeIn(gra[0], shift=RIGHT * 0.2), run_time=0.4)
            for bb in (gra[1], gra[2]):
                b.play(FadeIn(bb[0]), FadeIn(bb[1]),
                       GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.55)

        def tilde(col=VERDE):
            m = VMobject(stroke_color=col, stroke_width=9)
            m.set_points_as_corners([[-0.20, 0.02, 0], [-0.05, -0.18, 0], [0.24, 0.26, 0]])
            return m

        with self.beat(3) as b:
            tick = VGroup(*[tilde() for _ in range(2)])
            tick[0].next_to(lev[1][3], RIGHT, buff=0.25)
            tick[1].next_to(gra[1][3], RIGHT, buff=0.25)
            b.play(FadeIn(tick[0], scale=0.5), run_time=0.4)
            b.play(FadeIn(tick[1], scale=0.5), run_time=0.4)
            pre = self.fit(Text("A gana 2 de 2", font=FONT, font_size=48,
                                weight=BOLD, color=CELESTE), 8.0).move_to(DOWN * 2.80)
            b.play(FadeIn(pre, shift=UP * 0.15), run_time=0.5)

        tot = self.bloque("TODOS juntos", TOT, 2.55, "B")
        with self.beat(4) as b:
            b.play(FadeOut(VGroup(lev, gra, tick, pre)), run_time=0.45)
            b.play(FadeIn(tot[0], shift=RIGHT * 0.2), run_time=0.4)
            for bb in (tot[1], tot[2]):
                b.play(FadeIn(bb[0]), FadeIn(bb[1]),
                       GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.6)

        with self.beat(5) as b:
            b.play(Circumscribe(tot[2], color=VERDE, buff=0.18, stroke_width=6),
                   run_time=0.8)
            g = self.fit(Text("gana B", font=FONT, font_size=70, weight=BOLD,
                              color=ROSA), 8.0).move_to(DOWN * 0.75)
            ne = self.fit(Text("y no hay ningún error", font=FONT, font_size=44,
                               weight=BOLD, color=WHITE), 8.0).next_to(g, DOWN, buff=0.4)
            b.play(FadeIn(g, scale=0.7), run_time=0.6)
            b.play(FadeIn(ne, shift=UP * 0.15), run_time=0.5)

        with self.beat(6) as b:
            b.play(FadeOut(VGroup(tot, g, ne, cab)), run_time=0.4)
            nom = VGroup(
                self.fit(Text("PARADOJA", font=FONT, font_size=72, weight=BOLD,
                              color=AMBAR), 8.2),
                self.fit(Text("DE SIMPSON", font=FONT, font_size=72, weight=BOLD,
                              color=AMBAR), 8.2),
            ).arrange(DOWN, buff=0.2).move_to(UP * 2.6)
            b.play(FadeIn(nom, scale=0.75), run_time=0.8)
            b.play(Flash(nom, color=AMBAR, line_length=0.6, num_lines=20,
                         flash_radius=3.0), run_time=0.7)

        def reparto(letra, col, leves, graves, y):
            lab = Text(letra, font=FONT, font_size=50, weight=BOLD, color=col)
            lab.move_to([-3.65, y, 0])
            n = leves + graves
            wl = leves / n * BARW
            a = Rectangle(width=max(wl, 0.05), height=0.7, stroke_width=0,
                          fill_color=VERDE, fill_opacity=1).move_to([XL, y, 0], LEFT)
            c = Rectangle(width=max(BARW - wl, 0.05), height=0.7, stroke_width=0,
                          fill_color=ROSA, fill_opacity=1).next_to(a, RIGHT, buff=0)
            nl = Text(str(leves), font=FONT, font_size=30, weight=BOLD, color="#0B0F1A").move_to(a)
            ng = Text(str(graves), font=FONT, font_size=30, weight=BOLD, color="#0B0F1A").move_to(c)
            return VGroup(lab, a, c, nl, ng)

        leyenda = VGroup(
            VGroup(Square(0.34, stroke_width=0, fill_color=VERDE, fill_opacity=1),
                   Text("leves", font=FONT, font_size=32, color=WHITE)).arrange(RIGHT, buff=0.18),
            VGroup(Square(0.34, stroke_width=0, fill_color=ROSA, fill_opacity=1),
                   Text("graves", font=FONT, font_size=32, color=WHITE)).arrange(RIGHT, buff=0.18),
        ).arrange(RIGHT, buff=0.8).move_to(UP * 0.55)

        rA = reparto("A", CELESTE, 87, 263, -0.65)
        rB = reparto("B", ROSA, 270, 80, -1.75)

        with self.beat(7) as b:
            b.play(nom.animate.scale(0.55).move_to(UP * 5.3), run_time=0.5)
            b.play(FadeIn(leyenda), run_time=0.4)
            b.play(FadeIn(rA[0]), GrowFromEdge(VGroup(rA[1], rA[2]), LEFT),
                   FadeIn(VGroup(rA[3], rA[4])), run_time=0.8)
            b.play(Circumscribe(rA[2], color=ROSA, buff=0.1, stroke_width=5), run_time=0.7)

        with self.beat(8) as b:
            b.play(FadeIn(rB[0]), GrowFromEdge(VGroup(rB[1], rB[2]), LEFT),
                   FadeIn(VGroup(rB[3], rB[4])), run_time=0.8)
            b.play(Circumscribe(rB[1], color=VERDE, buff=0.1, stroke_width=5), run_time=0.7)

        with self.beat(9) as b