|
1 | | -# PyEhsa |
2 | | -Welcome to the PyEhsa repository. |
| 1 | +# PyEhsa: Emerging Hot Spot Analysis in Python |
| 2 | + |
| 3 | +[](https://badge.fury.io/py/pyehsa) |
| 4 | +[](https://pypi.org/project/pyehsa) |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | +[](https://github.com/cloudwalk/PyEhsa/actions) |
| 7 | + |
| 8 | +**PyEhsa** is a Python library for **Emerging Hot Spot Analysis (EHSA)** of spatio-temporal data, providing functionality similar to R's `sfdep` package. It enables researchers and analysts to identify and classify spatial-temporal patterns, emerging hotspots, and coldspots in geographic data. |
| 9 | + |
| 10 | +## 🚀 Key Features |
| 11 | + |
| 12 | +- **Emerging Hot Spot Analysis**: Identify emerging patterns in spatio-temporal data |
| 13 | +- **Spatial Statistics**: Calculate Getis-Ord Gi* statistics for hotspot detection |
| 14 | +- **Mann-Kendall Trend Analysis**: Detect trends in time series data |
| 15 | +- **Multiple Classifications**: Comprehensive hotspot pattern classification |
| 16 | +- **Interactive Visualization**: HTML tool with time series analysis and mapping |
| 17 | +- **Flexible Data Input**: Support for pandas DataFrames and GeoPandas GeoDataFrames |
| 18 | + |
| 19 | +## 📦 Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install pyehsa |
| 23 | +``` |
| 24 | + |
| 25 | +## 🔧 Requirements |
| 26 | + |
| 27 | +- Python 3.9+ |
| 28 | +- pandas >= 1.5.0 |
| 29 | +- geopandas >= 0.13.0 |
| 30 | +- numpy >= 1.21.0 |
| 31 | +- scipy >= 1.9.0 |
| 32 | +- libpysal >= 4.6.0 |
| 33 | + |
| 34 | +## 📖 Quick Start Guide |
| 35 | + |
| 36 | +### Step 1: Import Libraries |
| 37 | + |
| 38 | +```python |
| 39 | +import pandas as pd |
| 40 | +import geopandas as gpd |
| 41 | +from datetime import datetime, timedelta |
| 42 | +from pyehsa import EmergingHotspotAnalysis |
| 43 | +``` |
| 44 | + |
| 45 | +### Step 2: Prepare Your Data |
| 46 | + |
| 47 | +Your dataset should have: |
| 48 | +- **`region_id`**: Spatial identifier (e.g., 'location_001', 'geohash_abc') |
| 49 | +- **`time_period`**: Temporal column (datetime format) |
| 50 | +- **`value`**: Numeric variable to analyze |
| 51 | +- **`geometry`**: Spatial geometry (Point or Polygon from shapely) |
| 52 | + |
| 53 | +```python |
| 54 | +# Example: Create or load your GeoDataFrame |
| 55 | +data = pd.DataFrame({ |
| 56 | + 'region_id': ['area_1', 'area_1', 'area_2', 'area_2'], |
| 57 | + 'time_period': [datetime(2024, 1, 1), datetime(2024, 2, 1), |
| 58 | + datetime(2024, 1, 1), datetime(2024, 2, 1)], |
| 59 | + 'value': [10.5, 15.2, 8.3, 12.1], |
| 60 | + 'geometry': [polygon_1, polygon_1, polygon_2, polygon_2] # Your geometries |
| 61 | +}) |
| 62 | + |
| 63 | +gdf = gpd.GeoDataFrame(data, geometry='geometry', crs='EPSG:4326') |
| 64 | +``` |
| 65 | + |
| 66 | +### Step 3: Run Emerging Hot Spot Analysis |
| 67 | + |
| 68 | +```python |
| 69 | +results = EmergingHotspotAnalysis.emerging_hotspot_analysis( |
| 70 | + gdf, |
| 71 | + region_id_field='region_id', |
| 72 | + time_period_field='time_period', |
| 73 | + value='value', |
| 74 | + k=1, # Temporal lags to include |
| 75 | + nsim=99 # Monte Carlo simulations for significance testing |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Step 4: View Results |
| 80 | + |
| 81 | +```python |
| 82 | +# See classification distribution |
| 83 | +print(results['classification'].value_counts()) |
| 84 | + |
| 85 | +# View detailed results |
| 86 | +results.head() |
| 87 | +``` |
| 88 | + |
| 89 | +### Step 5: Create Interactive Visualization |
| 90 | + |
| 91 | +```python |
| 92 | +from pyehsa import EhsaPlotting |
| 93 | + |
| 94 | +# Merge geometry with results for visualization |
| 95 | +locations = gdf[['region_id', 'geometry']].drop_duplicates() |
| 96 | +viz_data = results.merge(locations, left_on=results.columns[0], right_on='region_id') |
| 97 | + |
| 98 | +# Create interactive map |
| 99 | +ehsa_map = EhsaPlotting.plot_ehsa_map_interactive( |
| 100 | + df=viz_data, |
| 101 | + region_id_field='region_id', |
| 102 | + title="Emerging Hotspots Analysis" |
| 103 | +) |
| 104 | +ehsa_map.show() |
| 105 | +``` |
| 106 | + |
| 107 | +## 📊 Classification Types |
| 108 | + |
| 109 | +The analysis returns classifications such as: |
| 110 | +- **new hotspot**: Recently emerged statistically significant hotspots |
| 111 | +- **consecutive hotspot**: Hotspots significant for multiple consecutive periods |
| 112 | +- **sporadic hotspot**: Intermittent hotspot patterns |
| 113 | +- **intensifying hotspot**: Hotspots with increasing trend |
| 114 | +- **persistent hotspot**: Long-term stable hotspots |
| 115 | +- **oscillating hotspot**: Alternating hotspot patterns |
| 116 | +- **historical hotspot**: Previously significant hotspots |
| 117 | +- **coldspot variations**: Similar patterns for low-value clusters |
| 118 | +- **no pattern detected**: Areas without significant patterns |
| 119 | + |
| 120 | +## 🛠️ Advanced Usage |
| 121 | + |
| 122 | +### Interactive HTML Visualization Tool |
| 123 | + |
| 124 | +```python |
| 125 | +# Launch interactive visualization tool |
| 126 | +EmergingHotspotAnalysis.launch_visualization(results) |
| 127 | + |
| 128 | +# Or save to file |
| 129 | +EmergingHotspotAnalysis.save_visualization('my_analysis.html', results) |
| 130 | +``` |
| 131 | + |
| 132 | +The HTML tool provides: |
| 133 | +- Interactive map with polygon visualization |
| 134 | +- Time series charts for each region |
| 135 | +- Classification details and statistics |
| 136 | +- Mann-Kendall trend analysis results |
| 137 | + |
| 138 | +### Static Map Visualization |
| 139 | + |
| 140 | +```python |
| 141 | +plotter = EhsaPlotting() |
| 142 | +fig = plotter.plot_ehsa_map_static(viz_data, title="My Analysis") |
| 143 | +fig.savefig('hotspots_map.png', dpi=300, bbox_inches='tight') |
| 144 | +``` |
| 145 | + |
| 146 | +## 📚 Complete Example |
| 147 | + |
| 148 | +See `examples/demo_sp.ipynb` for a complete working example with: |
| 149 | +- Synthetic grid data covering São Paulo |
| 150 | +- Full EHSA workflow from data creation to visualization |
| 151 | +- Interactive map generation |
| 152 | + |
| 153 | +## 🤝 Contributing |
| 154 | + |
| 155 | +We welcome contributions! Please: |
| 156 | +1. Fork the repository |
| 157 | +2. Create a feature branch (`git checkout -b feature/amazing-feature`) |
| 158 | +3. Commit your changes (`git commit -m 'Add amazing feature'`) |
| 159 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 160 | +5. Open a Pull Request |
| 161 | + |
| 162 | +## 📄 Citation |
| 163 | + |
| 164 | +If you use PyEhsa in your research, please cite: |
| 165 | + |
| 166 | +```bibtex |
| 167 | +@software{pyehsa2025, |
| 168 | + author = {CloudWalk}, |
| 169 | + title = {PyEhsa: Emerging Hot Spot Analysis in Python}, |
| 170 | + year = {2025}, |
| 171 | + url = {https://github.com/cloudwalk/PyEhsa}, |
| 172 | + version = {0.1.0} |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## 📋 License |
| 177 | + |
| 178 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 179 | + |
| 180 | +## 🔬 Related Work |
| 181 | + |
| 182 | +PyEhsa is inspired by and provides similar functionality to: |
| 183 | +- R's `sfdep` package for spatial dependence analysis |
| 184 | +- ArcGIS's Emerging Hot Spot Analysis tool |
| 185 | +- PySAL ecosystem for spatial analysis |
| 186 | + |
| 187 | +## 📈 Development & Support |
| 188 | + |
| 189 | +- **Repository**: [github.com/cloudwalk/PyEhsa](https://github.com/cloudwalk/PyEhsa) |
| 190 | +- **Issues**: [GitHub Issues](https://github.com/cloudwalk/PyEhsa/issues) |
| 191 | +- **PyPI**: [pypi.org/project/pyehsa](https://pypi.org/project/pyehsa) |
| 192 | + |
| 193 | +## 🙏 Acknowledgments |
| 194 | + |
| 195 | +Developed by CloudWalk's data science team to provide open-source spatial analysis tools for the Python community. |
0 commit comments