-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm_s5.py
217 lines (177 loc) · 5.93 KB
/
algorithm_s5.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
"""
cv-tbox Diversity Check / Split Maker
Standard Common Voice CorporaCreator algorithm with 99 recordings for a sentences is allowed
"""
###########################################################################
# algorithm-s99.py
#
# Runs Common Voice Corpora Creator with -s 5 parameter
#
# If destination already exists, it is skipped,
# else Corpora Creator is run.
#
# The result will only include train, dev, tes tsv files.
#
# Uses multiprocessing with N-1 cores.
#
# Use:
# python algorithm-s99.py
#
# This script is part of Common Voice ToolBox Package
#
# github: https://github.com/HarikalarKutusu/cv-tbox-split-maker
# Copyright: (c) Bülent Özden, License: AGPL v3.0
###########################################################################
# Standard Lib
from argparse import Namespace
from dataclasses import dataclass
from datetime import datetime, timedelta
import os
import sys
import shutil
import glob
import multiprocessing as mp
import threading
# External Dependencies
from tqdm import tqdm
from corporacreator import parse_args
import pandas as pd
import psutil
# Module
from typedef import AlgorithmSpecs, Globals
from lib import LocalCorpus
from lib import df_read, final_report, remove_deleted_users
import conf
#
# Globals
#
HERE: str = os.path.dirname(os.path.realpath(__file__))
if not HERE in sys.path:
sys.path.append(HERE)
PROC_COUNT: int = psutil.cpu_count(logical=True) or 1 # Full usage
output_lock = threading.Lock()
g = Globals()
aspecs = AlgorithmSpecs(
src_algo_dir="s1", dst_algo_dir="s5", duplicate_sentence_count=5
)
#
# PROCESS
# Handle one split creation, this is where calculations happen
#
# def corpora_creator_original(lc: str, val_path: str, dst_path: str, duplicate_sentences: int) -> bool:
def corpora_creator_original(val_path: str) -> bool:
"""Processes validated.tsv and create new train, dev, test splits"""
dst_exppath: str = os.path.join(
conf.SM_DATA_DIR, "experiments", aspecs.dst_algo_dir
)
# results: list[bool] = []
src_corpus_dir: str = os.path.split(val_path)[0]
lc: str = os.path.split(src_corpus_dir)[1]
ver: str = os.path.split(os.path.split(src_corpus_dir)[0])[1]
dst_corpus_dir: str = os.path.join(dst_exppath, ver, lc)
# temp dir
temp_path: str = os.path.join(HERE, ".temp", ver, lc)
# call corpora creator with only validated (we don't need others)
df_corpus: pd.DataFrame = df_read(val_path)
num_original: int = df_corpus.shape[0]
# Must have records in it, else no go
if num_original == 0:
return False
# Remove users who requested data deletion
df_corpus = remove_deleted_users(df_corpus)
if num_original != df_corpus.shape[0] and conf.VERBOSE:
print(
f"\nUSER RECORDS DELETED FROM VALIDATED {ver}-{lc} = {num_original - df_corpus.shape[0]}"
)
# Here, it has records in it
# create temp dir
os.makedirs(temp_path, exist_ok=True)
# handle corpus
cc_args: Namespace = parse_args(
[
"-d",
temp_path,
"-f",
val_path,
"-s",
str(aspecs.duplicate_sentence_count),
]
)
corpus: LocalCorpus = LocalCorpus(cc_args, lc, df_corpus)
corpus.create()
corpus.save(temp_path)
# move required files to destination
os.makedirs(dst_corpus_dir, exist_ok=True)
shutil.move(os.path.join(temp_path, lc, "train.tsv"), dst_corpus_dir)
shutil.move(os.path.join(temp_path, lc, "dev.tsv"), dst_corpus_dir)
shutil.move(os.path.join(temp_path, lc, "test.tsv"), dst_corpus_dir)
shutil.rmtree(os.path.join(temp_path, lc))
return True
#
# Main loop for experiments-versions-locales
#
def main() -> None:
"""Original Corpora Creator with -s 5 option for Common Voice Datasets"""
#
# Callback
#
def pool_callback(res: bool) -> None:
"""Callback to append results and increment bar"""
pbar.update()
if res:
g.processed_cnt += 1
else:
g.skipped_nodata += 1
#
# Main
#
print("=== Original Corpora Creator with -s 5 option for Common Voice Datasets ===")
# Paths
src_exppath: str = os.path.join(
conf.SM_DATA_DIR, "experiments", aspecs.src_algo_dir
)
dst_exppath: str = os.path.join(
conf.SM_DATA_DIR, "experiments", aspecs.dst_algo_dir
)
# Get total for progress display
all_validated: "list[str]" = glob.glob(
os.path.join(src_exppath, "**", "validated.tsv"), recursive=True
)
# For each corpus
g.start_time = datetime.now()
final_list: list[str] = []
# clean unneeded/skipped
if conf.FORCE_CREATE:
final_list = all_validated
else:
for p in all_validated:
src_corpus_dir: str = os.path.split(p)[0]
lc: str = os.path.split(src_corpus_dir)[1]
ver: str = os.path.split(os.path.split(src_corpus_dir)[0])[1]
dst_corpus_dir: str = os.path.join(dst_exppath, ver, lc)
if os.path.isfile(os.path.join(dst_corpus_dir, "train.tsv")):
g.skipped_exists += 1
else:
final_list.append(p)
g.total_cnt = len(all_validated)
g.src_cnt = len(final_list)
# schedule mp
print(
f"Re-splitting for {g.src_cnt} out of {g.total_cnt} corpora in {PROC_COUNT} processes."
)
print(f"Skipping {g.skipped_exists} as they already exist.")
chunk_size: int = min(
10, g.src_cnt // PROC_COUNT + 0 if g.src_cnt % PROC_COUNT == 0 else 1
)
with mp.Pool(PROC_COUNT) as pool:
with tqdm(total=g.src_cnt) as pbar:
for result in pool.imap_unordered(
corpora_creator_original, final_list, chunksize=chunk_size
):
pool_callback(result)
# remove temp directory structure
# _ = [shutil.rmtree(d) for d in glob.glob(os.path.join(HERE, ".temp", "*"), recursive=False)]
final_report(g)
if __name__ == "__main__":
main()