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")
Exemplo n.º 2
0
    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,
                                         var_names=headers,
                                         link_colorbar_label='MCI')
    if save_images:
        plot = tp.plot_time_series_graph(val_matrix=results['val_matrix'],
                                         link_matrix=link_matrix,
                                         var_names=headers,
                                         link_colorbar_label='MCI',
                                         save_name="causal_time_series.png")
Exemplo n.º 3
0
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')
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()

# ## 非线性测试

# In[15]:

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
## use `%matplotlib notebook` for interactive figures
# plt.style.use('ggplot')
import sklearn
Exemplo n.º 5
0
print('\n\n#### Target Value Predictors: #### \n')
print(*[
    str(data_filled.columns[tup[0]]) + ', lag = ' + str(tup[1])
    for tup in predictors[target]
],
      sep="\n")
link_matrix = np.zeros((N, N, tau_max + 1), dtype='bool')
for j in [target]:
    for p in predictors[j]:
        link_matrix[p[0], j, abs(p[1])] = 1

# Plot time series graph
tp.plot_time_series_graph(
    figsize=(6, 3),
    #     node_aspect=2.,
    val_matrix=np.ones(link_matrix.shape),
    link_matrix=link_matrix,
    var_names=None,
    link_colorbar_label='',
)
plt.show()

#------

pred.fit(target_predictors=predictors,
         selected_targets=[target],
         tau_max=tau_max,
         return_data=True)

new_data_vals = data_filled[study_vars].copy(
) if use_study_vars == True else data_filled.copy()
new_data_vals = new_data_vals.replace(mssng, np.nan)
Exemplo n.º 6
0
def caus_gpdc(data, var_names):
    import numpy as np
    import matplotlib as mpl
    from matplotlib import pyplot as plt
    import sklearn

    import tigramite
    from tigramite import data_processing as pp
    from tigramite import plotting as tp
    from tigramite.pcmci import PCMCI
    from tigramite.independence_tests import ParCorr, GPDC, CMIknn, CMIsymb
    from tigramite.models import LinearMediation, Prediction

    data_mask_row = np.zeros(len(data))
    for i in range(68904):
        if (i % 72) < 30 or (i % 72) > 47:
            data_mask_row[i] = True
    data_mask = np.zeros(data.shape)

    data_mask[:, 0] = data_mask_row
    data_mask[:, 1] = data_mask_row
    data_mask[:, 2] = data_mask_row
    data_mask[:, 9] = data_mask_row
    data_mask[:, 10] = data_mask_row
    data_mask[:, 11] = data_mask_row

    dataframe = pp.DataFrame(data, mask=data_mask)
    datatime = np.arange(len(data))

    # tp.plot_timeseries(data, datatime, var_names, use_mask=True,
    #                    mask=data_mask, grey_masked_samples='data')

    gpdc = GPDC(significance='analytic',
                gp_params=None,
                use_mask=True,
                mask_type='y')
    gpdc.generate_and_save_nulldists(sample_sizes=range(495, 501),
                                     null_dist_filename='dc_nulldists.npz')
    gpdc.null_dist_filename = 'dc_nulldists.npz'
    pcmci_gpdc = PCMCI(dataframe=dataframe,
                       cond_ind_test=gpdc,
                       var_names=var_names,
                       verbosity=1)

    # correlations = pcmci.get_lagged_dependencies(tau_max=20)
    # lag_func_matrix = tp.plot_lagfuncs(val_matrix=correlations,
    #                                    setup_args={'var_names':var_names,
    #                                    'x_base':5, 'y_base':.5})

    results = pcmci_gpdc.run_pcmci(tau_max=6, tau_min=1, pc_alpha=0.01)

    # print("p-values")
    # print (results['p_matrix'].round(3))
    # print("MCI partial correlations")
    # print (results['val_matrix'].round(2))

    q_matrix = pcmci_gpdc.get_corrected_pvalues(p_matrix=results['p_matrix'],
                                                fdr_method='fdr_bh')
    pcmci_gpdc._print_significant_links(p_matrix=results['p_matrix'],
                                        q_matrix=q_matrix,
                                        val_matrix=results['val_matrix'],
                                        alpha_level=0.01)

    link_matrix = pcmci_gpdc._return_significant_parents(
        pq_matrix=q_matrix, val_matrix=results['val_matrix'],
        alpha_level=0.01)['link_matrix']

    tp.plot_time_series_graph(
        val_matrix=results['val_matrix'],
        link_matrix=link_matrix,
        var_names=var_names,
        link_colorbar_label='MCI',
    )
    return results, link_matrix