from cdt.causality.graph import CCDr from cdt.causality.graph import CGNN from cdt.causality.graph import GES from cdt.causality.graph import LiNGAM from cdt.causality.graph import PC from cdt.causality.graph import CAM from cdt.utils.graph import clr import networkx as nx device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Read the data data = pd.read_csv('combined.csv', header=None) true_CM = pd.read_csv('true_CM.csv', header=None) true_CM = np.array(true_CM) start_time = time.time() aupr_matrix, dir_adj_matrix = CDVAE() end_time = time.time() - start_time print("--- Execution time : %4.4s seconds ---" % end_time) #Retrieve SHD and AUPR shd = SHD(true_CM, dir_adj_matrix, double_for_anticausal=False) print('SHD:', shd) aupr, curve = precision_recall(true_CM, aupr_matrix) print('AUPR for aupr_matrix:', aupr)
def test_precision_recall(): assert precision_recall(*init())
def baselines(data): # Tests start_time = time.time() obj = PC() output = obj.predict(data) adj_mat = nx.adjacency_matrix(output).todense() output = clr(adj_mat) output[np.isnan(output)] = 0 output[np.isposinf(output)] = 1 predicted = retrieve_adjacency_matrix(output) true_matrix = pd.read_csv('true_CM.csv', header=None) true_matrix = np.array(true_matrix) shd = SHD(np.array(true_matrix), predicted, double_for_anticausal=False) aupr, curve = precision_recall(np.array(true_matrix), output) end_time = (time.time() - start_time) print("--- Execution time : %4.4s seconds ---" % end_time) results_pc = ['PC', aupr, shd, end_time] print(results_pc) # Tests start_time = time.time() obj = GES() output = obj.predict(data) adj_mat = nx.adjacency_matrix(output).todense() output = clr(adj_mat) output[np.isnan(output)] = 0 output[np.isposinf(output)] = 1 predicted = retrieve_adjacency_matrix(output) true_matrix = pd.read_csv('true_CM.csv', header=None) true_matrix = np.array(true_matrix) shd = SHD(np.array(true_matrix), predicted, double_for_anticausal=False) aupr, curve = precision_recall(np.array(true_matrix), output) end_time = (time.time() - start_time) print("--- Execution time : %4.4s seconds ---" % end_time) results_ges = ['GES', aupr, shd, end_time] print(results_ges) # Tests start_time = time.time() obj = LiNGAM() output = obj.predict(data) adj_mat = nx.adjacency_matrix(output).todense() output = clr(adj_mat) output[np.isnan(output)] = 0 output[np.isposinf(output)] = 1 predicted = retrieve_adjacency_matrix(output) true_matrix = pd.read_csv('true_CM.csv', header=None) true_matrix = np.array(true_matrix) shd = SHD(np.array(true_matrix), predicted, double_for_anticausal=False) aupr, curve = precision_recall(np.array(true_matrix), output) end_time = (time.time() - start_time) print("--- Execution time : %4.4s seconds ---" % end_time) results_lingam = ['LiNGAM', aupr, shd, end_time] print(results_lingam) # Tests start_time = time.time() obj = CCDr() output = obj.predict(data) adj_mat = nx.adjacency_matrix(output).todense() output = clr(adj_mat) output[np.isnan(output)] = 0 output[np.isposinf(output)] = 1 predicted = retrieve_adjacency_matrix(output) true_matrix = pd.read_csv('true_CM.csv', header=None) true_matrix = np.array(true_matrix) shd = SHD(np.array(true_matrix), predicted, double_for_anticausal=False) aupr, curve = precision_recall(np.array(true_matrix), output) end_time = (time.time() - start_time) print("--- Execution time : %4.4s seconds ---" % end_time) results_ccdr = ['CCDR', aupr, shd, end_time] print(results_ccdr) return results_pc, results_ges, results_lingam, results_ccdr
def graph_score(self, test_graph: nx.DiGraph) -> float: pr_score, _ = precision_recall(self._ground_truth_graph, test_graph) shd_score = SHD(self._ground_truth_graph, test_graph) return float(pr_score - shd_score) # Higher better
results_ccdr = ['CCDR', aupr, shd, end_time] print(results_ccdr) return results_pc, results_ges, results_lingam, results_ccdr # Tests start_time = time.time() obj = CGNN(nruns=1, train_epochs=500, test_epochs=500) output = obj.predict(data) adj_mat = nx.adjacency_matrix(output).todense() output = clr(adj_mat) output[np.isnan(output)] = 0 output[np.isposinf(output)] = 1 predicted = retrieve_adjacency_matrix(output) true_matrix = pd.read_csv('true_CM.csv', header=None) true_matrix = np.array(true_matrix) shd = SHD(np.array(true_matrix), predicted, double_for_anticausal=False) aupr, curve = precision_recall(np.array(true_matrix), output) end_time = (time.time() - start_time) print("--- Execution time : %4.4s seconds ---" % end_time) results_cgnn = ['CGNN', aupr, shd, end_time] print(results_cgnn)