File size: 3,799 Bytes
413f321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20b3f03
413f321
 
 
 
20b3f03
413f321
 
 
20b3f03
 
 
413f321
 
20b3f03
 
 
 
413f321
 
 
 
 
20b3f03
413f321
 
 
20b3f03
 
413f321
 
 
 
20b3f03
413f321
 
 
 
 
 
 
c2b84ec
413f321
20b3f03
413f321
 
 
 
 
20b3f03
413f321
 
 
 
 
 
 
 
 
 
c2b84ec
413f321
20b3f03
413f321
 
20b3f03
271d100
413f321
 
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
87
88
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)