From aecc0a2d30f8436b11a10ee8dbabc60d5ac2d32e Mon Sep 17 00:00:00 2001 From: alephcero Date: Thu, 14 Nov 2024 10:24:59 -0300 Subject: [PATCH 1/4] Fixes #181 --- urbantrips/run_interactive_tools.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/urbantrips/run_interactive_tools.py b/urbantrips/run_interactive_tools.py index eb1738c..9b8cb1e 100644 --- a/urbantrips/run_interactive_tools.py +++ b/urbantrips/run_interactive_tools.py @@ -31,6 +31,7 @@ def cargar_tabla_sql(tabla_sql, tipo_conexion="dash", query=""): st.session_state.configs = utils.leer_configs_generales() configs = st.session_state.configs +h3_legs_res = configs["resolucion_h3"] alias = configs["alias_db_data"] use_branches = configs["lineas_contienen_ramales"] metadata_lineas = cargar_tabla_sql("metadata_lineas", "insumos")[ @@ -123,7 +124,9 @@ def seleccionar_linea(nombre_columna, key_input, key_select, branch_key, conn_in col1, col2, col3 = st.columns([1, 3, 3]) with col1: - h3_res_comp = st.slider("Resolución H3", min_value=7, max_value=9, value=8) + h3_res_comp = st.slider( + "Resolución H3", min_value=7, max_value=h3_legs_res, value=h3_legs_res + ) if st.button("Comparar líneas"): for i in [1, 2]: From b4abe0868433d930e89da1f0c7935d1a8db963d2 Mon Sep 17 00:00:00 2001 From: alephcero Date: Thu, 14 Nov 2024 14:40:05 -0300 Subject: [PATCH 2/4] Handle errors en overlapping --- urbantrips/kpi/overlapping.py | 22 +++++- urbantrips/run_interactive_tools.py | 103 +++++++++++++++++----------- urbantrips/viz/overlapping.py | 9 ++- 3 files changed, 88 insertions(+), 46 deletions(-) diff --git a/urbantrips/kpi/overlapping.py b/urbantrips/kpi/overlapping.py index 793ac83..de241f9 100644 --- a/urbantrips/kpi/overlapping.py +++ b/urbantrips/kpi/overlapping.py @@ -349,11 +349,27 @@ def compute_supply_overlapping( # Crate linestring for each branch base_route_gdf = route_geoms.loc[route_geoms.route_id == base_route_id, "geometry"] - linestring_base = base_route_gdf.item() - # Crate linestring for each branch comp_route_gdf = route_geoms.loc[route_geoms.route_id == comp_route_id, "geometry"] - linestring_comp = comp_route_gdf.item() + + if (len(base_route_gdf) == 0) or (len(comp_route_gdf) == 0): + error_str = ( + "No es posible la comparación de oferta para esta combinación de rutas. " + ) + if len(base_route_gdf) == 0: + error_str += f"Ruta base {base_route_id} no encontrada. " + if len(comp_route_gdf) == 0: + error_str += f"Ruta comp {comp_route_id} no encontrada. " + return { + "base": {"line": None, "h3": None}, + "comp": {"line": None, "h3": None}, + "text_base_v_comp": error_str, + "text_comp_v_base": error_str, + } + + else: + linestring_base = base_route_gdf.item() + linestring_comp = comp_route_gdf.item() # Turn linestring into coarse h3 indexes base_h3 = create_coarse_h3_from_line( diff --git a/urbantrips/run_interactive_tools.py b/urbantrips/run_interactive_tools.py index 9b8cb1e..38e0c1c 100644 --- a/urbantrips/run_interactive_tools.py +++ b/urbantrips/run_interactive_tools.py @@ -212,61 +212,82 @@ def seleccionar_linea(nombre_columna, key_input, key_select, branch_key, conn_in f = ovl_viz.plot_interactive_supply_overlapping(overlapping_dict) # Muestra la salida solo en col1 with col1: - folium_static(f, width=800, height=600) - st.write( - st.session_state[f"supply_overlapping_{base_route_id}_{comp_route_id}"] - ) - st.write( - st.session_state[f"supply_overlapping_{comp_route_id}_{base_route_id}"] - ) + if f is not None: + folium_static(f, width=800, height=600) + st.write( + st.session_state[ + f"supply_overlapping_{base_route_id}_{comp_route_id}" + ] + ) + st.write( + st.session_state[ + f"supply_overlapping_{comp_route_id}_{base_route_id}" + ] + ) + else: + st.error(overlapping_dict["text_base_v_comp"]) # Cálculo y visualización de la demanda, si no se ha realizado previamente if ( f"base_demand_comp_demand_{base_route_id}_{comp_route_id}" not in st.session_state ): - demand_overlapping = ovl.compute_demand_overlapping( - st.session_state.id_linea_1, - st.session_state.id_linea_2, - "weekday", - base_route_id, - comp_route_id, - overlapping_dict["base"]["h3"], - overlapping_dict["comp"]["h3"], - ) - st.session_state[ - f"base_demand_comp_demand_{base_route_id}_{comp_route_id}" - ] = demand_overlapping + base_gdf = overlapping_dict["base"]["h3"] + comp_gdf = overlapping_dict["comp"]["h3"] + if (base_gdf is not None) and (comp_gdf is not None): + demand_overlapping = ovl.compute_demand_overlapping( + st.session_state.id_linea_1, + st.session_state.id_linea_2, + "weekday", + base_route_id, + comp_route_id, + base_gdf, + comp_gdf, + ) + st.session_state[ + f"base_demand_comp_demand_{base_route_id}_{comp_route_id}" + ] = demand_overlapping - st.session_state[f"demand_overlapping_{base_route_id}_{comp_route_id}"] = ( - demand_overlapping["base"]["output_text"] - ) - st.session_state[f"demand_overlapping_{comp_route_id}_{base_route_id}"] = ( - demand_overlapping["comp"]["output_text"] - ) + st.session_state[ + f"demand_overlapping_{base_route_id}_{comp_route_id}" + ] = demand_overlapping["base"]["output_text"] + st.session_state[ + f"demand_overlapping_{comp_route_id}_{base_route_id}" + ] = demand_overlapping["comp"]["output_text"] + else: + st.session_state[ + f"base_demand_comp_demand_{base_route_id}_{comp_route_id}" + ] = None demand_overlapping = st.session_state[ f"base_demand_comp_demand_{base_route_id}_{comp_route_id}" ] - base_demand = demand_overlapping["base"]["data"] - comp_demand = demand_overlapping["comp"]["data"] # Renderiza el segundo mapa y muestra el texto justo después del mapa en col2 - demand_overlapping_fig = ovl_viz.plot_interactive_demand_overlapping( - base_demand, comp_demand, overlapping_dict - ) - fig = demand_overlapping_fig["fig"] - base_gdf_to_db = demand_overlapping_fig["base_gdf_to_db"] - comp_gdf_to_db = demand_overlapping_fig["comp_gdf_to_db"] + if demand_overlapping is not None: + base_demand = demand_overlapping["base"]["data"] + comp_demand = demand_overlapping["comp"]["data"] + + demand_overlapping_fig = ovl_viz.plot_interactive_demand_overlapping( + base_demand, comp_demand, overlapping_dict + ) + fig = demand_overlapping_fig["fig"] + base_gdf_to_db = demand_overlapping_fig["base_gdf_to_db"] + comp_gdf_to_db = demand_overlapping_fig["comp_gdf_to_db"] + + with col2: + folium_static(fig, width=800, height=600) + st.write( + st.session_state[ + f"demand_overlapping_{base_route_id}_{comp_route_id}" + ] + ) # Muestra la segunda salida justo después del mapa + st.write( + st.session_state[ + f"demand_overlapping_{comp_route_id}_{base_route_id}" + ] + ) # Muestra la segunda salida justo después del mapa - with col2: - folium_static(fig, width=800, height=600) - st.write( - st.session_state[f"demand_overlapping_{base_route_id}_{comp_route_id}"] - ) # Muestra la segunda salida justo después del mapa - st.write( - st.session_state[f"demand_overlapping_{comp_route_id}_{base_route_id}"] - ) # Muestra la segunda salida justo después del mapa with st.expander("Exportar datos", expanded=True): col1_db, col2_db = st.columns([2, 2]) diff --git a/urbantrips/viz/overlapping.py b/urbantrips/viz/overlapping.py index 9820ff2..8be8e0a 100644 --- a/urbantrips/viz/overlapping.py +++ b/urbantrips/viz/overlapping.py @@ -30,8 +30,13 @@ def get_route_metadata(route_id): def plot_interactive_supply_overlapping(overlapping_dict): - base_route_id = overlapping_dict["base"]["h3"].route_id.unique()[0] - comp_route_id = overlapping_dict["comp"]["h3"].route_id.unique()[0] + base_h3 = overlapping_dict["base"]["h3"] + comp_h3 = overlapping_dict["comp"]["h3"] + if (base_h3 is None) or (comp_gdf is None): + return None + + base_route_id = base_h3.route_id.unique()[0] + comp_route_id = comp_h3.overlapping_dict["comp"]["h3"].route_id.unique()[0] base_route_metadata = get_route_metadata(base_route_id) comp_route_metadata = get_route_metadata(comp_route_id) From 4f04f70fba17614f9760ded07488b7ecbf2176ea Mon Sep 17 00:00:00 2001 From: alephcero Date: Thu, 14 Nov 2024 14:41:00 -0300 Subject: [PATCH 3/4] Updates overlapping notebooks --- notebooks/Overlapping.ipynb | 56 ++++++++++++++----------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/notebooks/Overlapping.ipynb b/notebooks/Overlapping.ipynb index f31db77..b53daa5 100644 --- a/notebooks/Overlapping.ipynb +++ b/notebooks/Overlapping.ipynb @@ -36,7 +36,7 @@ "# Se leen los datos de las lineas\n", "metadata_lineas = pd.read_sql(\"select id_linea,nombre_linea, modo from metadata_lineas;\", conn_insumos)\n", "# Se puede buscar por nombre de linea que contenga alguna palabra o numero\n", - "metadata_lineas[metadata_lineas.nombre_linea.str.contains(\"68\") #reemplazar 17 por lo que se desee buscar en el nombre de la linea\n", + "metadata_lineas[metadata_lineas.nombre_linea.str.contains(\"17\") #reemplazar 17 por lo que se desee buscar en el nombre de la linea\n", " ]" ] }, @@ -90,7 +90,7 @@ "outputs": [], "source": [ "# get routes from the combination\n", - "base_route_id, comp_route_id = ovl.get_route_ids_from_combination(base_line_id, comp_line_id, route_id_combination)\n" + "base_route_id, comp_route_id = ovl.get_route_ids_from_combination(base_line_id, comp_line_id, route_id_combination)" ] }, { @@ -112,53 +112,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Se visualiza la superposicion de la oferta de la linea base con la de la linea de comparacion\n", "f = ovl_viz.plot_interactive_supply_overlapping(overlapping_dict)\n", - "f.save(f\"resultados/html/{alias}_supply_overlapping_base_{base_route_id}_comp_{comp_route_id}_h3_{h3_res_comp}.html\")\n", + "if f is not None:\n", + " f.save(f\"resultados/html/{alias}_supply_overlapping_base_{base_route_id}_comp_{comp_route_id}_h3_{h3_res_comp}.html\")\n", "f" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Calcula la demanda de la linea base y la de comparacion\n", - "demand_overlapping = ovl.compute_demand_overlapping(base_line_id,comp_line_id,day,\n", - " base_route_id,comp_route_id,\n", - " base_gdf,comp_gdf)\n", + "if (base_gdf is not None) and (comp_gdf is not None):\n", + " # Calcula la demanda de la linea base y la de comparacion\n", + " demand_overlapping = ovl.compute_demand_overlapping(base_line_id,comp_line_id,day,\n", + " base_route_id,comp_route_id,\n", + " base_gdf,comp_gdf)\n", "\n", - "base_demand = demand_overlapping[\"base\"][\"data\"]\n", - "comp_demand = demand_overlapping[\"comp\"][\"data\"]\n", - "print(demand_overlapping[\"base\"][\"output_text\"])\n", - "print(demand_overlapping[\"comp\"][\"output_text\"])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Se visualiza la superposicion de la demanda de la linea base con la de la linea de comparacion\n", - "demand_overlapping_fig = ovl_viz.plot_interactive_demand_overlapping(base_demand, comp_demand, overlapping_dict)\n", - "fig = demand_overlapping_fig['fig']\n", - "fig.save(f\"resultados/html/{alias}_demand_overlapping_base_{base_route_id}_comp_{comp_route_id}_h3_{h3_res_comp}.html\")\n", - "fig" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "demand_overlapping_fig['comp_gdf_to_db']" + " base_demand = demand_overlapping[\"base\"][\"data\"]\n", + " comp_demand = demand_overlapping[\"comp\"][\"data\"]\n", + " print(demand_overlapping[\"base\"][\"output_text\"])\n", + " print(demand_overlapping[\"comp\"][\"output_text\"])\n", + "\n", + " # Se visualiza la superposicion de la demanda de la linea base con la de la linea de comparacion\n", + " demand_overlapping_fig = ovl_viz.plot_interactive_demand_overlapping(base_demand, comp_demand, overlapping_dict)\n", + " fig = demand_overlapping_fig['fig']\n", + " fig.save(f\"resultados/html/{alias}_demand_overlapping_base_{base_route_id}_comp_{comp_route_id}_h3_{h3_res_comp}.html\")\n", + " fig" ] }, { From 6411c95d35bf6bb8db239bcc72e7892508512c5f Mon Sep 17 00:00:00 2001 From: alephcero Date: Thu, 14 Nov 2024 15:12:17 -0300 Subject: [PATCH 4/4] Fixes typo --- urbantrips/viz/overlapping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/urbantrips/viz/overlapping.py b/urbantrips/viz/overlapping.py index 8be8e0a..faec06c 100644 --- a/urbantrips/viz/overlapping.py +++ b/urbantrips/viz/overlapping.py @@ -32,7 +32,7 @@ def plot_interactive_supply_overlapping(overlapping_dict): base_h3 = overlapping_dict["base"]["h3"] comp_h3 = overlapping_dict["comp"]["h3"] - if (base_h3 is None) or (comp_gdf is None): + if (base_h3 is None) or (comp_h3 is None): return None base_route_id = base_h3.route_id.unique()[0]