File size: 13,589 Bytes
2f1e493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import numpy as np
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt

st.set_page_config(page_title="Fiche biomécanique Zebris", layout="wide")

REQUIRED_COLUMNS = {
    "nom": ["Nom"],
    "date": ["Date"],
    "vitesse": ["Vitesse (km/h)", "Vitesse"],
    "cadence": ["Cadence (pas/min)", "Cadence"],
    "contact": ["Contact (%)", "Total contact", "Total contact (%)"],
    "flight": ["Flight (%)", "Total flight", "Total flight (%)"],
    "force_talon_g": ["Force talon G (N)"],
    "force_talon_d": ["Force talon D (N)"],
    "force_avant_g": ["Force avant-pied G (N)", "Force avant pied G (N)"],
    "force_avant_d": ["Force avant-pied D (N)", "Force avant pied D (N)"],
    "pression_talon_g": ["Pression talon G (N/cm²)", "Pression talon G (N/cm2)"],
    "pression_talon_d": ["Pression talon D (N/cm²)", "Pression talon D (N/cm2)"],
    "cop_g": ["COP G (mm)"],
    "cop_d": ["COP D (mm)"],
    "rotation_g": ["Rotation G (°)", "Rotation G"],
    "rotation_d": ["Rotation D (°)", "Rotation D"],
    "transition_g": ["Transition G (s)"],
    "transition_d": ["Transition D (s)"],
    "longueur_foulee": ["Longueur foulée (cm)", "Longueur foulee (cm)"],
    "largeur_pas": ["Largeur pas (cm)"],
    "poids": ["Poids (kg)", "Poids"],
}

SAMPLE_DF = pd.DataFrame(
    [
        {
            "Nom": "Baptiste PRETOT",
            "Date": "03/03/2026",
            "Vitesse (km/h)": 8,
            "Cadence (pas/min)": 154,
            "Contact (%)": 74.5,
            "Flight (%)": 25.5,
            "Force talon G (N)": 520,
            "Force talon D (N)": 500,
            "Force avant-pied G (N)": 450,
            "Force avant-pied D (N)": 470,
            "Pression talon G (N/cm²)": 22,
            "Pression talon D (N/cm²)": 21,
            "COP G (mm)": 230,
            "COP D (mm)": 228,
            "Rotation G (°)": 4,
            "Rotation D (°)": 11,
            "Transition G (s)": 0.08,
            "Transition D (s)": 0.08,
            "Longueur foulée (cm)": 180,
            "Largeur pas (cm)": 4,
            "Poids (kg)": 70,
        },
        {
            "Nom": "Baptiste PRETOT",
            "Date": "03/03/2026",
            "Vitesse (km/h)": 14,
            "Cadence (pas/min)": 167,
            "Contact (%)": 71.8,
            "Flight (%)": 28.2,
            "Force talon G (N)": 638.7,
            "Force talon D (N)": 563.6,
            "Force avant-pied G (N)": 577.8,
            "Force avant-pied D (N)": 613.4,
            "Pression talon G (N/cm²)": 26.3,
            "Pression talon D (N/cm²)": 25.4,
            "COP G (mm)": 242.6,
            "COP D (mm)": 235.3,
            "Rotation G (°)": 4.6,
            "Rotation D (°)": 16.0,
            "Transition G (s)": 0.07,
            "Transition D (s)": 0.06,
            "Longueur foulée (cm)": 280,
            "Largeur pas (cm)": 3,
            "Poids (kg)": 70,
        },
        {
            "Nom": "Baptiste PRETOT",
            "Date": "03/03/2026",
            "Vitesse (km/h)": 18,
            "Cadence (pas/min)": 176,
            "Contact (%)": 69.2,
            "Flight (%)": 30.8,
            "Force talon G (N)": 700,
            "Force talon D (N)": 650,
            "Force avant-pied G (N)": 690,
            "Force avant-pied D (N)": 710,
            "Pression talon G (N/cm²)": 28,
            "Pression talon D (N/cm²)": 27,
            "COP G (mm)": 248,
            "COP D (mm)": 243,
            "Rotation G (°)": 5,
            "Rotation D (°)": 17,
            "Transition G (s)": 0.06,
            "Transition D (s)": 0.05,
            "Longueur foulée (cm)": 330,
            "Largeur pas (cm)": 3,
            "Poids (kg)": 70,
        },
    ]
)

def normalize_header(value):
    return (
        str(value)
        .strip()
        .lower()
        .replace("é", "e")
        .replace("è", "e")
        .replace("ê", "e")
        .replace("à", "a")
        .replace("ù", "u")
        .replace("ç", "c")
        .replace("²", "2")
    )

def resolve_column(df, candidates):
    normalized = {normalize_header(c): c for c in df.columns}
    for candidate in candidates:
        key = normalize_header(candidate)
        if key in normalized:
            return normalized[key]
    return None

def standardize_dataframe(df):
    out = pd.DataFrame()
    missing = []

    for target, candidates in REQUIRED_COLUMNS.items():
        col = resolve_column(df, candidates)
        if col is None:
            if target == "poids":
                out[target] = np.nan
                continue
            missing.append(candidates[0])
            continue
        out[target] = df[col]

    if missing:
        st.error("Colonnes manquantes : " + ", ".join(missing))
        st.stop()

    for col in out.columns:
        if col not in ["nom", "date"]:
            out[col] = pd.to_numeric(out[col], errors="coerce")

    return out.dropna(subset=["nom", "vitesse"]).reset_index(drop=True)

def avg(a, b):
    return (float(a) + float(b)) / 2

def asym(a, b):
    m = avg(a, b)
    if m == 0:
        return 0.0
    return abs(float(a) - float(b)) / m * 100

def clamp_score(value, low, high, reverse=False):
    if pd.isna(value):
        return 0
    score = (value - low) / (high - low) * 100
    score = max(0, min(100, score))
    return 100 - score if reverse else score

def compute_metrics(row, poids_override):
    poids_n = poids_override * 9.81 if poids_override else np.nan

    force_talon_moy = avg(row["force_talon_g"], row["force_talon_d"])
    force_avant_moy = avg(row["force_avant_g"], row["force_avant_d"])
    pression_moy = avg(row["pression_talon_g"], row["pression_talon_d"])
    cop_moy = avg(row["cop_g"], row["cop_d"])
    transition_moy = avg(row["transition_g"], row["transition_d"])

    asym_talon = asym(row["force_talon_g"], row["force_talon_d"])
    asym_avant = asym(row["force_avant_g"], row["force_avant_d"])
    asym_cop = asym(row["cop_g"], row["cop_d"])
    diff_rotation = abs(row["rotation_g"] - row["rotation_d"])

    force_talon_bw = force_talon_moy / poids_n if poids_n and not pd.isna(poids_n) else np.nan
    ratio_talon_avant = force_talon_moy / force_avant_moy if force_avant_moy else np.nan

    impact = round(
        0.6 * clamp_score(force_talon_bw if not pd.isna(force_talon_bw) else force_talon_moy,
                          0.6 if not pd.isna(force_talon_bw) else 400,
                          1.1 if not pd.isna(force_talon_bw) else 750)
        + 0.4 * clamp_score(pression_moy, 15, 30)
    )

    dynamique = round(
        0.6 * clamp_score(row["cadence"], 150, 185)
        + 0.4 * clamp_score(row["contact"], 68, 76, reverse=True)
    )

    symetrie = round(100 - min(100, (asym_talon + asym_avant + asym_cop + diff_rotation) * 2.5))

    technique = round(
        0.5 * clamp_score(cop_moy, 210, 260)
        + 0.5 * clamp_score(transition_moy, 0.05, 0.09, reverse=True)
    )

    attaque = "mixte"
    if ratio_talon_avant > 1.05:
        attaque = "talon"
    elif ratio_talon_avant < 0.95:
        attaque = "avant-pied"

    return {
        "impact": impact,
        "dynamique": dynamique,
        "symetrie": symetrie,
        "technique": technique,
        "attaque": attaque,
        "force_talon_moy": force_talon_moy,
        "force_talon_bw": force_talon_bw,
        "asym_talon": asym_talon,
        "cop_moy": cop_moy,
        "diff_rotation": diff_rotation,
    }

def build_summary(row, metrics):
    impact_txt = "marqué" if metrics["impact"] >= 70 else "modéré" if metrics["impact"] >= 45 else "faible"
    dyn_txt = "bonne" if metrics["dynamique"] >= 70 else "moyenne" if metrics["dynamique"] >= 45 else "faible"
    sym_txt = "satisfaisante" if metrics["symetrie"] >= 70 else "perfectible"
    tech_txt = "efficace" if metrics["technique"] >= 70 else "à surveiller"
    return (
        f"À {row['vitesse']} km/h, {row['nom']} présente une attaque {metrics['attaque']}, "
        f"un impact {impact_txt}, une dynamique {dyn_txt}, une symétrie {sym_txt} "
        f"et un déroulé du pied {tech_txt}."
    )

def draw_radar(metrics):
    labels = ["Impact", "Dynamique", "Symétrie", "Technique"]
    values = [metrics["impact"], metrics["dynamique"], metrics["symetrie"], metrics["technique"]]
    values += values[:1]
    angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
    angles += angles[:1]

    fig = plt.figure(figsize=(5, 5))
    ax = plt.subplot(111, polar=True)
    ax.plot(angles, values, linewidth=2)
    ax.fill(angles, values, alpha=0.25)
    ax.set_xticks(angles[:-1])
    ax.set_xticklabels(labels)
    ax.set_ylim(0, 100)
    ax.set_yticks([25, 50, 75, 100])
    ax.set_title("Radar biomécanique", pad=20)
    return fig

def draw_evolution(df, poids_override):
    data = []
    for _, row in df.sort_values("vitesse").iterrows():
        m = compute_metrics(row, poids_override)
        data.append({
            "Vitesse": row["vitesse"],
            "Impact": m["impact"],
            "Dynamique": m["dynamique"],
            "Symétrie": m["symetrie"],
            "Technique": m["technique"],
        })

    evo = pd.DataFrame(data)
    fig, ax = plt.subplots(figsize=(8, 4))
    for col in ["Impact", "Dynamique", "Symétrie", "Technique"]:
        ax.plot(evo["Vitesse"], evo[col], marker="o", label=col)
    ax.set_ylim(0, 100)
    ax.set_xlabel("Vitesse (km/h)")
    ax.set_ylabel("Score /100")
    ax.set_title("Évolution avec l’allure")
    ax.legend()
    ax.grid(True, alpha=0.3)
    return fig

st.title("Fiche biomécanique Zebris")
st.caption("Version test — mode démo + import Excel standardisé")

with st.sidebar:
    st.header("Mode")
    mode_demo = st.toggle("Utiliser le jeu de données de démonstration", value=True)
    uploaded_file = None
    if not mode_demo:
        uploaded_file = st.file_uploader(
            "Importer un fichier .xlsx, .xls ou .csv",
            type=["xlsx", "xls", "csv"],
            accept_multiple_files=False,
        )
        if uploaded_file is not None and uploaded_file.size > 5 * 1024 * 1024:
            st.error("Fichier trop volumineux (>5 MB)")
            st.stop()

try:
    if mode_demo:
        df = standardize_dataframe(SAMPLE_DF)
    else:
        if uploaded_file is None:
            st.info("Importe un fichier ou active le mode démonstration.")
            st.stop()
        if uploaded_file.name.lower().endswith(".csv"):
            raw_df = pd.read_csv(uploaded_file)
        else:
            raw_df = pd.read_excel(uploaded_file)
        df = standardize_dataframe(raw_df)
except Exception as e:
    st.error(f"Erreur de lecture du fichier : {e}")
    st.stop()

athletes = sorted(df["nom"].dropna().unique().tolist())
selected_athlete = st.selectbox("Sportif", athletes)
sub_df = df[df["nom"] == selected_athlete].sort_values("vitesse")

allures = sub_df["vitesse"].tolist()
selected_speed = st.selectbox("Allure analysée (km/h)", allures)
row = sub_df[sub_df["vitesse"] == selected_speed].iloc[0]

poids_default = row["poids"] if pd.notna(row["poids"]) else 70.0
poids_override = st.number_input("Poids du sportif (kg)", min_value=0.0, value=float(poids_default), step=0.1)

metrics = compute_metrics(row, poids_override)
summary = build_summary(row, metrics)

c1, c2, c3, c4 = st.columns(4)
with c1:
    st.metric("Impact", f"{metrics['impact']}/100")
with c2:
    st.metric("Dynamique", f"{metrics['dynamique']}/100")
with c3:
    st.metric("Symétrie", f"{metrics['symetrie']}/100")
with c4:
    st.metric("Technique", f"{metrics['technique']}/100")

left, right = st.columns([1.2, 1])

with left:
    st.subheader("Carte d’identité biomécanique")
    st.write(summary)

    indicators = pd.DataFrame(
        {
            "Indicateur": [
                "Cadence",
                "Contact",
                "Flight",
                "Force talon moyenne",
                "Force talon normalisée",
                "Asymétrie talon",
                "COP moyen",
                "Différence rotation",
                "Attaque",
            ],
            "Valeur": [
                f"{row['cadence']:.1f} pas/min",
                f"{row['contact']:.1f} %",
                f"{row['flight']:.1f} %",
                f"{metrics['force_talon_moy']:.1f} N",
                f"{metrics['force_talon_bw']:.2f} BW" if not pd.isna(metrics['force_talon_bw']) else "N/A",
                f"{metrics['asym_talon']:.1f} %",
                f"{metrics['cop_moy']:.1f} mm",
                f"{metrics['diff_rotation']:.1f}°",
                metrics["attaque"],
            ],
        }
    )
    st.dataframe(indicators, hide_index=True, use_container_width=True)

    st.subheader("Points d’attention")
    if metrics["impact"] >= 70:
        st.warning("Contraintes d’impact à surveiller")
    if metrics["symetrie"] < 55:
        st.warning("Asymétrie fonctionnelle à contrôler")
    if metrics["technique"] < 55:
        st.warning("Déroulé / transition à surveiller")
    if metrics["attaque"] == "avant-pied":
        st.warning("Charge distale potentiellement plus élevée")
    if not (
        metrics["impact"] >= 70
        or metrics["symetrie"] < 55
        or metrics["technique"] < 55
        or metrics["attaque"] == "avant-pied"
    ):
        st.success("Aucun point d’attention majeur sur cette allure.")

with right:
    st.subheader("Radar biomécanique")
    st.pyplot(draw_radar(metrics), use_container_width=True)

st.subheader("Évolution avec l’allure")
st.pyplot(draw_evolution(sub_df, poids_override), use_container_width=True)