|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# PyEhsa Demo - São Paulo\n" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import sys\n", |
| 17 | + "import os\n", |
| 18 | + "sys.path.append(os.path.join(os.path.dirname(''), '..', 'src'))\n", |
| 19 | + "\n", |
| 20 | + "import pandas as pd\n", |
| 21 | + "import geopandas as gpd\n", |
| 22 | + "import numpy as np\n", |
| 23 | + "from shapely.geometry import Point\n", |
| 24 | + "from datetime import datetime, timedelta\n", |
| 25 | + "\n", |
| 26 | + "from pyehsa.emerging_hotspot_analysis import EmergingHotspotAnalysis\n", |
| 27 | + "\n", |
| 28 | + "np.random.seed(42)\n" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "# Criar dados sintéticos - Grid 5x5 no centro de SP\n", |
| 38 | + "center_lat, center_lon = -23.5489, -46.6388\n", |
| 39 | + "step = 0.01\n", |
| 40 | + "\n", |
| 41 | + "data = []\n", |
| 42 | + "for i in range(5):\n", |
| 43 | + " for j in range(5):\n", |
| 44 | + " lat = center_lat + (i - 2) * step\n", |
| 45 | + " lon = center_lon + (j - 2) * step\n", |
| 46 | + " location_id = f'SP_{i}_{j}'\n", |
| 47 | + " \n", |
| 48 | + " for month in range(6):\n", |
| 49 | + " time_period = datetime(2024, 1, 1) + timedelta(days=30*month)\n", |
| 50 | + " \n", |
| 51 | + " value = np.random.poisson(10)\n", |
| 52 | + " \n", |
| 53 | + " # Hotspot emergente no canto superior direito\n", |
| 54 | + " if i >= 3 and j >= 3 and month >= 2:\n", |
| 55 | + " value += (month - 1) * 8\n", |
| 56 | + " \n", |
| 57 | + " value += np.random.normal(0, 2)\n", |
| 58 | + " value = max(0, value)\n", |
| 59 | + " \n", |
| 60 | + " data.append({\n", |
| 61 | + " 'location_id': location_id,\n", |
| 62 | + " 'time_period': time_period,\n", |
| 63 | + " 'value': value,\n", |
| 64 | + " 'geometry': Point(lon, lat)\n", |
| 65 | + " })\n", |
| 66 | + "\n", |
| 67 | + "gdf = gpd.GeoDataFrame(data, geometry='geometry', crs='EPSG:4326')\n", |
| 68 | + "print(f\"Dataset: {len(gdf)} observações, {gdf['location_id'].nunique()} locais\")\n" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "# Executar análise EHSA\n", |
| 78 | + "results = EmergingHotspotAnalysis.emerging_hotspot_analysis(\n", |
| 79 | + " gdf,\n", |
| 80 | + " region_id_field='location_id',\n", |
| 81 | + " time_period_field='time_period', \n", |
| 82 | + " value='value',\n", |
| 83 | + " k=1,\n", |
| 84 | + " nsim=99\n", |
| 85 | + ")\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "# Mostrar resultados\n", |
| 95 | + "print(\"Padrões identificados:\")\n", |
| 96 | + "print(results['classification'].value_counts())\n", |
| 97 | + "print(\"\\nPrimeiros resultados:\")\n", |
| 98 | + "results[['region_id', 'classification', 'tau', 'p_value']].head()\n" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# Criar mapa\n", |
| 108 | + "import folium\n", |
| 109 | + "\n", |
| 110 | + "locations = gdf[['location_id', 'geometry']].drop_duplicates()\n", |
| 111 | + "viz_data = results.merge(locations, left_on='region_id', right_on='location_id')\n", |
| 112 | + "\n", |
| 113 | + "m = folium.Map(location=[center_lat, center_lon], zoom_start=13)\n", |
| 114 | + "\n", |
| 115 | + "color_map = {\n", |
| 116 | + " 'no pattern detected': 'gray',\n", |
| 117 | + " 'new hotspot': 'red',\n", |
| 118 | + " 'consecutive hotspot': 'darkred',\n", |
| 119 | + " 'sporadic hotspot': 'orange'\n", |
| 120 | + "}\n", |
| 121 | + "\n", |
| 122 | + "for _, row in viz_data.iterrows():\n", |
| 123 | + " color = color_map.get(row['classification'], 'gray')\n", |
| 124 | + " \n", |
| 125 | + " folium.CircleMarker(\n", |
| 126 | + " location=[row['geometry'].y, row['geometry'].x],\n", |
| 127 | + " radius=8,\n", |
| 128 | + " popup=f\"{row['region_id']}: {row['classification']}\",\n", |
| 129 | + " color='black',\n", |
| 130 | + " fillColor=color,\n", |
| 131 | + " fillOpacity=0.7\n", |
| 132 | + " ).add_to(m)\n", |
| 133 | + "\n", |
| 134 | + "m\n" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "metadata": { |
| 139 | + "language_info": { |
| 140 | + "name": "python" |
| 141 | + } |
| 142 | + }, |
| 143 | + "nbformat": 4, |
| 144 | + "nbformat_minor": 2 |
| 145 | +} |
0 commit comments