Spaces:
Sleeping
Sleeping
Commit ·
a832d75
1
Parent(s): 75c6e74
Finaliza integração com HF Dataset
Browse files- app_04.py +6 -0
- download_data.py +48 -18
- load_process.py +63 -55
app_04.py
CHANGED
|
@@ -6,7 +6,13 @@ from load_process import load_files
|
|
| 6 |
from layout_01 import criar_layout
|
| 7 |
from callbacks import registrar_callbacks
|
| 8 |
from flask_caching import Cache
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# Leitura dos dados
|
|
|
|
| 6 |
from layout_01 import criar_layout
|
| 7 |
from callbacks import registrar_callbacks
|
| 8 |
from flask_caching import Cache
|
| 9 |
+
from load_process import load_bairros, load_coleta, load_descartes
|
| 10 |
|
| 11 |
+
print("📦 Carregando dados geoespaciais...")
|
| 12 |
+
gdf_bairros = load_bairros()
|
| 13 |
+
gdf_coleta = load_coleta()
|
| 14 |
+
gdf_descartes = load_descartes()
|
| 15 |
+
print("✅ Dados carregados")
|
| 16 |
|
| 17 |
|
| 18 |
# Leitura dos dados
|
download_data.py
CHANGED
|
@@ -1,25 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
|
| 4 |
-
REPO_ID = "albertoakel/dados_belem"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
SUBDIR = "data/process"
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
|
| 4 |
+
# REPO_ID = "albertoakel/dados_belem"
|
| 5 |
+
# SUBDIR = "data/process"
|
| 6 |
+
|
| 7 |
+
# FILES = [
|
| 8 |
+
# "shape_bairros.gpkg",
|
| 9 |
+
# "shape_coleta.gpkg",
|
| 10 |
+
# "Pontos_descartes_ML.gpkg",
|
| 11 |
+
# "tabela_total_com_DIEs.csv",
|
| 12 |
+
# "Bairros_Ncoleta.csv",
|
| 13 |
+
# ]
|
| 14 |
+
|
| 15 |
+
# def ensure_data():
|
| 16 |
+
# paths = {}
|
| 17 |
+
# for f in FILES:
|
| 18 |
+
# path = hf_hub_download(
|
| 19 |
+
# repo_id=REPO_ID,
|
| 20 |
+
# filename=f"{SUBDIR}/{f}",
|
| 21 |
+
# repo_type="dataset"
|
| 22 |
+
# )
|
| 23 |
+
# paths[f] = path
|
| 24 |
+
# return paths
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
DATASET_ID = "albertoakel/dados_belem"
|
| 29 |
SUBDIR = "data/process"
|
| 30 |
|
| 31 |
+
# diretório temporário (persistente durante o runtime)
|
| 32 |
+
BASE_DIR = "/tmp/dados_belem"
|
| 33 |
+
os.makedirs(BASE_DIR, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def get_data_file(filename: str) -> str:
|
| 37 |
+
"""
|
| 38 |
+
Baixa o arquivo do HF Dataset apenas se não existir localmente.
|
| 39 |
+
Retorna o caminho local do arquivo.
|
| 40 |
+
"""
|
| 41 |
+
local_path = os.path.join(BASE_DIR, filename)
|
| 42 |
+
|
| 43 |
+
if not os.path.exists(local_path):
|
| 44 |
+
print(f"⬇️ Baixando {filename} do Hugging Face Dataset...")
|
| 45 |
+
hf_hub_download(
|
| 46 |
+
repo_id=DATASET_ID,
|
| 47 |
+
filename=f"{SUBDIR}/{filename}",
|
| 48 |
+
repo_type="dataset",
|
| 49 |
+
local_dir=BASE_DIR,
|
| 50 |
+
local_dir_use_symlinks=False
|
| 51 |
)
|
| 52 |
+
else:
|
| 53 |
+
print(f"✅ Usando cache local: {filename}")
|
| 54 |
|
| 55 |
+
return local_path
|
load_process.py
CHANGED
|
@@ -1,44 +1,48 @@
|
|
| 1 |
|
| 2 |
# load_process.py
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import geopandas as gpd
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
paths = ensure_data()
|
| 14 |
|
| 15 |
-
gdf = gpd.read_file(paths["shape_bairros.gpkg"]).rename(columns={'NM_BAIRRO': 'Bairro'})
|
| 16 |
-
df1 = pd.read_csv(paths["tabela_total_com_DIEs.csv"])
|
| 17 |
-
df2 = pd.read_csv(paths["Bairros_Ncoleta.csv"])
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
# gdf = gpd.read_file(os.path.join(DATA_DIR, "shape_bairros.gpkg")).rename(columns={'NM_BAIRRO': 'Bairro'})
|
| 23 |
-
# df1 = pd.read_csv(os.path.join(DATA_DIR, "tabela_total_com_DIEs.csv"))
|
| 24 |
-
# df2 = pd.read_csv(os.path.join(DATA_DIR, "Bairros_Ncoleta.csv"))
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#gdf = gpd.read_file(path + 'shape_bairros.gpkg').rename(columns={'NM_BAIRRO': 'Bairro'})
|
| 28 |
-
#df1 = pd.read_csv(path + 'tabela_total_com_DIEs.csv')
|
| 29 |
-
#df2 = pd.read_csv(path + 'Bairros_Ncoleta.csv')
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
df = df1.merge(df2, on='Bairro', how='left')
|
| 32 |
gdf_m = gdf.merge(df, on='Bairro', how='left')
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
gdf_m['NS']=(gdf_m['Mor']-gdf_m['N_ren'])/gdf_m['Mor']
|
| 36 |
colunas = list(gdf_m.columns)
|
| 37 |
colunas.remove('NS')
|
| 38 |
colunas.insert(7, 'NS')
|
| 39 |
gdf_m = gdf_m[colunas]
|
| 40 |
-
# Função de categorização
|
| 41 |
|
|
|
|
| 42 |
def categorizar_dies(dies):
|
| 43 |
if dies == 0:
|
| 44 |
return 1
|
|
@@ -51,40 +55,44 @@ def load_files(ponto_descarte=None):
|
|
| 51 |
|
| 52 |
gdf_m['Risco'] = gdf_m['DIEs'].apply(categorizar_dies)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
list_feature = df_plot.drop(columns='Risco').select_dtypes(include=['number']).columns
|
| 57 |
|
| 58 |
-
feat_options = []
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
return gdf_m,df_plot,list_feature, feat_options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
# load_process.py
|
| 3 |
+
import os
|
| 4 |
import pandas as pd
|
| 5 |
import geopandas as gpd
|
| 6 |
+
from download_data import get_data_file
|
| 7 |
+
#modify to freehun
|
| 8 |
|
| 9 |
+
def load_bairros():
|
| 10 |
+
return gpd.read_file(get_data_file("shape_bairros.gpkg"))
|
| 11 |
|
| 12 |
|
| 13 |
+
def load_coleta():
|
| 14 |
+
return gpd.read_file(get_data_file("shape_coleta.gpkg"))
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def load_descartes():
|
| 18 |
+
return gpd.read_file(get_data_file("Pontos_descartes_ML.gpkg"))
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def load_tabelas():
|
| 22 |
+
df1 = pd.read_csv(get_data_file("tabela_total_com_DIEs.csv"))
|
| 23 |
+
df2 = pd.read_csv(get_data_file("Bairros_Ncoleta.csv"))
|
| 24 |
+
return df1, df2
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def load_files(ponto_descarte=None):
|
| 28 |
+
# Leitura dos dados principais
|
| 29 |
+
gdf = gpd.read_file(get_data_file("shape_bairros.gpkg")).rename(columns={'NM_BAIRRO': 'Bairro'})
|
| 30 |
+
|
| 31 |
+
df1 = pd.read_csv(get_data_file("tabela_total_com_DIEs.csv"))
|
| 32 |
+
df2 = pd.read_csv(get_data_file("Bairros_Ncoleta.csv"))
|
| 33 |
+
|
| 34 |
+
# Merge das tabelas
|
| 35 |
df = df1.merge(df2, on='Bairro', how='left')
|
| 36 |
gdf_m = gdf.merge(df, on='Bairro', how='left')
|
| 37 |
|
| 38 |
+
# % de moradores sem renda
|
| 39 |
+
gdf_m['NS'] = (gdf_m['Mor'] - gdf_m['N_ren']) / gdf_m['Mor']
|
| 40 |
colunas = list(gdf_m.columns)
|
| 41 |
colunas.remove('NS')
|
| 42 |
colunas.insert(7, 'NS')
|
| 43 |
gdf_m = gdf_m[colunas]
|
|
|
|
| 44 |
|
| 45 |
+
# Classificação de risco
|
| 46 |
def categorizar_dies(dies):
|
| 47 |
if dies == 0:
|
| 48 |
return 1
|
|
|
|
| 55 |
|
| 56 |
gdf_m['Risco'] = gdf_m['DIEs'].apply(categorizar_dies)
|
| 57 |
|
| 58 |
+
# Dados para gráficos
|
| 59 |
+
df_plot = gdf_m.drop(columns=['geometry', 'V_setores_val'])
|
| 60 |
list_feature = df_plot.drop(columns='Risco').select_dtypes(include=['number']).columns
|
| 61 |
|
| 62 |
+
feat_options = [{'label': f, 'value': f, 'description': None} for f in list_feature]
|
| 63 |
+
|
| 64 |
+
descr = [
|
| 65 |
+
'Área do Bairro (km²)',
|
| 66 |
+
'Número Total de Habitações',
|
| 67 |
+
'Número Total de Moradores',
|
| 68 |
+
'Relação Moradores/Habitação',
|
| 69 |
+
'Número de Moradores com Renda',
|
| 70 |
+
'% de moradores sem renda',
|
| 71 |
+
'Renda média do Morador',
|
| 72 |
+
'Mediana da renda do Morador',
|
| 73 |
+
'Taxa de alfabetização',
|
| 74 |
+
'IDH Renda',
|
| 75 |
+
'IDH Longevidade',
|
| 76 |
+
'IDH Educação',
|
| 77 |
+
'Índice de Desenvolvimento Humano',
|
| 78 |
+
'Quantidade de Depósitos Irregulares',
|
| 79 |
+
'Concentração de riqueza por área',
|
| 80 |
+
'Percentual da população com rendimento',
|
| 81 |
+
'Quantidade estimada de Depósitos Irregulares',
|
| 82 |
+
'Média de dias de coleta de lixo',
|
| 83 |
+
'Quantidade de setores/rotas de coleta'
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
for i, d in enumerate(descr):
|
| 87 |
+
if i < len(feat_options):
|
| 88 |
+
feat_options[i]['description'] = d
|
| 89 |
+
|
| 90 |
+
if ponto_descarte:
|
| 91 |
+
gdf_p = gpd.read_file(
|
| 92 |
+
get_data_file("Pontos_descartes_ML.gpkg")
|
| 93 |
+
)
|
| 94 |
+
return gdf_m, df_plot, list_feature, feat_options, gdf_p
|
| 95 |
+
|
| 96 |
+
return gdf_m, df_plot, list_feature, feat_options
|
| 97 |
+
|
| 98 |
+
|