hedtorresca's picture
Update app.py
54796ce verified
raw
history blame
3.24 kB
import gradio as gr
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
from io import BytesIO
from PIL import Image
import pandas as pd
def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
errors = []
if A < AB + AC - ABC:
errors.append("A no puede ser menor que la suma de AB y AC menos ABC.")
if B < AB + BC - ABC:
errors.append("B no puede ser menor que la suma de AB y BC menos ABC.")
if C < AC + BC - ABC:
errors.append("C no puede ser menor que la suma de AC y BC menos ABC.")
if U < A + B + C - AB - AC - BC + ABC:
errors.append("El conjunto universal U es menor que la suma total de los conjuntos y sus intersecciones.")
return errors
def suggest_intersections(A, B, C, U):
max_AB = min(A, B, U - (A + B + C - A - B))
max_AC = min(A, C, U - (A + B + C - A - C))
max_BC = min(B, C, U - (A + B + C - B - C))
max_ABC = min(max_AB, max_AC, max_BC)
min_AB = max(0, A + B + C - A - B - C + ABC - U)
min_AC = max(0, A + C + B - A - C - B + ABC - U)
min_BC = max(0, B + C + A - B - C - A + ABC - U)
min_ABC = max(0, ABC - (A + B + C - AB - AC - BC + ABC))
suggestions = {
"Mínimo valor sugerido para A ∩ B": min_AB,
"Máximo valor sugerido para A ∩ B": max_AB,
"Mínimo valor sugerido para A ∩ C": min_AC,
"Máximo valor sugerido para A ∩ C": max_AC,
"Mínimo valor sugerido para B ∩ C": min_BC,
"Máximo valor sugerido para B ∩ C": max_BC,
"Mínimo valor sugerido para A ∩ B ∩ C": min_ABC,
"Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
}
return suggestions
def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
total = U if U > 0 else (A + B + C - AB - AC - BC + ABC)
if total == 0:
return {
"P(A)": 0,
"P(B)": 0,
"P(C)": 0,
"P(A ∩ B)": 0,
"P(A ∩ C)": 0,
"P(B ∩ C)": 0,
"P(A ∩ B ∩ C)": 0,
}
P_A = A / total
P_B = B / total
P_C = C / total
P_AB = AB / total
P_AC = AC / total
P_BC = BC / total
P_ABC = ABC / total
PA_given_B = P_AB / P_B if P_B > 0 else 0
PA_given_C = P_AC / P_C if P_C > 0 else 0
PB_given_C = P_BC / P_C if P_C > 0 else 0
formatted_probs = {
"P(A)": f"{P_A:.2%} ({A}/{total})",
"P(B)": f"{P_B:.2%} ({B}/{total})",
"P(C)": f"{P_C:.2%} ({C}/{total})",
"P(A ∩ B)": f"{P_AB:.2%} ({AB}/{total})",
"P(A ∩ C)": f"{P_AC:.2%} ({AC}/{total})",
"P(B ∩ C)": f"{P_BC:.2%} ({BC}/{total})",
"P(A ∩ B ∩ C)": f"{P_ABC:.2%} ({ABC}/{total})",
"P(A | B)": f"{PA_given_B:.2%}",
"P(A | C)": f"{PA_given_C:.2%}",
"P(B | C)": f"{PB_given_C:.2%}",
"U (Universal Set)": total,
"Complemento de A U B U C": U - (A + B + C - AB - AC - BC + ABC)
}
# Convert to DataFrame for better visualization
df = pd.DataFrame(list(formatted_probs.items()), columns=["Descripción", "Valor"])
return df
# This function should be integrated with the main part of the app or interface
# It could be connected to a gradio UI, for example, to allow interactive use