Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineTSP committed Apr 17, 2024
2 parents fc8d0c7 + db639c9 commit b1e4cfc
Showing 1 changed file with 79 additions and 12 deletions.
91 changes: 79 additions & 12 deletions utils/viz.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import utils.stat as stat

from IPython.display import display, Markdown

def graphePerso(prenom, nom, data, titre):
densite=stat.dens(data['duration'], bins = stat.idealBins(len(data['duration'])))
fcubic=stat.lissage(densite, sep = True,beginend = (data['duration'].min(),data['duration'].max()))
z=data.index[data.Athlète==(nom.upper()+" "+prenom.capitalize())]
temps = data.loc[z[0],'duration']
x= np.linspace(data['duration'].min(),data['duration'].max(), int(1e5))

def graphePerso(prenom, nom, data, titre, nb_bins=10_000):
times_in_seconds = pd.to_datetime(data.h_duration, format='%H:%M:%S').dt.time.apply(
lambda time: 3600*time.hour+60*time.minute+time.second)
bins = np.linspace(times_in_seconds.iloc[0], times_in_seconds.iloc[-1], nb_bins)
cdf = [(times_in_seconds <= bin_).sum()/len(times_in_seconds) for bin_ in bins]

z = data.index[data['Athlète'] == (nom.upper() + " " + prenom.capitalize())]
temps = data.loc[z[0], 'duration']
cdf_proportion = (times_in_seconds <= temps).sum() / len(times_in_seconds)

fig = go.Figure()

fig.add_trace(go.Scatter(
x=x, y=fcubic(x),
x=bins, y=cdf,
fill='tozeroy',
mode='lines',
mode='lines',
line_color='blue',
name='densité lissée & interpolée'
name='Distribution des temps'
))

fig.add_trace(go.Scatter(
x=[temps, temps], y=[0, max(fcubic(x))],
x=[temps, temps], y=[0, max(cdf)],
mode='lines',
line_color='red',
name=nom.upper()+" "+prenom.capitalize()
))

fig.add_shape(
type="line",
x0=bins[0],
y0=cdf_proportion,
x1=temps,
y1=cdf_proportion,
line=dict(
color="black",
width=1,
dash="dashdot",
),
)

fig.add_annotation(
x=temps,
y=cdf_proportion,
text=f"<b>{100*cdf_proportion:.2f}%</b>",
font=dict(size=14),
showarrow=True,
arrowhead=7,
ax=0,
ay=-30,
)

fig.update_layout(
title=titre,
xaxis_title="Durée pour franchir la ligne d'arrivée",
Expand All @@ -41,9 +68,49 @@ def graphePerso(prenom, nom, data, titre):

fig.show()


def display_header(header):
display(Markdown(f"**Compétition:** {header['nom']}"))
display(Markdown(f"**Lieu:** {header['lieu']}"))
display(Markdown(f"**Date:** {header['date']}"))
display(Markdown(f"**Dept:** {header['dept']}"))
display(Markdown(f"**Label:** {header['label']}"))
display(Markdown(f"**Label:** {header['label']}"))


def display_podium(data):
data.h_duration = pd.to_datetime(data.h_duration, format='%H:%M:%S').dt.time
podium = data.sort_values(by='h_duration').iloc[[2, 0, 1]]

fig = go.Figure()

fig.add_trace(go.Bar(
y=[1, 3, 2],
x=podium['Athlète'],
orientation='v',
marker=dict(
color=['#614e1a', '#a57c00', '#c7d1da'], # Couleur du podium (bronze, gold, silver)
line=dict(color='black', width=1) # Couleur de la bordure
)
))

for i, annotation_text in enumerate(['🥉', '🥇', '🥈']):
fig.add_annotation(
x=podium.iloc[i]['Athlète'],
y=[1.2, 3.2, 2.2][i],
text=annotation_text,
showarrow=False,
font=dict(size=70)
)

fig.update_layout(
title="Podium",
yaxis_title="Durée",
yaxis=dict(
tickvals=[1, 2, 3],
ticktext=podium['h_duration'].sort_values(ascending=False)
),
xaxis=dict(
tickfont=dict(size=16)
)
)
fig.show()

0 comments on commit b1e4cfc

Please sign in to comment.