From f5cfd3fe4bad663f2f78831846142fda9e6dd1f0 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:15:21 +0200 Subject: [PATCH 01/15] Update pyproject.toml switch to poetry --- pyproject.toml | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3062b0f..bef7851 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,12 +1,30 @@ # pyproject.toml -[build-system] -requires = ["setuptools>=42", "wheel"] -build-backend = "setuptools.build_meta" - -[project] +[tool.poetry] name = "cpm_python" version = "0.1.0" +description = "" +authors = ["Nils Winter "] +license = "MIT" + +[tool.poetry.dependencies] +python = ">=3.8" +numpy = "*" +pandas = "*" +scikit-learn = "*" +pingouin = "*" +streamlit = "*" +nilearn = "*" +typer = "*" +pytest = "*" +pytest-cov = "*" +bleach = "^6.1.0" +tinycss2 = "^1.3.0" -[tool.setuptools.packages.find] -where = ["cpm"] +[tool.poetry.dev-dependencies] + +[tool.poetry.scripts] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" From 743b7b69ef6565e0ec7d48612379a98c8ed44ce1 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:16:17 +0200 Subject: [PATCH 02/15] Update test_edge_selection.py --- tests/test_edge_selection.py | 75 ++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/tests/test_edge_selection.py b/tests/test_edge_selection.py index 45cb136..35999fc 100644 --- a/tests/test_edge_selection.py +++ b/tests/test_edge_selection.py @@ -4,9 +4,9 @@ import pandas as pd import pingouin as pg -from scipy.stats import pearsonr, spearmanr +from scipy.stats import pearsonr, spearmanr, t -from cpm.simulate_data import simulate_regression_data +from cpm.simulate_data import simulate_data from cpm.edge_selection import (pearson_correlation_with_pvalues, spearman_correlation_with_pvalues, semi_partial_correlation_pearson, semi_partial_correlation_spearman) @@ -14,52 +14,59 @@ class TestEdgeStatistics(unittest.TestCase): def setUp(self): super(TestEdgeStatistics, self).setUp() - self.X, self.y, self.covariates = simulate_regression_data(n_samples=100, n_features=45) - - def _test_correlation(self, method, cpm_func, scipy_func): - """Generalized test for correlation with p-values""" - cpm_r, cpm_p = cpm_func(self.y, self.X) - scipy_r, scipy_p = [], [] - - for feature in range(self.X.shape[1]): - c = scipy_func(self.X[:, feature], self.y) - scipy_r.append(c.correlation if method == 'pearson' else c.statistic) - scipy_p.append(c.pvalue) - - np.testing.assert_almost_equal(np.array(scipy_r), cpm_r, decimal=10) - np.testing.assert_almost_equal(np.array(scipy_p), cpm_p, decimal=10) + self.X, self.y, self.covariates = simulate_data(n_samples=100, n_features=45) def test_cpm_pearson(self): - self._test_correlation('pearson', pearson_correlation_with_pvalues, pearsonr) + """Test CPM implementation of Pearson correlation with p-values""" + cpm_r, cpm_p = pearson_correlation_with_pvalues(self.y, self.X) + scipy_r = list() + scipy_p = list() + for feature in range(self.X.shape[1]): + c, p = pearsonr(self.X[:, feature], self.y) + scipy_r.append(c) + scipy_p.append(p) + scipy_r = np.array(scipy_r) + scipy_p = np.array(scipy_p) + np.testing.assert_almost_equal(scipy_r, cpm_r, decimal=10) + np.testing.assert_almost_equal(scipy_p, cpm_p, decimal=10) def test_cpm_spearman(self): - self._test_correlation('spearman', spearman_correlation_with_pvalues, spearmanr) + """Test CPM implementation of Spearman correlation with p-values""" + cpm_r, cpm_p = spearman_correlation_with_pvalues(self.y, self.X) + scipy_r = list() + scipy_p = list() + for feature in range(self.X.shape[1]): + c, p = spearmanr(self.X[:, feature], self.y) + scipy_r.append(c) + scipy_p.append(p) + scipy_r = np.array(scipy_r) + scipy_p = np.array(scipy_p) + np.testing.assert_almost_equal(scipy_r, cpm_r, decimal=10) + np.testing.assert_almost_equal(scipy_p, cpm_p, decimal=10) - def _test_semi_partial_correlation(self, method, func): + def test_semi_partial_correlation_pearson(self): # Calculate partial correlation using the provided function - partial_corr, p_values = func(self.y, self.X, self.covariates) + partial_corr, p_values = semi_partial_correlation_pearson(self.y, self.X, self.covariates) - # Prepare DataFrame + # Calculate partial correlation using pingouin df = pd.DataFrame(np.column_stack([self.y, self.X, self.covariates]), - columns=["y"] + [f"x{i}" for i in range(self.X.shape[1])] + [f"cov{i}" for i in - range(self.covariates.shape[1])]) - pcorr_pingouin, pval_pingouin = [], [] - + columns=["y"] + [f"x{i}" for i in range(self.X.shape[1])] + [f"cov{i}" for i in range(self.covariates.shape[1])]) + pcorr_pingouin = [] + pval_pingouin = [] for i in range(self.X.shape[1]): - result = pg.partial_corr(data=df, x="y", y=f"x{i}", - covar=[f"cov{j}" for j in range(self.covariates.shape[1])], - method=method) + result = pg.partial_corr(data=df, x="y", y=f"x{i}", covar=[f"cov{j}" for j in range(self.covariates.shape[1])], method='pearson') pcorr_pingouin.append(result['r'].values[0]) pval_pingouin.append(result['p-val'].values[0]) - np.testing.assert_almost_equal(partial_corr, np.array(pcorr_pingouin), decimal=10) - np.testing.assert_almost_equal(p_values, np.array(pval_pingouin), decimal=10) + # Convert to numpy arrays for easier comparison + pcorr_pingouin = np.array(pcorr_pingouin) + pval_pingouin = np.array(pval_pingouin) - def test_semi_partial_correlation_pearson(self): - self._test_semi_partial_correlation('pearson', semi_partial_correlation_pearson) + # Assert that the partial correlation results are almost equal between the two methods + np.testing.assert_almost_equal(partial_corr, pcorr_pingouin, decimal=10) - def test_semi_partial_correlation_spearman(self): - self._test_semi_partial_correlation('spearman', semi_partial_correlation_spearman) + # Assert that the p-values results are almost equal between the two methods + np.testing.assert_almost_equal(p_values, pval_pingouin, decimal=10) if __name__ == '__main__': From 8774e7e2a821354e96686213f78c9e77ba7e1c5b Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:23:38 +0200 Subject: [PATCH 03/15] Update python-test.yml --- .github/workflows/python-test.yml | 33 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index cad34ac..a344736 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -1,7 +1,7 @@ -# This workflow will install Python dependencies, run tests and lint with a single version of Python +# This workflow will install Python dependencies, run tests, and build distribution artifacts with Poetry # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: CPM test and test deploy +name: CPM test and build on: push: @@ -18,17 +18,24 @@ jobs: steps: - uses: actions/checkout@v3 + name: Checkout code - name: Set up Python 3.11 uses: actions/setup-python@v4 with: python-version: 3.11 + - name: Install Poetry + run: | + curl -sSL https://install.python-poetry.org | python3 - + - name: Configure Poetry + run: | + poetry config virtualenvs.create false - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install scikit-learn pandas numpy pingouin typer pytest pytest-cov + poetry install - name: Test with pytest run: | - PYTHONPATH=./ pytest ./tests --cov=./cpm_python --full-trace + poetry run pytest ./tests --cov=./cpm_python --full-trace + build: name: Build distribution runs-on: ubuntu-latest @@ -42,9 +49,17 @@ jobs: uses: actions/setup-python@v4 with: python-version: 3.11 - - name: Install pypa/build - run: pip install build pbr wheel - - name: Build a binary wheel and a source tarball - run: python -m build -n --sdist --wheel --outdir dist/ + - name: Install Poetry + run: | + curl -sSL https://install.python-poetry.org | python3 - + - name: Configure Poetry + run: | + poetry config virtualenvs.create false # Use the system environment + - name: Install dependencies + run: | + poetry install + - name: Build distribution + run: | + poetry build --no-interaction - name: List built artifacts run: ls -al dist/ From 193eb64a87e8d929e701bddc3d23cad29f3dd628 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:27:44 +0200 Subject: [PATCH 04/15] Update pyproject.toml --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index bef7851..f660802 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,9 @@ tinycss2 = "^1.3.0" [tool.poetry.dev-dependencies] +[tool.poetry.packages] +include = ["cpm", "cpm.*"] + [tool.poetry.scripts] [build-system] From dc0d03209e4bc50fcd72bfa94b35646153a3b639 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:29:26 +0200 Subject: [PATCH 05/15] Update pyproject.toml --- pyproject.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f660802..169f249 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,9 @@ tinycss2 = "^1.3.0" [tool.poetry.dev-dependencies] [tool.poetry.packages] -include = ["cpm", "cpm.*"] +packages = [ + { include = "cpm" } +] [tool.poetry.scripts] From 41e79fe8e3666eb1c20ef124e4a35da7c5315dee Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 22 Aug 2024 15:41:04 +0200 Subject: [PATCH 06/15] Update python-test.yml --- .github/workflows/python-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index a344736..79de160 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -34,6 +34,7 @@ jobs: poetry install - name: Test with pytest run: | + export PYTHONPATH=$(pwd) poetry run pytest ./tests --cov=./cpm_python --full-trace build: From a5b8ca90012e830f7dbc147ab313a55093e19c2f Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:12:40 +0200 Subject: [PATCH 07/15] Update pyproject.toml --- pyproject.toml | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 169f249..8e2dada 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,14 +1,17 @@ -# pyproject.toml +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" [tool.poetry] name = "cpm_python" version = "0.1.0" description = "" -authors = ["Nils Winter "] -license = "MIT" +authors = [""] +readme = "README.md" +packages = [{ include = "cpm" }] [tool.poetry.dependencies] -python = ">=3.8" +python = ">=3.6" numpy = "*" pandas = "*" scikit-learn = "*" @@ -18,18 +21,3 @@ nilearn = "*" typer = "*" pytest = "*" pytest-cov = "*" -bleach = "^6.1.0" -tinycss2 = "^1.3.0" - -[tool.poetry.dev-dependencies] - -[tool.poetry.packages] -packages = [ - { include = "cpm" } -] - -[tool.poetry.scripts] - -[build-system] -requires = ["poetry-core>=1.0.0"] -build-backend = "poetry.core.masonry.api" From 128f7b99c585925755b07fcc74310d6006a0d833 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:18:04 +0200 Subject: [PATCH 08/15] Update pyproject.toml --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 8e2dada..cacb260 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,8 @@ packages = [{ include = "cpm" }] [tool.poetry.dependencies] python = ">=3.6" +bleach = ">=5.0.0,<5.0.1" +tinycss2 = ">=1.1.0,<1.2.0" numpy = "*" pandas = "*" scikit-learn = "*" From 1f251e84268b09aa9fa2ad7e03b6dd1a14f06de1 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:19:01 +0200 Subject: [PATCH 09/15] Update python-test.yml --- .github/workflows/python-test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 79de160..3e3abce 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -31,7 +31,8 @@ jobs: poetry config virtualenvs.create false - name: Install dependencies run: | - poetry install + pip install --upgrade poetry + poetry install --verbose - name: Test with pytest run: | export PYTHONPATH=$(pwd) From 22a3e076f99a4a1198c7604d117751e1962eff8b Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:21:44 +0200 Subject: [PATCH 10/15] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cacb260..b8cd9f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ readme = "README.md" packages = [{ include = "cpm" }] [tool.poetry.dependencies] -python = ">=3.6" +python = ">=3.11" bleach = ">=5.0.0,<5.0.1" tinycss2 = ">=1.1.0,<1.2.0" numpy = "*" From 93206b5a0698e67dd5ca9e9df77bc959789991ac Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:23:50 +0200 Subject: [PATCH 11/15] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b8cd9f3..03da5a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api" name = "cpm_python" version = "0.1.0" description = "" -authors = [""] +authors = ["Nils Winter "] readme = "README.md" packages = [{ include = "cpm" }] From 33f913f9da12372e9fb24d0cdbf7ec2d16aa44cf Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:31:19 +0200 Subject: [PATCH 12/15] Update test_edge_selection.py --- tests/test_edge_selection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_edge_selection.py b/tests/test_edge_selection.py index 35999fc..d1a292b 100644 --- a/tests/test_edge_selection.py +++ b/tests/test_edge_selection.py @@ -6,7 +6,7 @@ from scipy.stats import pearsonr, spearmanr, t -from cpm.simulate_data import simulate_data +from cpm.simulate_data import simulate_regression_data from cpm.edge_selection import (pearson_correlation_with_pvalues, spearman_correlation_with_pvalues, semi_partial_correlation_pearson, semi_partial_correlation_spearman) @@ -14,7 +14,7 @@ class TestEdgeStatistics(unittest.TestCase): def setUp(self): super(TestEdgeStatistics, self).setUp() - self.X, self.y, self.covariates = simulate_data(n_samples=100, n_features=45) + self.X, self.y, self.covariates = simulate_regression_data(n_samples=100, n_features=45) def test_cpm_pearson(self): """Test CPM implementation of Pearson correlation with p-values""" From 903cd62d3a50fc690b2e2f0a3328c51db532389c Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:33:04 +0200 Subject: [PATCH 13/15] Delete setup.cfg --- setup.cfg | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index dce84a8..0000000 --- a/setup.cfg +++ /dev/null @@ -1,30 +0,0 @@ -# setup.cfg -[metadata] -name = Connectome-based Predictive Modeling Python Package -version = 0.1.0 -author = MMLL -author_email = nils.r.winter@uni-muenster.de -description = An example Python package -long_description = file: README.md -long_description_content_type = text/markdown -classifiers = - Programming Language :: Python :: 3 - License :: OSI Approved :: MIT License - Operating System :: OS Independent - -[options] -package_dir = - = cpm -packages = find: -python_requires = >=3.6 -install_requires = - numpy - pandas - scikit-learn - pingouin - streamlit - nilearn - typer - -[options.packages.find] -where = src \ No newline at end of file From 61a14f4eb9bbb715dd661e49ca5d74b055a1cc2b Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:34:57 +0200 Subject: [PATCH 14/15] Update pyproject.toml --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 03da5a5..cfb9f39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,8 +12,8 @@ packages = [{ include = "cpm" }] [tool.poetry.dependencies] python = ">=3.11" -bleach = ">=5.0.0,<5.0.1" -tinycss2 = ">=1.1.0,<1.2.0" +bleach = "*" +tinycss2 = "*" numpy = "*" pandas = "*" scikit-learn = "*" From 2b2b451f22760d541203e2370280f3688053ce31 Mon Sep 17 00:00:00 2001 From: juri-debug Date: Thu, 29 Aug 2024 15:38:51 +0200 Subject: [PATCH 15/15] implement poetry to prevent redundant dependencies --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index cfb9f39..5c8d99d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,4 @@ +# pyproject.toml [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"