|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Compatibility test: statsmodels vs scipy |
| 4 | +
|
| 5 | +This script verifies that statsmodels >= 0.14.5 is compatible with scipy >= 1.16.0, |
| 6 | +resolving the import error caused by the removal of `_lazywhere` in scipy internals. |
| 7 | +
|
| 8 | +Reference: |
| 9 | +- statsmodels 0.14.5 release notes: https://github.com/statsmodels/statsmodels/releases/tag/v0.14.5 |
| 10 | +
|
| 11 | +Usage: |
| 12 | + python test_statsmodels_scipy_compatibility.py |
| 13 | + pytest test_statsmodels_scipy_compatibility.py |
| 14 | +""" |
| 15 | + |
| 16 | +import sys |
| 17 | +import importlib |
| 18 | + |
| 19 | + |
| 20 | +def check_versions(): |
| 21 | + """Print installed versions of relevant packages.""" |
| 22 | + print("Installed package versions:") |
| 23 | + for pkg in ["scipy", "statsmodels", "numpy"]: |
| 24 | + try: |
| 25 | + mod = importlib.import_module(pkg) |
| 26 | + print(f" {pkg:<12} {mod.__version__}") |
| 27 | + except ImportError: |
| 28 | + print(f" {pkg:<12} not installed") |
| 29 | + print() |
| 30 | + |
| 31 | + |
| 32 | +def test_statsmodels_import(): |
| 33 | + """Ensure statsmodels can be imported and has expected version.""" |
| 34 | + import statsmodels |
| 35 | + assert statsmodels.__version__ >= "0.14.5" |
| 36 | + |
| 37 | + |
| 38 | +def test_statsmodels_api_import(): |
| 39 | + """Ensure statsmodels.api can be imported without triggering ImportError.""" |
| 40 | + import statsmodels.api as sm |
| 41 | + assert hasattr(sm, "OLS") |
| 42 | + assert len(dir(sm)) > 10 # Sanity check |
| 43 | + |
| 44 | + |
| 45 | +def test_problematic_modules(): |
| 46 | + """Test modules previously affected by _lazywhere import failure.""" |
| 47 | + problematic_modules = [ |
| 48 | + "statsmodels.genmod._tweedie_compound_poisson", |
| 49 | + "statsmodels.distributions.discrete", |
| 50 | + ] |
| 51 | + |
| 52 | + for module_name in problematic_modules: |
| 53 | + module = importlib.import_module(module_name) |
| 54 | + assert module is not None |
| 55 | + |
| 56 | + |
| 57 | +def run_cli_mode(): |
| 58 | + """Run all tests manually in CLI mode with status output.""" |
| 59 | + print("=" * 60) |
| 60 | + print("Running statsmodels/scipy compatibility checks") |
| 61 | + print("=" * 60) |
| 62 | + check_versions() |
| 63 | + |
| 64 | + all_passed = True |
| 65 | + |
| 66 | + try: |
| 67 | + test_statsmodels_import() |
| 68 | + print("statsmodels import: PASSED") |
| 69 | + except Exception as e: |
| 70 | + print(f"statsmodels import: FAILED ({e})") |
| 71 | + all_passed = False |
| 72 | + |
| 73 | + try: |
| 74 | + test_statsmodels_api_import() |
| 75 | + print("statsmodels.api import: PASSED") |
| 76 | + except Exception as e: |
| 77 | + print(f"statsmodels.api import: FAILED ({e})") |
| 78 | + all_passed = False |
| 79 | + |
| 80 | + try: |
| 81 | + test_problematic_modules() |
| 82 | + print("problematic module imports: PASSED") |
| 83 | + except Exception as e: |
| 84 | + print(f"problematic module imports: FAILED ({e})") |
| 85 | + all_passed = False |
| 86 | + |
| 87 | + print("=" * 60) |
| 88 | + if all_passed: |
| 89 | + print("All compatibility checks passed.") |
| 90 | + sys.exit(0) |
| 91 | + else: |
| 92 | + print("One or more compatibility checks failed.") |
| 93 | + sys.exit(1) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + run_cli_mode() |
0 commit comments