/
fastPC.py
806 lines (655 loc) · 27.9 KB
/
fastPC.py
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
from __future__ import print_function
from itertools import combinations, permutations
import logging
import networkx as nx
import numpy as np
import scipy.stats as spst
import torch
from numba import cuda
import time
import pandas as pd
import miceforest as mf
import argparse
import matplotlib
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
_logger = logging.getLogger(__name__)
# This is a function to merge several nodes into one in a Networkx graph
def merge_nodes(G, nodes, new_node):
"""
Merges the selected `nodes` of the graph G into one `new_node`,
meaning that all the edges that pointed to or from one of these
`nodes` will point to or from the `new_node`.
attr_dict and **attr are defined as in `G.add_node`.
"""
H = G.copy()
H.add_node(new_node)
for n1,n2 in G.edges(data=False):
# For all edges related to one of the nodes to merge,
# make an edge going to or coming from the `new gene`.
if n1 in nodes:
H.add_edge(new_node,n2)
elif n2 in nodes:
H.add_edge(n1,new_node)
for n in nodes:
H.remove_node(n)
return H
def _create_complete_graph(node_ids):
"""Create a complete graph from the list of node ids.
Args:
node_ids: a list of node ids
Returns:
An undirected graph (as a networkx.Graph)
"""
g = nx.Graph()
g.add_nodes_from(node_ids)
for (i, j) in combinations(node_ids, 2):
g.add_edge(i, j)
return g
def func_z_test(corr_matrix, ijk, l, g, sep_set, sample_size):
global cont
# Move ijk to GPU
ijk = torch.LongTensor(ijk)
if cuda:
ijk = ijk.to(device)
if l == 0:
H = corr_matrix[ijk[:,0:2].repeat(1,2).view(-1,2,2).transpose(1,2),
ijk[:,0:2].repeat(1,2).view(-1,2,2)]
if cuda:
H = H.to(device)
else:
M0 = corr_matrix[ijk[:,0:2].repeat(1,2).view(-1,2,2).transpose(1,2),
ijk[:,0:2].repeat(1,2).view(-1,2,2)]
M1 = corr_matrix[ijk[:,0:2].repeat(1,l).view(-1, l, 2).transpose(1,2),
ijk[:,2:].repeat(1,2).view(-1, 2, l)]
M2 = corr_matrix[ijk[:,2:].repeat(1,l).view(-1,l,l).transpose(1,2),
ijk[:,2:].repeat(1,l).view(-1,l,l)]
if cuda:
M0 = M0.to(device)
M1 = M1.to(device)
M2 = M2.to(device)
H = M0-torch.matmul(torch.matmul(M1, torch.inverse(M2)), M1.transpose(2,1))
rho_ijs = (H[:,0,1]/torch.sqrt(H[:,0,0] * H[:,1,1]))
# Absolute value of r, respect cut threshold
CUT_THR = 0.999999
rho_ijs = torch.abs(rho_ijs)
rho_ijs = torch.clamp(rho_ijs, min=0.0 ,max=CUT_THR)
# Note: log1p for more numerical stability, see "Aaux.R";
z_val = 1/2 * torch.log1p((2*rho_ijs)/(1-rho_ijs))
tau = torch.tensor(spst.norm.ppf(1-alpha/2)/np.sqrt(sample_size - l - 3) * np.ones(shape=(ijk.shape[0],)), dtype=torch.float32)
if cuda:
tau = tau.to(device)
if cuda:
ii = ijk[z_val <= tau, 0].cpu().numpy()
jj = ijk[z_val <= tau, 1].cpu().numpy()
kk = ijk[z_val <= tau, 2:].cpu().numpy()
else:
ii = ijk[z_val <= tau, 0].numpy()
jj = ijk[z_val <= tau, 1].numpy()
kk = ijk[z_val <= tau, 2:].numpy()
for t in range(len(ii)):
if g.has_edge(ii[t], jj[t]):
g.remove_edge(ii[t], jj[t])
cont = True
sep_set[ii[t]][jj[t]] |= set(kk[t,:])
sep_set[jj[t]][ii[t]] |= set(kk[t,:])
return g, sep_set
def estimate_skeleton(corr_matrix, sample_size, alpha, init_graph, know_edge_list, **kwargs):
global cont
"""Estimate a skeleton graph from the statistis information.
Args:
indep_test_func: the function name for a conditional
independency test.
data_matrix: data (as a numpy array).
alpha: the significance level.
kwargs:
'max_reach': maximum value of l (see the code). The
value depends on the underlying distribution.
'method': if 'stable' given, use stable-PC algorithm
(see [Colombo2014]).
'init_graph': initial structure of skeleton graph
(as a networkx.Graph). If not specified,
a complete graph is used.
other parameters may be passed depending on the
indep_test_func()s.
Returns:
g: a skeleton graph (as a networkx.Graph).
sep_set: a separation set (as an 2D-array of set()).
"""
def method_stable(kwargs):
return ('method' in kwargs) and kwargs['method'] == "stable"
node_ids = range(corr_matrix.shape[0])
node_size = corr_matrix.shape[0]
sep_set = [[set() for i in range(node_size)] for j in range(node_size)]
g = init_graph
l = node_size - 2
batch_size = 5000
while l >= 0:
print(f"==================> Performing round {l} .....")
cont = False
ijk = np.empty(shape=(batch_size,(2 + l)), dtype = int)
index = 0
for (i, j) in permutations(node_ids, 2):
### Known edges
if know_edge_list:
if [i, j] in know_edge_list or [j,i] in know_edge_list:
continue
adj_i = list(g.neighbors(i)) # g is actually changed on-the-fly, so we need g_save to test edges
if j not in adj_i:
continue
else:
adj_i.remove(j)
if len(adj_i) >= l:
_logger.debug('testing %s and %s' % (i,j))
_logger.debug('neighbors of %s are %s' % (i, str(adj_i)))
if len(adj_i) < l:
continue
for k in combinations(adj_i, l):
ijk[index, 0:2] = [i,j] # torch.LongTensor([i, j]) # .cuda(device=device)
ijk[index, 2:] = k # torch.LongTensor(k) # .cuda(device=device)
index += 1
if index == batch_size:
g, sep_set = func_z_test(corr_matrix, ijk, l, g, sep_set, sample_size)
index = 0
if index != 0:
ijk_batch = ijk[:index, :]
g, sep_set = func_z_test(corr_matrix, ijk_batch, l, g, sep_set, sample_size)
l -= 1
return (g, sep_set)
def estimate_cpdag(skel_graph, sep_set, timeInfoDict, know_edge_list, blacklist_single):
"""Estimate a CPDAG from the skeleton graph and separation sets
returned by the estimate_skeleton() function.
Args:
skel_graph: A skeleton graph (an undirected networkx.Graph).
sep_set: An 2D-array of separation set.
The contents look like something like below.
sep_set[i][j] = set([k, l, m])
tiers: A dictionary of node lists. {time order: [nodes]}
Returns:
An estimated DAG.
"""
dag = skel_graph.to_directed()
node_ids = skel_graph.nodes()
### Direct based on black list edges
if blacklist_single:
for [i, j] in blacklist_single:
if dag.has_edge(i, j):
dag.remove_edge(i, j)
### Direct based on Known edges
if know_edge_list:
for [i, j] in know_edge_list:
if dag.has_edge(j, i):
dag.remove_edge(j, i)
##### Direct based on Time information
if timeInfoDict:
node_time_dict = dict()
for k, v in timeInfoDict.items():
for node in v:
node_time_dict[node] = k
for (i, j) in combinations(node_ids, 2):
if i in node_time_dict and j in node_time_dict:
if node_time_dict[i] > node_time_dict[j] and dag.has_edge(i, j): # i <---- j
_logger.debug('S: remove edge (%s, %s)' % (j, i))
dag.remove_edge(i, j)
if node_time_dict[i] < node_time_dict[j] and dag.has_edge(j, i): # i ----> j
_logger.debug('S: remove edge (%s, %s)' % (i, j))
dag.remove_edge(j, i)
#### V-structure
for (i, j) in combinations(node_ids, 2):
adj_i = set(dag.successors(i))
if j in adj_i:
continue
adj_j = set(dag.successors(j))
if i in adj_j:
continue
if sep_set[i][j] is None:
continue
common_k = adj_i & adj_j
for k in common_k:
if k not in sep_set[i][j]:
if dag.has_edge(k, i):
_logger.debug('S: remove edge (%s, %s)' % (k, i))
dag.remove_edge(k, i)
if dag.has_edge(k, j):
_logger.debug('S: remove edge (%s, %s)' % (k, j))
dag.remove_edge(k, j)
def _has_both_edges(dag, i, j):
return dag.has_edge(i, j) and dag.has_edge(j, i)
def _has_any_edge(dag, i, j):
return dag.has_edge(i, j) or dag.has_edge(j, i)
def _has_one_edge(dag, i, j):
return ((dag.has_edge(i, j) and (not dag.has_edge(j, i))) or
(not dag.has_edge(i, j)) and dag.has_edge(j, i))
def _has_no_edge(dag, i, j):
return (not dag.has_edge(i, j)) and (not dag.has_edge(j, i))
#### For all the combination of nodes i and j, apply the following
#### rules.
old_dag = dag.copy()
while True:
for (i, j) in combinations(node_ids, 2):
# Rule 1: Orient i-j into i->j whenever there is an arrow k->i
# such that k and j are nonadjacent.
#
# Check if i-j.
if _has_both_edges(dag, i, j):
# Look all the predecessors of i.
for k in dag.predecessors(i):
# Skip if there is an arrow i->k.
if dag.has_edge(i, k):
continue
# Skip if k and j are adjacent.
if _has_any_edge(dag, k, j):
continue
# Make i-j into i->j
_logger.debug('R1: remove edge (%s, %s)' % (j, i))
dag.remove_edge(j, i)
break
# Rule 2: Orient i-j into i->j whenever there is a chain
# i->k->j.
#
# Check if i-j.
if _has_both_edges(dag, i, j):
# Find nodes k where k is i->k.
succs_i = set()
for k in dag.successors(i):
if not dag.has_edge(k, i):
succs_i.add(k)
# Find nodes j where j is k->j.
preds_j = set()
for k in dag.predecessors(j):
if not dag.has_edge(j, k):
preds_j.add(k)
# Check if there is any node k where i->k->j.
if len(succs_i & preds_j) > 0:
# Make i-j into i->j
_logger.debug('R2: remove edge (%s, %s)' % (j, i))
dag.remove_edge(j, i)
# Rule 3: Orient i-j into i->j whenever there are two chains
# i-k->j and i-l->j such that k and l are nonadjacent.
#
# Check if i-j.
if _has_both_edges(dag, i, j):
# Find nodes k where i-k.
adj_i = set()
for k in dag.successors(i):
if dag.has_edge(k, i):
adj_i.add(k)
# For all the pairs of nodes in adj_i,
for (k, l) in combinations(adj_i, 2):
# Skip if k and l are adjacent.
if _has_any_edge(dag, k, l):
continue
# Skip if not k->j.
if dag.has_edge(j, k) or (not dag.has_edge(k, j)):
continue
# Skip if not l->j.
if dag.has_edge(j, l) or (not dag.has_edge(l, j)):
continue
# Make i-j into i->j.
_logger.debug('R3: remove edge (%s, %s)' % (j, i))
dag.remove_edge(j, i)
break
# Rule 4: Orient i-j into i->j whenever there are two chains
# i-k->l and k->l->j such that k and j are nonadjacent.
#
# However, this rule is not necessary when the PC-algorithm
# is used to estimate a DAG.
if nx.is_isomorphic(dag, old_dag):
break
old_dag = dag.copy()
return dag
def stdmtx(X):
"""
Convert Normal Distribution to Standard Normal Distribution
Input:
X: Each column is a variable
Output:
X: Standard Normal Distribution
"""
means = X.mean(axis = 0)
stds = X.std(axis = 0, ddof=1)
X = X - means[np.newaxis, :]
X = X / stds[np.newaxis, :]
return np.nan_to_num(X)
def nameMapping(df):
### Map integer to name
mapping = {i: name for i, name in enumerate(df.columns)}
return mapping
def heatmap(data, row_labels, col_labels, ax=None,
cbar_kw={}, cbarlabel="", **kwargs):
"""
Create a heatmap from a numpy array and two lists of labels.
Parameters
----------
data
A 2D numpy array of shape (N, M).
row_labels
A list or array of length N with the labels for the rows.
col_labels
A list or array of length M with the labels for the columns.
ax
A `matplotlib.axes.Axes` instance to which the heatmap is plotted. If
not provided, use current axes or create a new one. Optional.
cbar_kw
A dictionary with arguments to `matplotlib.Figure.colorbar`. Optional.
cbarlabel
The label for the colorbar. Optional.
**kwargs
All other arguments are forwarded to `imshow`.
"""
if not ax:
ax = plt.gca()
# Plot the heatmap
im = ax.imshow(data, **kwargs)
# Create colorbar
cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom")
# We want to show all ticks...
ax.set_xticks(np.arange(data.shape[1]))
ax.set_yticks(np.arange(data.shape[0]))
# ... and label them with the respective list entries.
ax.set_xticklabels(col_labels)
ax.set_yticklabels(row_labels)
# Let the horizontal axes labeling appear on top.
ax.tick_params(top=True, bottom=False,
labeltop=True, labelbottom=False)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=-30, ha="right",
rotation_mode="anchor")
# Turn spines off and create white grid.
for edge, spine in ax.spines.items():
spine.set_visible(False)
ax.set_xticks(np.arange(data.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(data.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=3)
ax.tick_params(which="minor", bottom=False, left=False)
return im, cbar
def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
textcolors=("black", "white"),
threshold=None, **textkw):
"""
A function to annotate a heatmap.
Parameters
----------
im
The AxesImage to be labeled.
data
Data used to annotate. If None, the image's data is used. Optional.
valfmt
The format of the annotations inside the heatmap. This should either
use the string format method, e.g. "$ {x:.2f}", or be a
`matplotlib.ticker.Formatter`. Optional.
textcolors
A pair of colors. The first is used for values below a threshold,
the second for those above. Optional.
threshold
Value in data units according to which the colors from textcolors are
applied. If None (the default) uses the middle of the colormap as
separation. Optional.
**kwargs
All other arguments are forwarded to each call to `text` used to create
the text labels.
"""
if not isinstance(data, (list, np.ndarray)):
data = im.get_array()
# Normalize the threshold to the images color range.
if threshold is not None:
threshold = im.norm(threshold)
else:
threshold = im.norm(data.max())/2.
# Set default alignment to center, but allow it to be
# overwritten by textkw.
kw = dict(horizontalalignment="center",
verticalalignment="center")
kw.update(textkw)
# Get the formatter in case a string is supplied
if isinstance(valfmt, str):
valfmt = matplotlib.ticker.StrMethodFormatter(valfmt)
# Loop over the data and create a `Text` for each "pixel".
# Change the text's color depending on the data.
texts = []
for i in range(data.shape[0]):
for j in range(data.shape[1]):
kw.update(color=textcolors[int(im.norm(data[i, j]) > threshold)])
text = im.axes.text(j, i, valfmt(data[i, j], None), **kw)
texts.append(text)
return texts
def plotgraph(g, mapping):
g = nx.relabel_nodes(g, mapping)
plt.figure(num=None, figsize=(18, 18), dpi=80)
plt.axis('off')
fig = plt.figure(1)
pos = nx.shell_layout(g)
nx.draw_networkx_nodes(g,pos)
nx.draw_networkx_edges(g,pos)
nx.draw_networkx_labels(g,pos)
"""
### Plot adjacency matrix
A = nx.adjacency_matrix(g).todense()
x_labels = mapping.values()
y_labels = mapping.values()
print(x_labels)
print(y_labels)
fig, ax = plt.subplots()
im = ax.imshow(A)
# We want to show all ticks...
ax.set_xticks(np.arange(len(x_labels)))
ax.set_yticks(np.arange(len(y_labels)))
# ... and label them with the respective list entries
ax.set_xticklabels(x_labels)
ax.set_yticklabels(y_labels)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(x_labels)):
for j in range(len(y_labels)):
text = ax.text(j, i, A[i, j],
ha="center", va="center", color="w")
ax.set_title("Causal Relations")
fig.tight_layout()
plt.show()
fig, ax = plt.subplots()
im, cbar = heatmap(A, x_labels, y_labels, ax=ax,
cmap="YlGn", cbarlabel="harvest [t/year]")
texts = annotate_heatmap(im, valfmt="{x:.1f} t")
fig.tight_layout()
plt.show()
"""
def savegraph(gs, corr_matrix, mapping, edgeType):
from collections import Counter, OrderedDict # if MI_DATASET is 1, still need to run this
edges_all = [e for g in gs for e in list(g.edges)]
edges_appear_count = Counter(edges_all)
edges_keep = edges_all # [v for v, num in edges_all.items() if num == MI_DATASET]
g=nx.empty_graph(corr_matrix.shape[0],create_using=nx.DiGraph())
g.add_edges_from(edges_keep)
### save edges to excel
strength = []
g_edges = list(g.edges) # all edges
for (i, j) in g_edges:
if edgeType == 's':
if cuda:
# print(corr_matrix[i, j].cpu().item())
strength.append(corr_matrix[i, j].cpu().item())
else:
# print(corr_matrix[i, j].item())
strength.append(corr_matrix[i, j].item())
elif edgeType == 'c':
strength.append(edges_appear_count[(i, j)])
else:
strength.append(np.nan)
graph_excel = {'Cause': [mapping[e[0]] for e in g_edges], 'Effect': [mapping[e[1]] for e in g_edges], 'Strength': [round(a, 3) for a in strength]}
graph_excel = pd.DataFrame.from_dict(graph_excel)
graph_excel.to_csv("graph_excel.csv", index=False)
### Seperate Single and Bidirectional edges
graph_excel_single = {'Cause': [], 'Effect': [], 'Strength':[]}
graph_excel_bi = {'Cause': [], 'Effect': [], 'Strength':[]}
for m, (i, j) in enumerate(g_edges):
if (j, i) not in g_edges: # Single directional
graph_excel_single['Cause'].append(mapping[i])
graph_excel_single['Effect'].append(mapping[j])
graph_excel_single['Strength'].append(strength[m])
else: # bidirectional edges
graph_excel_bi['Cause'].append(mapping[i])
graph_excel_bi['Effect'].append(mapping[j])
graph_excel_bi['Strength'].append(strength[m])
graph_excel_single = pd.DataFrame.from_dict(graph_excel_single)
graph_excel_single.to_csv("graph_excel_single_direction.csv", index=False)
graph_excel_bi = pd.DataFrame.from_dict(graph_excel_bi)
graph_excel_bi.to_csv("graph_excel_bidirection.csv", index=False)
def getblackList(df, blacklist, node_size):
node_ids = range(node_size)
init_graph = _create_complete_graph(node_ids)
black_edges = set()
with open(blacklist, 'rb') as f:
for line in f.readlines():
cause, effect = line.splitlines()[0].decode("utf-8").split(',')
i, j = df.columns.get_loc(cause.strip()), df.columns.get_loc(effect.strip())
black_edges |= {(i, j)}
blacklist_single = set((i,j) for (i,j) in black_edges if (j, i) not in black_edges)
init_graph.remove_edges_from([(i,j) for (i, j) in black_edges if i < j and (j, i) in black_edges])
return init_graph, blacklist_single
def getTiers(tiers, mapping_r):
with open(tiers, 'rb') as f:
timeinfodict = dict()
n = 1
for line in f.readlines():
line = line.splitlines()[0].decode("utf-8").split(',')
line = [mapping_r[i.strip()] for i in line]
timeinfodict[n] = line
n+=1
return timeinfodict
def getknownedges(knownedges, mapping_r):
# nonlocal mapping_r
know_edge_list = []
with open(knownedges, 'rb') as f:
for line in f.readlines():
cause, effect = line.splitlines()[0].decode("utf-8").split(',')
know_edge_list.append([mapping_r[cause.strip()], mapping_r[effect.strip()]])
return know_edge_list
def main(dataFile, alpha, cuda, knownEdgesFile, blackListFile, tiersFile, imputation, edgeType):
df = pd.read_csv(dataFile)
## check corr=1
corr = np.corrcoef(df.values.T)
for i in range(corr.shape[0]):
for j in range(i+1, corr.shape[0]):
if abs(corr[i,j]) > 0.999999:
raise Exception('Feature ' + str(df.columns[i]) + ' and feature ' + str(df.columns[j]) + ' are strongly correlated ' + str(corr[i,j]) + ', you might want to delete one feature.')
mapping = {i: name for i, name in enumerate(df.columns)}
mapping_r = {name:i for i, name in mapping.items()}
# def checkNull(imputation, edgeType):
# if df.isnull().values.any():
# txt = input("Dataframe contains missing value(s), do you want to perform Multiple Imputation? (Y/N)")
# if txt.strip() in ['Y', 'y']:
# imputation = True
# edgeType = 'c'
# elif txt.strip() in ['N', 'n']:
# sys.exit("Execution terminated: please fill missing data in the dataframe.")
# else:
# print('Invalid input')
# checkNull()
# return imputation, edgeType
# imputation, edgeType = checkNull(imputation, edgeType)
if df.isnull().values.any():
imputation=True
### Multiple Imputation
datasets = []
if imputation:
kernel = mf.MultipleImputedKernel(
data=df,
datasets=MI_DATASET,
save_all_iterations=True,
random_state=1991,
mean_match_candidates=5
)
# Run the MICE algorithm for 3 iterations on each of the datasets
kernel.mice(1, verbose=True, n_jobs=2)
datasets = []
for i in range(MI_DATASET):
datasets.append(pd.get_dummies( kernel.complete_data(i))) # Categorical to continuous
else:
datasets.append(df)
gs = []
for df in datasets:
N = df.shape[0]
node_size = df.shape[1]
corr_matrix = np.corrcoef(df.values.T)
corr_matrix = torch.tensor(corr_matrix, dtype=torch.float32)
# stablize
corr_matrix += 1e-6*np.random.random(corr_matrix.shape)
if cuda:
corr_matrix = corr_matrix.to(device)
st = time.time()
### Blacklist
if blackListFile:
init_graph, blacklist_single = getblackList(df, blackListFile, node_size)
else:
init_graph = _create_complete_graph(range(node_size))
blacklist_single = None
### Tiers
if tiersFile:
timeInfoDict = getTiers(tiersFile, mapping_r)
else:
timeInfoDict = None
### knowngraphs
if knownEdgesFile:
know_edge_list = getknownedges(knownEdgesFile, mapping_r)
else:
know_edge_list = []
(g, sep_set) = estimate_skeleton(corr_matrix=corr_matrix,
sample_size=N,
alpha=alpha,
init_graph=init_graph,
know_edge_list=know_edge_list,
method='stable')
g = estimate_cpdag(skel_graph=g,
sep_set=sep_set,
timeInfoDict=timeInfoDict,
know_edge_list=know_edge_list,
blacklist_single=blacklist_single)
en = time.time()
print("Total running time:", en-st)
print('Edges are:', g.edges(), end='')
### Integer to real name
gs.append(g)
plotgraph(g, mapping)
savegraph(gs, corr_matrix, mapping, edgeType)
parser = argparse.ArgumentParser(description='fastPC: A Cuda-based Parallel PC Algorithm')
parser.add_argument('--significanceLevel', type=float, default=10**-6, help='Learning rate (default: 10^-6)')
parser.add_argument('--gpu', dest='cuda', action='store_true')
parser.add_argument('--no-gpu', dest='cuda', action='store_false')
parser.set_defaults(cuda=False)
parser.add_argument('--imputation', default=False, help='Use Multiple Imputation (default: False)')
parser.add_argument('--MI_DATASET', type=int, default=5, help='Number of Imputatation Dataset (default: 5)')
parser.add_argument('--edgeType', type=str, default='s', choices=['s', 'c'], help='Edge Type is correlation coefficient or confidence (default: correlation coefficient)')
parser.add_argument('dataFile', help='(Path to) input dataset. Required file format: csv with each column as a random variable.')
parser.add_argument('--knownEdgesFile', nargs='?', help='(Path to) txt file containing known edges. Required file format: txt with a row (format: variable1, variable2) for each known directed edge: variable1 --> variable2.')
parser.add_argument('--blackListFile', nargs='?', help='(Path to) txt file containing edges should not appear. Required file format: txt with a row (format: variable1, variable2) for each known directed edge: variable1 --> variable2.')
parser.add_argument('--tiersFile', nargs='?', help='(Path to) txt file containing tiers in terms of time. Required file format: txt with a row (format: [variable1, variable2, variable3]) for each tier starting the earliest tiers.')
args = parser.parse_args()
print("Arguments:", args)
alpha = args.significanceLevel
cuda = args.cuda
imputation = args.imputation
edgeType = args.edgeType
dataFile = args.dataFile
MI_DATASET = args.MI_DATASET
if args.knownEdgesFile is not None:
knownEdgesFile = args.knownEdgesFile
else:
knownEdgesFile=None
if args.blackListFile is not None:
blackListFile = args.blackListFile
else:
blackListFile=None
if args.tiersFile is not None:
tiersFile = args.tiersFile
else:
tiersFile=None
if cuda:
device = torch.device(1)
torch.cuda.set_device(device)
torch.cuda.current_device()
else:
device = torch.device('cpu')
main(dataFile, alpha, cuda, knownEdgesFile, blackListFile, tiersFile, imputation, edgeType)