|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +### Development Setup |
| 8 | +```bash |
| 9 | +# Install for development with all dependencies |
| 10 | +pip install -e .[dev] |
| 11 | + |
| 12 | +# Install with specific extras |
| 13 | +pip install -e .[docs] # Documentation dependencies |
| 14 | +pip install -e .[cross-validation] # R comparison tools |
| 15 | +``` |
| 16 | + |
| 17 | +### Testing |
| 18 | +```bash |
| 19 | +# Run all tests |
| 20 | +pytest |
| 21 | + |
| 22 | +# Run with coverage |
| 23 | +pytest --cov=pycancensus --cov-report=xml |
| 24 | + |
| 25 | +# Run specific test categories |
| 26 | +pytest tests/test_basic.py |
| 27 | +pytest tests/integration/ |
| 28 | +pytest tests/performance/ |
| 29 | + |
| 30 | +# Run R cross-validation tests (requires R and rpy2) |
| 31 | +pytest tests/cross_validation/test_r_equivalence.py --ignore-pytest-ini |
| 32 | +``` |
| 33 | + |
| 34 | +### Code Quality |
| 35 | +```bash |
| 36 | +# Format code |
| 37 | +black pycancensus |
| 38 | + |
| 39 | +# Check formatting without changing files |
| 40 | +black --check pycancensus |
| 41 | + |
| 42 | +# Lint code |
| 43 | +flake8 pycancensus --count --select=E9,F63,F7,F82 --show-source --statistics |
| 44 | +flake8 pycancensus --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics |
| 45 | +``` |
| 46 | + |
| 47 | +### Documentation |
| 48 | +```bash |
| 49 | +# Build documentation |
| 50 | +cd docs && make html |
| 51 | + |
| 52 | +# Check for broken links |
| 53 | +cd docs && make linkcheck |
| 54 | + |
| 55 | +# Clean and rebuild |
| 56 | +cd docs && make clean html |
| 57 | +``` |
| 58 | + |
| 59 | +### API Key Setup |
| 60 | +```bash |
| 61 | +# Set CensusMapper API key |
| 62 | +export CANCENSUS_API_KEY="your_api_key_here" |
| 63 | +``` |
| 64 | + |
| 65 | +## Architecture |
| 66 | + |
| 67 | +### Core Design Principles |
| 68 | +- **R Compatibility**: Mirror the R cancensus library's function signatures and behavior |
| 69 | +- **Production Grade**: Enterprise-level error handling, retries, and rate limiting |
| 70 | +- **Analysis Ready**: Return pandas/GeoPandas DataFrames ready for analysis |
| 71 | +- **Performance**: Connection pooling, caching, and progress indicators for large operations |
| 72 | + |
| 73 | +### Module Structure |
| 74 | +- `core.py`: Main `get_census()` function that retrieves census data |
| 75 | +- `datasets.py`: List available census datasets (CA21, CA16, etc.) |
| 76 | +- `regions.py`: List and search geographic regions |
| 77 | +- `vectors.py`: List, search, and find census variables |
| 78 | +- `hierarchy.py`: Navigate parent/child relationships between variables |
| 79 | +- `geometry.py`: Handle geographic data and spatial operations |
| 80 | +- `cache.py`: Caching system to minimize API calls |
| 81 | +- `settings.py`: API key and configuration management |
| 82 | +- `resilience.py`: Error handling, retry logic, rate limiting |
| 83 | +- `progress.py`: Progress bars for long-running operations |
| 84 | +- `utils.py`: Shared utility functions |
| 85 | +- `cli.py`: Command-line interface |
| 86 | + |
| 87 | +### Key Technical Details |
| 88 | + |
| 89 | +#### API Integration |
| 90 | +- All API calls go through `resilience.py` for error handling |
| 91 | +- Automatic retry with exponential backoff on failures |
| 92 | +- Rate limiting respects CensusMapper API constraints |
| 93 | +- Connection pooling for improved performance |
| 94 | + |
| 95 | +#### Data Processing |
| 96 | +- Census data returned as pandas DataFrames |
| 97 | +- Geographic data returned as GeoPandas GeoDataFrames |
| 98 | +- Handles census-specific NA values correctly |
| 99 | +- Column naming matches R cancensus for compatibility |
| 100 | + |
| 101 | +#### Caching Strategy |
| 102 | +- File-based caching in `~/.pycancensus/cache/` |
| 103 | +- Cache keys based on request parameters |
| 104 | +- Automatic cache invalidation after 30 days |
| 105 | +- Option to disable caching per request |
| 106 | + |
| 107 | +#### Error Handling |
| 108 | +- Custom exception hierarchy in `resilience.py` |
| 109 | +- Helpful error messages with suggestions |
| 110 | +- Special handling for rate limits with retry-after headers |
| 111 | +- Connection error resilience with retry logic |
| 112 | + |
| 113 | +### Testing Approach |
| 114 | +- Unit tests mock API responses for reliability |
| 115 | +- Integration tests use real API calls (requires API key) |
| 116 | +- Cross-validation tests ensure R equivalence |
| 117 | +- Performance tests verify handling of large datasets |
| 118 | + |
| 119 | +### Important Patterns |
| 120 | +- Always check for existing API key before making requests |
| 121 | +- Use progress bars for operations over 100 items |
| 122 | +- Return consistent DataFrame structures across all functions |
| 123 | +- Maintain exact R function signatures for compatibility |
| 124 | + |
| 125 | +## New Functions Added |
| 126 | + |
| 127 | +### dataset_attribution() |
| 128 | +- **Location**: `pycancensus/datasets.py` |
| 129 | +- **Purpose**: Get combined attribution text for multiple datasets, merging similar attributions that only differ by year |
| 130 | +- **Usage**: `pc.dataset_attribution(['CA16', 'CA21'])` |
| 131 | +- **Testing**: Comprehensive cross-validation with R cancensus shows perfect equivalence |
| 132 | + |
| 133 | +### label_vectors() |
| 134 | +- **Location**: `pycancensus/vectors.py` |
| 135 | +- **Purpose**: Extract census vector metadata from DataFrames returned by get_census() |
| 136 | +- **Usage**: `pc.label_vectors(census_data)` where census_data was retrieved with vectors |
| 137 | +- **Implementation**: Stores metadata in DataFrame.attrs['census_vectors'] attribute |
| 138 | +- **Testing**: Works with both regular and GeoDataFrames, handles short/detailed labels |
| 139 | + |
| 140 | +### get_intersecting_geometries() (Partial Implementation) |
| 141 | +- **Location**: `pycancensus/intersect_geometry.py` |
| 142 | +- **Purpose**: Find census regions that intersect with given geometries |
| 143 | +- **Status**: Implementation complete but API endpoint requires premium access |
| 144 | +- **Note**: Function framework ready for when API access is available |
| 145 | + |
| 146 | +## Cross-Validation Results |
| 147 | + |
| 148 | +The comprehensive cross-validation test suite reveals: |
| 149 | + |
| 150 | +### ✅ Perfect Equivalence |
| 151 | +- `list_census_datasets()`: 100% equivalent with R |
| 152 | +- `dataset_attribution()`: 100% equivalent with R |
| 153 | +- `list_census_vectors()`: 100% equivalent with R |
| 154 | + |
| 155 | +### ⚠️ Known Differences |
| 156 | +- `search_census_vectors()`: Python returns more results (broader search) |
| 157 | +- `get_census()`: Python includes additional metadata columns |
| 158 | +- `list_census_regions()`: API endpoint differences between implementations |
| 159 | + |
| 160 | +### Test Coverage |
| 161 | +- **15 comprehensive cross-validation tests** covering major functions |
| 162 | +- **Unit tests** for new functions with edge cases |
| 163 | +- **Integration tests** ensuring functions work together |
| 164 | +- **Performance tests** for large datasets |
0 commit comments