AntOG96's picture
Update src/utils/common.py
c2b84ec verified
Raw History Blame Contribute Delete
3.8 kB
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
DATA_URL = ("https://full-stack-assets.s3.eu-west-3.amazonaws.com/Deployment/get_around_delay_analysis.xlsx")
## LOAD DATA
@st.cache_data
def load_data():
data = pd.read_excel(DATA_URL)
# Outliers delay_at_checkout_in_minutes
valeur_palier_haut = data["delay_at_checkout_in_minutes"].mean() + 3 * data["delay_at_checkout_in_minutes"].std()
valeur_palier_bas = data["delay_at_checkout_in_minutes"].mean() - 3 * data["delay_at_checkout_in_minutes"].std()
outlier_condition = (data["delay_at_checkout_in_minutes"] > valeur_palier_haut) | (data["delay_at_checkout_in_minutes"] < valeur_palier_bas)
data = data[~outlier_condition]
# Extracting the checkout delay for each rental
delay_checkout = data[["rental_id", "delay_at_checkout_in_minutes"]].rename(
columns={"rental_id": "previous_ended_rental_id", "delay_at_checkout_in_minutes": "previous_delay_at_checkout_in_minutes"}
)
# Joining it to the following rental to determine the checkout delay of the previous rental
data = data.merge(delay_checkout, on="previous_ended_rental_id", how="left")
# We create an 'overlap' column representing the potential overlap between two locations: time_delta - delay
# The more negative the overlap, the more severe the overlap.
# The more positive it is, the greater the safety margin.
data["overlap"] = data["time_delta_with_previous_rental_in_minutes"] - data["previous_delay_at_checkout_in_minutes"]
# Issues
# problem = True = The previous booking overlapped with this one (conflict).
# problem = False = No timing issue. The car was ready on time.
data["problem"] = data["overlap"] < 0
return data
## THRESHOLD VIZ
def plot_checkin_thresholds(data, thresholds, column, y_label, title, percentage=False):
fig = go.Figure()
# Threshold selection with a slider
selected_threshold = st.slider("Choose a threshold (minutes)",
min_value=int(thresholds[0]),
max_value=int(thresholds[-1]), step=1,
key=f"slider_{title}")
# Total curve
total_values = []
for threshold in thresholds:
total_count = ((data[column] <= threshold) & (data[column] >= 0)).sum()
if percentage:
total_count = total_count / data.shape[0] * 100
total_values.append(total_count)
fig.add_trace(go.Scatter(x=thresholds, y=total_values, mode="lines+markers", name="Total", hovertemplate="Threshold: %{x}<br>Value: %{y:.2f}"))
# Curves by check-in type
for checkin_type in data["checkin_type"].unique():
values = []
mask_checkin = (data["checkin_type"] == checkin_type)
for threshold in thresholds:
# Number of at-risk bookings, but whose overlap can be absorbed by the threshold.
count = (mask_checkin & (data[column] <= threshold) & (data[column] >= 0)).sum()
if percentage:
count = count / data.shape[0] * 100
values.append(count)
ratios = [x*100/y for x, y in zip(values, total_values)]
fig.add_trace(go.Scatter(x=thresholds, y=values, mode="lines+markers", name=checkin_type,
customdata=np.array(ratios).reshape(-1, 1),
hovertemplate="Threshold: %{x}<br>Value: %{y:.2f}<br>Percentage compared to Total: %{customdata[0]:.2f}"))
# Vertical line
fig.add_vline(x=selected_threshold, line_dash="dash", line_color="red")
# Layout
fig.update_layout(title=title, xaxis_title="Threshold (minutes)", yaxis_title=y_label)
st.plotly_chart(fig, use_container_width=True)