|
| 1 | +# QuantEcon.py Development Instructions |
| 2 | + |
| 3 | +QuantEcon.py is a high-performance, open-source Python library for quantitative economics. The library provides tools for economics research including Markov chains, dynamic programming, game theory, quadrature, and optimization algorithms. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Environment Setup (REQUIRED) |
| 10 | +- **ALWAYS use conda environment**: `conda env create -f environment.yml` |
| 11 | + - Takes 3-5 minutes to complete. NEVER CANCEL. Set timeout to 10+ minutes. |
| 12 | + - Creates environment named 'qe' with Python 3.13 and all dependencies |
| 13 | +- **Activate environment**: `eval "$(conda shell.bash hook)" && conda activate qe` |
| 14 | +- **Development install**: `flit install` |
| 15 | + - Installs package in development mode for testing changes |
| 16 | + - Takes < 30 seconds |
| 17 | + |
| 18 | +### Build and Test Workflow |
| 19 | +- **Linting**: `flake8 --select F401,F405,E231 quantecon` |
| 20 | + - Takes < 30 seconds |
| 21 | + - Note: Repository has some existing style violations - this is expected |
| 22 | +- **Full test suite**: `coverage run -m pytest quantecon` |
| 23 | + - Takes 5 minutes 11 seconds. NEVER CANCEL. Set timeout to 15+ minutes. |
| 24 | + - Runs 536 tests across all modules |
| 25 | + - All tests should pass with 2 warnings (expected) |
| 26 | +- **Quick smoke test**: `python -c "import quantecon as qe; print('Version:', qe.__version__)"` |
| 27 | +- **Package build**: `flit build` |
| 28 | + - Creates wheel and source distributions in dist/ |
| 29 | + - Takes < 1 minute |
| 30 | + |
| 31 | +### Validation Scenarios |
| 32 | +After making changes, ALWAYS run these validation steps: |
| 33 | +1. **Import test**: `python -c "import quantecon as qe; print('Import successful, version:', qe.__version__)"` |
| 34 | +2. **Basic functionality test**: |
| 35 | +```python |
| 36 | +python -c " |
| 37 | +from quantecon.markov import DiscreteDP |
| 38 | +import numpy as np |
| 39 | +R = np.array([[10, 8], [6, 4]]) |
| 40 | +Q = np.array([[[0.9, 0.1], [0.8, 0.2]], [[0.7, 0.3], [0.6, 0.4]]]) |
| 41 | +ddp = DiscreteDP(R, Q, 0.95) |
| 42 | +result = ddp.solve(method='policy_iteration') |
| 43 | +print('DiscreteDP test successful, policy:', result.sigma) |
| 44 | +" |
| 45 | +``` |
| 46 | +3. **Run relevant tests**: `pytest quantecon/tests/test_[module].py -v` for specific modules |
| 47 | +4. **Run flake8 linting** before committing |
| 48 | + |
| 49 | +## Key Project Structure |
| 50 | + |
| 51 | +### Core Modules |
| 52 | +- `quantecon/` - Main package source code |
| 53 | + - `markov/` - Markov chain and dynamic programming tools |
| 54 | + - `game_theory/` - Game theory algorithms and utilities |
| 55 | + - `optimize/` - Optimization algorithms |
| 56 | + - `random/` - Random number generation utilities |
| 57 | + - `tests/` - Main test suite (536 tests total) |
| 58 | + |
| 59 | +### Configuration Files |
| 60 | +- `pyproject.toml` - Main project configuration using flit build system |
| 61 | +- `environment.yml` - Conda environment specification with all dependencies |
| 62 | +- `.github/workflows/ci.yml` - CI pipeline (tests on Python 3.11, 3.12, 3.13) |
| 63 | +- `pytest.ini` - Test configuration including slow test markers |
| 64 | + |
| 65 | +### Dependencies |
| 66 | +Core runtime dependencies (auto-installed in conda env): |
| 67 | +- `numba>=0.49.0` - JIT compilation for performance |
| 68 | +- `numpy>=1.17.0` - Array operations |
| 69 | +- `scipy>=1.5.0` - Scientific computing |
| 70 | +- `sympy` - Symbolic mathematics |
| 71 | +- `requests` - HTTP library |
| 72 | + |
| 73 | +## Timing Expectations and Timeouts |
| 74 | + |
| 75 | +**CRITICAL TIMING INFORMATION:** |
| 76 | +- **Conda environment creation**: 3-5 minutes (timeout: 10+ minutes) |
| 77 | +- **Full test suite**: 5 minutes 11 seconds (timeout: 15+ minutes) |
| 78 | +- **Package build**: < 30 seconds (timeout: 2 minutes) |
| 79 | +- **Development install**: < 30 seconds (timeout: 2 minutes) |
| 80 | +- **Linting**: < 30 seconds (timeout: 2 minutes) |
| 81 | + |
| 82 | +**NEVER CANCEL these operations** - they may appear to hang but are processing large dependency trees or running comprehensive tests. |
| 83 | + |
| 84 | +## Common Tasks |
| 85 | + |
| 86 | +### Making Code Changes |
| 87 | +1. Ensure conda environment is active: `conda activate qe` |
| 88 | +2. Make your changes to files in `quantecon/` |
| 89 | +3. Run development install: `flit install` |
| 90 | +4. Test imports: `python -c "import quantecon as qe; print('Import OK')"` |
| 91 | +5. Run relevant tests: `pytest quantecon/tests/test_[relevant_module].py` |
| 92 | +6. Run linting: `flake8 --select F401,F405,E231 quantecon` |
| 93 | + |
| 94 | +### Adding New Features |
| 95 | +1. Add code to appropriate module in `quantecon/` |
| 96 | +2. Add tests to `quantecon/tests/test_[module].py` |
| 97 | +3. Update `quantecon/__init__.py` if exposing new public API |
| 98 | +4. Run full test suite to ensure no regressions |
| 99 | +5. Validate with example usage |
| 100 | + |
| 101 | +### Debugging Failed Tests |
| 102 | +1. Run specific test: `pytest quantecon/tests/test_[module].py::test_function -v` |
| 103 | +2. Use pytest markers: `pytest -m "not slow"` to skip long-running tests |
| 104 | +3. Check test output and traceback for specific failure modes |
| 105 | +4. Many tests use numerical algorithms - check for convergence issues |
| 106 | + |
| 107 | +## Important Notes |
| 108 | + |
| 109 | +### CI/CD Pipeline |
| 110 | +- GitHub Actions runs tests on Windows, Ubuntu, and macOS |
| 111 | +- Tests Python 3.11, 3.12, and 3.13 |
| 112 | +- Includes flake8 linting and coverage reporting |
| 113 | +- Publishing to PyPI is automated on git tags |
| 114 | + |
| 115 | +### Network Limitations |
| 116 | +- **pip install from PyPI may fail** due to network timeouts in sandboxed environments |
| 117 | +- **Always use conda environment** for reliable dependency management |
| 118 | +- Documentation building may fail due to missing dependencies - focus on code functionality |
| 119 | + |
| 120 | +### Performance Considerations |
| 121 | +- Many algorithms use numba JIT compilation - first run may be slower |
| 122 | +- Test suite includes performance-sensitive numerical algorithms |
| 123 | +- Some tests marked as "slow" - use `pytest -m "not slow"` to skip them during development |
| 124 | + |
| 125 | +### Repository Status |
| 126 | +- Current version: Check `quantecon/__init__.py` for `__version__` variable |
| 127 | +- Build system: flit (modern Python packaging) |
| 128 | +- License: MIT |
| 129 | +- Documentation: ReadTheDocs (quanteconpy.readthedocs.io) |
| 130 | + |
| 131 | +### Maintenance Notes |
| 132 | +- When creating new releases, verify that timing expectations and test counts in these instructions remain accurate |
| 133 | +- Version information is dynamically referenced to avoid hardcoded values |
| 134 | + |
| 135 | +## Quick Reference Commands |
| 136 | + |
| 137 | +```bash |
| 138 | +# Setup (do once) |
| 139 | +conda env create -f environment.yml |
| 140 | +eval "$(conda shell.bash hook)" && conda activate qe |
| 141 | + |
| 142 | +# Development workflow |
| 143 | +flit install # Install in development mode |
| 144 | +python -c "import quantecon as qe; print(qe.__version__)" # Test import |
| 145 | +pytest quantecon/tests/test_[module].py # Test specific module |
| 146 | +flake8 --select F401,F405,E231 quantecon # Lint code |
| 147 | + |
| 148 | +# Full validation (before committing) |
| 149 | +coverage run -m pytest quantecon # Full test suite (5+ minutes) |
| 150 | +flit build # Build packages |
| 151 | +``` |
| 152 | + |
| 153 | +Remember: This is a numerical computing library with complex dependencies. Always use the conda environment and expect longer build/test times than typical Python projects. |
0 commit comments