From 9ad2c8ea3cafcd4ac6208107e03b3edb0b90039a Mon Sep 17 00:00:00 2001 From: ZhiyiHuang Date: Sun, 30 Jun 2024 21:22:48 +0800 Subject: [PATCH] fix: hsic stat Signed-off-by: ZhiyiHuang --- .github/workflows/python-package.yml | 1 + causaldmir/datasets/simlulators.py | 9 +++++---- causaldmir/datasets/tests/test_datasets_utils.py | 4 ++-- .../datasets/tests/test_hawkes_simulator.py | 6 ++++-- causaldmir/datasets/tests/test_iid_simulator.py | 7 +++---- .../datasets/tests/test_time_lag_simulator.py | 1 + causaldmir/datasets/utils.py | 7 ++++--- causaldmir/discovery/constraint/pc.py | 2 +- causaldmir/discovery/constraint/tests/test_pc.py | 3 ++- .../Causal_Discovery_in_LHM.py | 8 ++++---- .../FindCausalCluster.py | 9 +++++---- .../LearningHierarchicalStructure/HSIC.py | 1 + .../Infer_Causal_Order.py | 2 +- .../LearningHierarchicalStructure/MakeGraph.py | 2 +- .../LearningHierarchicalStructure/MergeCluster.py | 7 ++++--- .../MergeCluster_backup.py | 7 ++++--- .../LearningHierarchicalStructure/Orientation.py | 4 ++-- .../Overlap_Merge.py | 7 ++++--- .../LearningHierarchicalStructure/TetradMethod.py | 5 +++-- .../LearningHierarchicalStructure/VanishedTest.py | 9 +++++---- .../indTest/FisherTest.py | 1 + .../LearningHierarchicalStructure/indTest/HSIC.py | 1 + .../indTest/HSIC2.py | 10 +++++----- .../indTest/HSICBlockTestObject.py | 7 ++++--- .../indTest/HSICPermutationTestObject.py | 3 ++- .../indTest/HSICSpectralTestObject.py | 5 +++-- .../indTest/HSICTestObject.py | 15 ++++++++------- .../indTest/HSICtestImpure.py | 2 +- .../indTest/TestObject.py | 1 + .../indTest/fastHSIC.py | 10 +++++----- .../indTest/independence.py | 2 +- .../LearningHierarchicalStructure/independence.py | 2 +- .../LearningHierarchicalStructure/mutual.py | 6 +++--- .../LearningHierarchicalStructure/test_LHS.py | 2 +- causaldmir/discovery/funtional_based/anm/ANM.py | 15 +++++++-------- .../funtional_based/anm/tests/test_anm.py | 11 ++++++----- .../funtional_based/lingam_based/ica_lingam.py | 5 +++-- causaldmir/discovery/point_process/SHP.py | 7 ++++--- .../discovery/point_process/tests/TestSHP.py | 6 +++--- causaldmir/discovery/score_based/__init__.py | 3 +-- .../entropy_based_discovery/lbfgsb_scipy.py | 2 +- .../entropy_based_discovery/linear.py | 3 ++- .../entropy_based_discovery/linear_ent.py | 7 ++++--- .../entropy_based_discovery/locally_connected.py | 3 ++- .../entropy_based_discovery/nonlinear.py | 9 +++++---- .../entropy_based_discovery/nonlinear_ent.py | 12 +++++++----- .../test/test_entropy_based_discovery.py | 5 +++-- .../entropy_based_discovery/utils.py | 15 ++++++++------- .../entropy_based/pytorch_expm/expm32.py | 4 ++-- .../entropy_based/pytorch_expm/expm64.py | 4 ++-- .../entropy_based/pytorch_expm/expm_pade.py | 2 +- .../entropy_based/pytorch_expm/expm_taylor.py | 3 ++- .../score_based/greedy_equivalence_search.py | 6 +++--- .../discovery/score_based/tests/ges_test.py | 11 ++++++----- .../discovery/score_based/tests/test_ges.py | 8 +++++--- causaldmir/graph/__init__.py | 1 - causaldmir/graph/dag2cpdag.py | 2 +- causaldmir/graph/pdag2dag.py | 7 ++++--- causaldmir/graph/tests/test_digraph.py | 5 ++--- causaldmir/graph/tests/test_graph.py | 10 ++++------ causaldmir/graph/tests/test_grpah_transform.py | 9 ++++----- causaldmir/utils/independence/__init__.py | 2 +- causaldmir/utils/independence/dsep.py | 3 +-- causaldmir/utils/independence/fisherz.py | 1 + .../utils/independence/functional/fisherz.py | 2 +- causaldmir/utils/independence/kernel_based.py | 9 +++++---- causaldmir/utils/independence/tests/test_desp.py | 6 +++--- causaldmir/utils/kernel/test/test_gaussian.py | 3 ++- causaldmir/utils/kernel/test/test_linear.py | 3 ++- causaldmir/utils/kernel/test/test_polynomial.py | 3 ++- causaldmir/utils/local_score/__init__.py | 2 +- causaldmir/utils/local_score/bdeu_score.py | 2 +- causaldmir/utils/local_score/bic_score.py | 2 +- .../utils/local_score/cross_validated_base.py | 2 +- causaldmir/utils/local_score/marginal_base.py | 4 ++-- causaldmir/utils/local_score/score_utils.py | 5 +++-- causaldmir/utils/metrics/__init__.py | 8 +++++++- .../utils/metrics/tests/test_graph_evaluation.py | 11 +++++++++-- causaldmir/visual/plot_graph.py | 2 +- causaldmir/visual/tests/test_plot_graph.py | 6 ++++-- 80 files changed, 234 insertions(+), 185 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 9af38ee..1cd75e4 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -25,6 +25,7 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install flake8 pytest + python -m pip install numpy if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | diff --git a/causaldmir/datasets/simlulators.py b/causaldmir/datasets/simlulators.py index 846d004..b5a8027 100644 --- a/causaldmir/datasets/simlulators.py +++ b/causaldmir/datasets/simlulators.py @@ -1,10 +1,11 @@ +from itertools import product +from typing import List, Optional, Tuple, Union + import networkx as nx import numpy as np -from sklearn.gaussian_process import GaussianProcessRegressor -from typing import List, Tuple, Union, Optional import pandas as pd -from itertools import product -from tick.hawkes import SimuHawkes, HawkesKernelExp +from sklearn.gaussian_process import GaussianProcessRegressor +from tick.hawkes import HawkesKernelExp, SimuHawkes try: from scipy.special import expit as sigmoid diff --git a/causaldmir/datasets/tests/test_datasets_utils.py b/causaldmir/datasets/tests/test_datasets_utils.py index 625abbc..98c55fa 100644 --- a/causaldmir/datasets/tests/test_datasets_utils.py +++ b/causaldmir/datasets/tests/test_datasets_utils.py @@ -1,7 +1,7 @@ -import numpy as np import networkx as nx +import numpy as np -from causaldmir.datasets.utils import erdos_renyi, np2nx, generate_lag_transitions +from causaldmir.datasets.utils import erdos_renyi, generate_lag_transitions, np2nx def test_erdos_renyi(): diff --git a/causaldmir/datasets/tests/test_hawkes_simulator.py b/causaldmir/datasets/tests/test_hawkes_simulator.py index caf383e..0331c46 100644 --- a/causaldmir/datasets/tests/test_hawkes_simulator.py +++ b/causaldmir/datasets/tests/test_hawkes_simulator.py @@ -1,8 +1,10 @@ from unittest import TestCase + import numpy as np import pandas as pd -from ..simlulators import HawkesSimulator -from ..utils import erdos_renyi, generate_lag_transitions + +from causaldmir.datasets.simlulators import HawkesSimulator +from causaldmir.datasets.utils import erdos_renyi, generate_lag_transitions class TestHawkesSimulator(TestCase): diff --git a/causaldmir/datasets/tests/test_iid_simulator.py b/causaldmir/datasets/tests/test_iid_simulator.py index 1061145..c0f42b5 100644 --- a/causaldmir/datasets/tests/test_iid_simulator.py +++ b/causaldmir/datasets/tests/test_iid_simulator.py @@ -1,13 +1,12 @@ +from unittest import TestCase + import numpy as np from causaldmir.datasets.simlulators import IIDSimulator from causaldmir.datasets.utils import erdos_renyi -class TestIIDSimulator: - - pass - +class TestIIDSimulator(TestCase): def test_simulate_linear_anm_gauss(self): n_nodes = 10 n_edges = 20 diff --git a/causaldmir/datasets/tests/test_time_lag_simulator.py b/causaldmir/datasets/tests/test_time_lag_simulator.py index c65a9c8..89229f1 100644 --- a/causaldmir/datasets/tests/test_time_lag_simulator.py +++ b/causaldmir/datasets/tests/test_time_lag_simulator.py @@ -1,4 +1,5 @@ from unittest import TestCase + import numpy as np from causaldmir.datasets.simlulators import TimeLagSimulator diff --git a/causaldmir/datasets/utils.py b/causaldmir/datasets/utils.py index 9398a8b..f6dfa08 100644 --- a/causaldmir/datasets/utils.py +++ b/causaldmir/datasets/utils.py @@ -1,9 +1,10 @@ +import random +from typing import List, Optional, Sequence, Tuple, Union + +import networkx as nx import numpy as np import pandas as pd -import networkx as nx import torch -import random -from typing import List, Tuple, Union, Optional, Sequence def set_random_seed(seed, set_torch_seed=True): diff --git a/causaldmir/discovery/constraint/pc.py b/causaldmir/discovery/constraint/pc.py index 58fceea..ac9655d 100644 --- a/causaldmir/discovery/constraint/pc.py +++ b/causaldmir/discovery/constraint/pc.py @@ -3,7 +3,7 @@ from causaldmir.utils import data_form_converter_for_class_method from causaldmir.utils.independence import ConditionalIndependentTest -from .adjacency_search import adjacency_search +from causaldmir.discovery.constraint.adjacency_search import adjacency_search class PC(object): diff --git a/causaldmir/discovery/constraint/tests/test_pc.py b/causaldmir/discovery/constraint/tests/test_pc.py index 30584b8..b3aed2e 100644 --- a/causaldmir/discovery/constraint/tests/test_pc.py +++ b/causaldmir/discovery/constraint/tests/test_pc.py @@ -9,7 +9,8 @@ from scipy import stats from causaldmir.discovery.constraint import PC -from causaldmir.graph import DiGraph, Edge, dag2cpdag +from causaldmir.graph import DiGraph, Edge +from causaldmir.graph.dag2cpdag import dag2cpdag from causaldmir.utils.independence import Dsep, FisherZ from causaldmir.utils.metrics.graph_evaluation import graph_equal diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Causal_Discovery_in_LHM.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Causal_Discovery_in_LHM.py index a35ef04..317b15c 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Causal_Discovery_in_LHM.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Causal_Discovery_in_LHM.py @@ -1,11 +1,11 @@ -import numpy as np -import pandas as pd import FindCausalCluster +import MakeGraph import MergeCluster as MC +import numpy as np +import Orientation +import pandas as pd import UpdataData import Utils -import Orientation -import MakeGraph diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/FindCausalCluster.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/FindCausalCluster.py index 5d1aefa..72aa9e1 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/FindCausalCluster.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/FindCausalCluster.py @@ -1,10 +1,11 @@ -import VanishedTest as VT -import numpy as np -import pandas as pd import itertools -import TetradMethod + import MergeCluster +import numpy as np import Overlap_Merge +import pandas as pd +import TetradMethod +import VanishedTest as VT def FindCausalCluster(data, PureCluster, ImpureCluster, alpha=0.01): ''' diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/HSIC.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/HSIC.py index 00d63f9..78c32da 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/HSIC.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/HSIC.py @@ -19,6 +19,7 @@ #HSIC with pval return from __future__ import division + import numpy as np from scipy.stats import gamma diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Infer_Causal_Order.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Infer_Causal_Order.py index f6fc3e1..7116f3b 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Infer_Causal_Order.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Infer_Causal_Order.py @@ -1,6 +1,6 @@ +import GIN2 as GIN #GIN based on mutual information import numpy as np import pandas as pd -import GIN2 as GIN #GIN based on mutual information # identifying causal order by identifying root node recursively. diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MakeGraph.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MakeGraph.py index 688761d..b7d7249 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MakeGraph.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MakeGraph.py @@ -1,5 +1,5 @@ -import networkx as nx import matplotlib.pyplot as plt +import networkx as nx def Make_graph(LatentIndex,order = [],ShareCommonChild = []): clusters = [] diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster.py index 41276ee..d14e706 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster.py @@ -1,9 +1,10 @@ -import VanishedTest as VT +import itertools + import numpy as np import pandas as pd -import itertools -import Utils import UpdataData +import Utils +import VanishedTest as VT diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster_backup.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster_backup.py index d042fb0..57a9ca0 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster_backup.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/MergeCluster_backup.py @@ -1,9 +1,10 @@ -import VanishedTest as VT +import itertools + import numpy as np import pandas as pd -import itertools -import Utils import UpdataData +import Utils +import VanishedTest as VT diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Orientation.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Orientation.py index 3ba5fed..b88a7c0 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Orientation.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Orientation.py @@ -1,8 +1,8 @@ +import GIN2 as GIN +import Infer_Causal_Order import numpy as np import pandas as pd -import GIN2 as GIN import Utils -import Infer_Causal_Order def Orientation_Cluster(Ora_data, LatentIndex, PureClusters, AllCausalCluster): pass diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Overlap_Merge.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Overlap_Merge.py index 6628b08..3b04133 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Overlap_Merge.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/Overlap_Merge.py @@ -1,8 +1,9 @@ -import numpy as np -import pandas as pd -from itertools import combinations import itertools +from itertools import combinations + import networkx as nx +import numpy as np +import pandas as pd '''Merge overlap causal cluster''' def merge_list(L2): diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/TetradMethod.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/TetradMethod.py index b96ae52..998332b 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/TetradMethod.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/TetradMethod.py @@ -8,10 +8,11 @@ # Copyright: (c) YY 2022 # Licence: #------------------------------------------------------------------------------- -import VanishedTest as VT +import itertools + import numpy as np import pandas as pd -import itertools +import VanishedTest as VT ''' Function: Check whether a variable set C is a causal cluster diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/VanishedTest.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/VanishedTest.py index 18db2d1..821e5ed 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/VanishedTest.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/VanishedTest.py @@ -1,10 +1,11 @@ -from scipy.stats import norm -import scipy -import numpy as np -import pandas as pd import math import random + +import numpy as np +import pandas as pd +import scipy +from scipy.stats import norm from sklearn.utils import resample diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/FisherTest.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/FisherTest.py index d18f79e..4bc3d0a 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/FisherTest.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/FisherTest.py @@ -1,4 +1,5 @@ import math + from scipy.stats import chi2 #independent test by Fisher'method diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC.py index 4f80f46..82904c0 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC.py @@ -19,6 +19,7 @@ #HSIC with pval return from __future__ import division + import numpy as np from scipy.stats import gamma diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC2.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC2.py index 950effe..33670b7 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC2.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSIC2.py @@ -1,13 +1,13 @@ #API for HSIC test import sys sys.path.append("./indTest") -from kerpy.GaussianKernel import GaussianKernel -from HSICTestObject import HSICTestObject -from numpy import shape,savetxt,loadtxt,transpose,shape,reshape,concatenate -from independence_testing.HSICSpectralTestObject import HSICSpectralTestObject -from independence_testing.HSICBlockTestObject import HSICBlockTestObject import numpy as np import pandas as pd +from HSICTestObject import HSICTestObject +from independence_testing.HSICBlockTestObject import HSICBlockTestObject +from independence_testing.HSICSpectralTestObject import HSICSpectralTestObject +from kerpy.GaussianKernel import GaussianKernel +from numpy import concatenate, loadtxt, reshape, savetxt, shape, shape, transpose #method 1:HSIC test and return boolean #x and y: diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICBlockTestObject.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICBlockTestObject.py index e7df269..f8dab7a 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICBlockTestObject.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICBlockTestObject.py @@ -1,8 +1,9 @@ -from TestObject import TestObject +import time + from HSICTestObject import HSICTestObject -from numpy import mean, sum, zeros, var, sqrt +from numpy import mean, sqrt, sum, var, zeros from scipy.stats import norm -import time +from TestObject import TestObject class HSICBlockTestObject(HSICTestObject): def __init__(self,num_samples, data_generator=None, kernelX=None, kernelY=None, diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICPermutationTestObject.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICPermutationTestObject.py index 0420f51..fda8ee1 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICPermutationTestObject.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICPermutationTestObject.py @@ -3,9 +3,10 @@ @author: qinyi ''' -from HSICTestObject import HSICTestObject import time +from HSICTestObject import HSICTestObject + class HSICPermutationTestObject(HSICTestObject): diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICSpectralTestObject.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICSpectralTestObject.py index 23ceb86..8a61396 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICSpectralTestObject.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICSpectralTestObject.py @@ -3,10 +3,11 @@ @author: qinyi ''' -from HSICTestObject import HSICTestObject -import numpy as np import time +import numpy as np +from HSICTestObject import HSICTestObject + class HSICSpectralTestObject(HSICTestObject): def __init__(self, num_samples, data_generator=None, diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICTestObject.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICTestObject.py index c0ec2f6..91adc0d 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICTestObject.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICTestObject.py @@ -1,12 +1,13 @@ -from numpy import shape, fill_diagonal, zeros, mean, sqrt,identity,dot,diag -from numpy.random import permutation, randn -from independence_testing.TestObject import TestObject -import numpy as np +import time from abc import abstractmethod + +import numpy as np +from independence_testing.TestObject import TestObject from kerpy.Kernel import Kernel -import time -from scipy.linalg import sqrtm,inv -from numpy.linalg import eigh,svd +from numpy import diag, dot, fill_diagonal, identity, mean, shape, sqrt, zeros +from numpy.linalg import eigh, svd +from numpy.random import permutation, randn +from scipy.linalg import inv, sqrtm diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICtestImpure.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICtestImpure.py index 8c0cb5a..ce0b6ad 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICtestImpure.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/HSICtestImpure.py @@ -8,9 +8,9 @@ # Copyright: (c) YY 2021 # Licence: #------------------------------------------------------------------------------- +import GIN import numpy as np import pandas as pd -import GIN import SimulationData as SD diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/TestObject.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/TestObject.py index 84de02e..adc90e9 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/TestObject.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/TestObject.py @@ -1,4 +1,5 @@ from abc import abstractmethod + from scipy.stats import norm as normaldist diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/fastHSIC.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/fastHSIC.py index 4ead53a..1b38096 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/fastHSIC.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/fastHSIC.py @@ -1,12 +1,12 @@ import sys sys.path.append("./indTest") -from kerpy.GaussianKernel import GaussianKernel -from HSICTestObject import HSICTestObject -from numpy import shape,savetxt,loadtxt,transpose,shape,reshape,concatenate -from independence_testing.HSICSpectralTestObject import HSICSpectralTestObject -from independence_testing.HSICBlockTestObject import HSICBlockTestObject import numpy as np import pandas as pd +from HSICTestObject import HSICTestObject +from independence_testing.HSICBlockTestObject import HSICBlockTestObject +from independence_testing.HSICSpectralTestObject import HSICSpectralTestObject +from kerpy.GaussianKernel import GaussianKernel +from numpy import concatenate, loadtxt, reshape, savetxt, shape, shape, transpose def test(x,y,alph=0.05): diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/independence.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/independence.py index 8a16029..f4cb141 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/independence.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/indTest/independence.py @@ -1,6 +1,6 @@ +import mutual as MI import numpy as np import pandas as pd -import mutual as MI from sklearn import metrics #estimating mutual information by sklearn diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/independence.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/independence.py index 8a16029..f4cb141 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/independence.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/independence.py @@ -1,6 +1,6 @@ +import mutual as MI import numpy as np import pandas as pd -import mutual as MI from sklearn import metrics #estimating mutual information by sklearn diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/mutual.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/mutual.py index e9859cb..eeefa80 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/mutual.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/mutual.py @@ -7,11 +7,11 @@ These computations rely on nearest-neighbor statistics ''' import numpy as np - -from scipy.special import gamma,psi +from numpy import pi from scipy import ndimage from scipy.linalg import det -from numpy import pi + +from scipy.special import gamma, psi from sklearn.neighbors import NearestNeighbors diff --git a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/test_LHS.py b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/test_LHS.py index a5941b5..d7a7884 100644 --- a/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/test_LHS.py +++ b/causaldmir/discovery/funtional_based/LearningHierarchicalStructure/test_LHS.py @@ -1,5 +1,5 @@ -import Paper_simulation as SimulationData import Causal_Discovery_in_LHM as LHM +import Paper_simulation as SimulationData diff --git a/causaldmir/discovery/funtional_based/anm/ANM.py b/causaldmir/discovery/funtional_based/anm/ANM.py index a7de711..69b4363 100644 --- a/causaldmir/discovery/funtional_based/anm/ANM.py +++ b/causaldmir/discovery/funtional_based/anm/ANM.py @@ -2,9 +2,9 @@ Additive Noise Model. """ +import numpy as np from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.preprocessing import scale -import numpy as np def rbf_dot2(p1, p2, deg): @@ -72,7 +72,7 @@ def FastHsicTestGamma(X, Y, sig=[-1, -1], maxpnt=200): def normalized_hsic(x, y): x = (x - np.mean(x)) / np.std(x) y = (y - np.mean(y)) / np.std(y) - h = FastHsicTestGamma(x, y) + h = FastHsicTestGamma(x, y, maxpnt=2000) return h @@ -92,16 +92,15 @@ def cause_or_effect(self, x, y, **kwargs): x = scale(x).reshape((-1, 1)) y = scale(y).reshape((-1, 1)) # calculate the x->y score - p_value_forward = self.anm_score(x, y) + nonindepscore_forward = self.anm_score(x, y) # calculate the y->x score - p_value_backward = self.anm_score(y, x) + nonindepscore_backward = self.anm_score(y, x) - return p_value_forward, p_value_backward + return nonindepscore_forward, nonindepscore_backward def anm_score(self, x, y): gp = GaussianProcessRegressor().fit(x, y) y_predict = gp.predict(x) + nonindepscore = normalized_hsic(y_predict - y, x) - indepscore = normalized_hsic(y - y_predict, x) - - return indepscore + return nonindepscore diff --git a/causaldmir/discovery/funtional_based/anm/tests/test_anm.py b/causaldmir/discovery/funtional_based/anm/tests/test_anm.py index 817e4af..a5996ff 100644 --- a/causaldmir/discovery/funtional_based/anm/tests/test_anm.py +++ b/causaldmir/discovery/funtional_based/anm/tests/test_anm.py @@ -1,7 +1,9 @@ -from causaldmir.discovery.funtional_based.anm.ANM import ANM +from unittest import TestCase + import numpy as np import pandas as pd -from unittest import TestCase + +from causaldmir.discovery.funtional_based.anm.ANM import ANM class TestANM(TestCase): @@ -12,6 +14,5 @@ def test_anm_using_simulation(self): X = np.random.uniform(size=10000) Y = np.power(X, 3) + np.random.uniform(size=10000) anm = ANM() - p_value_forward, p_value_backward = anm.cause_or_effect(X, Y) - - assert p_value_forward < p_value_backward + nonindepscore_forward, nonindepscore_backward = anm.cause_or_effect(X, Y) + assert nonindepscore_forward < nonindepscore_backward diff --git a/causaldmir/discovery/funtional_based/lingam_based/ica_lingam.py b/causaldmir/discovery/funtional_based/lingam_based/ica_lingam.py index 908fd08..4783084 100644 --- a/causaldmir/discovery/funtional_based/lingam_based/ica_lingam.py +++ b/causaldmir/discovery/funtional_based/lingam_based/ica_lingam.py @@ -1,11 +1,12 @@ from __future__ import annotations +import itertools + import numpy as np +import scipy.stats from numpy import ndarray from pandas import DataFrame from sklearn.decomposition import FastICA -import itertools -import scipy.stats from sklearn.linear_model import LinearRegression diff --git a/causaldmir/discovery/point_process/SHP.py b/causaldmir/discovery/point_process/SHP.py index 6c20356..210d699 100644 --- a/causaldmir/discovery/point_process/SHP.py +++ b/causaldmir/discovery/point_process/SHP.py @@ -1,8 +1,9 @@ -import pandas as pd +from itertools import count, product + +import networkx as nx import numpy as np +import pandas as pd from tqdm import tqdm -import networkx as nx -from itertools import count, product diff --git a/causaldmir/discovery/point_process/tests/TestSHP.py b/causaldmir/discovery/point_process/tests/TestSHP.py index 4c6705b..d91b707 100644 --- a/causaldmir/discovery/point_process/tests/TestSHP.py +++ b/causaldmir/discovery/point_process/tests/TestSHP.py @@ -1,14 +1,14 @@ -import numpy as np +import logging from unittest import TestCase +import numpy as np + from causaldmir.datasets.simlulators import HawkesSimulator from causaldmir.utils.metrics.graph_evaluation import get_performance from ..SHP import SHP -import logging - logging.basicConfig(level=logging.DEBUG, format=' %(levelname)s :: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') diff --git a/causaldmir/discovery/score_based/__init__.py b/causaldmir/discovery/score_based/__init__.py index e54c2d2..dc4cc7a 100644 --- a/causaldmir/discovery/score_based/__init__.py +++ b/causaldmir/discovery/score_based/__init__.py @@ -1,5 +1,4 @@ -from .greedy_equivalence_search import GES -from .greedy_equivalence_search import dist2 +from .greedy_equivalence_search import dist2, GES __all__ = [ "GES", diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/lbfgsb_scipy.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/lbfgsb_scipy.py index eb3d9b4..5a1a3c3 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/lbfgsb_scipy.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/lbfgsb_scipy.py @@ -1,5 +1,5 @@ -import torch import scipy.optimize as sopt +import torch class LBFGSBScipy(torch.optim.Optimizer): diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear.py index b880096..5c78b46 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear.py @@ -1,7 +1,8 @@ import numpy as np import scipy.optimize as sopt -from scipy.special import expit as sigmoid import scipy.sparse.linalg as slin +from scipy.special import expit as sigmoid + from . import utils as ut diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear_ent.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear_ent.py index da2a1c2..a20718c 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear_ent.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/linear_ent.py @@ -1,13 +1,14 @@ import numpy as np +import pandas as pd import torch import torch.nn as nn -from torch.nn.parameter import Parameter import torch.optim as optim import torch.utils.data as Data +from torch.nn.parameter import Parameter +from torch.optim import LBFGS + from . import utils from .lbfgsb_scipy import LBFGSBScipy -from torch.optim import LBFGS -import pandas as pd debug = False is_cuda = False diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/locally_connected.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/locally_connected.py index cfa9f46..43b4037 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/locally_connected.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/locally_connected.py @@ -1,6 +1,7 @@ +import math + import torch import torch.nn as nn -import math class LocallyConnected(nn.Module): diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear.py index 941153a..2621dea 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear.py @@ -1,10 +1,11 @@ -from locally_connected import LocallyConnected -from lbfgsb_scipy import LBFGSBScipy +import math + +import numpy as np import torch import torch.nn as nn -import numpy as np -import math import utils as utils +from lbfgsb_scipy import LBFGSBScipy +from locally_connected import LocallyConnected class NotearsMLP(nn.Module): diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear_ent.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear_ent.py index 35328fe..ae642ac 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear_ent.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/nonlinear_ent.py @@ -1,11 +1,13 @@ -from .locally_connected import LocallyConnected -from .lbfgsb_scipy import LBFGSBScipy +import math + +import numpy as np +import pandas as pd import torch import torch.nn as nn -import numpy as np -import math + from . import utils -import pandas as pd +from .lbfgsb_scipy import LBFGSBScipy +from .locally_connected import LocallyConnected class NotearsMLP(nn.Module): diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/test/test_entropy_based_discovery.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/test/test_entropy_based_discovery.py index 3f91766..f837992 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/test/test_entropy_based_discovery.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/test/test_entropy_based_discovery.py @@ -1,10 +1,11 @@ import logging from unittest import TestCase -# D:\letscausalleaern\causal-dmir\causaldmir\discovery\score_based\entropy_based\entropy_based_discovery\test\test_entropy_based_discovery.py -from ..utils import set_seed from ..linear_ent import run from ..nonlinear_ent import run as run_nonlinear +# D:\letscausalleaern\causal-dmir\causaldmir\discovery\score_based\entropy_based\entropy_based_discovery\test\test_entropy_based_discovery.py + +from ..utils import set_seed logging.basicConfig(level=logging.DEBUG, format=' %(levelname)s :: %(message)s', diff --git a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/utils.py b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/utils.py index 4cf9890..5ea62e0 100644 --- a/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/utils.py +++ b/causaldmir/discovery/score_based/entropy_based/entropy_based_discovery/utils.py @@ -1,13 +1,14 @@ -import numpy as np -from scipy.special import expit as sigmoid -import igraph as ig -import random -import networkx as nx -import torch -import matplotlib.pyplot as plt import math import os +import random + +import igraph as ig +import matplotlib.pyplot as plt +import networkx as nx +import numpy as np import pandas as pd +import torch +from scipy.special import expit as sigmoid def set_random_seed(seed): diff --git a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm32.py b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm32.py index de29c0b..b4e0ef5 100644 --- a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm32.py +++ b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm32.py @@ -8,14 +8,14 @@ # Jake Vanderplas, August 2012 (Sparse Updates) # -from __future__ import division, print_function, absolute_import +from __future__ import absolute_import, division, print_function import math import numpy as np +import scipy.special import torch -import scipy.special def _onenorm_matrix_power_nnm(A, p): """ diff --git a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm64.py b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm64.py index 95fabae..2f5e3b2 100644 --- a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm64.py +++ b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm64.py @@ -8,14 +8,14 @@ # Jake Vanderplas, August 2012 (Sparse Updates) # -from __future__ import division, print_function, absolute_import +from __future__ import absolute_import, division, print_function import math import numpy as np +import scipy.special import torch -import scipy.special def _onenorm_matrix_power_nnm(A, p): """ diff --git a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_pade.py b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_pade.py index d0632e5..d36d3d8 100644 --- a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_pade.py +++ b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_pade.py @@ -1,5 +1,5 @@ -import torch import numpy as np +import torch from expm32 import expm32 from expm64 import expm64 diff --git a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_taylor.py b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_taylor.py index 834dddf..85f33b3 100644 --- a/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_taylor.py +++ b/causaldmir/discovery/score_based/entropy_based/pytorch_expm/expm_taylor.py @@ -1,6 +1,7 @@ -import torch import math +import torch + degs = [1, 2, 4, 8, 12, 18] thetas_dict = {"single": [1.192092800768788e-07, # m_vals = 1 diff --git a/causaldmir/discovery/score_based/greedy_equivalence_search.py b/causaldmir/discovery/score_based/greedy_equivalence_search.py index df77781..1516feb 100644 --- a/causaldmir/discovery/score_based/greedy_equivalence_search.py +++ b/causaldmir/discovery/score_based/greedy_equivalence_search.py @@ -6,12 +6,12 @@ import numpy as np -from causaldmir.graph import dag2cpdag, Edge, Mark +from causaldmir.graph import Edge, Mark +from causaldmir.graph.dag2cpdag import dag2cpdag from causaldmir.graph.pdag2dag import pdag2dag -from causaldmir.utils.local_score import * +from causaldmir.utils.local_score import BaseLocalScoreFunction, BDeuScore, BICScore, GeneralCVScore, MultiCVScore, GeneralMarginalScore, MultiMarginalScore from causaldmir.graph.pdag import PDAG - def kernel(x, xKern, theta): # KERNEL Compute the rbf kernel n2 = dist2(x, xKern) diff --git a/causaldmir/discovery/score_based/tests/ges_test.py b/causaldmir/discovery/score_based/tests/ges_test.py index 746ea78..f0a3acd 100644 --- a/causaldmir/discovery/score_based/tests/ges_test.py +++ b/causaldmir/discovery/score_based/tests/ges_test.py @@ -1,14 +1,15 @@ +import random +from unittest import TestCase + import numpy as np import pandas as pd from numpy.random import normal from scipy import stats -import random -from causaldmir.discovery.score_based import GES -from causaldmir.utils.local_score import BICScore -from unittest import TestCase + +from causaldmir.discovery.score_based import dist2, GES from causaldmir.discovery.score_based.greedy_equivalence_search import Score_G -from causaldmir.discovery.score_based import dist2 from causaldmir.graph.pdag import PDAG +from causaldmir.utils.local_score import BICScore class TestGES(TestCase): def test_dist2(self): diff --git a/causaldmir/discovery/score_based/tests/test_ges.py b/causaldmir/discovery/score_based/tests/test_ges.py index 5c4f42e..9a67e1f 100644 --- a/causaldmir/discovery/score_based/tests/test_ges.py +++ b/causaldmir/discovery/score_based/tests/test_ges.py @@ -1,14 +1,16 @@ +import random from unittest import TestCase + import numpy as np import pandas as pd from numpy.random import normal from scipy import stats -import random + +from causaldmir.discovery.score_based import dist2 # from causaldmir.discovery.score_based import GES from causaldmir.discovery.score_based.greedy_equivalence_search import Score_G -from causaldmir.utils.local_score import BICScore -from causaldmir.discovery.score_based import dist2 from causaldmir.graph.pdag import PDAG +from causaldmir.utils.local_score import BICScore class TestGES(TestCase): def test_dist2(self): diff --git a/causaldmir/graph/__init__.py b/causaldmir/graph/__init__.py index 8b1a487..40c2b09 100644 --- a/causaldmir/graph/__init__.py +++ b/causaldmir/graph/__init__.py @@ -3,4 +3,3 @@ from .mark import Mark from .pdag import PDAG from .digraph import DiGraph -from .dag2cpdag import dag2cpdag diff --git a/causaldmir/graph/dag2cpdag.py b/causaldmir/graph/dag2cpdag.py index 68e7f4d..4ce0537 100644 --- a/causaldmir/graph/dag2cpdag.py +++ b/causaldmir/graph/dag2cpdag.py @@ -1,6 +1,6 @@ from itertools import combinations, permutations -from causaldmir.graph import PDAG, DiGraph +from causaldmir.graph import DiGraph, PDAG def dag2cpdag(dag: DiGraph): diff --git a/causaldmir/graph/pdag2dag.py b/causaldmir/graph/pdag2dag.py index 1b5bd6b..6d98cc0 100644 --- a/causaldmir/graph/pdag2dag.py +++ b/causaldmir/graph/pdag2dag.py @@ -1,10 +1,11 @@ from copy import deepcopy -from . import Edge, DiGraph -from .pdag import PDAG -from .mark import Mark import numpy as np +from . import DiGraph, Edge +from .mark import Mark +from .pdag import PDAG + # function Gd = PDAG2DAG(G) % transform a PDAG to DAG def pdag2dag(pdag: PDAG): diff --git a/causaldmir/graph/tests/test_digraph.py b/causaldmir/graph/tests/test_digraph.py index 2979035..5b63fa2 100644 --- a/causaldmir/graph/tests/test_digraph.py +++ b/causaldmir/graph/tests/test_digraph.py @@ -1,9 +1,8 @@ +import logging +from copy import deepcopy from unittest import TestCase from .. import DiGraph, Edge, Mark -from copy import deepcopy - -import logging logging.basicConfig(level=logging.DEBUG, format=' %(levelname)s :: %(message)s', diff --git a/causaldmir/graph/tests/test_graph.py b/causaldmir/graph/tests/test_graph.py index 738eed6..d6973b0 100644 --- a/causaldmir/graph/tests/test_graph.py +++ b/causaldmir/graph/tests/test_graph.py @@ -1,11 +1,9 @@ -from unittest import TestCase - -from .. import Graph -from .. import Edge -from .. import Mark +import logging from copy import deepcopy -import logging +from unittest import TestCase + +from .. import Edge, Graph, Mark logging.basicConfig(level=logging.DEBUG, format=' %(levelname)s :: %(message)s', diff --git a/causaldmir/graph/tests/test_grpah_transform.py b/causaldmir/graph/tests/test_grpah_transform.py index 48a0f16..90cff62 100644 --- a/causaldmir/graph/tests/test_grpah_transform.py +++ b/causaldmir/graph/tests/test_grpah_transform.py @@ -1,13 +1,12 @@ +import logging + +from copy import deepcopy from unittest import TestCase -from .. import Edge, DiGraph, PDAG, Graph, Mark +from .. import DiGraph, Edge, Graph, Mark, PDAG from ..dag2cpdag import dag2cpdag from ..pdag2dag import pdag2dag -from copy import deepcopy - -import logging - logging.basicConfig(level=logging.DEBUG, format=' %(levelname)s :: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') diff --git a/causaldmir/utils/independence/__init__.py b/causaldmir/utils/independence/__init__.py index e7022ac..3fcc940 100644 --- a/causaldmir/utils/independence/__init__.py +++ b/causaldmir/utils/independence/__init__.py @@ -1,7 +1,7 @@ from .basic_independence import ConditionalIndependentTest from .dsep import Dsep from .fisherz import FisherZ -from .kci import KCI +# from .kci import KCI from .kernel_based import KCI __all__ = [ diff --git a/causaldmir/utils/independence/dsep.py b/causaldmir/utils/independence/dsep.py index 05b6946..790accc 100644 --- a/causaldmir/utils/independence/dsep.py +++ b/causaldmir/utils/independence/dsep.py @@ -1,8 +1,7 @@ from typing import Iterable from causaldmir.graph import DiGraph - -from .basic_independence import ConditionalIndependentTest +from causaldmir.utils.independence.basic_independence import ConditionalIndependentTest class Dsep(ConditionalIndependentTest): diff --git a/causaldmir/utils/independence/fisherz.py b/causaldmir/utils/independence/fisherz.py index d925b10..7720dc8 100644 --- a/causaldmir/utils/independence/fisherz.py +++ b/causaldmir/utils/independence/fisherz.py @@ -1,4 +1,5 @@ from __future__ import annotations + from typing import Iterable, List, Tuple from numpy import corrcoef diff --git a/causaldmir/utils/independence/functional/fisherz.py b/causaldmir/utils/independence/functional/fisherz.py index 3eeae7f..500a6e9 100644 --- a/causaldmir/utils/independence/functional/fisherz.py +++ b/causaldmir/utils/independence/functional/fisherz.py @@ -1,6 +1,6 @@ from typing import Iterable -from numpy import (array, concatenate, corrcoef, ix_, log1p, ndarray, reshape, sqrt) +from numpy import array, concatenate, corrcoef, ix_, log1p, ndarray, reshape, sqrt from numpy.linalg import inv from scipy.stats import norm diff --git a/causaldmir/utils/independence/kernel_based.py b/causaldmir/utils/independence/kernel_based.py index 45db08b..eadd548 100644 --- a/causaldmir/utils/independence/kernel_based.py +++ b/causaldmir/utils/independence/kernel_based.py @@ -4,16 +4,17 @@ from typing import List, Tuple import numpy as np -from numpy import ndarray, isnan, trace, shape, sqrt +from numpy import isnan, ndarray, shape, sqrt, trace from numpy.linalg import eigh, eigvalsh from pandas import DataFrame -from scipy.stats import zscore, gamma +from scipy.stats import gamma, zscore from sklearn.gaussian_process import GaussianProcessRegressor -from sklearn.gaussian_process.kernels import RBF, WhiteKernel, ConstantKernel +from sklearn.gaussian_process.kernels import ConstantKernel, RBF, WhiteKernel -from ._base import BaseConditionalIndependenceTest from ..kernel import BaseKernel, GaussianKernel +from ._base import BaseConditionalIndependenceTest + class KCI(BaseConditionalIndependenceTest): """ diff --git a/causaldmir/utils/independence/tests/test_desp.py b/causaldmir/utils/independence/tests/test_desp.py index 873a7f1..e43fe63 100644 --- a/causaldmir/utils/independence/tests/test_desp.py +++ b/causaldmir/utils/independence/tests/test_desp.py @@ -1,9 +1,9 @@ import numpy as np -from causaldmir.utils.independence import Dsep + +from causaldmir.discovery.constraint.pc import PC from causaldmir.graph.digraph import DiGraph from causaldmir.graph.edge import Edge -from causaldmir.utils.independence import FisherZ -from causaldmir.discovery.constraint.pc import PC +from causaldmir.utils.independence import Dsep, FisherZ def test_desp(): np.random.seed(10) diff --git a/causaldmir/utils/kernel/test/test_gaussian.py b/causaldmir/utils/kernel/test/test_gaussian.py index 1eed0b1..7a3b598 100644 --- a/causaldmir/utils/kernel/test/test_gaussian.py +++ b/causaldmir/utils/kernel/test/test_gaussian.py @@ -1,6 +1,7 @@ -from causaldmir.utils.kernel.gaussian import GaussianKernel import numpy as np +from causaldmir.utils.kernel.gaussian import GaussianKernel + #arr1 = np.array([[1, 2, 3, 4], [1, 3, 4, 5]]) #arr2 = np.array([[1, 2, 3, 4], [3, 4, 5, 6]]) diff --git a/causaldmir/utils/kernel/test/test_linear.py b/causaldmir/utils/kernel/test/test_linear.py index 23eed52..6b80ee7 100644 --- a/causaldmir/utils/kernel/test/test_linear.py +++ b/causaldmir/utils/kernel/test/test_linear.py @@ -1,6 +1,7 @@ import numpy as np -from causaldmir.utils.kernel.linear import LinearKernel + from causaldmir.utils.kernel._base import BaseKernel +from causaldmir.utils.kernel.linear import LinearKernel def test_linear(): arr1=np.array([1,2,3]) diff --git a/causaldmir/utils/kernel/test/test_polynomial.py b/causaldmir/utils/kernel/test/test_polynomial.py index b0dfb9f..74f23f2 100644 --- a/causaldmir/utils/kernel/test/test_polynomial.py +++ b/causaldmir/utils/kernel/test/test_polynomial.py @@ -1,6 +1,7 @@ -from causaldmir.utils.kernel.polynomial import PolynomialKernel import numpy as np +from causaldmir.utils.kernel.polynomial import PolynomialKernel + def test_polynomial(): #多项式核函数 (const+x.dot(y.T))^2 arr1=np.array([[1,2,3]]) arr2=np.array([[1,1,1]]) diff --git a/causaldmir/utils/local_score/__init__.py b/causaldmir/utils/local_score/__init__.py index 1d046bd..4ea71fb 100644 --- a/causaldmir/utils/local_score/__init__.py +++ b/causaldmir/utils/local_score/__init__.py @@ -1,6 +1,6 @@ from ._base import BaseLocalScoreFunction -from .bic_score import BICScore from .bdeu_score import BDeuScore +from .bic_score import BICScore from .cross_validated_base import GeneralCVScore, MultiCVScore from .marginal_base import GeneralMarginalScore, MultiMarginalScore diff --git a/causaldmir/utils/local_score/bdeu_score.py b/causaldmir/utils/local_score/bdeu_score.py index 083a20d..e08625b 100644 --- a/causaldmir/utils/local_score/bdeu_score.py +++ b/causaldmir/utils/local_score/bdeu_score.py @@ -3,7 +3,7 @@ from math import lgamma from typing import Iterable -from numpy import ndarray, shape, log, unique, asarray +from numpy import asarray, log, ndarray, shape, unique from pandas import DataFrame from ._base import BaseLocalScoreFunction diff --git a/causaldmir/utils/local_score/bic_score.py b/causaldmir/utils/local_score/bic_score.py index f037d21..044fc49 100644 --- a/causaldmir/utils/local_score/bic_score.py +++ b/causaldmir/utils/local_score/bic_score.py @@ -2,7 +2,7 @@ from typing import Iterable -from numpy import ndarray, corrcoef, shape, log, mat, ix_ +from numpy import corrcoef, ix_, log, mat, ndarray, shape from numpy.linalg import inv from pandas import DataFrame diff --git a/causaldmir/utils/local_score/cross_validated_base.py b/causaldmir/utils/local_score/cross_validated_base.py index 0961be3..82e80b1 100644 --- a/causaldmir/utils/local_score/cross_validated_base.py +++ b/causaldmir/utils/local_score/cross_validated_base.py @@ -4,7 +4,7 @@ from typing import Iterable import numpy as np -from numpy import ndarray, shape, asmatrix +from numpy import asmatrix, ndarray, shape from numpy.linalg import inv from pandas import DataFrame diff --git a/causaldmir/utils/local_score/marginal_base.py b/causaldmir/utils/local_score/marginal_base.py index 00be494..6726105 100644 --- a/causaldmir/utils/local_score/marginal_base.py +++ b/causaldmir/utils/local_score/marginal_base.py @@ -4,12 +4,12 @@ from typing import Iterable import numpy as np -from numpy import ndarray, corrcoef, shape, log, mat, ix_, asmatrix +from numpy import asmatrix, corrcoef, ix_, log, mat, ndarray, shape from numpy.linalg import inv from pandas import DataFrame from ._base import BaseLocalScoreFunction -from .score_utils import kernel, eigdec, minimize, gpr_multi_new +from .score_utils import eigdec, gpr_multi_new, kernel, minimize class GeneralMarginalScore(BaseLocalScoreFunction): diff --git a/causaldmir/utils/local_score/score_utils.py b/causaldmir/utils/local_score/score_utils.py index 3ae0669..3b744b6 100644 --- a/causaldmir/utils/local_score/score_utils.py +++ b/causaldmir/utils/local_score/score_utils.py @@ -1,9 +1,10 @@ import sys import warnings -import scipy -import scipy.sparse + import numpy as np import numpy.linalg +import scipy +import scipy.sparse def kernel(x, xKern, theta): # KERNEL Compute the rbf kernel diff --git a/causaldmir/utils/metrics/__init__.py b/causaldmir/utils/metrics/__init__.py index 81903b0..9954677 100644 --- a/causaldmir/utils/metrics/__init__.py +++ b/causaldmir/utils/metrics/__init__.py @@ -1 +1,7 @@ -from .graph_evaluation import graph_equal, skeleton_evaluation, arrow_evaluation, directed_edge_evaluation, shd \ No newline at end of file +from .graph_evaluation import ( + arrow_evaluation, + directed_edge_evaluation, + graph_equal, + shd, + skeleton_evaluation, +) \ No newline at end of file diff --git a/causaldmir/utils/metrics/tests/test_graph_evaluation.py b/causaldmir/utils/metrics/tests/test_graph_evaluation.py index ada9703..2514204 100644 --- a/causaldmir/utils/metrics/tests/test_graph_evaluation.py +++ b/causaldmir/utils/metrics/tests/test_graph_evaluation.py @@ -2,8 +2,15 @@ import numpy as np -from causaldmir.utils.metrics import graph_equal, skeleton_evaluation, arrow_evaluation, directed_edge_evaluation, shd -from causaldmir.graph import Graph, PDAG, DiGraph, Edge, Mark +from causaldmir.graph import DiGraph, Edge, Graph, Mark, PDAG + +from causaldmir.utils.metrics import ( + arrow_evaluation, + directed_edge_evaluation, + graph_equal, + shd, + skeleton_evaluation, +) class TestGraphEvaluation(TestCase): diff --git a/causaldmir/visual/plot_graph.py b/causaldmir/visual/plot_graph.py index 5a3eed8..d20698b 100644 --- a/causaldmir/visual/plot_graph.py +++ b/causaldmir/visual/plot_graph.py @@ -1,6 +1,6 @@ import matplotlib.pyplot as plt from matplotlib import patches -from numpy import abs, cos, sin, sqrt, sign +from numpy import abs, cos, sign, sin, sqrt from causaldmir.graph import Mark diff --git a/causaldmir/visual/tests/test_plot_graph.py b/causaldmir/visual/tests/test_plot_graph.py index 1fa2391..f51a5d0 100644 --- a/causaldmir/visual/tests/test_plot_graph.py +++ b/causaldmir/visual/tests/test_plot_graph.py @@ -1,7 +1,9 @@ from unittest import TestCase -from ...graph import Graph, Edge, Mark -from .. import circular_layout, plot_graph +from causaldmir.visual.graph_layout import circular_layout +from causaldmir.visual.plot_graph import plot_graph + +from causaldmir.graph import Edge, Graph, Mark class TestPlotGraph(TestCase):