Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 1,701 Bytes
afd65d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os 
import random 
import json

random.seed(20)

def _get_filepaths(path): 
    filepaths = []
    for f in os.listdir(path): 
        f_path = os.path.join(path, f)
        if os.path.isfile(f_path): 
            filepaths.append(os.path.normpath(f_path))
        elif os.path.isdir(f_path): 
            filepaths += _get_filepaths(f_path)

    return filepaths

def get_split(path, train_split: float, must_be_in_train):
    filepaths = _get_filepaths(path)
    random.shuffle(filepaths)

    boundary = int(train_split * len(filepaths))

    train_paths = filepaths[:boundary]
    valid_paths = filepaths[boundary:]

    print("TRAIN SPLIT (number in train, number in val): ", len(train_paths), len(valid_paths))

    for path in must_be_in_train: 
        normed_path = os.path.normpath(path)
        assert normed_path in train_paths or normed_path in valid_paths, f"{normed_path} not in paths"
        if normed_path in valid_paths: 
            print(f"MOVING {path} to validation set")
            valid_paths.remove(path)
            train_paths.append(path)

    return train_paths, valid_paths

def main(): 
    train_rate = 0.95
    splits = {}

    args = [
            ("books", ["books/stein/stein.tex", "books/trench/TRENCH_REAL_ANALYSIS.tex"]), 
            ("formal", ["formal/setmm/set.mm"]), 
           ]

    for subdir, must_be_in_train in args: 
        print(subdir, must_be_in_train)
        train, valid = get_split(subdir, train_rate, must_be_in_train)
        splits[subdir + "-train"] = train
        splits[subdir + "-valid"] = valid 

    with open("splits.json", "w") as f: 
        f.write(json.dumps(splits, indent=4))


if __name__=="__main__": 
    main()