def a_run_pcmciplus(a_pcmciplus, a_pcmciplus_params): # Unpack the pcmci and the true parents, and common parameters dataframe, true_graph, links_coeffs, tau_min, tau_max = a_pcmciplus # Unpack the parameters ( pc_alpha, contemp_collider_rule, conflict_resolution, reset_lagged_links, cond_ind_test_class, ) = a_pcmciplus_params if cond_ind_test_class == 'oracle_ci': cond_ind_test = OracleCI(links_coeffs) elif cond_ind_test_class == 'par_corr': cond_ind_test = ParCorr() # Run the PCMCI algorithm with the given parameters pcmci = PCMCI(dataframe=dataframe, cond_ind_test=cond_ind_test, verbosity=2) results = pcmci.run_pcmciplus( selected_links=None, tau_min=tau_min, tau_max=tau_max, pc_alpha=pc_alpha, contemp_collider_rule=contemp_collider_rule, conflict_resolution=conflict_resolution, reset_lagged_links=reset_lagged_links, max_conds_dim=None, max_conds_py=None, max_conds_px=None, ) # Print true links print("************************") print("\nTrue Graph") for lag in range(tau_max): print("Lag %d = ", lag) print(true_graph[:, :, lag]) # pcmci.print_significant_links( # p_matrix=(true_graph != ""), # val_matrix=true_graph, # conf_matrix=None, # q_matrix=None, # graph=true_graph, # ambiguous_triples=None, # alpha_level=0.05) # Return the results and the expected result return results['graph'], true_graph
def test_order_independence_pcmciplus(a_pcmciplus_order_independence, a_pcmciplus_params_order_independence): # Unpack the pcmci and the true parents, and common parameters dataframe, true_graph, links_coeffs, tau_min, tau_max = \ a_pcmciplus_order_independence data = dataframe.values T, N = data.shape # Unpack the parameters ( pc_alpha, contemp_collider_rule, conflict_resolution, reset_lagged_links, cond_ind_test_class, ) = a_pcmciplus_params_order_independence if cond_ind_test_class == 'oracle_ci': cond_ind_test = OracleCI(links_coeffs) elif cond_ind_test_class == 'par_corr': cond_ind_test = ParCorr() # Run the PCMCI algorithm with the given parameters pcmci = PCMCI(dataframe=dataframe, cond_ind_test=cond_ind_test, verbosity=1) print("************************") print("\nTrue Graph") pcmci.print_significant_links(p_matrix=(true_graph == 0), val_matrix=true_graph, conf_matrix=None, q_matrix=None, graph=true_graph, ambiguous_triples=None, alpha_level=0.05) results = pcmci.run_pcmciplus( selected_links=None, tau_min=tau_min, tau_max=tau_max, pc_alpha=pc_alpha, contemp_collider_rule=contemp_collider_rule, conflict_resolution=conflict_resolution, reset_lagged_links=reset_lagged_links, max_conds_dim=None, max_conds_py=None, max_conds_px=None, ) correct_results = results['graph'] for perm in itertools.permutations(range(N)): print(perm) data_new = np.copy(data[:, perm]) dataframe = pp.DataFrame(data_new, var_names=list(perm)) pcmci = PCMCI(dataframe=dataframe, cond_ind_test=cond_ind_test, verbosity=1) results = pcmci.run_pcmciplus( selected_links=None, tau_min=tau_min, tau_max=tau_max, pc_alpha=pc_alpha, contemp_collider_rule=contemp_collider_rule, conflict_resolution=conflict_resolution, reset_lagged_links=reset_lagged_links, max_conds_dim=None, max_conds_py=None, max_conds_px=None, ) tmp = np.take(correct_results, perm, axis=0) back_converted_result = np.take(tmp, perm, axis=1) for tau in range(tau_max + 1): if not np.allclose(results['graph'][:, :, tau], back_converted_result[:, :, tau]): print(tau) print(results['graph'][:, :, tau]) print(back_converted_result[:, :, tau]) print(back_converted_result[:, :, tau] - results['graph'][:, :, tau]) print(perm) # np.allclose(results['graph'], back_converted_result) np.testing.assert_equal(results['graph'], back_converted_result)
def build_link_pcmci_noself(p_data_values, p_agent_names, p_var_sou, p_var_tar): """ build links by n column data """ [times_num, agent_num] = p_data_values.shape # set the data for PCMCI data_frame = pp.DataFrame(p_data_values, var_names=p_agent_names, missing_flag=BaseConfig.BACKGROUND_VALUE) # new PCMCI pcmci = PCMCI(dataframe=data_frame, cond_ind_test=ParCorr()) # run PCMCI alpha_level = 0.01 results_pcmci = pcmci.run_pcmciplus(tau_min=0, tau_max=2, pc_alpha=alpha_level) # get the result graph_pcmci = results_pcmci['graph'] q_matrix = results_pcmci['q_matrix'] p_matrix = results_pcmci['p_matrix'] val_matrix = results_pcmci['val_matrix'] conf_matrix = results_pcmci['conf_matrix'] ambiguous_triples = results_pcmci['ambiguous_triples'] # filter these links links_df = pd.DataFrame(columns=('VarSou', 'VarTar', 'Source', 'Target', 'TimeLag', 'Strength', 'Unoriented')) if graph_pcmci is not None: sig_links = (graph_pcmci != "") * (graph_pcmci != "<--") elif q_matrix is not None: sig_links = (q_matrix <= alpha_level) else: sig_links = (p_matrix <= alpha_level) for j in range(agent_num): links = {(p[0], -p[1]): np.abs(val_matrix[p[0], j, abs(p[1])]) for p in zip(*np.where(sig_links[:, j, :]))} # Sort by value sorted_links = sorted(links, key=links.get, reverse=True) for p in sorted_links: VarSou = p_var_sou VarTar = p_var_tar Source = p_agent_names[j] Target = p_agent_names[p[0]] TimeLag = p[1] Strength = val_matrix[p[0], j, abs(p[1])] Unoriented = None if graph_pcmci is not None: if p[1] == 0 and graph_pcmci[j, p[0], 0] == "o-o": Unoriented = 1 # "unoriented link" elif graph_pcmci[p[0], j, abs(p[1])] == "x-x": Unoriented = 1 # "unclear orientation due to conflict" else: Unoriented = 0 links_df = links_df.append(pd.DataFrame({ 'VarSou': [VarSou], 'VarTar': [VarTar], 'Source': [Source], 'Target': [Target], 'TimeLag': [TimeLag], 'Strength': [Strength], 'Unoriented': [Unoriented] }), ignore_index=True) # remove the self correlation edges links_df = links_df.loc[links_df['Source'] != links_df['Target']] return links_df
# set the data for PCMCI data_frame = pp.DataFrame(p_data_values, var_names=p_agent_names, missing_flag=BaseConfig.BACKGROUND_VALUE) # new PCMCI pcmci = PCMCI(dataframe=data_frame, cond_ind_test=ParCorr(significance='analytic')) # correlations = pcmci.run_bivci(tau_max=10, val_only=True)['val_matrix'] # lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations, # setup_args={'var_names':p_agent_names, 'figsize':(15, 10), # 'x_base':5, 'y_base':.5}) # run PCMCI alpha_level = None results_pcmci = pcmci.run_pcmciplus(tau_min=0, tau_max=2, pc_alpha=alpha_level) # get the result graph_pcmci = results_pcmci['graph'] q_matrix = results_pcmci['q_matrix'] p_matrix = results_pcmci['p_matrix'] val_matrix = results_pcmci['val_matrix'] conf_matrix = results_pcmci['conf_matrix'] ambiguous_triples = results_pcmci['ambiguous_triples'] pcmci.print_significant_links(p_matrix=results_pcmci['p_matrix'], q_matrix=q_matrix, val_matrix=results_pcmci['val_matrix'], alpha_level=0.01) # filter these links links_df = pd.DataFrame(columns=('VarSou', 'VarTar', 'Source', 'Target',