forked from jhetherly/EnglishSpeechUpsampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_train_split.py
51 lines (38 loc) · 1.8 KB
/
test_train_split.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
import os
import csv
import json
import numpy as np
os.chdir('..')
def write_csv(filename, pairs):
with open(filename, 'w', newline='') as csvfile:
csv.writer(csvfile).writerows(pairs)
settings_file = os.path.join('settings', 'data_settings.json')
settings = json.load(open(settings_file))
validation_fraction = settings['validation_fraction']
test_fraction = settings['test_fraction']
file_dir_base = settings['output_dir_name_base']
output_dir = settings['output_dir_name_base']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
truth_ds_pairs = []
file_dir_truth = os.path.join(file_dir_base, 'splices')
file_dir_ds = os.path.join(file_dir_base, 'downsampled_splices')
for filename in os.listdir(file_dir_truth):
truth_input_filename = os.path.join(file_dir_truth, filename)
ds_input_filename = os.path.join(file_dir_ds, filename)
if not os.path.isfile(truth_input_filename) or not os.path.isfile(ds_input_filename):
continue
truth_ds_pairs.append([truth_input_filename, ds_input_filename])
np.random.seed(0)
np.random.shuffle(truth_ds_pairs)
validation_start_index = 0
validation_end_index = validation_start_index + int(len(truth_ds_pairs) * validation_fraction)
test_start_index = validation_end_index
test_end_index = test_start_index + int(len(truth_ds_pairs) * validation_fraction)
train_start_index = test_end_index
validation_truth_ds_pairs = truth_ds_pairs[validation_start_index:validation_end_index]
write_csv(os.path.join(output_dir, 'validation_files.csv'), validation_truth_ds_pairs)
test_truth_ds_pairs = truth_ds_pairs[test_start_index:test_end_index]
write_csv(os.path.join(output_dir, 'test_files.csv'), test_truth_ds_pairs)
train_truth_ds_pairs = truth_ds_pairs[train_start_index:]
write_csv(os.path.join(output_dir, 'train_files.csv'), train_truth_ds_pairs)