File size: 2,503 Bytes
462abe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
    # Validate ratio
    if not 0 < val_ratio < 1:
        raise ValueError("Validation split ratio (--split) must be between 0 and 1 (exclusive).")

    # Set paths
    dataset_path = Path(dataset_dir) / dataset_name
    train_path = dataset_path / "Train"
    val_path = dataset_path / "Val"

    # Validate dataset directory
    if not dataset_path.exists():
        raise FileNotFoundError(f"Dataset folder '{dataset_path}' not found.")

    # Create Train/Val subfolders
    train_path.mkdir(exist_ok=True)
    val_path.mkdir(exist_ok=True)

    # Find .parquet files
    parquet_files = [f for f in dataset_path.glob("*.parquet")]
    if not parquet_files:
        raise ValueError(f"No .parquet files found in '{dataset_path}'.")

    # Shuffle files deterministically
    random.seed(seed)
    random.shuffle(parquet_files)

    # Split into train and val
    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'")

    # Move files to Val
    for file in tqdm(val_files, desc="📁 Moving to Val", unit="file"):
        shutil.move(str(file), str(val_path / file.name))

    # Move files to Train
    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)