diff --git a/causallearn/utils/KCI/KCI.py b/causallearn/utils/KCI/KCI.py index baf7620..379894e 100644 --- a/causallearn/utils/KCI/KCI.py +++ b/causallearn/utils/KCI/KCI.py @@ -146,7 +146,11 @@ def kernel_matrix(self, data_x, data_y): raise Exception('Undefined kernel function') data_x = stats.zscore(data_x, axis=0) + data_x[np.isnan(data_x)] = 0. + data_y = stats.zscore(data_y, axis=0) + data_y[np.isnan(data_y)] = 0. + Kx = kernelX.kernel(data_x) Ky = kernelY.kernel(data_y) return Kx, Ky @@ -323,8 +327,13 @@ def kernel_matrix(self, data_x, data_y, data_z): """ # normalize the data data_x = stats.zscore(data_x, axis=0) + data_x[np.isnan(data_x)] = 0. + data_y = stats.zscore(data_y, axis=0) + data_y[np.isnan(data_y)] = 0. + data_z = stats.zscore(data_z, axis=0) + data_z[np.isnan(data_z)] = 0. # concatenate x and z data_x = np.concatenate((data_x, 0.5 * data_z), axis=1) diff --git a/causallearn/utils/cit.py b/causallearn/utils/cit.py index da30891..2f916b3 100644 --- a/causallearn/utils/cit.py +++ b/causallearn/utils/cit.py @@ -1,4 +1,5 @@ from math import log, sqrt +from collections.abc import Iterable import numpy as np from scipy.stats import chi2, norm @@ -59,9 +60,18 @@ def _unique(column): } def kci(self, X, Y, condition_set): + if type(X) == int: + X = [X] + elif type(X) != list: + Y = list(X) + if type(Y) == int: + Y = [Y] + elif type(Y) != list: + Y = list(Y) + if len(condition_set) == 0: - return self.kci_ui.compute_pvalue(self.data[:, [X]], self.data[:, [Y]])[0] - return self.kci_ci.compute_pvalue(self.data[:, [X]], self.data[:, [Y]], self.data[:, list(condition_set)])[0] + return self.kci_ui.compute_pvalue(self.data[:, X], self.data[:, Y])[0] + return self.kci_ci.compute_pvalue(self.data[:, X], self.data[:, Y], self.data[:, list(condition_set)])[0] def fisherz(self, X, Y, condition_set): """ @@ -326,7 +336,23 @@ def __call__(self, X, Y, condition_set=None, *args): else: assert len(args) == 2, "Arguments other than skel and prt_m are provided for mc_fisherz." if condition_set is None: condition_set = tuple() - assert X not in condition_set and Y not in condition_set, "X, Y cannot be in condition_set." + + if type(X) == int and type(Y) == int: + assert X not in condition_set and Y not in condition_set, "X, Y cannot be in condition_set." + else: + if isinstance(X, Iterable): + assert len(set(condition_set).intersection(X)) == 0, "X cannot be in condition_set." + elif isinstance(X, int): + assert X not in condition_set, "X cannot be in condition_set." + else: + raise Exception("Undefined type of X, X should be int or Iterable") + if isinstance(Y, Iterable): + assert len(set(condition_set).intersection(Y)) == 0, "Y cannot be in condition_set." + elif isinstance(Y, int): + assert Y not in condition_set, "Y cannot be in condition_set." + else: + raise Exception("Undefined type of Y, Y should be int or Iterable") + i, j = (X, Y) if (X < Y) else (Y, X) cache_key = (i, j, frozenset(condition_set))