Update app.py
Browse files
app.py
CHANGED
@@ -84,41 +84,45 @@ class GraphTheoryAnalysis:
|
|
84 |
def build_graph(self, level=2, r2_threshold=0.5):
|
85 |
"""
|
86 |
Construye el grafo basado en el nivel de combinación.
|
87 |
-
|
88 |
Args:
|
89 |
level (int): Nivel de combinación (2 o 3).
|
90 |
r2_threshold (float): Umbral de R² para incluir una interacción.
|
91 |
"""
|
|
|
|
|
|
|
92 |
self.graph.clear() # Limpiar grafo existente
|
93 |
|
94 |
-
if level
|
95 |
-
#
|
96 |
for pair in combinations(self.all_factors, 2):
|
97 |
self.experiment.set_active_factors(list(pair))
|
98 |
self.experiment.fit_models()
|
99 |
r2 = (self.experiment.r2_variable1 + self.experiment.r2_variable2) / 2
|
100 |
if r2 >= r2_threshold:
|
101 |
self.graph.add_edge(pair[0], pair[1], weight=r2)
|
102 |
-
|
103 |
-
|
|
|
104 |
factor_r2 = {}
|
105 |
for factor in self.all_factors:
|
106 |
-
# Filtrar las aristas que contienen el factor
|
107 |
edges = [data['weight'] for u, v, data in self.graph.edges(data=True) if u == factor or v == factor]
|
108 |
factor_r2[factor] = sum(edges) / len(edges) if edges else 0
|
109 |
|
110 |
# Seleccionar los 3 factores con mayor R²
|
111 |
top_factors = sorted(factor_r2, key=factor_r2.get, reverse=True)[:3]
|
112 |
|
113 |
-
#
|
|
|
|
|
|
|
114 |
for pair in combinations(top_factors, 2):
|
115 |
self.experiment.set_active_factors(list(pair))
|
116 |
self.experiment.fit_models()
|
117 |
r2 = (self.experiment.r2_variable1 + self.experiment.r2_variable2) / 2
|
118 |
if r2 >= r2_threshold:
|
119 |
self.graph.add_edge(pair[0], pair[1], weight=r2)
|
120 |
-
else:
|
121 |
-
raise ValueError("Nivel de combinación no soportado. Debe ser 2 o 3.")
|
122 |
|
123 |
def visualize_graph(self, style='Style 1'):
|
124 |
pos = nx.spring_layout(self.graph)
|
|
|
84 |
def build_graph(self, level=2, r2_threshold=0.5):
|
85 |
"""
|
86 |
Construye el grafo basado en el nivel de combinación.
|
87 |
+
|
88 |
Args:
|
89 |
level (int): Nivel de combinación (2 o 3).
|
90 |
r2_threshold (float): Umbral de R² para incluir una interacción.
|
91 |
"""
|
92 |
+
if level not in [2, 3]:
|
93 |
+
raise ValueError("Nivel de combinación no soportado. Debe ser 2 o 3.")
|
94 |
+
|
95 |
self.graph.clear() # Limpiar grafo existente
|
96 |
|
97 |
+
if level >= 2:
|
98 |
+
# Construir interacciones de nivel 2
|
99 |
for pair in combinations(self.all_factors, 2):
|
100 |
self.experiment.set_active_factors(list(pair))
|
101 |
self.experiment.fit_models()
|
102 |
r2 = (self.experiment.r2_variable1 + self.experiment.r2_variable2) / 2
|
103 |
if r2 >= r2_threshold:
|
104 |
self.graph.add_edge(pair[0], pair[1], weight=r2)
|
105 |
+
|
106 |
+
if level == 3:
|
107 |
+
# Calcular R² acumulado por factor
|
108 |
factor_r2 = {}
|
109 |
for factor in self.all_factors:
|
|
|
110 |
edges = [data['weight'] for u, v, data in self.graph.edges(data=True) if u == factor or v == factor]
|
111 |
factor_r2[factor] = sum(edges) / len(edges) if edges else 0
|
112 |
|
113 |
# Seleccionar los 3 factores con mayor R²
|
114 |
top_factors = sorted(factor_r2, key=factor_r2.get, reverse=True)[:3]
|
115 |
|
116 |
+
# Limpiar el grafo para incluir solo las interacciones entre los 3 factores
|
117 |
+
self.graph.clear()
|
118 |
+
|
119 |
+
# Añadir interacciones entre los 3 factores seleccionados
|
120 |
for pair in combinations(top_factors, 2):
|
121 |
self.experiment.set_active_factors(list(pair))
|
122 |
self.experiment.fit_models()
|
123 |
r2 = (self.experiment.r2_variable1 + self.experiment.r2_variable2) / 2
|
124 |
if r2 >= r2_threshold:
|
125 |
self.graph.add_edge(pair[0], pair[1], weight=r2)
|
|
|
|
|
126 |
|
127 |
def visualize_graph(self, style='Style 1'):
|
128 |
pos = nx.spring_layout(self.graph)
|