From 01a9ab4fe6e4329d5536738a29667f436a3a047d Mon Sep 17 00:00:00 2001 From: juaristi22 Date: Mon, 28 Jul 2025 11:32:59 +0200 Subject: [PATCH 1/2] test microsimulation works without optional dependencies --- changelog_entry.yaml | 4 ++ .../datasets/cps/enhanced_cps.py | 8 +-- .../test_datasets/test_data_us_pipeline.py | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..0cc3ef1f 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: minor + changes: + changed: + - Added test to ensure microsimulation runs with only us-data direct dependencies. \ No newline at end of file diff --git a/policyengine_us_data/datasets/cps/enhanced_cps.py b/policyengine_us_data/datasets/cps/enhanced_cps.py index 59fed93b..08959d85 100644 --- a/policyengine_us_data/datasets/cps/enhanced_cps.py +++ b/policyengine_us_data/datasets/cps/enhanced_cps.py @@ -21,12 +21,6 @@ from pathlib import Path -try: - import torch -except ImportError: - torch = None - - def reweight( original_weights, loss_matrix, @@ -38,6 +32,8 @@ def reweight( init_mean=0.999, # initial proportion with non-zero weights, set near 0 temperature=0.5, # Usual values .5 to 3, .5 was working better ): + import torch + target_names = np.array(loss_matrix.columns) is_national = loss_matrix.columns.str.startswith("nation/") loss_matrix = torch.tensor(loss_matrix.values, dtype=torch.float32) diff --git a/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py b/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py new file mode 100644 index 00000000..81a12aaf --- /dev/null +++ b/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py @@ -0,0 +1,52 @@ +""" +Tests to ensure Microsimulations can run with the us-data direct dependencies only. +""" + +import subprocess +import sys +import tempfile +import os + + +def test_microsimulation_runs(): + # Create a test script that only imports direct dependencies + test_script = """ +import sys +# Remove optional dependencies from sys.modules if present +optional_deps = ["black", + "pytest", + "quantile-forest", + "tabulate", + "furo", + "jupyter-book", + "yaml-changelog", + "build", + "tomli", + "itables"] +for dep in optional_deps: + if dep in sys.modules: + del sys.modules[dep] + +from policyengine_us import Microsimulation +from policyengine_us_data.datasets.cps import EnhancedCPS_2024 + +sim = Microsimulation(dataset=EnhancedCPS_2024) +result = sim.calculate("employment_income", map_to="household", period=2025) +print("SUCCESS") +""" + + with tempfile.NamedTemporaryFile( + mode="w", suffix=".py", delete=False + ) as f: + f.write(test_script) + f.flush() + + # Run in subprocess with minimal environment + result = subprocess.run( + [sys.executable, f.name], capture_output=True, text=True + ) + + os.unlink(f.name) + + assert result.returncode == 0, f"Test failed: {result.stderr}" + assert "SUCCESS" in result.stdout From fc65b4bb8e9430cea9317f3a4b4466a833d5d68e Mon Sep 17 00:00:00 2001 From: juaristi22 Date: Mon, 28 Jul 2025 18:23:26 +0200 Subject: [PATCH 2/2] run simple microsim check from actions with no dev deps --- .github/workflows/pr_code_changes.yaml | 3 ++ .../test_datasets/test_data_us_pipeline.py | 49 ++----------------- 2 files changed, 7 insertions(+), 45 deletions(-) diff --git a/.github/workflows/pr_code_changes.yaml b/.github/workflows/pr_code_changes.yaml index 28f3fc77..6ad920a8 100644 --- a/.github/workflows/pr_code_changes.yaml +++ b/.github/workflows/pr_code_changes.yaml @@ -48,6 +48,9 @@ jobs: - name: Test specific core import run: python -c "from policyengine_core.data import Dataset; print('Core import OK')" + - name: Test Microsimulation with direct dependencies only + run: python -m pytest policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py -v + Test: runs-on: ubuntu-latest needs: Lint diff --git a/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py b/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py index 81a12aaf..fc3ae037 100644 --- a/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py +++ b/policyengine_us_data/tests/test_datasets/test_data_us_pipeline.py @@ -2,51 +2,10 @@ Tests to ensure Microsimulations can run with the us-data direct dependencies only. """ -import subprocess -import sys -import tempfile -import os - def test_microsimulation_runs(): - # Create a test script that only imports direct dependencies - test_script = """ -import sys -# Remove optional dependencies from sys.modules if present -optional_deps = ["black", - "pytest", - "quantile-forest", - "tabulate", - "furo", - "jupyter-book", - "yaml-changelog", - "build", - "tomli", - "itables"] -for dep in optional_deps: - if dep in sys.modules: - del sys.modules[dep] - -from policyengine_us import Microsimulation -from policyengine_us_data.datasets.cps import EnhancedCPS_2024 - -sim = Microsimulation(dataset=EnhancedCPS_2024) -result = sim.calculate("employment_income", map_to="household", period=2025) -print("SUCCESS") -""" - - with tempfile.NamedTemporaryFile( - mode="w", suffix=".py", delete=False - ) as f: - f.write(test_script) - f.flush() - - # Run in subprocess with minimal environment - result = subprocess.run( - [sys.executable, f.name], capture_output=True, text=True - ) - - os.unlink(f.name) + from policyengine_us import Microsimulation + from policyengine_us_data.datasets.cps import EnhancedCPS_2024 - assert result.returncode == 0, f"Test failed: {result.stderr}" - assert "SUCCESS" in result.stdout + sim = Microsimulation(dataset=EnhancedCPS_2024) + sim.calculate("employment_income", map_to="household", period=2025)