Spaces:
Sleeping
Sleeping
Delete zebris_extractor.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255import ioimport numpy as npimport pandas as pd
STANDARD_COLUMNS = [ "Nom", "Date", "Poids (kg)", "Vitesse (km/h)", "Cadence (pas/min)", "Contact (%)", "Flight (%)", "Force talon G (N)", "Force talon D (N)", "Force avant-pied G (N)", "Force avant-pied D (N)", "Pression talon G (N/cm²)", "Pression talon D (N/cm²)", "COP G (mm)", "COP D (mm)", "Rotation G (°)", "Rotation D (°)", "Transition G (s)", "Transition D (s)", "Longueur foulée (cm)", "Largeur pas (cm)",]
def _norm(text): return ( str(text) .strip() .lower() .replace("é", "e") .replace("è", "e") .replace("ê", "e") .replace("à", "a") .replace("ù", "u") .replace("ç", "c") .replace("²", "2") .replace("°", "") )
COLUMN_CANDIDATES = { "Nom": [ ("last name", "first name"), ("nom", "prenom"), ("last name", "first name "), ], "Date": [ "date", "recording date", "enregistrement", ], "Poids (kg)": [ "weight (kg)", "poids (kg)", "weight", "poids", ], "Vitesse (km/h)": [ "speed (km/h)", "vitesse (km/h)", "speed", "vitesse", ], "Cadence (pas/min)": [ "cadence (steps/min)", "cadence (pas/min)", "cadence", "cadence, pass/min", ], "Contact (%)": [ "total contact (%)", "contact (%)", "total contact", ], "Flight (%)": [ "total flight (%)", "flight (%)", "total flight", ], "Force talon G (N)": [ "heel (three zones) left force max (n)", "mean force heel left (n)", "heel left force max (n)", "heel left (n)", ], "Force talon D (N)": [ "heel (three zones) right force max (n)", "mean force heel right (n)", "heel right force max (n)", "heel right (n)", ], "Force avant-pied G (N)": [ "forefoot (three zones) left force max (n)", "mean force forefoot left (n)", "forefoot left force max (n)", "forefoot left (n)", ], "Force avant-pied D (N)": [ "forefoot (three zones) right force max (n)", "mean force forefoot right (n)", "forefoot right force max (n)", "forefoot right (n)", ], "Pression talon G (N/cm²)": [ "heel (three zones) left pressure max (n/cm2)", "mean pressure heel left (n/cm2)", "heel left pressure max (n/cm2)", ], "Pression talon D (N/cm²)": [ "heel (three zones) right pressure max (n/cm2)", "mean pressure heel right (n/cm2)", "heel right pressure max (n/cm2)", ], "COP G (mm)": [ "cop length left (mm)", "cop parameters running left length (mm)", "cop left (mm)", ], "COP D (mm)": [ "cop length right (mm)", "cop parameters running right length (mm)", "cop right (mm)", ], "Rotation G (°)": [ "foot rotation left (deg)", "foot rotation left", "rotation left", ], "Rotation D (°)": [ "foot rotation right (deg)", "foot rotation right", "rotation right", ], "Transition G (s)": [ "heel to forefoot transition left (s)", "transition left (s)", "instant du passage du talon vers l'avant-pied gauche (s)", ], "Transition D (s)": [ "heel to forefoot transition right (s)", "transition right (s)", "instant du passage du talon vers l'avant-pied droite (s)", ], "Longueur foulée (cm)": [ "stride length (cm)", "longueur de la foulee (cm)", "longueur de la foulee", ], "Largeur pas (cm)": [ "step width (cm)", "largeur du pas (cm)", "largeur du pas", ],}
def _to_numeric(series): return pd.to_numeric( series.astype(str).str.replace(",", ".", regex=False), errors="coerce" )
def _read_csv_flex(uploaded_file): raw = uploaded_file.read() uploaded_file.seek(0)
for encoding in ["utf-8-sig", "utf-8", "latin1", "cp1252"]: for sep in [";", ",", "\t"]: try: txt = raw.decode(encoding) df = pd.read_csv(io.StringIO(txt), sep=sep) if df.shape[1] > 1: return df except Exception: pass
# dernier essai naïf return pd.read_csv(uploaded_file)
def _find_column(df, candidates): normalized_cols = {_norm(c): c for c in df.columns} for cand in candidates: key = _norm(cand) if key in normalized_cols: return normalized_cols[key] return None
def extract_zebris_csv(uploaded_file): df = _read_csv_flex(uploaded_file)
out = pd.DataFrame(index=df.index) mapping_debug = {} manquantes = []
# Nom nom_done = False for cand_pair in COLUMN_CANDIDATES["Nom"]: if isinstance(cand_pair, tuple): c1 = _find_column(df, [cand_pair[0]]) c2 = _find_column(df, [cand_pair[1]]) if c1 and c2: out["Nom"] = df[c1].astype(str).str.strip() + " " + df[c2].astype(str).str.strip() mapping_debug["Nom"] = [c1, c2] nom_done = True break
if not nom_done: c = _find_column(df, ["nom"]) if c: out["Nom"] = df[c].astype(str) mapping_debug["Nom"] = c else: out["Nom"] = "Inconnu" manquantes.append("Nom")
# Autres colonnes for target in STANDARD_COLUMNS: if target == "Nom": continue
candidates = COLUMN_CANDIDATES.get(target, []) col = _find_column(df, candidates)
if col is None: out[target] = np.nan manquantes.append(target) else: mapping_debug[target] = col if target == "Date": out[target] = df[col] else: out[target] = _to_numeric(df[col])
# Garde lignes avec allure if "Vitesse (km/h)" in out.columns: out = out[out["Vitesse (km/h)"].notna()].copy()
out = out.reindex(columns=STANDARD_COLUMNS).reset_index(drop=True)
debug = { "mapping": mapping_debug, "manquantes": manquantes, "colonnes_csv": list(df.columns), }
return out, debugzebris_extractor.pyDeleted-255