-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.py
61 lines (46 loc) · 1.39 KB
/
splitter.py
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
import glob
import os
import random
def get_chunks(data_prefix):
return glob.glob(data_prefix + "*.gz")
print("Starting splitter")
num_chunks = 1000000
num_test = 105000
num_train = 905000
path = "data-*/"
chunks = []
for d in glob.glob(path):
chunks += get_chunks(d)
random.shuffle(chunks)
n_test = len(chunks) // 10
print("{}, {}".format(n_test, len(chunks) - n_test))
for i in range(n_test):
path, filename = os.path.split(chunks[i])
os.makedirs("input/test/" + path, exist_ok=True)
os.rename(chunks[i], "input/test/" + chunks[i])
for i in range(n_test, len(chunks)):
path, filename = os.path.split(chunks[i])
os.makedirs("input/train/" + path, exist_ok=True)
os.rename(chunks[i], "input/train/" + chunks[i])
print("Trimming test")
chunks = []
for d in glob.glob("input/test/data-*/"):
chunks += get_chunks(d)
print("sorting {} chunks...".format(len(chunks)), end='')
chunks.sort(key=os.path.getmtime, reverse=True)
print("[done]")
delet = chunks[num_test:]
print("Deleting {}/{}".format(len(delet), len(chunks)))
for f in delet:
os.remove(f)
print("Trimming train")
chunks = []
for d in glob.glob("input/train/data-*/"):
chunks += get_chunks(d)
print("sorting {} chunks...".format(len(chunks)), end='')
chunks.sort(key=os.path.getmtime, reverse=True)
print("[done]")
delet = chunks[num_train:]
print("Deleting {}/{}".format(len(delet), len(chunks)))
for f in delet:
os.remove(f)