| |
| """ |
| Prépare le dataset mobitag_audit pour publication sur Hugging Face. |
| |
| - Recalcule toutes les colonnes temporelles en heure locale NC (UTC+11) |
| - Split temporel dynamique : test = les TEST_MONTHS derniers mois complets |
| - Compression ZSTD niveau 3 |
| - Écrit dans data/train-*.parquet et data/test-*.parquet |
| |
| Usage : remplacer mobitag_audit.parquet et relancer le script. |
| """ |
|
|
| import pathlib |
| import pandas as pd |
| import pyarrow as pa |
| import pyarrow.parquet as pq |
| from dateutil.relativedelta import relativedelta |
|
|
| TEST_MONTHS = 6 |
| NC_UTC_OFFSET = 11 |
| INPUT_FILE = "mobitag_audit.parquet" |
| OUTPUT_DIR = pathlib.Path("data") |
|
|
| WRITE_OPTIONS = dict( |
| compression="zstd", |
| compression_level=3, |
| use_dictionary=True, |
| write_statistics=True, |
| row_group_size=100_000, |
| ) |
|
|
|
|
| def fix_types(df: pd.DataFrame) -> pd.DataFrame: |
| |
| local = df["date"] + pd.Timedelta(hours=NC_UTC_OFFSET) |
| df["hour"] = local.dt.hour.astype("int8") |
| df["day_name"] = local.dt.day_name() |
| df["year"] = local.dt.year.astype("int16") |
| df["month"] = local.dt.month.astype("int8") |
| df["year_month"] = local.dt.to_period("M").astype(str) |
| return df |
|
|
|
|
| def write_split(df: pd.DataFrame, path: pathlib.Path) -> None: |
| table = pa.Table.from_pandas(df, preserve_index=False) |
| pq.write_table(table, path, **WRITE_OPTIONS) |
| size_mb = path.stat().st_size / 1024 / 1024 |
| print(f" {path.name}: {len(df):,} lignes — {size_mb:.2f} MB") |
|
|
|
|
| def main() -> None: |
| print(f"Lecture de {INPUT_FILE}...") |
| df = pd.read_parquet(INPUT_FILE) |
| df = fix_types(df) |
|
|
| |
| |
| latest_local = df["date"].max() + pd.Timedelta(hours=NC_UTC_OFFSET) |
| local_month_start = latest_local.replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
| local_split = local_month_start - relativedelta(months=TEST_MONTHS) |
| split_date = local_split - pd.Timedelta(hours=NC_UTC_OFFSET) |
| print(f"Mois le plus récent : {latest_local.strftime('%Y-%m')} (heure locale NC)") |
| print(f"Split dynamique : train < {local_split.date()} local | test >= {local_split.date()} local") |
|
|
| train = df[df["date"] < split_date].copy() |
| test = df[df["date"] >= split_date].copy() |
|
|
| print(f" train : {len(train):,} lignes ({len(train)/len(df)*100:.1f}%)") |
| print(f" test : {len(test):,} lignes ({len(test)/len(df)*100:.1f}%)") |
|
|
| OUTPUT_DIR.mkdir(exist_ok=True) |
|
|
| print("\nÉcriture des fichiers...") |
| write_split(train, OUTPUT_DIR / "train-00000-of-00001.parquet") |
| write_split(test, OUTPUT_DIR / "test-00000-of-00001.parquet") |
|
|
| print("\nDone. Structure prête pour `huggingface-cli upload`.") |
| print(" → hf repos cp data/ opt-nc/mobitag --repo-type dataset") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|