Download benchmarks/scripts/transform2sequential.py from yandex/yambda: direct link, hf CLI and curl.
- Browser
- Download file 2.33 kB
-
https://huggingface.co/datasets/yandex/yambda/resolve/main/benchmarks/scripts/transform2sequential.py
- Command line
-
hf download hf://datasets/yandex/yambda/benchmarks/scripts/transform2sequential.py
-
curl -L -o transform2sequential.py https://huggingface.co/datasets/yandex/yambda/resolve/main/benchmarks/scripts/transform2sequential.py
2.33 kB
| import os | |
| import click | |
| import polars as pl | |
| def cli(src_dir: str, dst_dir: str, files: list[str], aggregation: str): | |
| transform2sequential(src_dir, dst_dir, files, aggregation) | |
| def transform2sequential(src_dir: str, dst_dir: str, files: list[str], aggregation: str): | |
| for file in files: | |
| print(f"Processing file: {file}") | |
| src_path = os.path.join(src_dir, file) | |
| parquet_path = os.path.join(dst_dir, file) | |
| if os.path.exists(parquet_path): | |
| parquet_path = os.path.join(dst_dir, f"{aggregation}_" + file) | |
| assert not os.path.exists(parquet_path) | |
| os.makedirs(dst_dir, exist_ok=True) | |
| df = pl.scan_parquet(src_path) | |
| if aggregation == "structs": | |
| seq_df = ( | |
| df.select("uid", pl.struct(pl.all().exclude("uid")).alias("events")) | |
| .group_by("uid", maintain_order=True) | |
| .agg(pl.col("events")) | |
| ) | |
| seq_df.sink_parquet( | |
| parquet_path, | |
| compression="lz4", | |
| statistics=True, | |
| ) | |
| elif aggregation == "columns": | |
| col_agg_df = df.group_by("uid", maintain_order=True).agg(pl.all().exclude("uid")) | |
| col_agg_df.sink_parquet( | |
| parquet_path, | |
| compression="lz4", | |
| statistics=True, | |
| ) | |
| if __name__ == "__main__": | |
| cli() | |