-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_splits.py
164 lines (137 loc) · 4.44 KB
/
pack_splits.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""
###########################################################################
# pack_splits.py
#
# Pack split files into .xz for download from cv-tbox-dataset-analyzer
#
# Use:
# python pack_splits.py
#
#
#
# This script is part of Common Voice ToolBox Package
#
# github: https://github.com/HarikalarKutusu/cv-tbox-dataset-compiler
# Copyright: (c) Bülent Özden, License: AGPL v3.0
###########################################################################
"""
from collections import Counter
import os
import sys
import shutil
import glob
import multiprocessing as mp
import psutil
from tqdm import tqdm
# This package
import const as c
import conf
from lib import init_directories, report_results
from typedef import Globals
#
# Constants
#
HERE: str = os.path.dirname(os.path.realpath(__file__))
if not HERE in sys.path:
sys.path.append(HERE)
# Program parameters
PROC_COUNT: int = min(60, int(2 * (psutil.cpu_count(logical=True) or 1))) # OVER usage
BATCH_SIZE: int = 5
g: Globals = Globals()
ver_counter: Counter[str] = Counter()
lc_counter: Counter[str] = Counter()
algo_counter: Counter[str] = Counter()
def handle_ds(p: str) -> str:
"""Handle a single version/lc in multi-processing"""
plist: list[str] = p.split(os.sep)
algo: str = plist[-1]
lc: str = plist[-2]
ver: str = plist[-3].split("-")[2]
upload_dir: str = os.path.join(
conf.COMPRESSED_RESULTS_BASE_DIR, c.UPLOAD_DIRNAME, lc
)
os.makedirs(upload_dir, exist_ok=True)
shutil.make_archive(
base_name=os.path.join(upload_dir, f"{lc}_{ver}_{algo}"),
format="xztar",
root_dir=os.path.split(p)[0],
base_dir=algo,
)
g.processed_algo += 1
# report back
return f"{ver}_{lc}_{algo}"
# MAIN PROCESS
def main() -> None:
"""Main loop to compress split files for download"""
# Get a list of available language codes in every version
dspaths: list[str] = glob.glob(
os.path.join(conf.DATA_BASE_DIR, c.VC_DIRNAME, "**", c.ALGORITHMS[0]),
recursive=True,
)
dspaths = [os.path.split(p)[0] for p in dspaths]
#
# Drop already compressed
#
compressed_list: list[str] = glob.glob(
os.path.join(conf.COMPRESSED_RESULTS_BASE_DIR, "**", "*.tar.xz"), recursive=True
)
compressed_list = [
p.split(os.sep)[-1].replace(".tar.xz", "") for p in compressed_list
]
final_list: list[str] = []
for p in dspaths:
plist: list[str] = p.split(os.sep)
ver: str = plist[-2].split("-")[2]
lc: str = plist[-1]
ver_counter.update([ver])
lc_counter.update([lc])
for algo in c.ALGORITHMS:
maybe_p: str = os.path.join(p, algo)
if os.path.isdir(maybe_p):
algo_counter.update([algo])
if not "_".join([lc, ver, algo]) in compressed_list:
final_list.append(maybe_p)
else:
g.skipped_exists += 1
else:
g.skipped_nodata += 1
total_cnt: int = len(final_list)
# record totals
g.total_ver = len(ver_counter.values())
g.total_lc = lc_counter.total()
g.total_algo = algo_counter.total()
g.total_splits = 3 * g.total_algo
ver_counter.clear()
lc_counter.clear()
algo_counter.clear()
#
# Process with multi-processing
#
if total_cnt > 0:
print(
f"Compressing {len(final_list)} missing algorithms from {len(dspaths)} datasets...\n"
)
with mp.Pool(processes=PROC_COUNT, maxtasksperchild=BATCH_SIZE) as pool:
# pool.map(handle_ds, dspaths)
with tqdm(total=total_cnt, desc="Datasets") as pbar:
for res in pool.imap_unordered(
handle_ds, final_list, chunksize=BATCH_SIZE
):
if conf.VERBOSE:
pbar.write(f"Finished: {res}")
reslist: list[str] = res.split("_")
ver_counter.update([reslist[0]])
lc_counter.update([reslist[1]])
algo_counter.update([reslist[2]])
pbar.update()
# done
g.processed_ver = len(ver_counter.keys())
g.processed_lc = len(lc_counter.keys())
g.processed_algo = len(algo_counter.keys())
print(ver_counter.most_common())
report_results(g)
if __name__ == "__main__":
print("=== cv-tbox-dataset-analyzer - Split Compressiom ===")
init_directories()
main()