File size: 3,885 Bytes
ea04ecb
b0e8a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10980e
b0e8a64
 
 
 
 
 
 
 
 
 
 
 
 
ea04ecb
8112f0b
 
 
ea04ecb
 
 
 
 
 
 
 
 
 
d10980e
8112f0b
b0e8a64
 
 
 
 
 
 
 
 
 
 
 
 
ea04ecb
b0e8a64
ea04ecb
 
d10980e
b0e8a64
8112f0b
 
 
b0e8a64
 
 
 
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]

    # On extrait le délai de checkout pour chaque location
    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"}
    )

    # On le joint à la location suivante : afin d'avoir le délai de checkout de la location précédente
    data = data.merge(delay_checkout, on="previous_ended_rental_id", how="left")

    
    # Nous créons une colonne overlap qui représente le chevauchement potentiel entre deux locations : time_delta - delay
    # Plus overlap est négatif, plus le chevauchement est sévère.
    # Plus il est positif, plus il y a de marge de sécurité.
    data["overlap"] = data["time_delta_with_previous_rental_in_minutes"] - data["previous_delay_at_checkout_in_minutes"]

    # Problèmes
    # problem = True	= La location précédente a empiété sur celle-ci (chevauchement).
    # problem = False	= Aucun problème de timing. La voiture était prête à temps.
    data["problem"] = data["overlap"] < 0

    return data


## VISUALISATION DES SEUILS
def plot_checkin_thresholds(data, thresholds, column, y_label, title, percentage=False):
    fig = go.Figure()

    # Sélection du seuil via un slider
    selected_threshold = st.slider("Choisir un seuil (minutes)", 
                                   min_value=int(thresholds[0]), 
                                   max_value=int(thresholds[-1]), step=1,
                                   key=f"slider_{title}")

    # Courbe totale
    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="Seuil: %{x}<br>Valeur: %{y:.2f}"))

    # Courbes par type de checkin
    for checkin_type in data["checkin_type"].unique():
        values = []
        mask_checkin = (data["checkin_type"] == checkin_type)

        for threshold in thresholds:
            # Nombre de locations à risque, mais dont le chevauchement est absorbable par le seuil.
            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="Seuil: %{x}<br>Valeur: %{y:.2f}<br>Pourcentage par rapport au Total: %{customdata[0]:.2f}"))

    # Ligne verticale
    fig.add_vline(x=selected_threshold, line_dash="dash", line_color="red")

    # Mise en page
    fig.update_layout(title=title, xaxis_title="Seuil (minutes)", yaxis_title=y_label)

    st.plotly_chart(fig, use_container_width=True)