Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev test02 #180

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 162 additions & 83 deletions urbantrips/dashboard/dash_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,60 +354,124 @@ def create_data_folium(etapas,
agg_hora=False,
agg_distancia=False,
agg_cols_etapas=[],
agg_cols_viajes=[]):

etapas = calculate_weighted_means_ods(etapas,
agg_cols_etapas,
['distance_osm_drive', 'lat1_norm', 'lon1_norm', 'lat2_norm',
'lon2_norm', 'lat3_norm', 'lon3_norm', 'lat4_norm', 'lon4_norm'],

'factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
zero_to_nan=['lat1_norm', 'lon1_norm', 'lat2_norm', 'lon2_norm', 'lat3_norm', 'lon3_norm', 'lat4_norm', 'lon4_norm'])

etapas[['lat1_norm',
'lon1_norm',
'lat2_norm',
'lon2_norm',
'lat3_norm',
'lon3_norm',
'lat4_norm',
'lon4_norm']] = etapas[['lat1_norm',
'lon1_norm',
'lat2_norm',
'lon2_norm',
'lat3_norm',
'lon3_norm',
'lat4_norm',
'lon4_norm']].fillna(0)

viajes = calculate_weighted_means_ods(etapas,
agg_cols_viajes,
['distance_osm_drive',
'lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm'],
'factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
zero_to_nan=['lat1_norm', 'lon1_norm', 'lat4_norm', 'lon4_norm'])
viajes[['lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm']] = viajes[['lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm']].fillna(0)

if 'id_polygon' not in viajes_matrices.columns:
viajes_matrices['id_polygon'] = 'NONE'
agg_cols_viajes=[],
desc_etapas = True,
desc_viajes = True,
desc_origenes = True,
desc_destinos = True,
desc_transferencias = False):


if desc_transferencias:

t1 = etapas.loc[etapas.transfer1_norm!='', ['zona',
'transfer1_norm',
'lat2_norm',
'lon2_norm',
'transferencia',
'modo_agregado',
'rango_hora',
'distancia',
'factor_expansion_linea',]].rename(columns={'transfer1_norm':'transfer', 'lat2_norm':'lat_norm', 'lon2_norm':'lon_norm'})
t2 = etapas.loc[etapas.transfer2_norm!='', ['zona',
'transfer2_norm',
'lat3_norm',
'lon3_norm',
'transferencia',
'modo_agregado',
'rango_hora',
'distancia',
'factor_expansion_linea',]].rename(columns={'transfer2_norm':'transfer', 'lat3_norm':'lat_norm', 'lon3_norm':'lon_norm'})
transferencias = pd.concat([t1,t2], ignore_index=True)
transferencias['id_polygon'] = 'NONE'

trans_cols_o = ['id_polygon',
'zona',
'transfer',
'transferencia',
'modo_agregado',
'rango_hora',
'distancia']

transferencias = creo_bubble_od(transferencias,
aggregate_cols=trans_cols_o,
weighted_mean_cols=['lat_norm', 'lon_norm'],
weight_col='factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
od='transfer',
lat='lat_norm',
lon='lon_norm')
else:
transferencias = pd.DataFrame([])

if desc_etapas | desc_transferencias:
etapas = calculate_weighted_means_ods(etapas,
agg_cols_etapas,
['distance_osm_drive', 'lat1_norm', 'lon1_norm', 'lat2_norm',
'lon2_norm', 'lat3_norm', 'lon3_norm', 'lat4_norm', 'lon4_norm'],
'factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
zero_to_nan=['lat1_norm', 'lon1_norm', 'lat2_norm', 'lon2_norm', 'lat3_norm', 'lon3_norm', 'lat4_norm', 'lon4_norm'])

etapas[['lat1_norm',
'lon1_norm',
'lat2_norm',
'lon2_norm',
'lat3_norm',
'lon3_norm',
'lat4_norm',
'lon4_norm']] = etapas[['lat1_norm',
'lon1_norm',
'lat2_norm',
'lon2_norm',
'lat3_norm',
'lon3_norm',
'lat4_norm',
'lon4_norm']].fillna(0)

etapas = df_to_linestrings(etapas,
lat_cols=['lat1_norm', 'lat2_norm', 'lat3_norm', 'lat4_norm'], lon_cols=['lon1_norm', 'lon2_norm', 'lon3_norm', 'lon4_norm'])

etapas = etapas[etapas.inicio_norm != etapas.fin_norm].copy()

if desc_viajes:
viajes = calculate_weighted_means_ods(etapas,
agg_cols_viajes,
['distance_osm_drive',
'lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm'],
'factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
zero_to_nan=['lat1_norm', 'lon1_norm', 'lat4_norm', 'lon4_norm'])
viajes[['lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm']] = viajes[['lat1_norm',
'lon1_norm',
'lat4_norm',
'lon4_norm']].fillna(0)

if 'id_polygon' not in viajes_matrices.columns:
viajes_matrices['id_polygon'] = 'NONE'

viajes = df_to_linestrings(viajes,
lat_cols=['lat1_norm', 'lat4_norm'], lon_cols=['lon1_norm', 'lon4_norm'])

viajes = viajes[viajes.inicio_norm != viajes.fin_norm].copy()
else:
viajes = pd.DataFrame([])

matriz = agg_matriz(viajes_matrices,
aggregate_cols=['id_polygon', 'zona', 'Origen', 'Destino',
'transferencia', 'modo_agregado', 'rango_hora', 'distancia'],
Expand All @@ -429,37 +493,41 @@ def create_data_folium(etapas,
bubble_cols_d = ['id_polygon', 'zona', 'fin',
'transferencia', 'modo_agregado', 'rango_hora', 'distancia']

origen = creo_bubble_od(viajes_matrices,
aggregate_cols=bubble_cols_o,
weighted_mean_cols=['lat1', 'lon1'],
weight_col='factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
od='inicio',
lat='lat1',
lon='lon1')

destino = creo_bubble_od(viajes_matrices,
aggregate_cols=bubble_cols_d,
weighted_mean_cols=['lat4', 'lon4'],
weight_col='factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
od='fin',
lat='lat4',
lon='lon4')

etapas = df_to_linestrings(etapas,
lat_cols=['lat1_norm', 'lat2_norm', 'lat3_norm', 'lat4_norm'], lon_cols=['lon1_norm', 'lon2_norm', 'lon3_norm', 'lon4_norm'])
if desc_origenes:
origen = creo_bubble_od(viajes_matrices,
aggregate_cols=bubble_cols_o,
weighted_mean_cols=['lat1', 'lon1'],
weight_col='factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
od='inicio',
lat='lat1',
lon='lon1')
else:
origen = pd.DataFrame([])

if desc_destinos:
destino = creo_bubble_od(viajes_matrices,
aggregate_cols=bubble_cols_d,
weighted_mean_cols=['lat4', 'lon4'],
weight_col='factor_expansion_linea',
agg_transferencias=agg_transferencias,
agg_modo=agg_modo,
agg_hora=agg_hora,
agg_distancia=agg_distancia,
od='fin',
lat='lat4',
lon='lon4')
else:
destino = pd.DataFrame([])

if not desc_etapas:
etapas = pd.DataFrame([])

viajes = df_to_linestrings(viajes,
lat_cols=['lat1_norm', 'lat4_norm'], lon_cols=['lon1_norm', 'lon4_norm'])

return etapas, viajes, matriz, origen, destino
return etapas, viajes, matriz, origen, destino, transferencias


@st.cache_data
Expand Down Expand Up @@ -518,4 +586,15 @@ def extract_hex_colors_from_cmap(cmap, n=5):
# Convert the colors to hex format
hex_colors = [mcolors.rgb2hex(color) for color in colors]

return hex_colors
return hex_colors

@st.cache_data
def bring_latlon():
try:
latlon = levanto_tabla_sql('agg_etapas', 'dash', 'SELECT lat1_norm, lon1_norm FROM agg_etapas ORDER BY RANDOM() LIMIT 100;')
lat = latlon['lat1_norm'].mean()
lon = latlon['lon1_norm'].mean()
latlon = [lat, lon]
except:
latlon = [-34.593, -58.451]
return latlon
Loading
Loading