Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
chore: refactor for new api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
d116626 committed Feb 15, 2024
1 parent d2316e0 commit 7277411
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 141 deletions.
178 changes: 88 additions & 90 deletions app/Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,107 +49,105 @@ def fetch_and_update_data(bypass_cash=False):
cameras = fetch_and_update_data(bypass_cash=True)
st.success("Data updated successfully!")

cameras_attr, cameras_identifications = treat_data(cameras)
col1, col2 = st.columns(2)

with col1:
objects = cameras_identifications["object"].unique().tolist()
objects.sort()
# dropdown to filter by object
object_filter = st.selectbox(
"Filtrar por objeto",
objects,
index=objects.index(DEFAULT_OBJECT),
)
cameras_identifications = treat_data(cameras)

if len(cameras_identifications) > 0:

with col2:
labels = (
cameras_identifications[
cameras_identifications["object"] == object_filter
][ # noqa
"label"
]
.dropna()
.unique()
.tolist()
)
labels_default = labels.copy()

# if object_filter == "road_blockade":
# labels_default.remove("normal")
# dropdown to select label given selected object
label_filter = st.multiselect(
"Filtrar por label",
labels,
# if object_filter return minor and major, else return all labels
default=labels_default,
)


(
cameras_identifications_merged,
cameras_filter,
cameras_identifications_filter,
) = get_filted_cameras_objects(
cameras_attr, cameras_identifications, object_filter, label_filter
)
col1, col2 = st.columns(2)


# make two cols
col1, col2 = st.columns(2)
folium_map = create_map(cameras_identifications_merged.reset_index())

with col1:
selected_cols = [
"index",
"id",
"object",
"label",
"timestamp",
"snapshot_timestamp",
"bairro",
"old_snapshot",
]
aggrid_table = cameras_identifications_merged.reset_index()

aggrid_table["index"] = aggrid_table["label"].apply(
lambda label: get_icon_color(label=label, type="emoji")
)
aggrid_table = aggrid_table[selected_cols]
# aggrid_table = aggrid_table[selected_cols]
st.markdown("### 📈 Identificações")
selected_row = display_agrid_table(aggrid_table) # noqa

with col2:
if selected_row:
camera_id = selected_row[0]["id"]
row = cameras_filter.loc[camera_id]

camera_location = [row["latitude"], row["longitude"]]
folium_map = create_map(
cameras_identifications_merged.reset_index(),
location=camera_location, # noqa
with col1:
objects = cameras_identifications["object"].unique().tolist()
objects.sort()
# dropdown to filter by object
object_filter = st.selectbox(
"Filtrar por objeto",
objects,
index=objects.index(DEFAULT_OBJECT),
)

display_camera_details(
row=row, cameras_identifications=cameras_identifications
) # noqa
else:
st.markdown(
"""
### 📷 Câmera snapshot
Selecione uma Câmera na tabela para visualizar mais detalhes.
"""
with col2:
labels = (
cameras_identifications[
cameras_identifications["object"] == object_filter
][ # noqa
"label"
]
.dropna()
.unique()
.tolist()
)
labels_default = labels.copy()

# if object_filter == "road_blockade":
# labels_default.remove("normal")
# dropdown to select label given selected object
label_filter = st.multiselect(
"Filtrar por label",
labels,
# if object_filter return minor and major, else return all labels
default=labels_default,
)

with col1:
st.markdown("### 📍 Mapa")
st_folium(folium_map, key="fig1", height=600, width="100%")
cameras_identifications_filter = get_filted_cameras_objects(
cameras_identifications, object_filter, label_filter
)
# make two cols
col1, col2 = st.columns(2)
folium_map = create_map(cameras_identifications_filter)

with col1:
selected_cols = [
"index",
"id",
"object",
"label",
"timestamp",
"snapshot_timestamp",
"bairro",
]
aggrid_table = cameras_identifications_filter.copy()
aggrid_table["index"] = aggrid_table["label"].apply(
lambda label: get_icon_color(label=label, type="emoji")
)
aggrid_table = aggrid_table[selected_cols]
# aggrid_table = aggrid_table[selected_cols]
st.markdown("### 📈 Identificações")
selected_row = display_agrid_table(aggrid_table) # noqa

with col2:
if selected_row:
camera_id = selected_row[0]["id"]
row = cameras_identifications_filter[
cameras_identifications_filter["id"] == camera_id
]
# get first row
row = row.head(1).to_dict("records")[0]
camera_location = [row["latitude"], row["longitude"]]
folium_map = create_map(
cameras_identifications_filter,
location=camera_location, # noqa
)

display_camera_details(
row=row, cameras_identifications=cameras_identifications
) # noqa
else:
st.markdown(
"""
### 📷 Câmera snapshot
Selecione uma Câmera na tabela para visualizar mais detalhes.
"""
)

with col1:
st.markdown("### 📍 Mapa")
st_folium(folium_map, key="fig1", height=600, width="100%")

# for camera_id in cameras_identifications_filter.index:
# row = cameras_filter.loc[camera_id]
# display_camera_details(
# row=row, cameras_identifications=cameras_identifications
# )
# time.sleep(2)
else:
st.error("No cameras with identifications")
98 changes: 48 additions & 50 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Union

import folium
import numpy as np
import pandas as pd
import streamlit as st
from st_aggrid import GridOptionsBuilder # noqa
Expand Down Expand Up @@ -137,29 +136,27 @@ def get_prompts_cache(page_size=100, timeout=120):

def treat_data(response):
cameras_aux = pd.read_csv("./data/database/cameras_aux.csv", dtype=str)
cameras_aux = cameras_aux.rename(columns={"id_camera": "id"}).set_index(
"id"
) # noqa
# To dataframe
cameras = pd.DataFrame(response).set_index("id")

cameras_aux = cameras_aux.rename(columns={"id_camera": "camera_id"})
cameras = pd.DataFrame(response)
cameras = cameras.rename(columns={"id": "camera_id"})
cameras = cameras[cameras["identifications"].apply(lambda x: len(x) > 0)]
cameras["snapshot_timestamp"] = pd.to_datetime(
cameras["snapshot_timestamp"]
).dt.tz_convert("America/Sao_Paulo")
if len(cameras) == 0:
return None, None
cameras = cameras.merge(cameras_aux, on="camera_id", how="left")

cameras = cameras.sort_values(by=["snapshot_timestamp"])
cameras = cameras.merge(cameras_aux, on="id", how="left")
cameras_attr = cameras[
[
"camera_id",
"bairro",
"subprefeitura",
"name",
# "rtsp_url",
# "update_interval",
"latitude",
"longitude",
"snapshot_url",
"snapshot_timestamp",
"identifications",
# "snapshot_url",
# "id_h3",
# "id_bolsao",
# "bolsao_latitude",
Expand All @@ -170,19 +167,38 @@ def treat_data(response):
# "geometry_bolsao_buffer_0.002",
]
]
exploded_df = cameras.explode("identifications")
cameras_identifications = pd.DataFrame(
exploded_df["identifications"].tolist(), index=exploded_df.index

cameras_identifications_explode = explode_df(
cameras_attr, "identifications"
) # noqa

cameras_identifications_explode = cameras_identifications_explode.rename(
columns={"id": "object_id"}
).rename(columns={"camera_id": "id"})
cameras_identifications_explode = cameras_identifications_explode.rename(
columns={
"snapshot.id": "snapshot_id",
"snapshot.camera_id": "snapshot_camera_id",
"snapshot.image_url": "snapshot_url",
"snapshot.timestamp": "snapshot_timestamp",
}
)

cameras_identifications["timestamp"] = pd.to_datetime(
cameras_identifications["timestamp"]
cameras_identifications_explode["timestamp"] = pd.to_datetime(
cameras_identifications_explode["timestamp"]
).dt.tz_convert("America/Sao_Paulo")

cameras_identifications_explode["snapshot_timestamp"] = pd.to_datetime(
cameras_identifications_explode["snapshot_timestamp"]
).dt.tz_convert("America/Sao_Paulo")
cameras_identifications = cameras_identifications.sort_values(
["timestamp", "label"], ascending=False

cameras_identifications_explode = (
cameras_identifications_explode.sort_values( # noqa
["timestamp", "label"], ascending=False
)
)

return cameras_attr, cameras_identifications
return cameras_identifications_explode


def explode_df(dataframe, column_to_explode, prefix=None):
Expand Down Expand Up @@ -212,40 +228,20 @@ def get_objetcs_labels_df(objects, keep_null=False):


def get_filted_cameras_objects(
cameras_attr, cameras_identifications, object_filter, label_filter
):
cameras_identifications, object_filter, label_filter
): # noqa
# filter both dfs by object and label
cameras_filter = cameras_attr[
cameras_attr.index.isin(
cameras_identifications[
cameras_identifications["object"] == object_filter
].index
)
]

cameras_identifications_filter = cameras_identifications[
(cameras_identifications["object"] == object_filter)
& (cameras_identifications["label"].isin(label_filter))
]

# show cameras dfs
cameras_identifications_merged = pd.merge(
cameras_filter, cameras_identifications_filter, on="id"
)
cameras_identifications_merged = cameras_identifications_merged.sort_values( # noqa
cameras_identifications_filter = cameras_identifications_filter.sort_values( # noqa
by=["timestamp", "label"], ascending=False
)
cameras_identifications_merged["old_snapshot"] = np.where(
cameras_identifications_merged["snapshot_timestamp"]
> cameras_identifications_merged["timestamp"],
"Sim",
"Não",
)
return (
cameras_identifications_merged,
cameras_filter,
cameras_identifications_filter,
)

return cameras_identifications_filter


def get_icon_color(label: Union[bool, None], type=None):
Expand Down Expand Up @@ -335,10 +331,10 @@ def create_map(chart_data, location=None):


def display_camera_details(row, cameras_identifications):
camera_id = row.name
camera_id = row["id"]
image_url = row["snapshot_url"]
camera_name = row["name"]
snapshot_timestamp = row["snapshot_timestamp"].strftime("%d/%m/%Y %H:%M") # noqa
# snapshot_timestamp = row["snapshot_timestamp"].strftime("%d/%m/%Y %H:%M") # noqa

st.markdown(f"### 📷 Camera snapshot") # noqa
st.markdown(f"Endereço: {camera_name}")
Expand All @@ -355,8 +351,10 @@ def display_camera_details(row, cameras_identifications):
st.markdown("<br>", unsafe_allow_html=True)

st.markdown("### 📃 Detalhes")
camera_identifications = cameras_identifications[
cameras_identifications["id"] == camera_id
] # noqa

camera_identifications = cameras_identifications.loc[camera_id] # noqa
camera_identifications = camera_identifications.reset_index(drop=True)

camera_identifications[""] = camera_identifications["label"].apply(
Expand Down Expand Up @@ -411,7 +409,7 @@ def display_agrid_table(table):
wrapText=True,
hide=True,
)
gb.configure_column("old_snapshot", header_name="Predição Desatualizada")
# gb.configure_column("old_snapshot", header_name="Predição Desatualizada")
gb.configure_side_bar()
gb.configure_selection("single", use_checkbox=False)
gb.configure_grid_options(enableCellTextSelection=True)
Expand Down
2 changes: 1 addition & 1 deletion data/temp/mock_api_data.json

Large diffs are not rendered by default.

0 comments on commit 7277411

Please sign in to comment.