hedtorresca commited on
Commit
54796ce
·
verified ·
1 Parent(s): e0ff385

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -44
app.py CHANGED
@@ -3,6 +3,7 @@ import matplotlib.pyplot as plt
3
  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 = []
@@ -21,11 +22,20 @@ def suggest_intersections(A, B, C, U):
21
  max_AC = min(A, C, U - (A + B + C - A - C))
22
  max_BC = min(B, C, U - (A + B + C - B - C))
23
  max_ABC = min(max_AB, max_AC, max_BC)
24
-
 
 
 
 
 
25
  suggestions = {
 
26
  "Máximo valor sugerido para A ∩ B": max_AB,
 
27
  "Máximo valor sugerido para A ∩ C": max_AC,
 
28
  "Máximo valor sugerido para B ∩ C": max_BC,
 
29
  "Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
30
  }
31
  return suggestions
@@ -50,7 +60,11 @@ def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
50
  P_AC = AC / total
51
  P_BC = BC / total
52
  P_ABC = ABC / total
53
-
 
 
 
 
54
  formatted_probs = {
55
  "P(A)": f"{P_A:.2%} ({A}/{total})",
56
  "P(B)": f"{P_B:.2%} ({B}/{total})",
@@ -59,48 +73,16 @@ def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
59
  "P(A ∩ C)": f"{P_AC:.2%} ({AC}/{total})",
60
  "P(B ∩ C)": f"{P_BC:.2%} ({BC}/{total})",
61
  "P(A ∩ B ∩ C)": f"{P_ABC:.2%} ({ABC}/{total})",
 
 
 
 
 
62
  }
63
 
64
- return formatted_probs
65
-
66
- def plot_venn(A, B, C, AB, AC, BC, ABC):
67
- plt.figure(figsize=(8, 8))
68
- venn = venn3(subsets=(A, B, AB, C, AC, BC, ABC), set_labels=('A', 'B', 'C'))
69
- plt.title("Venn Diagram")
70
-
71
- buf = BytesIO()
72
- plt.savefig(buf, format='png')
73
- buf.seek(0)
74
- img = Image.open(buf)
75
- return img
76
-
77
- def main(A, B, C, AB, AC, BC, ABC, U):
78
- errors = validate_inputs(A, B, C, AB, AC, BC, ABC, U)
79
- if errors:
80
- suggestions = suggest_intersections(A, B, C, U)
81
- return None, {"error": "\n".join(errors), "sugerencias": suggestions}
82
-
83
- venn_diagram = plot_venn(A, B, C, AB, AC, BC, ABC)
84
- probabilities = calculate_probabilities(A, B, C, AB, AC, BC, ABC, U)
85
- return venn_diagram, probabilities
86
-
87
- iface = gr.Interface(
88
- fn=main,
89
- inputs=[
90
- gr.Number(label="Cantidad en A"),
91
- gr.Number(label="Cantidad en B"),
92
- gr.Number(label="Cantidad en C"),
93
- gr.Number(label="Cantidad en A ∩ B"),
94
- gr.Number(label="Cantidad en A ∩ C"),
95
- gr.Number(label="Cantidad en B ∩ C"),
96
- gr.Number(label="Cantidad en A ∩ B ∩ C"),
97
- gr.Number(label="Conjunto Universal (U)")
98
- ],
99
- outputs=[
100
- gr.Image(type="pil", label="Diagrama de Venn"),
101
- gr.JSON(label="Resultados")
102
- ],
103
- live=True
104
- )
105
 
106
- iface.launch()
 
 
3
  from matplotlib_venn import venn3
4
  from io import BytesIO
5
  from PIL import Image
6
+ import pandas as pd
7
 
8
  def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
9
  errors = []
 
22
  max_AC = min(A, C, U - (A + B + C - A - C))
23
  max_BC = min(B, C, U - (A + B + C - B - C))
24
  max_ABC = min(max_AB, max_AC, max_BC)
25
+
26
+ min_AB = max(0, A + B + C - A - B - C + ABC - U)
27
+ min_AC = max(0, A + C + B - A - C - B + ABC - U)
28
+ min_BC = max(0, B + C + A - B - C - A + ABC - U)
29
+ min_ABC = max(0, ABC - (A + B + C - AB - AC - BC + ABC))
30
+
31
  suggestions = {
32
+ "Mínimo valor sugerido para A ∩ B": min_AB,
33
  "Máximo valor sugerido para A ∩ B": max_AB,
34
+ "Mínimo valor sugerido para A ∩ C": min_AC,
35
  "Máximo valor sugerido para A ∩ C": max_AC,
36
+ "Mínimo valor sugerido para B ∩ C": min_BC,
37
  "Máximo valor sugerido para B ∩ C": max_BC,
38
+ "Mínimo valor sugerido para A ∩ B ∩ C": min_ABC,
39
  "Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
40
  }
41
  return suggestions
 
60
  P_AC = AC / total
61
  P_BC = BC / total
62
  P_ABC = ABC / total
63
+
64
+ PA_given_B = P_AB / P_B if P_B > 0 else 0
65
+ PA_given_C = P_AC / P_C if P_C > 0 else 0
66
+ PB_given_C = P_BC / P_C if P_C > 0 else 0
67
+
68
  formatted_probs = {
69
  "P(A)": f"{P_A:.2%} ({A}/{total})",
70
  "P(B)": f"{P_B:.2%} ({B}/{total})",
 
73
  "P(A ∩ C)": f"{P_AC:.2%} ({AC}/{total})",
74
  "P(B ∩ C)": f"{P_BC:.2%} ({BC}/{total})",
75
  "P(A ∩ B ∩ C)": f"{P_ABC:.2%} ({ABC}/{total})",
76
+ "P(A | B)": f"{PA_given_B:.2%}",
77
+ "P(A | C)": f"{PA_given_C:.2%}",
78
+ "P(B | C)": f"{PB_given_C:.2%}",
79
+ "U (Universal Set)": total,
80
+ "Complemento de A U B U C": U - (A + B + C - AB - AC - BC + ABC)
81
  }
82
 
83
+ # Convert to DataFrame for better visualization
84
+ df = pd.DataFrame(list(formatted_probs.items()), columns=["Descripción", "Valor"])
85
+ return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ # This function should be integrated with the main part of the app or interface
88
+ # It could be connected to a gradio UI, for example, to allow interactive use