File size: 2,349 Bytes
14191f5 1e370bc 14191f5 3a2ec5a b20ccd0 14191f5 b20ccd0 14191f5 a89372c b20ccd0 e5dc41c b20ccd0 a89372c 14191f5 b20ccd0 14191f5 b20ccd0 138a1ea b20ccd0 138a1ea 859a678 b20ccd0 14191f5 b20ccd0 14191f5 9fe787e 7eccd43 14191f5 138a1ea 14191f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 | from dash import Dash, dcc, html, Input, Output
import pandas as pd
import numpy as np
import plotly.express as px
import folium
from folium.plugins import MarkerCluster
from branca.element import Figure
import polyline
def get_segment(df , lId):
df = df[df['id'].isin(lId)]
df["polyline"] = df["map_pol"].apply(polyline.decode)
return df["polyline"].tolist()
df_seg = pd.read_csv("segments.csv")
df_tsne = pd.read_csv("tnse_vect_2d.csv")
# app
app = Dash(__name__)
server = app.server # req Hugging Face
scatter_fig = px.scatter(
df_tsne,
x="x",
y="y",
hover_name="item",
hover_data=["item"],
custom_data=["item"],
title="t-SNE visualization Segments Embeddings"
)
scatter_fig.update_layout(dragmode='lasso')
app.layout = html.Div([
html.H3("Select Segment(Embeddings) to visualize them on a Map(location)"),
html.Div([
dcc.Graph(id="scatter", figure=scatter_fig, style={"width": "50%", "height": "500px"}),
html.Iframe(id="folium_map", width="50%", height="500")
], style={"display": "flex", "justify-content": "space-between"}),
html.Div(id="output", style={"marginTop": "20px"})
])
@app.callback(
Output("output", "children"),
Output("folium_map", "srcDoc"),
Input("scatter", "selectedData")
)
def actualizar_mapa(selection):
if not selection or "points" not in selection:
return "Select at least one point.", ""
try:
ids = [p['customdata'][0] for p in selection['points']]
except (KeyError, TypeError):
# Fallback por si no llega customdata
ids = [int(p['hovertext']) for p in selection['points']]
global df_seg
lSegments = get_segment(df_seg , ids)
m = folium.Map(location=[-0.19204405740703162, -78.47700359958313], zoom_start=10)
color_list = [
'red', 'blue', 'green', 'orange', 'purple', 'darkred',
'cadetblue', 'darkgreen', 'pink', 'black'
]
for idx, route in enumerate(lSegments):
color = color_list[idx % len(color_list)]
folium.PolyLine(
locations=route,
color=color,
weight=5,
opacity=0.8
).add_to(m)
fig = Figure()
fig.add_child(m)
mapa_html = m.get_root().render()
return f"IDs : {ids}" , mapa_html
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|