Spaces:
Runtime error
Runtime error
hedtorresca
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,16 @@ from matplotlib_venn import venn3
|
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
|
8 |
errors = []
|
9 |
if A < AB + AC - ABC:
|
@@ -16,6 +26,154 @@ def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
|
|
16 |
errors.append("El conjunto universal U es menor que la suma total de los conjuntos y sus intersecciones.")
|
17 |
return errors
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def suggest_intersections(A, B, C, AB, AC, BC, ABC, U):
|
20 |
# Máximos valores permitidos para las intersecciones
|
21 |
max_AB = min(A, B, U - (A + B + C - AB - AC - BC + ABC))
|
|
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
|
7 |
+
def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
|
8 |
+
errors = []
|
9 |
+
if A < AB + AC - ABC:
|
10 |
+
errors.append("A no puede ser menor que la suma de AB y AC menos ABC.")
|
11 |
+
if B < AB + BC - ABC:import gradio as gr
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
from matplotlib_venn import venn3
|
14 |
+
from io import BytesIO
|
15 |
+
from PIL import Image
|
16 |
+
|
17 |
def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
|
18 |
errors = []
|
19 |
if A < AB + AC - ABC:
|
|
|
26 |
errors.append("El conjunto universal U es menor que la suma total de los conjuntos y sus intersecciones.")
|
27 |
return errors
|
28 |
|
29 |
+
def suggest_intersections(A, B, C, AB, AC, BC, ABC, U):
|
30 |
+
max_AB = min(A, B, U - (A + B + C - AB - AC - BC + ABC))
|
31 |
+
max_AC = min(A, C, U - (A + B + C - AB - AC - BC + ABC))
|
32 |
+
max_BC = min(B, C, U - (A + B + C - AB - AC - BC + ABC))
|
33 |
+
max_ABC = min(max_AB, max_AC, max_BC)
|
34 |
+
|
35 |
+
min_AB = max(0, A + B - U + C)
|
36 |
+
min_AC = max(0, A + C - U + B)
|
37 |
+
min_BC = max(0, B + C - U + A)
|
38 |
+
min_ABC = max(0, A + B + C - U)
|
39 |
+
|
40 |
+
max_A = U - (B + C - BC)
|
41 |
+
max_B = U - (A + C - AC)
|
42 |
+
max_C = U - (A + B - AB)
|
43 |
+
min_A = max(AB + AC - ABC, 0)
|
44 |
+
min_B = max(AB + BC - ABC, 0)
|
45 |
+
min_C = max(AC + BC - ABC, 0)
|
46 |
+
|
47 |
+
suggestions = {
|
48 |
+
"Máximo valor sugerido para A": max_A,
|
49 |
+
"Mínimo valor sugerido para A": min_A,
|
50 |
+
"Máximo valor sugerido para B": max_B,
|
51 |
+
"Mínimo valor sugerido para B": min_B,
|
52 |
+
"Máximo valor sugerido para C": max_C,
|
53 |
+
"Mínimo valor sugerido para C": min_C,
|
54 |
+
"Máximo valor sugerido para A ∩ B": max_AB,
|
55 |
+
"Mínimo valor sugerido para A ∩ B": min_AB,
|
56 |
+
"Máximo valor sugerido para A ∩ C": max_AC,
|
57 |
+
"Mínimo valor sugerido para A ∩ C": min_AC,
|
58 |
+
"Máximo valor sugerido para B ∩ C": max_BC,
|
59 |
+
"Mínimo valor sugerido para B ∩ C": min_BC,
|
60 |
+
"Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
|
61 |
+
"Mínimo valor sugerido para A ∩ B ∩ C": min_ABC,
|
62 |
+
}
|
63 |
+
return suggestions
|
64 |
+
|
65 |
+
def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
|
66 |
+
total = U
|
67 |
+
if total == 0:
|
68 |
+
return {
|
69 |
+
"P(A)": 0,
|
70 |
+
"P(B)": 0,
|
71 |
+
"P(C)": 0,
|
72 |
+
"P(A ∩ B)": 0,
|
73 |
+
"P(A ∩ C)": 0,
|
74 |
+
"P(B ∩ C)": 0,
|
75 |
+
"P(A ∩ B ∩ C)": 0,
|
76 |
+
}
|
77 |
+
|
78 |
+
P_A = A / total
|
79 |
+
P_B = B / total
|
80 |
+
P_C = C / total
|
81 |
+
P_AB = AB / total
|
82 |
+
P_AC = AC / total
|
83 |
+
P_BC = BC / total
|
84 |
+
P_ABC = ABC / total
|
85 |
+
|
86 |
+
# Probabilidades condicionales
|
87 |
+
P_A_given_B = P_AB / P_B if P_B > 0 else 0
|
88 |
+
P_B_given_A = P_AB / P_A if P_A > 0 else 0
|
89 |
+
P_A_given_C = P_AC / P_C if P_C > 0 else 0
|
90 |
+
P_C_given_A = P_AC / P_A if P_A > 0 else 0
|
91 |
+
P_B_given_C = P_BC / P_C if P_C > 0 else 0
|
92 |
+
P_C_given_B = P_BC / P_B if P_B > 0 else 0
|
93 |
+
|
94 |
+
# Teorema de Bayes
|
95 |
+
P_B_given_A_bayes = (P_A_given_B * P_B) / P_A if P_A > 0 else 0
|
96 |
+
|
97 |
+
formatted_probs = {
|
98 |
+
"P(A)": f"{P_A:.2%} ({A}/{total})",
|
99 |
+
"P(B)": f"{P_B:.2%} ({B}/{total})",
|
100 |
+
"P(C)": f"{P_C:.2%} ({C}/{total})",
|
101 |
+
"P(A ∩ B)": f"{P_AB:.2%} ({AB}/{total})",
|
102 |
+
"P(A ∩ C)": f"{P_AC:.2%} ({AC}/{total})",
|
103 |
+
"P(B ∩ C)": f"{P_BC:.2%} ({BC}/{total})",
|
104 |
+
"P(A ∩ B ∩ C)": f"{P_ABC:.2%} ({ABC}/{total})",
|
105 |
+
"P(A | B)": f"{P_A_given_B:.2%}",
|
106 |
+
"P(B | A)": f"{P_B_given_A:.2%}",
|
107 |
+
"P(A | C)": f"{P_A_given_C:.2%}",
|
108 |
+
"P(C | A)": f"{P_C_given_A:.2%}",
|
109 |
+
"P(B | C)": f"{P_B_given_C:.2%}",
|
110 |
+
"P(C | B)": f"{P_C_given_B:.2%}",
|
111 |
+
"P(B | A) (Bayes)": f"{P_B_given_A_bayes:.2%}",
|
112 |
+
}
|
113 |
+
|
114 |
+
return formatted_probs
|
115 |
+
|
116 |
+
def plot_venn(A, B, C, AB, AC, BC, ABC, U):
|
117 |
+
subsets = {
|
118 |
+
'100': A - AB - AC + ABC,
|
119 |
+
'010': B - AB - BC + ABC,
|
120 |
+
'001': C - AC - BC + ABC,
|
121 |
+
'110': AB - ABC,
|
122 |
+
'101': AC - ABC,
|
123 |
+
'011': BC - ABC,
|
124 |
+
'111': ABC
|
125 |
+
}
|
126 |
+
plt.figure(figsize=(8, 8))
|
127 |
+
venn = venn3(subsets=subsets, set_labels=('A', 'B', 'C'))
|
128 |
+
plt.title(f"Diagrama de Venn con U = {U}")
|
129 |
+
|
130 |
+
buf = BytesIO()
|
131 |
+
plt.savefig(buf, format='png')
|
132 |
+
buf.seek(0)
|
133 |
+
img = Image.open(buf)
|
134 |
+
return img
|
135 |
+
|
136 |
+
def main(A, B, C, AB, AC, BC, ABC, U):
|
137 |
+
if U == 0:
|
138 |
+
U = A + B + C
|
139 |
+
|
140 |
+
errors = validate_inputs(A, B, C, AB, AC, BC, ABC, U)
|
141 |
+
suggestions = suggest_intersections(A, B, C, AB, AC, BC, ABC, U)
|
142 |
+
if errors:
|
143 |
+
return None, {"error": "\n".join(errors), "sugerencias": suggestions}
|
144 |
+
|
145 |
+
venn_diagram = plot_venn(A, B, C, AB, AC, BC, ABC, U)
|
146 |
+
probabilities = calculate_probabilities(A, B, C, AB, AC, BC, ABC, U)
|
147 |
+
return venn_diagram, probabilities
|
148 |
+
|
149 |
+
iface = gr.Interface(
|
150 |
+
fn=main,
|
151 |
+
inputs=[
|
152 |
+
gr.Number(label="Conjunto Universal (U)", value=0),
|
153 |
+
gr.Number(label="Cantidad en A"),
|
154 |
+
gr.Number(label="Cantidad en B"),
|
155 |
+
gr.Number(label="Cantidad en C"),
|
156 |
+
gr.Number(label="Cantidad en A ∩ B"),
|
157 |
+
gr.Number(label="Cantidad en A ∩ C"),
|
158 |
+
gr.Number(label="Cantidad en B ∩ C"),
|
159 |
+
gr.Number(label="Cantidad en A ∩ B ∩ C")
|
160 |
+
],
|
161 |
+
outputs=[
|
162 |
+
gr.Image(type="pil", label="Diagrama de Venn"),
|
163 |
+
gr.JSON(label="Resultados y Sugerencias")
|
164 |
+
],
|
165 |
+
live=True
|
166 |
+
)
|
167 |
+
|
168 |
+
iface.launch()
|
169 |
+
|
170 |
+
errors.append("B no puede ser menor que la suma de AB y BC menos ABC.")
|
171 |
+
if C < AC + BC - ABC:
|
172 |
+
errors.append("C no puede ser menor que la suma de AC y BC menos ABC.")
|
173 |
+
if U < A + B + C - AB - AC - BC + ABC:
|
174 |
+
errors.append("El conjunto universal U es menor que la suma total de los conjuntos y sus intersecciones.")
|
175 |
+
return errors
|
176 |
+
|
177 |
def suggest_intersections(A, B, C, AB, AC, BC, ABC, U):
|
178 |
# Máximos valores permitidos para las intersecciones
|
179 |
max_AB = min(A, B, U - (A + B + C - AB - AC - BC + ABC))
|