Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with CMIknn test #434

Open
Meet1995 opened this issue Nov 27, 2024 · 0 comments
Open

Help with CMIknn test #434

Meet1995 opened this issue Nov 27, 2024 · 0 comments

Comments

@Meet1995
Copy link

Hi, I'm trying to run PCMCI with the CMIknn test but am running into a few issues. I think I might be using it incorrectly. Could someone please take a look at my code and let me know where I'm going wrong? My data consists of real, continuous time series with no missing values.

from tigramite.data_processing import DataFrame as TigramiteDataFrame
from tigramite.independence_tests.cmiknn import CMIknn
from tigramite.pcmci import PCMCI

tig_data = TigramiteDataFrame(data=df_ts.values[:, :10], var_names=df_ts.columns[:10])

cmi_knn = CMIknn(significance="shuffle_test", verbosity=2, knn=0.2)
pcmci = PCMCI(dataframe=tig_data, cond_ind_test=cmi_knn, verbosity=2)

results = pcmci.run_pcmci(tau_max=5, pc_alpha=0.05)

I see the following logging messages before the error:

# Initialize conditional independence test

Parameters:
independence test = cmi_knn
significance = shuffle_test
sig_samples = 500
sig_blocklength = None
knn/T = 0.2
shuffle_neighbors = 5


##
## Step 1: PC1 algorithm for selecting lagged conditions
##

Parameters:
independence test = cmi_knn
tau_min = 1
tau_max = 5
pc_alpha = [0.05]
max_conds_dim = None
max_combinations = 1



## Variable avg_drive_gap_perct_region03

Iterating through pc_alpha = [0.05]:

# pc_alpha = 0.05 (1/1):

Testing condition sets of dimension 0:

    Link (avg_drive_gap_perct_region03 -1) -?> avg_drive_gap_perct_region03 (1/50):

Error trace:

KeyError                                  Traceback (most recent call last)
File _pydevd_sys_monitoring\\_pydevd_sys_monitoring_cython.pyx:592, in _pydevd_sys_monitoring_cython._get_func_code_info()

KeyError: (130, '/Users/tmp/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/independence_tests/cmiknn.py', <code object _get_nearest_neighbors at 0x123ee9000, file "/Users/tmp/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/independence_tests/cmiknn.py", line 130>)

During handling of the above exception, another exception occurred:

AssertionError                            Traceback (most recent call last)
Cell In[22], line 4
      1 cmi_knn = CMIknn(significance="shuffle_test", verbosity=2, knn=0.2)
      2 pcmci = PCMCI(dataframe=tig_data, cond_ind_test=cmi_knn, verbosity=2)
----> 4 results = pcmci.run_pcmci(tau_max=5, pc_alpha=0.05)

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/pcmci.py:1921, in PCMCI.run_pcmci(self, selected_links, link_assumptions, tau_min, tau_max, save_iterations, pc_alpha, max_conds_dim, max_combinations, max_conds_py, max_conds_px, alpha_level, fdr_method)
   1917     raise ValueError("selected_links is DEPRECATED, use link_assumptions instead.")
   1920 # Get the parents from run_pc_stable
-> 1921 all_parents = self.run_pc_stable(link_assumptions=link_assumptions,
   1922                                  tau_min=tau_min,
   1923                                  tau_max=tau_max,
   1924                                  save_iterations=save_iterations,
   1925                                  pc_alpha=pc_alpha,
   1926                                  max_conds_dim=max_conds_dim,
   1927                                  max_combinations=max_combinations)
   1929 # Get the results from run_mci, using the parents as the input
   1930 results = self.run_mci(link_assumptions=link_assumptions,
   1931                        tau_min=tau_min,
   1932                        tau_max=tau_max,
   (...)
   1936                        alpha_level=alpha_level,
   1937                        fdr_method=fdr_method)

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/pcmci.py:688, in PCMCI.run_pc_stable(self, selected_links, link_assumptions, tau_min, tau_max, save_iterations, pc_alpha, max_conds_dim, max_combinations)
    683     print("\n# pc_alpha = %s (%d/%d):" % (pc_alpha_here,
    684                                           iscore + 1,
    685                                           score.shape[0]))
    686 # Get the results for this alpha value
    687 results[pc_alpha_here] = \
--> 688     self._run_pc_stable_single(j,
    689                                link_assumptions_j=_int_link_assumptions[j],
    690                                tau_min=tau_min,
    691                                tau_max=tau_max,
    692                                save_iterations=save_iterations,
    693                                pc_alpha=pc_alpha_here,
    694                                max_conds_dim=max_conds_dim,
    695                                max_combinations=max_combinations)
    696 # Figure out the best score if there is more than one pc_alpha
    697 # value
    698 if select_optimal_alpha:

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/pcmci.py:418, in PCMCI._run_pc_stable_single(self, j, link_assumptions_j, tau_min, tau_max, save_iterations, pc_alpha, max_conds_dim, max_combinations)
    416     dependent = True
    417 else:
--> 418     val, pval, dependent = self.cond_ind_test.run_test(X=[parent],
    419                                 Y=[(j, 0)],
    420                                 Z=Z,
    421                                 tau_max=tau_max,
    422                                 alpha_or_thres=pc_alpha,
    423                                 )
    424 # Print some information if needed
    425 if self.verbosity > 1:

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/independence_tests/independence_tests_base.py:429, in CondIndTest.run_test(self, X, Y, Z, tau_max, cut_off, alpha_or_thres)
    426     pval = None if self.significance == 'fixed_thres' else 1.
    427 else:
    428     # Get the dependence measure, reycling residuals if need be
--> 429     val = self._get_dependence_measure_recycle(nonzero_X, nonzero_Y, nonzero_Z, 
    430                                 nonzero_xyz, nonzero_array, nonzero_data_type)
    431     # Get the p-value (None if significance = 'fixed_thres')
    432     dim, T = nonzero_array.shape

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/independence_tests/independence_tests_base.py:700, in CondIndTest._get_dependence_measure_recycle(self, X, Y, Z, xyz, array, data_type)
    697     return self.get_dependence_measure(array, xyz, 
    698                                 data_type=data_type)
    699 else:
--> 700     return self.get_dependence_measure(array, xyz)

File ~/Documents/CausalFramework/.venv/lib/python3.12/site-packages/tigramite/independence_tests/cmiknn.py:244, in CMIknn.get_dependence_measure(self, array, xyz)
    240 else:
    241     knn_here = max(1, int(self.knn))
--> 244 k_xz, k_yz, k_z = self._get_nearest_neighbors(array=array,
    245                                               xyz=xyz,
    246                                               knn=knn_here)
    248 val = special.digamma(knn_here) - (special.digamma(k_xz) +
    249                                    special.digamma(k_yz) -
    250                                    special.digamma(k_z)).mean()
    252 return val

File <stringsource>:69, in cfunc.to_py.__Pyx_CFunc_893235__29_pydevd_sys_monitoring_cython_object__lParen__etc_to_py_4code_18instruction_offset.wrap()

File _pydevd_sys_monitoring\\_pydevd_sys_monitoring_cython.pyx:1697, in _pydevd_sys_monitoring_cython._start_method_event()

File _pydevd_sys_monitoring\\_pydevd_sys_monitoring_cython.pyx:599, in _pydevd_sys_monitoring_cython._get_func_code_info()

AssertionError: <code object get_dependence_measure at 0x168625060
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant