Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Tests

on: [push]
on:
push:
pull_request:

jobs:
build:
Expand All @@ -11,22 +13,69 @@ jobs:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 lingam --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 lingam --count --exit-zero --ignore=E203,E501,E741,C901 --max-line-length=127 --statistics

- name: Test with pytest
run: |
pytest -v --cov=lingam --cov-report=term-missing

build-and-install:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Build sdist & wheel via pyproject.toml
run: |
python -m venv venv_b
source venv_b/bin/activate
python -m pip install --upgrade pip setuptools wheel build
python -m build
ls -la dist/
echo "List sdist contents:"
tar -tf dist/*.tar.gz | head -n 50

- name: Test install from wheel
run: |
python -m venv venv_w
source venv_w/bin/activate
pip install --no-cache-dir dist/*.whl
python -c "import lingam; print(lingam.__version__)"
pip check

- name: Test install from sdist
run: |
python -m venv venv_s
source venv_s/bin/activate
pip install --no-cache-dir dist/*.tar.gz
python -c "import lingam; print(lingam.__version__)"
pip check
2 changes: 1 addition & 1 deletion lingam/causal_based_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class CBSIUnobsCommonCauseLiNGAM(CBSILiNGAM):
def _check_causal_graph(self, causal_graph, X):
try:
# fill nan with zeros
causal_graph = check_array(causal_graph, force_all_finite="allow-nan", copy=True)
causal_graph = check_array(causal_graph, ensure_all_finite="allow-nan", copy=True)

n_features = X.shape[1]
if causal_graph.shape != (n_features, n_features):
Expand Down
9 changes: 8 additions & 1 deletion lingam/direct_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,20 @@ def _search_causal_order_gpu(self, X, U):
Returns the instance itself.
mlist: causal ordering
"""
Uc, _ = self._search_candidate(U)
if len(Uc) == 1:
return Uc[0]

cols = len(U)
rows = len(X)

arr = X[:, np.array(U)]
from lingam_cuda import causal_order as causal_order_gpu
mlist = causal_order_gpu(arr, rows, cols)
return U[np.argmax(mlist)]
M_list = np.asarray(mlist)
M_list = M_list[np.isin(U, Uc)]

return Uc[int(np.argmax(M_list))]

def _mutual_information(self, x1, x2, param):
"""Calculate the mutual informations."""
Expand Down
2 changes: 1 addition & 1 deletion lingam/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def bootstrap_with_imputation(
Elements which are not NaN are the imputation values.
"""
# check args
X = check_array(X, force_all_finite="allow-nan")
X = check_array(X, ensure_all_finite="allow-nan")

n_sampling = check_scalar(n_sampling, "n_sampling", (numbers.Integral, np.integer), min_val=1)

Expand Down
6 changes: 3 additions & 3 deletions lingam/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ def evaluate_model_fit(adjacency_matrix, X, is_ordinal=None):
"""

# check inputs
adj = check_array(adjacency_matrix, force_all_finite="allow-nan")
adj = check_array(adjacency_matrix, ensure_all_finite="allow-nan")
if adj.ndim != 2 or (adj.shape[0] != adj.shape[1]):
raise ValueError("adj must be an square matrix.")

Expand Down Expand Up @@ -1037,7 +1037,7 @@ def calculate_distance_from_root_nodes(adjacency_matrix, max_distance=None):
adjacency_matrix,
ensure_min_samples=2,
ensure_min_features=2,
force_all_finite="allow-nan",
ensure_all_finite="allow-nan",
)
if adjacency_matrix.shape[0] != adjacency_matrix.shape[1]:
raise ValueError("adjacency_matrix must be an square matrix.")
Expand Down Expand Up @@ -1103,7 +1103,7 @@ def calculate_total_effect(adjacency_matrix, from_index, to_index, is_continuous
adjacency_matrix,
ensure_min_samples=2,
ensure_min_features=2,
force_all_finite="allow-nan",
ensure_all_finite="allow-nan",
)
if adjacency_matrix.shape[0] != adjacency_matrix.shape[1]:
raise ValueError(
Expand Down
49 changes: 49 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[build-system]
requires = ["setuptools>=61", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "lingam"
description = "Python package for causal discovery based on LiNGAM."
readme = "README.md"
authors = [
{ name = "Takashi Ikeuchi" },
{ name = "Genya Haraoka" },
{ name = "Mayumi Ide" },
{ name = "Kouhei Nishikawa" },
{ name = "Yan Zeng" },
{ name = "Takashi Nicholas Maeda" },
{ name = "Wataru Kurebayashi" },
{ name = "Shohei Shimizu" }
]
license = { file = "LICENSE" }
requires-python = ">=3.9"
dependencies = [
"numpy",
"scipy",
"scikit-learn",
"graphviz",
"statsmodels",
"networkx",
"pandas",
"pygam",
"matplotlib",
"psy",
"semopy",
"autograd"
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
]
dynamic = ["version"]

[project.urls]
Homepage = "https://github.com/cdt15/lingam"

[tool.setuptools.packages.find]
exclude = ["tests"]

[tool.setuptools.dynamic]
version = { attr = "lingam.__version__" }
39 changes: 0 additions & 39 deletions setup.py

This file was deleted.

5 changes: 2 additions & 3 deletions tests/test_bottom_up_parce_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ def test_fit_invalid_data():
raise AssertionError

# Include non-numeric data
x0 = np.random.uniform(size=5)
x1 = np.array(['X', 'Y', 'X', 'Y', 'X'])
X = pd.DataFrame(np.array([x0, x1]).T, columns=['x0', 'x1'])
X = np.array([[1.2, 'X'],
[3.4, 'Y']], dtype=object)
try:
model = BottomUpParceLiNGAM()
model.fit(X)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_direct_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def test_fit_invalid_data():
raise AssertionError

# Include non-numeric data
x0 = np.random.uniform(size=5)
x1 = np.array(['X', 'Y', 'X', 'Y', 'X'])
X = pd.DataFrame(np.array([x0, x1]).T, columns=['x0', 'x1'])
X = np.array([[1.2, 'X'],
[3.4, 'Y']], dtype=object)
try:
model = DirectLiNGAM()
model.fit(X)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_high_dim_direct_lingam.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ def test_fit_invalid_data():
raise AssertionError

# Include non-numeric data
x0 = np.random.uniform(size=5)
x1 = np.array(['X', 'Y', 'X', 'Y', 'X'])
X = pd.DataFrame(np.array([x0, x1]).T, columns=['x0', 'x1'])
X = np.array([[1.2, 'X'],
[3.4, 'Y']], dtype=object)
try:
model = HighDimDirectLiNGAM()
model.fit(X)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_rcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ def test_fit_invalid_data():
raise AssertionError

# Include non-numeric data
x0 = np.random.uniform(size=5)
x1 = np.array(['X', 'Y', 'X', 'Y', 'X'])
X = pd.DataFrame(np.array([x0, x1]).T, columns=['x0', 'x1'])
X = np.array([[1.2, 'X'],
[3.4, 'Y']], dtype=object)
try:
model = RCD()
model.fit(X)
Expand Down