Skip to content

Commit

Permalink
refactor: move schedule to fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Oct 8, 2024
1 parent b905142 commit 890375b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 38 deletions.
32 changes: 17 additions & 15 deletions rssched/app/pages/03_Depots.py → rssched/app/pages/01_Depots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@

from rssched.app.utils.io import get_uploaded_data
from rssched.app.utils.plot import plot_depots_bar_chart
from rssched.app.utils.transform import flatten_depots
from rssched.app.utils.transform import flatten_depots, get_vehicle_summary
from rssched.visualization.depot_loads import plot_depot_vehicle_loads

st.title("Depots")

request, response, instance_name = get_uploaded_data()

tabs = st.tabs(["Overview", "Details"])
tabs = st.tabs(["Overview", "Details", "Data"])

df_depots = flatten_depots(request, response)


with tabs[0]:
st.plotly_chart(plot_depots_bar_chart(df_depots))
st.plotly_chart(plot_depots_bar_chart(df_depots, instance_name))
st.markdown("### Vehicle Demand")
st.dataframe(get_vehicle_summary(response), hide_index=True)

show_aggregated = st.checkbox("Aggregate", value=True)
with tabs[1]:
selected_depot: str = st.selectbox(
"Choose depot to see detailed statistics",
df_depots[df_depots["vehicles"] > 0]["depot_id"].unique(),
)
st.plotly_chart(
plot_depot_vehicle_loads(request, response, instance_name, selected_depot)
)
st.dataframe(df_depots[df_depots["depot_id"] == selected_depot], hide_index=True)

with tabs[2]:
show_aggregated = st.checkbox("Aggregate", value=False)

if show_aggregated:
df_grouped = (
Expand All @@ -29,14 +42,3 @@
st.dataframe(df_grouped, hide_index=True)
else:
st.dataframe(df_depots, hide_index=True)


with tabs[1]:
selected_depot: str = st.selectbox(
"Choose depot to see detailed statistics",
df_depots[df_depots["vehicles"] > 0]["depot_id"].unique(),
)
st.plotly_chart(
plot_depot_vehicle_loads(request, response, instance_name, selected_depot)
)
st.dataframe(df_depots[df_depots["depot_id"] == selected_depot], hide_index=True)
16 changes: 0 additions & 16 deletions rssched/app/pages/01_Schedule.py

This file was deleted.

16 changes: 13 additions & 3 deletions rssched/app/pages/02_Fleet.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
from typing import Any

import streamlit as st

from rssched.app.utils.io import get_uploaded_data
from rssched.visualization.active_events import plot_active_events_over_time
from rssched.visualization.fleet_efficiency import plot_fleet_efficiency
from rssched.visualization.vehicle_type_gantt import plot_gantt_per_vehicle_type
from rssched.visualization.vehicle_utilization import plot_vehicle_utilization

st.title("Fleet")

request, response, instance_name = get_uploaded_data()


tabs = st.tabs(["Active Events", "Vehicle Utilization", "Efficiency"])
tabs = st.tabs(
["Vehicle Circuits", "Active Events", "Vehicle Utilization", "Efficiency"]
)

with tabs[0]:
st.plotly_chart(plot_active_events_over_time(response, instance_name))
plots = plot_gantt_per_vehicle_type(response, instance_name)
selected_vehicle_type: str = st.selectbox("Choose vehicle type", plots.keys())
st.plotly_chart(plots[selected_vehicle_type])

with tabs[1]:
st.plotly_chart(plot_vehicle_utilization(response, instance_name))
st.plotly_chart(plot_active_events_over_time(response, instance_name))

with tabs[2]:
st.plotly_chart(plot_vehicle_utilization(response, instance_name))

with tabs[3]:
for fig in plot_fleet_efficiency(response, instance_name):
st.plotly_chart(fig)
4 changes: 2 additions & 2 deletions rssched/app/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from plotly.graph_objs import Figure


def plot_depots_bar_chart(df_depots: pd.DataFrame) -> Figure:
def plot_depots_bar_chart(df_depots: pd.DataFrame, instance_name: str) -> Figure:
# Group by depot_id to calculate the total capacity and sort by the number of vehicles
df_depots_grouped = (
df_depots[df_depots["vehicles"] > 0]
Expand Down Expand Up @@ -38,7 +38,7 @@ def plot_depots_bar_chart(df_depots: pd.DataFrame) -> Figure:
# Update layout of the bar chart
fig.update_layout(
barmode="group", # Bars grouped side by side
title="Depot Vehicles and Capacity",
title=f"Depot Usage (Instance: {instance_name})",
xaxis_title="Depot ID",
# yaxis_title="Count",
legend_title="Legend",
Expand Down
21 changes: 21 additions & 0 deletions rssched/app/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ def get_response_summary(response: Response) -> pd.DataFrame:
)


def get_vehicle_summary(response: Response):
data = []

for depot_load in response.schedule.depot_loads:
depot = depot_load.depot
for load in depot_load.load:
vehicle_type = load.vehicle_type
spawn_count = load.spawn_count
data.append(
{"location": depot, "vehicle_type": vehicle_type, "count": spawn_count}
)

return (
pd.DataFrame(data)
.groupby("vehicle_type")["count"]
.sum()
.sort_values()
.reset_index()
)


def flatten_depots(request: Request, response: Response) -> pd.DataFrame:
df_request = _flatten_request_depots(request.depots)
df_response = _flatten_response_depot_loads(response.schedule.depot_loads)
Expand Down
7 changes: 5 additions & 2 deletions rssched/visualization/vehicle_type_gantt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import plotly.figure_factory as ff
import plotly.graph_objects as go

from rssched.model.response import Response
from rssched.visualization.colors import EVENT_TYPES


def plot_gantt_per_vehicle_type(response: Response, instance_name: str) -> dict:
def plot_gantt_per_vehicle_type(
response: Response, instance_name: str
) -> dict[str, go.Figure]:
figures = {}

for vehicle_type in response.schedule.fleet:
Expand Down Expand Up @@ -39,7 +42,7 @@ def plot_gantt_per_vehicle_type(response: Response, instance_name: str) -> dict:
)
figures[vehicle_type.vehicle_type] = ff.create_gantt(
vis_data,
title=f"Rolling stock schedule: {vehicle_type.vehicle_type} (instance: {instance_name})",
title=f"Vehicle Circuits '{vehicle_type.vehicle_type}' (instance: {instance_name})",
colors=EVENT_TYPES,
index_col="Type",
show_colorbar=True,
Expand Down

0 comments on commit 890375b

Please sign in to comment.