コード例 #1
0
def run_pcmci(data, tau_max=8, plot_res=False):
	var_names = list(data.columns)
	pcmci = pcmci_setup(data)
	# correlations = pcmci.get_lagged_dependencies(tau_max=20, val_only=True)['val_matrix']
	results = pcmci.run_pcmci(tau_max=tau_max, pc_alpha=None)
	q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
	link_matrix = pcmci.return_significant_links(pq_matrix=q_matrix, val_matrix=results['val_matrix'], alpha_level=0.01)['link_matrix']
	if plot_res:
		tp.plot_graph(val_matrix=results['val_matrix'], link_matrix=link_matrix, var_names=var_names, link_colorbar_label='cross-MCI', node_colorbar_label='auto-MCI')
		plt.show()
	return link_matrix
コード例 #2
0
def plot_results(pcmci, results, cond_ind_test, pc_alpha, tau_min, tau_max,
                 var_names):
    """Generates a network and a timeseries plot for the given results of the PCMCI algorithm and saves them as files.

    Args:
        pcmci: The PCMCI object used.
        results: The results of the PCMCI algorithm.
        cond_ind_test: The conditional independence test used.
        pc_alpha: Alpha threshold for significance of links.
        tau_min: The minimum lag.
        tau_max: The maximum lag.
        var_names: The names of the variables in the data.
    """
    base_path = "experiments/causal_discovery/results/"
    q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'],
                                           fdr_method='fdr_bh')

    link_matrix = pcmci.return_significant_parents(
        pq_matrix=q_matrix, val_matrix=results['val_matrix'],
        alpha_level=0.01)['link_matrix']
    class_prefix = ""
    if isinstance(cond_ind_test, RCOT):
        class_prefix = f"{cond_ind_test.measure}_num_f_{cond_ind_test.num_f}"
    else:
        class_prefix = cond_ind_test.measure
    file_name_prefix = f"{class_prefix}_alpha_{pc_alpha:.5f}_tau_{tau_min}to{tau_max}"

    tp.plot_graph(val_matrix=results['val_matrix'],
                  link_matrix=link_matrix,
                  var_names=var_names,
                  link_colorbar_label='cross-MCI',
                  node_colorbar_label='auto-MCI',
                  figsize=(20, 20),
                  label_fontsize=24,
                  node_label_size=24,
                  link_label_fontsize=14,
                  save_name=base_path + file_name_prefix + "_graph.png")

    tp.plot_time_series_graph(val_matrix=results['val_matrix'],
                              link_matrix=link_matrix,
                              var_names=var_names,
                              link_colorbar_label='MCI',
                              figsize=(20, 20),
                              label_fontsize=24,
                              node_label_size=24,
                              save_name=base_path + file_name_prefix +
                              "_time_series_graph.png")
コード例 #3
0
ファイル: main.py プロジェクト: yangxhcaf/cybertraining
q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'],
                                       fdr_method='fdr_bh')
pcmci.print_significant_links(p_matrix=results['p_matrix'],
                              q_matrix=q_matrix,
                              val_matrix=results['val_matrix'],
                              alpha_level=0.01)

link_matrix = pcmci.return_significant_parents(
    pq_matrix=q_matrix, val_matrix=results['val_matrix'],
    alpha_level=0.01)['link_matrix']

graph = tp.plot_graph(
    val_matrix=results['val_matrix'],
    link_matrix=link_matrix,
    var_names=headers,
    link_colorbar_label='cross-MCI',
    node_colorbar_label='auto-MCI',
)

if verbose > 1:
    if display_images:
        graph[0].show()
    if save_images:
        graph[0].savefig("causal.png")

if verbose > 0:
    # Plot time series graph
    if display_images:
        plot = tp.plot_time_series_graph(val_matrix=results['val_matrix'],
                                         link_matrix=link_matrix,
コード例 #4
0
ファイル: NWPAC.py プロジェクト: n47-5c1/Personal_computer
data_mask = np.zeros(data.shape)
for t in range(1, T + 1):
    if (t % 73) >= 12 and (t % 73) <= 30:
        data_mask[t - 1, :] = True

# Initialize dataframe object, specify time axis and variable names
var_names = ['WPSH', 'IO', 'WNP', 'ENSO', 'NAO']
dataframe = pp.DataFrame(data, mask=data_mask)

parcorr = ParCorr(significance='analytic', mask_type='xyz')
pcmci = PCMCI(dataframe=dataframe, cond_ind_test=parcorr)
results = pcmci.run_pcmci(tau_max=12, pc_alpha=0.03)

# Correct p-values
q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'],
                                       fdr_method='fdr_bh')

# Plotting
link_matrix = pcmci.return_significant_parents(
    pq_matrix=q_matrix, val_matrix=results['val_matrix'],
    alpha_level=0.03)['link_matrix']

tp.plot_graph(val_matrix=results['val_matrix'],
              link_matrix=link_matrix,
              var_names=var_names)
"""
tp.plot_time_series_graph(val_matrix=results['val_matrix'],
                          link_matrix=link_matrix, var_names=var_names)
"""
plt.savefig('./NWPAC.svg')
コード例 #5
0
# In[11]:

link_matrix = pcmci.return_significant_links(pq_matrix=q_matrix,
                                             val_matrix=results['val_matrix'],
                                             alpha_level=0.01)['link_matrix']

# In[12]:

link_matrix.shape

# In[13]:

tp.plot_graph(
    figsize=(5, 5),
    val_matrix=results['val_matrix'],
    link_matrix=link_matrix,
    var_names=var_names,
    link_colorbar_label='cross-MCI',
    node_colorbar_label='auto-MCI',
)
plt.show()

# In[14]:

# Plot time series graph
tp.plot_time_series_graph(
    val_matrix=results['val_matrix'],
    link_matrix=link_matrix,
    var_names=var_names,
    link_colorbar_label='MCI',
)
plt.show()
コード例 #6
0
results = pcmci.run_pcmci(tau_min = min_lag, tau_max=max_lag, pc_alpha=None)
#
q_matrix = pcmci.get_corrected_pvalues(p_matrix=results['p_matrix'], fdr_method='fdr_bh')
#
pcmci.print_significant_links(
        p_matrix = results['p_matrix'], 
        q_matrix = q_matrix,
        val_matrix = results['val_matrix'],
        alpha_level = 0.05)

link_matrix = pcmci.return_significant_links(pq_matrix = results['p_matrix'],
                        val_matrix=results['val_matrix'], alpha_level=0.05)['link_matrix']
tp.plot_graph(
    val_matrix=results['val_matrix'],
    link_matrix=link_matrix,
    var_names=study_data.columns,
    link_colorbar_label='cross-MCI',
    node_colorbar_label='auto-MCI',
    )
plt.show()

#dataframe = dataframe.iloc[:,:-4]
G = nx.DiGraph()
for i , node_name in enumerate(dataframe.var_names):
#    G.add_node((i,{'name':node_name}))
    G.add_node(i,name = node_name, influenced_by = 0)
    
    
#make N^2 matrix
n_connections = 0
all_tau_link = np.max(link_matrix, axis = 2)
コード例 #7
0
            if 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"
            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']]
print(links_df.sort_values(by=['Source', 'Target']))
links_df.to_csv('C:\\Users\\thril\\Desktop\\123.csv')

# print(graph_pcmci)

tp.plot_graph(
    val_matrix=results_pcmci['val_matrix'],
    link_matrix=results_pcmci['graph'],
    var_names=p_agent_names,
    link_colorbar_label='cross-MCI (edges)',
    node_colorbar_label='auto-MCI (nodes)',
)
plt.show()