-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCausalMatricesDiff.py
More file actions
414 lines (341 loc) · 15.5 KB
/
CausalMatricesDiff.py
File metadata and controls
414 lines (341 loc) · 15.5 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import seaborn as sns
import warnings
from matplotlib.patches import Rectangle
from matplotlib.lines import Line2D
from typing import List, Optional, Tuple, Dict, Union
warnings.filterwarnings("ignore")
class CompareCausalMatrices:
"""
Visualize differences between two causal matrices.
"""
def __init__(
self,
true_dag: np.ndarray,
pred_dag: np.ndarray,
var_names: Optional[List[str]] = None
) -> None:
"""
Initialize the class with true and predicted DAG matrices and optional variable names.
Args:
true_dag (np.ndarray): The ground truth DAG matrix.
pred_dag (np.ndarray): The predicted DAG matrix.
var_names (Optional[List[str]]): List of variable names. Defaults to indices if not provided.
"""
self.true_dag: np.ndarray = true_dag
self.pred_dag: np.ndarray = pred_dag
self.fn_list, self.fp_list = self.get_all_not_equal()
self.var_names: List[str] = var_names or [
f"variable_{n}" for n in range(self.true_dag.shape[0])
]
def get_all_not_equal(self) -> Tuple[List[List[int]], List[List[int]]]:
"""
Identify false negatives and false positives by comparing the true and predicted DAGs.
Returns:
Tuple[List[List[int]], List[List[int]]]: Lists of false negatives and false positives.
"""
result_fn: List[List[int]] = []
result_fp: List[List[int]] = []
for i in range(self.pred_dag.shape[0]):
for j in range(self.pred_dag.shape[0]):
if self.pred_dag[i, j] == 0 and self.true_dag[i, j] == 1:
result_fn.append([i, j])
if self.pred_dag[i, j] == 1 and self.true_dag[i, j] == 0:
result_fp.append([i, j])
return result_fn, result_fp
def plot_causal_matrices_diff(
self,
save_name: Optional[str] = None,
figsize: Tuple[int, int] = (12, 6),
cmap_name: str = "Greys",
show_only_one_plot = True,
show_legend = False
) -> None:
"""
Visualize the differences between the true and predicted DAG matrices.
Args:
save_name (Optional[str]): If provided, saves the plot to the specified file path.
figsize (Tuple[int, int]): Figure size for the plot.
cmap_name (str): Colormap to use for the heatmap.
show_only_one_plot (bool): Show only predicted DAG or also true DAG.
show_legend (bool): Show square color codes.
"""
assert self.true_dag.shape[0] == len(
self.var_names
), "Length of variable list doesn't match the shape of the causal matrix"
N = 1 if show_only_one_plot else 2
fig, ax = plt.subplots(ncols=N, figsize=figsize)
if N == 1:
ax = [ax]
plt.style.use('ggplot')
for n in range(N):
sns.heatmap(
self.pred_dag if n == 0 else self.true_dag,
cmap=cmap_name,
ax=ax[n],
cbar=False,
linewidths=0,
linecolor="k",
facecolor="white",
edgecolor="k",
)
ax[n].set_xticklabels(self.var_names, rotation=90)
ax[n].set_yticklabels(self.var_names, rotation=0)
ax[n].set_aspect("equal")
ax[0].set_title("Pred DAG")
if show_only_one_plot == False:
ax[1].set_title("True DAG")
for j, i in self.fp_list:
ax[0].add_patch(
Rectangle((i, j), 1, 1, linewidth=0, facecolor="#D3D3D3", edgecolor="k")
)
for j, i in self.fn_list:
ax[0].add_patch(
Rectangle((i, j), 1, 1, linewidth=2, facecolor="white", edgecolor="k")
)
legend_handles = [
Line2D([0], [0], marker='s', color='black', markersize=10, label='Present in True DAG and Pred DAG', linestyle='None'),
Line2D([0], [0], marker='s', markerfacecolor='white', markeredgecolor='black', markersize=10, label='Present only in True DAG', linestyle='None'),
Line2D([0], [0], marker='s', color='#D3D3D3', markersize=10, label='Present only in Pred DAG', linestyle='None'),
]
if save_name is not None:
fig.savefig(save_name)
else:
plt.tight_layout()
if show_legend:
plt.legend(
handles = legend_handles,
loc='upper left',
frameon=True
)
plt.show()
def number_of_undirected(self) -> Dict[str, float]:
"""
Count the number of undirected edges in the true and predicted DAGs.
Returns:
Dict[str, float]: Counts of undirected edges for both DAGs.
"""
total_true: float = 0
total_pred: float = 0
for i in range(self.true_dag.shape[0]):
for j in range(self.true_dag.shape[0]):
if self.true_dag[i, j] == 1 and self.true_dag[i, j] == self.true_dag[j, i]:
total_true += 0.5
if self.pred_dag[i, j] == 1 and self.pred_dag[i, j] == self.pred_dag[j, i]:
total_pred += 0.5
return {
"# of undirected edges for True DAG": total_true,
"# of undirected edges for Pred DAG": total_pred,
}
def structural_hamming_distance(self) -> float:
"""
Compute the Structural Hamming Distance (SDH) between the True DAG and Pred DAG.
Returns:
float: The sum of absolute differences between the two DAGs.
"""
diff = np.abs(self.true_dag - self.pred_dag)
return float(np.sum(diff))
def metrics(self) -> dict:
"""
Bind metrics into one dictionary.
Returns:
dict: A dictionaty with all the metrics for easy access.
"""
metrics = dict()
metrics['shd'] = self.structural_hamming_distance()
metrics['undir'] = self.number_of_undirected()
return metrics
def format_differences_report(self,
false_negatives: List[List[str]] = None,
false_positives: List[List[str]] = None
) -> str:
"""
Generate a formatted report of differences between true and predicted DAGs.
Args:
false_negatives (List[List[str]]): List of false negative variable pairs.
false_positives (List[List[str]]): List of false positive variable pairs.
Returns:
str: A formatted string describing the differences.
"""
report = []
if false_negatives == None and false_positives == None:
false_negatives, false_positives = self.get_all_not_equal()
report.append("True DAG has unique causal paths from:")
for elem in false_negatives:
report.append(f"- {elem[0]} to {elem[1]}")
report.append("which are not present in Pred DAG")
report.append("-" * 30)
report.append("Pred DAG has unique causal paths from:")
for elem in false_positives:
report.append(f"- {elem[0]} to {elem[1]}")
report.append("which are not present in True DAG")
return "\n".join(report)
def list_differences(
self, return_text_description: bool = True
) -> Union[None, Dict[str, List[List[str]]], str]:
"""
List the differences between the true and predicted DAGs in terms of false positives
and false negatives.
Args:
return_text_description (bool): Whether to print a text description or return a dictionary.
Returns:
Union[None, Dict[str, List[List[str]]], str]: Formatted string if return_text_description is True,
or a dictionary of false negatives and positives otherwise.
"""
false_negatives_variables = [
[self.var_names[x], self.var_names[y]] for x, y in self.fn_list
]
false_positives_variables = [
[self.var_names[x], self.var_names[y]] for x, y in self.fp_list
]
if return_text_description:
return self.format_differences_report(
false_negatives=false_negatives_variables,
false_positives=false_positives_variables
)
else:
return {
"False Negatives": false_negatives_variables,
"False Positives": false_positives_variables,
}
def draw_dags(self, layout_num: int = -1) -> None:
"""
Draws the Directed Acyclic Graph(s) (DAG) with options for different layouts.
Highlights false positives with red edges and uses black edges for all other connections.
Allows the user to select a specific layout or visualize the graph using all available layouts.
Args:
layout_num (int): Specifies the layout to use. If set to -1, all layouts will be drawn sequentially.
Available Layouts:
0: Spring Layout (Force-directed)
1: Circular Layout
2: Kamada-Kawai Layout
3: Shell Layout
4: Spectral Layout
5: Planar Layout
6: Spiral Layout
7: Random Layout
"""
# Define available layouts
layouts: Dict[int, callable] = {
0: nx.spring_layout, # Force-directed layout
1: nx.circular_layout, # Nodes arranged in a circle
2: nx.kamada_kawai_layout, # Optimized force-directed layout
3: nx.shell_layout, # Nodes in concentric shells
4: nx.spectral_layout, # Based on graph Laplacian
5: nx.planar_layout, # Non-overlapping edges (if planar)
6: nx.spiral_layout, # Nodes arranged in a spiral
7: nx.random_layout # Random node positions
}
# Get false positives and negatives from the DAG comparison
false_positives = self.list_differences(return_text_description=False)['False Positives']
false_negatives = self.list_differences(return_text_description=False)['False Negatives']
# Create a directed graph
G: nx.DiGraph = nx.DiGraph()
for i, row in enumerate(self.pred_dag):
for j, value in enumerate(row):
if value == 1:
G.add_edge(self.var_names[i], self.var_names[j]) # Add edge based on prediction DAG
# Highlight false positives with red edges
red_edges: List[Tuple[str, str]] = []
for u, v in false_positives:
G.add_edge(u, v) # Add edge for false positives
red_edges.append((u, v))
# Add grey edges for false negatives
grey_edges: List[Tuple[str, str]] = []
for u, v in false_negatives:
grey_edges.append((u, v)) # False negatives are edges in true_dag but not in pred_dag
# Black edges for all other connections
black_edges = [edge for edge in G.edges if edge not in red_edges + grey_edges]
# If a specific layout is chosen, filter to that layout only
if layout_num != -1:
layouts = {layout_num: layouts[layout_num]}
# Draw the graph for each selected layout
for layout_number, layout in layouts.items():
pos = layout(G) # Compute node positions
# Draw the graph
nx.draw(
G, pos, with_labels=True, node_size=2000, font_size=10, font_color='white'
)
# Draw black edges
nx.draw_networkx_edges(G, pos, edgelist=black_edges, edge_color='black')
# Draw red edges dor false positives
nx.draw_networkx_edges(G, pos, edgelist=red_edges, edge_color='red', width=3)
# Draw grey edges for false negatives
nx.draw_networkx_edges(G, pos, edgelist=grey_edges, edge_color='grey', width=2)
# Add a title for the layout
if layout_num == -1:
plt.title(f"[{layout_number}] {layout.__name__}")
plt.show()
def legend_descriptions(self, show = 'both'):
descriptions = [
'''
Red edges are false positives - a path present in Pred DAG but absent in True DAG. \n
Grey edges are false negatives - a path present in True DAG but absent in Pred DAG. \n
Black edges present matching paths in True DAG and Pred DAG.
''',
'''
White squares represent a connections from variable in on the X axis to variable on Y axis only in True DAG.
Grey squares represent a connections from variable in on the X axis to variable on Y axis only in Pred DAG.
Black squares present a match in True DAG and Pred DAG.
'''
]
if show == 'dag':
return descriptions[0]
elif show == 'matrix':
return descriptions[1]
else:
return '\n'.join(descriptions)
def calculate_match_percentage(self) -> dict:
"""
Calculates the percentage of edges in the predicted DAG (pred_dag) that match the edges in
the true DAG (true_dag) and the number of additional edges that are present in pred_dag,
Args:
true_dag (np.ndarray): The ground truth DAG as a binary adjacency matrix (n x n).
pred_dag (np.ndarray): The predicted DAG as a binary adjacency matrix (n x n).
Returns:
dict: The match percentage (0-100) indicating the proportion of matched edges.
The total number of false positives ini pred_dag.
"""
if self.true_dag.shape != self.pred_dag.shape:
raise ValueError("The true_dag and pred_dag must have the same dimensions.")
# Total edges in the true DAG
total_true_edges = np.sum(self.true_dag)
# Matched edges between true DAG and predicted DAG
matched_edges = np.sum((self.true_dag == 1) & (self.pred_dag == 1))
# Avoid division by zero if the true DAG has no edges
if total_true_edges == 0:
return 100.0 if np.sum(self.pred_dag) == 0 else 0.0
# Calculate the match percentage
match_percentage = float(100 * matched_edges / total_true_edges)
false_positives_len = len(self.list_differences(return_text_description=False)['False Positives'])
return {
'percent of matched paths [0-1]': round(match_percentage,3)/100,
'additional paths': false_positives_len
}
def standard_pipeline(self, print_step_names = False):
if print_step_names:
print("##### Performing 'plot_causal_matrices_diff' ##### ")
plt.show(self.plot_causal_matrices_diff(show_legend=True))
if print_step_names:
print("##### Performing 'format_differences_report' ##### ")
print(self.format_differences_report())
print('')
if print_step_names:
print("##### Performing 'list_differences' ##### ")
print(self.list_differences())
print('')
if print_step_names:
print("##### Performing 'metrics' ##### ")
print(self.metrics())
if print_step_names:
print("##### Performing 'draw_dags' ##### ")
plt.show(self.draw_dags())
if print_step_names:
print("##### Performing 'legend_description' ##### ")
print(self.legend_descriptions())
print('')
if print_step_names:
print("##### Performing 'calculate_match_percentage' ##### ")
print(self.calculate_match_percentage())