From 94dd2d8ca4a0beb8a975fecbfab20b6d0c54df56 Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 19:13:35 +0100 Subject: [PATCH 1/8] uprev to actions/checkout@v4 --- .github/workflows/python-app.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-app.yaml b/.github/workflows/python-app.yaml index 8e26dfa..2187438 100644 --- a/.github/workflows/python-app.yaml +++ b/.github/workflows/python-app.yaml @@ -15,9 +15,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Python 3.9 - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.9 - name: Install dependencies From 2f9df91e5dbaff071eb809684bc7556da96f73ef Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 19:35:49 +0100 Subject: [PATCH 2/8] Added venv to gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cedf0d2..bfcc5c4 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,6 @@ data/tic-tac-toe.xlsx cegpy-binder docs/_build/* .mypy -build \ No newline at end of file +build +.venv +venv \ No newline at end of file From d716a6c572a31d1292643f053d03a061f7160d14 Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 19:46:45 +0100 Subject: [PATCH 3/8] Added networkx to requirements.txt --- .github/workflows/python-app.yaml | 2 +- requirements.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yaml b/.github/workflows/python-app.yaml index 2187438..3cd6f84 100644 --- a/.github/workflows/python-app.yaml +++ b/.github/workflows/python-app.yaml @@ -24,7 +24,7 @@ jobs: run: | python -m pip install --upgrade pip pip install wheel - pip install flake8 pytest pandas pydotplus IPython colorutils networkx + pip install flake8 pytest pandas pydotplus IPython colorutils if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with pytest run: | diff --git a/requirements.txt b/requirements.txt index c0cd6b1..d9ab9b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,4 @@ scipy>=1.7.0 colorutils>=0.3.0 xlsxwriter>=3.0.0 tox>=4.2.8 +networkx>=3.3 From 05d770867238832fdfe500618bffcbedeb8b4787 Mon Sep 17 00:00:00 2001 From: Aditi Shenvi Date: Wed, 28 Aug 2024 19:46:40 +0100 Subject: [PATCH 4/8] trying to fix a test --- src/tests/test_staged.py | 58 +++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/tests/test_staged.py b/src/tests/test_staged.py index 9ef8513..d4ccb54 100644 --- a/src/tests/test_staged.py +++ b/src/tests/test_staged.py @@ -16,37 +16,51 @@ class TestLogging: """Tests logging in stagedtree""" - def setup(self): - """Test setup""" - # pylint: disable=attribute-defined-outside-init - # stratified dataset + @pytest.fixture + def med_st(self): med_df_path = ( Path(__file__) .resolve() .parent.parent.joinpath("../data/medical_dm_modified.xlsx") ) - self.med_s_z_paths = None - self.med_df = pd.read_excel(med_df_path) - self.med_st = StagedTree( - dataframe=self.med_df, sampling_zero_paths=self.med_s_z_paths + med_s_z_paths = None + med_df = pd.read_excel(med_df_path) + med_st = StagedTree( + dataframe=med_df, sampling_zero_paths=med_s_z_paths ) - - # non-stratified dataset - fall_df_path = ( - Path(__file__).resolve().parent.parent.joinpath("../data/Falls_Data.xlsx") - ) - self.fall_s_z_paths = None - self.fall_df = pd.read_excel(fall_df_path) - self.fall_st = StagedTree( - dataframe=self.fall_df, - sampling_zero_paths=self.fall_s_z_paths, - ) - - def test_run_ahc_before_figure(self) -> None: + return med_st + + # def setup(self): + # """Test setup""" + # # pylint: disable=attribute-defined-outside-init + # # stratified dataset + # med_df_path = ( + # Path(__file__) + # .resolve() + # .parent.parent.joinpath("../data/medical_dm_modified.xlsx") + # ) + # self.med_s_z_paths = None + # self.med_df = pd.read_excel(med_df_path) + # self.med_st = StagedTree( + # dataframe=self.med_df, sampling_zero_paths=self.med_s_z_paths + # ) + + # # non-stratified dataset + # fall_df_path = ( + # Path(__file__).resolve().parent.parent.joinpath("../data/Falls_Data.xlsx") + # ) + # self.fall_s_z_paths = None + # self.fall_df = pd.read_excel(fall_df_path) + # self.fall_st = StagedTree( + # dataframe=self.fall_df, + # sampling_zero_paths=self.fall_s_z_paths, + # ) + + def test_run_ahc_before_figure(self, med_st) -> None: """Tests expected error message is in the log when running without running AHC""" try: - self.med_st.create_figure() + med_st.create_figure() except (InvocationException): pass From 4037a8f77699b2893355d8b278ca763910e43051 Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 19:55:22 +0100 Subject: [PATCH 5/8] TP: Removed dead code --- src/tests/test_staged.py | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/tests/test_staged.py b/src/tests/test_staged.py index d4ccb54..51f16e3 100644 --- a/src/tests/test_staged.py +++ b/src/tests/test_staged.py @@ -30,32 +30,6 @@ def med_st(self): ) return med_st - # def setup(self): - # """Test setup""" - # # pylint: disable=attribute-defined-outside-init - # # stratified dataset - # med_df_path = ( - # Path(__file__) - # .resolve() - # .parent.parent.joinpath("../data/medical_dm_modified.xlsx") - # ) - # self.med_s_z_paths = None - # self.med_df = pd.read_excel(med_df_path) - # self.med_st = StagedTree( - # dataframe=self.med_df, sampling_zero_paths=self.med_s_z_paths - # ) - - # # non-stratified dataset - # fall_df_path = ( - # Path(__file__).resolve().parent.parent.joinpath("../data/Falls_Data.xlsx") - # ) - # self.fall_s_z_paths = None - # self.fall_df = pd.read_excel(fall_df_path) - # self.fall_st = StagedTree( - # dataframe=self.fall_df, - # sampling_zero_paths=self.fall_s_z_paths, - # ) - def test_run_ahc_before_figure(self, med_st) -> None: """Tests expected error message is in the log when running without running AHC""" From cc63212fa88bacf52fd74becccb7491916977ec1 Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 20:04:59 +0100 Subject: [PATCH 6/8] TP: TestChangingDataFrame --- src/tests/test_event.py | 69 ++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/tests/test_event.py b/src/tests/test_event.py index bba4c8d..8cfab38 100644 --- a/src/tests/test_event.py +++ b/src/tests/test_event.py @@ -232,84 +232,97 @@ def test_fall_cats_per_var(self): class TestChangingDataFrame: - def setup(self): + + @pytest.fixture + def med_df(self): # stratified dataset med_df_path = ( Path(__file__) .resolve() .parent.parent.joinpath("../data/medical_dm_modified.xlsx") ) - self.med_s_z_paths = None - self.med_df = pd.read_excel(med_df_path) - self.med_et = EventTree( - dataframe=self.med_df, sampling_zero_paths=self.med_s_z_paths + + return pd.read_excel(med_df_path) + + @pytest.fixture + def med_et(self, med_df): + med_s_z_paths = None + med_et = EventTree( + dataframe=med_df, sampling_zero_paths=med_s_z_paths ) + return med_et + @pytest.fixture + def fall_df(self): # non-stratified dataset fall_df_path = ( Path(__file__).resolve().parent.parent.joinpath("../data/Falls_Data.xlsx") ) + return pd.read_excel(fall_df_path) + + @pytest.fixture + def fall_et(self, fall_df): self.fall_s_z_paths = None - self.fall_df = pd.read_excel(fall_df_path) - self.fall_et = EventTree( - dataframe=self.fall_df, sampling_zero_paths=self.fall_s_z_paths + return EventTree( + dataframe=fall_df, sampling_zero_paths=self.fall_s_z_paths ) - def test_add_empty_column(self) -> None: + + def test_add_empty_column(self, fall_et, med_df, med_et, fall_df) -> None: # adding empty column - med_empty_column_df = self.med_df + med_empty_column_df = med_df med_empty_column_df["extra"] = "" med_empty_column_et = EventTree(dataframe=med_empty_column_df) - assert med_empty_column_et.adj == self.med_et.adj + assert med_empty_column_et.adj == med_et.adj - fall_empty_column_df = self.fall_df + fall_empty_column_df = fall_df fall_empty_column_df["extra"] = "" fall_empty_column_et = EventTree(dataframe=fall_empty_column_df) - assert fall_empty_column_et.adj == self.fall_et.adj + assert fall_empty_column_et.adj == fall_et.adj - def test_add_NA_column(self) -> None: + def test_add_NA_column(self, fall_et, med_df, med_et, fall_df) -> None: # adding NA column - med_add_NA_df = self.med_df + med_add_NA_df = med_df med_add_NA_df["extra"] = np.nan med_add_NA_et = EventTree(dataframe=med_add_NA_df) - assert med_add_NA_et.adj == self.med_et.adj + assert med_add_NA_et.adj == med_et.adj - fall_add_NA_df = self.fall_df + fall_add_NA_df = fall_df fall_add_NA_df["extra"] = np.nan fall_add_NA_et = EventTree(dataframe=fall_add_NA_df) - assert fall_add_NA_et.adj == self.fall_et.adj + assert fall_add_NA_et.adj == fall_et.adj - def test_add_same_column(self) -> None: + def test_add_same_column(self, fall_et, med_df, med_et, fall_df) -> None: # adding column with no more information - med_add_same_df = self.med_df + med_add_same_df = med_df med_add_same_df["extra"] = "same for all" med_add_same_et = EventTree(dataframe=med_add_same_df) - assert len(med_add_same_et.leaves) == len(self.med_et.leaves) + assert len(med_add_same_et.leaves) == len(med_et.leaves) - fall_add_same_df = self.fall_df + fall_add_same_df = fall_df fall_add_same_df["extra"] = "same for all" fall_add_same_et = EventTree(dataframe=fall_add_same_df) - assert len(fall_add_same_et.leaves) == len(self.fall_et.leaves) + assert len(fall_add_same_et.leaves) == len(fall_et.leaves) - def test_add_same_column_int(self) -> None: + def test_add_same_column_int(self, fall_et, med_df, med_et, fall_df) -> None: # adding column with no more information - med_add_same_df = self.med_df + med_add_same_df = med_df med_add_same_df["extra"] = 1 med_add_same_et = EventTree(dataframe=med_add_same_df) try: med_add_same_et.create_figure("et_fig_path.pdf") except InvocationException: pass - assert len(med_add_same_et.leaves) == len(self.med_et.leaves) + assert len(med_add_same_et.leaves) == len(med_et.leaves) - fall_add_same_df = self.fall_df + fall_add_same_df = fall_df fall_add_same_df["extra"] = 1 fall_add_same_et = EventTree(dataframe=fall_add_same_df) try: fall_add_same_et.create_figure("et_fig_path.pdf") except InvocationException: pass - assert len(fall_add_same_et.leaves) == len(self.fall_et.leaves) + assert len(fall_add_same_et.leaves) == len(fall_et.leaves) class TestMissingLabels: From 9a361ea2eda7d683b4820cdb3596b03db7e37559 Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 20:22:35 +0100 Subject: [PATCH 7/8] Fixed tests classes to use unittest --- src/tests/test_ceg.py | 16 +++++++-------- src/tests/test_ceg_reducer.py | 4 ++-- src/tests/test_event.py | 38 +++++++++++++++-------------------- src/tests/test_staged.py | 4 +--- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/tests/test_ceg.py b/src/tests/test_ceg.py index a7c7711..ab24f62 100644 --- a/src/tests/test_ceg.py +++ b/src/tests/test_ceg.py @@ -2,13 +2,11 @@ # pylint: disable=protected-access import re from pathlib import Path -from typing import Dict, List, Mapping, Tuple +from typing import Dict, Mapping import unittest from unittest.mock import Mock, patch import networkx as nx import pandas as pd -import pytest -import pytest_mock from cegpy import StagedTree, ChainEventGraph from cegpy.graphs._ceg import ( CegAlreadyGenerated, @@ -16,14 +14,14 @@ ) -class TestMockedCEGMethods: +class TestMockedCEGMethods(unittest.TestCase): """Tests that Mock functions in ChainEventGraph""" node_prefix = "w" sink_suffix = "∞" staged: StagedTree - def setup(self): + def setUp(self): """Test setup""" df_path = ( Path(__file__) @@ -34,12 +32,12 @@ def setup(self): self.staged = StagedTree(dataframe=pd.read_excel(df_path)) self.staged.calculate_AHC_transitions() - def test_generate_argument(self, mocker: pytest_mock.MockerFixture): + @patch("cegpy.graphs._ceg.ChainEventGraph.generate", autospec=True) + def test_generate_argument(self, generate_mock: Mock): """When ChainEventGraph called with generate, the .generate() method is called.""" - mocker.patch("cegpy.graphs._ceg.ChainEventGraph.generate") - ceg = ChainEventGraph(self.staged, generate=True) - ceg.generate.assert_called_once() # pylint: disable=no-member + ChainEventGraph(self.staged, generate=True) + generate_mock.assert_called_once() # pylint: disable=no-member class TestUnitCEG(unittest.TestCase): diff --git a/src/tests/test_ceg_reducer.py b/src/tests/test_ceg_reducer.py index af4ac3b..82cb795 100644 --- a/src/tests/test_ceg_reducer.py +++ b/src/tests/test_ceg_reducer.py @@ -443,8 +443,8 @@ def test_str_out(self): assert str(node) in str_rep -class TestReducedCEGTwo(object): - def setup(self): +class TestReducedCEGTwo(unittest.TestCase): + def setUp(self): G = nx.MultiDiGraph() self.init_nodes = ["w0", "w1", "w2", "w3", "w4", "w5", "w6", "w_infinity"] self.init_edges = [ diff --git a/src/tests/test_event.py b/src/tests/test_event.py index 8cfab38..a52df58 100644 --- a/src/tests/test_event.py +++ b/src/tests/test_event.py @@ -38,8 +38,8 @@ def test_incorrect_sampling_zero_fails(self): ) -class TestEventTree: - def setup(self): +class TestEventTree(unittest.TestCase): + def setUp(self): df_path = ( Path(__file__) .resolve() @@ -148,8 +148,8 @@ def test_node_colours(self) -> None: assert event_node_colours[0] == "lightgrey" -class TestIntegration: - def setup(self): +class TestIntegration(unittest.TestCase): + def setUp(self): # stratified dataset med_df_path = ( Path(__file__) @@ -198,8 +198,8 @@ def check_list_contains_strings(str_list) -> bool: assert isinstance(elem, str) -class TestUsecase: - def setup(self): +class TestUsecase(unittest.TestCase): + def setUp(self): # stratified dataset med_df_path = ( Path(__file__) @@ -232,7 +232,6 @@ def test_fall_cats_per_var(self): class TestChangingDataFrame: - @pytest.fixture def med_df(self): # stratified dataset @@ -247,9 +246,7 @@ def med_df(self): @pytest.fixture def med_et(self, med_df): med_s_z_paths = None - med_et = EventTree( - dataframe=med_df, sampling_zero_paths=med_s_z_paths - ) + med_et = EventTree(dataframe=med_df, sampling_zero_paths=med_s_z_paths) return med_et @pytest.fixture @@ -263,10 +260,7 @@ def fall_df(self): @pytest.fixture def fall_et(self, fall_df): self.fall_s_z_paths = None - return EventTree( - dataframe=fall_df, sampling_zero_paths=self.fall_s_z_paths - ) - + return EventTree(dataframe=fall_df, sampling_zero_paths=self.fall_s_z_paths) def test_add_empty_column(self, fall_et, med_df, med_et, fall_df) -> None: # adding empty column @@ -325,8 +319,8 @@ def test_add_same_column_int(self, fall_et, med_df, med_et, fall_df) -> None: assert len(fall_add_same_et.leaves) == len(fall_et.leaves) -class TestMissingLabels: - def setup(self): +class TestMissingLabels(unittest.TestCase): + def setUp(self): array = [ np.array(["1", "NotANum", "Recover"]), np.array(["1", "Trt1", "NotANum"]), @@ -538,8 +532,8 @@ def test_complete_case_reduction(self) -> None: assert df_et.dataframe.equals(expected_df) is True -class TestVariablesFiltered: - def setup(self): +class TestVariablesFiltered(unittest.TestCase): + def setUp(self): array = [ np.array(["1", "NotANum", "Recover"]), np.array(["1", "Trt1", "NotANum"]), @@ -548,7 +542,7 @@ def setup(self): np.array(["1", "Trt1", "Recover"]), np.array(["1", "Trt2", "Recover"]), np.array(["1", "Trt2", "Dont Recover"]), - np.array(["1", np.NaN, "Dont Recover"]), + np.array(["1", np.nan, "Dont Recover"]), ] self.df = pd.DataFrame(array) @@ -575,8 +569,8 @@ def test_pd_nans_filtered_with_missing(self) -> None: assert df_et.categories_per_variable == expected_categories -class TestStageColours: - def setup(self): +class TestStageColours(unittest.TestCase): + def setUp(self): array = [ np.array(["1", "NotANum", "Recover"]), np.array(["1", "Trt1", "NotANum"]), @@ -585,7 +579,7 @@ def setup(self): np.array(["1", "Trt1", "Recover"]), np.array(["1", "Trt2", "Recover"]), np.array(["1", "Trt2", "Dont Recover"]), - np.array(["1", np.NaN, "Dont Recover"]), + np.array(["1", np.nan, "Dont Recover"]), ] self.df = pd.DataFrame(array) diff --git a/src/tests/test_staged.py b/src/tests/test_staged.py index 51f16e3..e9b81a9 100644 --- a/src/tests/test_staged.py +++ b/src/tests/test_staged.py @@ -25,9 +25,7 @@ def med_st(self): ) med_s_z_paths = None med_df = pd.read_excel(med_df_path) - med_st = StagedTree( - dataframe=med_df, sampling_zero_paths=med_s_z_paths - ) + med_st = StagedTree(dataframe=med_df, sampling_zero_paths=med_s_z_paths) return med_st def test_run_ahc_before_figure(self, med_st) -> None: From 2268b950789380f9b1dd9446a59235e93aeebd4c Mon Sep 17 00:00:00 2001 From: Gareth Walley Date: Wed, 28 Aug 2024 20:28:13 +0100 Subject: [PATCH 8/8] Fixed networkx 3.2.1 --- requirements.txt | 2 +- setup.cfg | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index d9ab9b6..95d007d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,4 @@ scipy>=1.7.0 colorutils>=0.3.0 xlsxwriter>=3.0.0 tox>=4.2.8 -networkx>=3.3 +networkx>=3.2.1 diff --git a/setup.cfg b/setup.cfg index 7bf4a7a..e8f3242 100644 --- a/setup.cfg +++ b/setup.cfg @@ -18,14 +18,14 @@ classifiers = package_dir = = src packages = find: -python_requires = >=3.6 +python_requires= >=3.9 install_requires = pandas >=1.3.0 pydotplus >=2.0.2 ipython >=7.25.0 scipy >=1.7.0 colorutils >=0.3.0 - networkx >=2.6.3 + networkx >=3.2.1 license_files= LICENSE