|
|
import os |
|
|
import random |
|
|
import shutil |
|
|
import argparse |
|
|
from pathlib import Path |
|
|
from tqdm import tqdm |
|
|
|
|
|
def SplitDataset(dataset_name: str, dataset_dir: str, val_ratio: float, seed: int = 42): |
|
|
|
|
|
if not 0 < val_ratio < 1: |
|
|
raise ValueError("Validation split ratio (--split) must be between 0 and 1 (exclusive).") |
|
|
|
|
|
|
|
|
dataset_path = Path(dataset_dir) / dataset_name |
|
|
train_path = dataset_path / "Train" |
|
|
val_path = dataset_path / "Val" |
|
|
|
|
|
|
|
|
if not dataset_path.exists(): |
|
|
raise FileNotFoundError(f"Dataset folder '{dataset_path}' not found.") |
|
|
|
|
|
|
|
|
train_path.mkdir(exist_ok=True) |
|
|
val_path.mkdir(exist_ok=True) |
|
|
|
|
|
|
|
|
parquet_files = [f for f in dataset_path.glob("*.parquet")] |
|
|
if not parquet_files: |
|
|
raise ValueError(f"No .parquet files found in '{dataset_path}'.") |
|
|
|
|
|
|
|
|
random.seed(seed) |
|
|
random.shuffle(parquet_files) |
|
|
|
|
|
|
|
|
val_count = int(len(parquet_files) * val_ratio) |
|
|
val_files = parquet_files[:val_count] |
|
|
train_files = parquet_files[val_count:] |
|
|
|
|
|
print(f"📦 Found {len(parquet_files)} parquet files in '{dataset_name}'") |
|
|
print(f"🔀 Splitting {val_count} to 'Val' ({val_ratio:.0%}) and {len(train_files)} to 'Train'") |
|
|
|
|
|
|
|
|
for file in tqdm(val_files, desc="📁 Moving to Val", unit="file"): |
|
|
shutil.move(str(file), str(val_path / file.name)) |
|
|
|
|
|
|
|
|
for file in tqdm(train_files, desc="📁 Moving to Train", unit="file"): |
|
|
shutil.move(str(file), str(train_path / file.name)) |
|
|
|
|
|
print("✅ Split complete.") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
parser = argparse.ArgumentParser(description="Split a dataset of .parquet files into Train and Val sets.") |
|
|
parser.add_argument( |
|
|
"-n", "--dataset_name", required=True, type=str, |
|
|
help="Name of the dataset folder inside the dataset directory." |
|
|
) |
|
|
parser.add_argument( |
|
|
"-d", "--dataset_dir", default="./src/Datasets", type=str, |
|
|
help="Path to the parent directory containing dataset folders. Default is './src/Datasets'." |
|
|
) |
|
|
parser.add_argument( |
|
|
"-s", "--split", default=0.1, type=float, |
|
|
help="Fraction of files to use for validation (between 0 and 1). Default is 0.1 (10%)." |
|
|
) |
|
|
|
|
|
args = parser.parse_args() |
|
|
SplitDataset(args.dataset_name, args.dataset_dir, args.split) |
|
|
|