Skip to content

Commit

Permalink
fixed issue with get_measure for variable data type
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrunge committed Nov 26, 2024
1 parent 6d4119a commit 9fbeecb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 59 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run(self):
# Run the setup
setup(
name="tigramite",
version="5.2.6.5",
version="5.2.6.6",
packages=["tigramite", "tigramite.independence_tests", "tigramite.toymodels"],
license="GNU General Public License v3.0",
description="Tigramite causal inference for time series",
Expand Down
30 changes: 15 additions & 15 deletions tigramite/data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,18 +1574,18 @@ def nonlin_f(x): return (x + 5. * x**2 * np.exp(-x**2 / 20.))

print(frame.T)

X=[(0, 0)]
Y=[(0, 0)]
Z=[(0, -3)]
tau_max=5
frame.construct_array(X, Y, Z, tau_max,
extraZ=None,
mask=None,
mask_type=None,
return_cleaned_xyz=False,
do_checks=True,
cut_off='2xtau_max',
verbosity=4)

print(ordinal_patt_array(data, array_mask=None, dim=2, step=1,
weights=False, verbosity=0)[0])
# X=[(0, 0)]
# Y=[(0, 0)]
# Z=[(0, -3)]
# tau_max=5
# frame.construct_array(X, Y, Z, tau_max,
# extraZ=None,
# mask=None,
# mask_type=None,
# return_cleaned_xyz=False,
# do_checks=True,
# cut_off='2xtau_max',
# verbosity=4)

# print(ordinal_patt_array(data, array_mask=None, dim=2, step=1,
# weights=False, verbosity=0)[0])
46 changes: 38 additions & 8 deletions tigramite/independence_tests/independence_tests_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,15 +837,45 @@ def get_measure(self, X, Y, Z=None, tau_max=0,
The test statistic value.
"""
# Make the array
array, xyz, (X, Y, Z), _ = self._get_array(X=X, Y=Y, Z=Z, tau_max=tau_max,
remove_constant_data=False)
D, T = array.shape
# Check it is valid
if np.isnan(array).sum() != 0:

if self.significance == 'fixed_thres' and alpha_or_thres is None:
raise ValueError("significance == 'fixed_thres' requires setting alpha_or_thres")

# Get the array to test on
(array, xyz, XYZ, data_type,
nonzero_array, nonzero_xyz, nonzero_XYZ, nonzero_data_type) = self._get_array(
X=X, Y=Y, Z=Z, tau_max=tau_max,
remove_constant_data=True,
verbosity=self.verbosity)
X, Y, Z = XYZ
nonzero_X, nonzero_Y, nonzero_Z = nonzero_XYZ

# Record the dimensions
# dim, T = array.shape

# Ensure it is a valid array
if np.any(np.isnan(array)):
raise ValueError("nans in the array!")
# Return the dependence measure
return self._get_dependence_measure_recycle(X, Y, Z, xyz, array)

# If all X or all Y are zero, then return pval=1, val=0, dependent=False
if len(nonzero_X) == 0 or len(nonzero_Y) == 0:
val = 0.
else:
# Get the dependence measure, reycling residuals if need be
val = self._get_dependence_measure_recycle(nonzero_X, nonzero_Y, nonzero_Z,
nonzero_xyz, nonzero_array, nonzero_data_type)

return val

# # Make the array
# array, xyz, (X, Y, Z), _ = self._get_array(X=X, Y=Y, Z=Z, tau_max=tau_max,
# remove_constant_data=False)
# D, T = array.shape
# # Check it is valid
# if np.isnan(array).sum() != 0:
# raise ValueError("nans in the array!")
# # Return the dependence measure
# return self._get_dependence_measure_recycle(X, Y, Z, xyz, array)

def get_confidence(self, X, Y, Z=None, tau_max=0,
data_type=None):
Expand Down
49 changes: 15 additions & 34 deletions tigramite/pcmci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2855,7 +2855,6 @@ def _run_pcalg_test(self, graph, i, abstau, j, S, lagged_parents, max_conds_py,
val, pval, dependent = self.cond_ind_test.run_test(X=[(i, -abstau)], Y=[(j, 0)],
Z=Z, tau_max=tau_max,
alpha_or_thres=alpha_or_thres,
# verbosity=self.verbosity
)

return val, pval, Z, dependent
Expand Down Expand Up @@ -3925,7 +3924,8 @@ def _optimize_pcmciplus_alpha(self,

if __name__ == '__main__':
from tigramite.independence_tests.parcorr import ParCorr
from tigramite.independence_tests.cmiknn import CMIknn
from tigramite.independence_tests.regression_ci import RegressionCI
# from tigramite.independence_tests.cmiknn import CMIknn

import tigramite.data_processing as pp
from tigramite.toymodels import structural_causal_processes as toys
Expand Down Expand Up @@ -4009,39 +4009,20 @@ def _optimize_pcmciplus_alpha(self,
# data, _ = toys.structural_causal_process(links_coeffs, T=T, seed=3)
# T, N = data.shape

# # Initialize dataframe object
# dataframe = pp.DataFrame(data)
# pcmci = PCMCI(
# dataframe=dataframe,
# cond_ind_test=ParCorr(),
# verbosity=0)

# multidata[0][40:100, :] = 999.

# dataframe = pp.DataFrame(multidata, analysis_mode='multiple',
# missing_flag = 999.,
# time_offsets = {0:50, 1:0}
# # reference_points=list(range(500, 1000))
# )

# pcmci = PCMCI(dataframe=dataframe,
# cond_ind_test=ParCorr(verbosity=0), verbosity=0)

# # results = pcmci.run_pcmciplus(tau_max=1)
multidata = np.random.randn(10, 100, 5)
data_type = np.zeros((10, 100, 5), dtype='bool')
data_type[:,:,:3] = True

# results = pcmci.run_sliding_window_of(
# window_step=499, window_length=500,
# method='run_pcmciplus', method_args={'tau_max':1,
# 'link_assumptions':{
# 0: {(0, -1): '-->'},
# 1: {(1, -1): '-->', (0, -1): '-!>'},
# }
# })
dataframe = pp.DataFrame(multidata,
data_type=data_type,
analysis_mode='multiple',
missing_flag = 999.,
time_offsets = {0:50, 1:0}
# reference_points=list(range(500, 1000))
)

# # tp.plot_graph(results['graph'])
# print(multidata[0].shape, multidata[1].shape)
# print(results['window_results']['val_matrix'])
# print(results['window_results']['val_matrix'][0][0,1])
# print(results['window_results']['val_matrix'][1][0,1])
pcmci = PCMCI(dataframe=dataframe,
cond_ind_test=RegressionCI(verbosity=0), verbosity=0)

# plt.show()
# results = pcmci.run_pcmciplus(tau_max=1)
2 changes: 1 addition & 1 deletion tigramite/toymodels/structural_causal_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def structural_causal_process(links, T, noises=None,
elif isinstance(intervention_type, str):
intervention_type = {j:intervention_type for j in intervention}
for j in intervention.keys():
if len(intervention[j]) != T:
if len(np.atleast_1d(intervention[j])) != T:
raise ValueError("intervention array for j=%s must be of length T = %d" %(j, T))
if j not in intervention_type.keys():
raise ValueError("intervention_type dictionary must contain entry for %s" %(j))
Expand Down

0 comments on commit 9fbeecb

Please sign in to comment.