Skip to content

Commit

Permalink
Merge pull request #246 from openclimatefix/Adding-map-on-the-dashboard
Browse files Browse the repository at this point in the history
Update to add map on the dashboard
  • Loading branch information
ADIMANV authored Jan 10, 2025
2 parents b5dbfa5 + f8b0bc6 commit 1f0ea67
Showing 1 changed file with 77 additions and 8 deletions.
85 changes: 77 additions & 8 deletions src/mlmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import pandas as pd
import streamlit as st
import plotly.express as px
from pvsite_datamodel.connection import DatabaseConnection
from pvsite_datamodel.read.model import get_models
from pvsite_datamodel.read.site import get_all_sites
from pvsite_datamodel.sqlmodels import GenerationSQL, SiteSQL


def color_survived(val):
now = pd.Timestamp.utcnow()
color = (
Expand All @@ -22,7 +22,7 @@ def color_survived(val):


def mlmodel_page():
"""Main page for pvsite forecast"""
"""Main page for PV Site Forecast"""

st.markdown(
f'<h1 style="color:#63BCAF;font-size:48px;">{"Site: ML Models"}</h1>',
Expand All @@ -34,11 +34,11 @@ def mlmodel_page():

with connection.get_session() as session:

# 1. Display the sites, and which models they are using
# add tick box to show all details
# 1. Display the sites and which models they are using
# Add tick box to show all details
show_all_sites = st.checkbox("Display all site parameters")

# load all sites
# Load all sites
sites = get_all_sites(session)

all_sites = []
Expand All @@ -47,14 +47,13 @@ def mlmodel_page():

if show_all_sites:
site_dict = {k: getattr(site, k) for k in keys if not k.startswith("_")}

else:
site_dict = {"client_site_name": site.client_site_name}

if site.ml_model is not None:
site_dict["ml_model_name"] = site.ml_model.name

# get last generation timestamp
# Get last generation timestamp
last_gen = (
session.query(GenerationSQL)
.filter(GenerationSQL.site_uuid == site.site_uuid)
Expand All @@ -70,7 +69,7 @@ def mlmodel_page():

all_sites = pd.DataFrame(all_sites)

# order by name
# Order by name
all_sites = all_sites.sort_values(by="client_site_name")

st.table(all_sites.style.applymap(color_survived, subset=["last_generation_datetime"]))
Expand All @@ -87,3 +86,73 @@ def mlmodel_page():

st.write("ML Models")
st.write(all_models)

# 3. Show site locations on the map
st.subheader("Site Locations on Map")

# Prepare site details for the map
site_details = []
for site in sites:
site_dict = {
"client_site_name": site.client_site_name,
"latitude": getattr(site, "latitude", None),
"longitude": getattr(site, "longitude", None),
"region": site.region,
"capacity_kw": site.capacity_kw,
"asset_type": str(site.asset_type),
}
if site_dict["latitude"] and site_dict["longitude"]: # Ensure latitude and longitude exist
site_details.append(site_dict)

# Convert to DataFrame
map_data = pd.DataFrame(site_details)

# Check if there is valid map data
if not map_data.empty:
# Sidebar filter for regions
regions = ["All"] + sorted(map_data["region"].dropna().unique().tolist())
selected_region = st.sidebar.selectbox("Select Region", regions)

# Filter map data by selected region
if selected_region != "All":
map_data = map_data[map_data["region"] == selected_region]

# Assign marker color based on asset type
map_data["color"] = map_data["asset_type"].apply(
lambda x: "orange" if x == "SiteAssetType.pv" else "blue"
)

# Display map using Plotly Express
fig = px.scatter_mapbox(
map_data,
lat="latitude",
lon="longitude",
color="asset_type",
size="capacity_kw",
hover_name="client_site_name",
hover_data={
"capacity_kw": True,
"region": True,
"latitude": False,
"longitude": False,
},
color_discrete_map={"SiteAssetType.pv": "orange", "SiteAssetType.wind": "blue"},
zoom=4,
height=600,
)

fig.update_layout(
mapbox_style="carto-positron",
legend_title_text="Asset Type",
margin={"r": 0, "t": 0, "l": 0, "b": 0},
)
st.plotly_chart(fig, use_container_width=True)

# Display site details in a table
st.subheader("Site Geographical Details")
st.dataframe(
map_data[["client_site_name", "region", "capacity_kw", "asset_type", "latitude", "longitude"]],
use_container_width=True,
)
else:
st.write("No valid site data available to display on the map.")

0 comments on commit 1f0ea67

Please sign in to comment.