-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_data.py
216 lines (150 loc) · 5.35 KB
/
prepare_data.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
import scipy.io as sio
import scipy
import os
import numpy as np
from scipy.stats import norm
from argparse import ArgumentParser
from tqdm import tqdm
from src.utils import (
phonemize_text,
phonetic_tokenize,
clean_text,
tokenize,
correct_channels,
)
def get_bound(_mean, _std, ep=1e-8):
part1 = np.sqrt(-np.log(ep * _std * np.pi) * 2)
high = _mean + _std * part1
low = _mean - _std * part1
return low, high
def filter_noises(spikePow, block_mean, block_std, ep=1e-8):
# _mean = spikePow.mean(0)
# _std = spikePow.std(0)
# low, high = get_bound(_mean=_mean, _std=_std, ep=1e-8)
low, high = get_bound(_mean=block_mean, _std=block_std, ep=1e-8)
spikePow = np.clip(spikePow, low, high)
return spikePow
def fix_text(text):
text = text.lower().strip()
replace_list = [
[".", " "],
["-", " "],
["?", " "],
[",", " "],
['"', " "],
["in' ", "ing "],
["in'.", "ing."],
["' ", " "], # 8 instances is not enough
["'em", "them"],
[" 'll", "'ll"],
[" 'd", "'d"],
[" '", " "],
["evidence'", "evidence"], # unique case
["!", " "],
['"', " "],
[":", " "],
[";", " "],
["stirrin", "stirring"],
[" hey've", "they've"],
["their's", "theirs"],
["cccountability", "accountability"],
["anythingt", "anything"],
["pricy", "pricey"],
["cucbers", "cucumbers"],
["premis", "premises"],
[" aking ", " making "],
[" aking ", " making "],
[" ain't ", " aren't "]
]
for fro, to in replace_list:
text = text.replace(fro, to)
text = " ".join(text.split())
cl_text = clean_text(text)
return cl_text
def load_file(path, has_labels=True):
data = sio.loadmat(path)
# sentenceText spikePow blockIdx
block_ids = list(set(data["blockIdx"].squeeze()))
spikePows = []
sentenceTexts = []
mean_stds = []
for b_idx in block_ids:
selected_ids = data["blockIdx"].squeeze() == b_idx
spikePow_block = data["spikePow"][0][selected_ids]
spikePow_block = [correct_channels(i) for i in spikePow_block]
# spikePow_block = [np.sqrt(i) for i in spikePow_block]
spikePow_block_lens = [len(a) for a in spikePow_block]
spikePow_block_start_indices = np.cumsum(spikePow_block_lens[:-1])
# block normalization
features = np.vstack(spikePow_block)
_mean = np.median(features, axis=0)
_std = np.median(np.abs(features - _mean), axis=0)
features = filter_noises(features, _mean, _std, ep=1e-8)
block_mean_std = np.vstack([_mean, _std])
block_mean_std = np.expand_dims(block_mean_std, 0)
block_mean_std = np.broadcast_to(
block_mean_std,
(len(spikePow_block), block_mean_std.shape[1], len(features[0])),
)
spikePow_block = np.split(
features, indices_or_sections=spikePow_block_start_indices
)
spikePows += spikePow_block
mean_stds += [block_mean_std]
if has_labels:
sentenceText_block = data["sentenceText"][selected_ids]
sentenceTexts += [fix_text(s) for s in sentenceText_block]
# sentenceTexts += sentenceText_block.tolist()
data = None
return spikePows, sentenceTexts, mean_stds
def load_dir(dir_path, has_labels=True):
data_file_paths = [os.path.join(dir_path, f) for f in sorted(os.listdir(dir_path))]
spikePows = []
sentenceTexts = []
mean_stds = []
for f in tqdm(data_file_paths):
sp, st, ms = load_file(path=f, has_labels=has_labels)
spikePows += sp
mean_stds += ms
if has_labels:
sentenceTexts += st
return spikePows, sentenceTexts, mean_stds
def prepare_data(input_dir, output_dir, has_labels=True):
spikePows, sentenceTexts, mean_stds = load_dir(
dir_path=input_dir, has_labels=has_labels
)
spikePow_lens = [0] + [len(a) for a in spikePows]
start_indices = np.cumsum(spikePow_lens[:-1])
end_indices = np.cumsum(spikePow_lens[1:])
spikePow_indices = np.vstack([start_indices, end_indices]).T
spikePows = np.vstack(spikePows)
mean_stds = np.vstack(mean_stds)
# save spikePows
np.save(os.path.join(output_dir, "spikePows.npy"), spikePows)
np.save(os.path.join(output_dir, "spikePow_indices.npy"), spikePow_indices)
np.save(os.path.join(output_dir, "spikePow_mean_stds.npy"), mean_stds)
if has_labels:
phonemized = phonemize_text(sentenceTexts)
sentenceTexts = np.array(sentenceTexts)
phonemized = np.array(phonemized)
np.save(os.path.join(output_dir, "sentenceTexts.npy"), sentenceTexts)
np.save(os.path.join(output_dir, "phonemizedTexts.npy"), phonemized)
def main():
input_dir = [
"./dataset/competitionData/train",
"./dataset/competitionData/test",
"./dataset/competitionData/competitionHoldOut",
]
output_dir = [
"./dataset/train",
"./dataset/valid",
"./dataset/test",
]
has_labels = [True, True, False]
for d in output_dir:
os.makedirs(d, exist_ok=True)
for inp, out, hl in zip(input_dir, output_dir, has_labels):
print(inp)
prepare_data(inp, out, hl)
if __name__ == "__main__":
main()