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

Fixes #181 #182

Merged
merged 4 commits into from
Nov 14, 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
56 changes: 21 additions & 35 deletions notebooks/Overlapping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
" ]"
]
},
Expand Down Expand Up @@ -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)"
]
},
{
Expand All @@ -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"
]
},
{
Expand Down
22 changes: 19 additions & 3 deletions urbantrips/kpi/overlapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
108 changes: 66 additions & 42 deletions urbantrips/run_interactive_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")[
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -209,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])
Expand Down
9 changes: 7 additions & 2 deletions urbantrips/viz/overlapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_h3 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)

Expand Down
Loading