-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
389 lines (343 loc) · 13.9 KB
/
test.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
'''
================================================
VOICEOME REPOSITORY
================================================
repository name: Voiceome
repository version: 1.0
repository link: https://github.com/jim-schwoebel/voiceome
author: Jim Schwoebel
author contact: [email protected]
description: A set of scripts related to the Voiceome Study, a clinical study for speech and language biomarker research N=6000+
license category: opensource
license: Apache 2.0 license
organization name: Sonde Health Inc.
location: Boston, MA
website: https://sondehealth.com
release date: 2021-06-13
This code (Voiceome) is hereby released under a Apache 2.0 license license.
For more information, check out the license terms below.
================================================
DESCRIPTION
================================================
A simple unit test script for the Voiceome repository.
Thank you for your interest in our work!
================================================
LICENSE TERMS
================================================
Copyright 2021 by Jim Schwoebel.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
######################################################################
## IMPORTS ##
######################################################################
import sys, os, json, time, shutil, argparse, random, math, unittest, shutil
import matplotlib.pyplot as plt
import soundfile as sf
import numpy as np
from pyvad import vad, trim, split
import pandas as pd
from datetime import datetime
import librosa
import matplotlib.pyplot as plt
import numpy as np
import difflib
import nltk
from nltk import word_tokenize
import speech_recognition as sr
from tqdm import tqdm
from beautifultable import BeautifulTable
# get current directory
curdir=os.getcwd()
######################################################################
## UNIT TESTS ##
######################################################################
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
class test_cleaning(unittest.TestCase):
'''
CLEANING API TESTS
Tests file cleaning capabilities by removing duplicates, etc.
across all file types.
'''
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
def test_cleaning(self, curdir=curdir):
os.chdir(curdir)
os.chdir('tests')
# get a fresh dataset in case it was manipulated
if 'voiceome_test' in os.listdir():
shutil.rmtree('voiceome_test')
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
else:
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
os.chdir('voiceome_test')
os.chdir('a871b730-cc8a-11eb-a78c-b9f05e289d42')
clean_dir=os.getcwd()
# pick a random wavfile and use this for cleaning
wavfile='nlx-4d2f5480-cc8b-11eb-aefd-7de9011dbebd.wav'
# now clean this one file
os.chdir(curdir)
os.system('python3 cli.py --command clean --dir %s'%(clean_dir))
os.chdir(clean_dir)
listdir=os.listdir()
cleanfiles=list()
for i in range(len(listdir)):
if listdir[i].find('cleaned.wav'):
cleanfiles.append(listdir[i])
if len(cleanfiles) == 1:
b=True
else:
b=False
msg='Featurizations failed, please check dependencies and try again.'
self.assertEqual(True, b, msg)
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
class test_featurization(unittest.TestCase):
'''
FEATURIZATION API TESTS
Tests featurization capabilities across all training scripts.
'''
#### ##### ##### ##### ##### ##### ##### ##### ##### #####
def test_features(self, curdir=curdir):
os.chdir(curdir)
os.chdir('tests')
# get a fresh dataset in case it was manipulated
if 'voiceome_test' in os.listdir():
shutil.rmtree('voiceome_test')
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
else:
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
os.chdir('voiceome_test')
os.chdir('a871b730-cc8a-11eb-a78c-b9f05e289d42')
features_dir=os.getcwd()
# pick a random wavfile and use this for cleaning
wavfile='nlx-4d2f5480-cc8b-11eb-aefd-7de9011dbebd.wav'
# now featurize this one file
os.chdir(curdir)
os.system('python3 cli.py --command features --dir %s'%(features_dir))
os.chdir(features_dir)
listdir=os.listdir()
jsonfiles=list()
for i in range(len(listdir)):
if listdir[i].endswith('.json'):
jsonfiles.append(listdir[i])
if len(jsonfiles) == 1:
b=True
else:
b=False
msg='Featurizations failed, please check dependencies and try again.'
self.assertEqual(True, b, msg)
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
class test_quality(unittest.TestCase):
'''
QUALITY API TESTS
Test calculating quality metrics from inputting a wav file.
'''
def test_quality_featurization(self, curdir=curdir):
os.chdir(curdir)
os.chdir('tests')
if 'voiceome_test_data' in os.listdir():
shutil.rmtree('voiceome_test')
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
else:
os.system('git clone https://github.com/jim-schwoebel/voiceome_test')
os.chdir('voiceome_test')
os.chdir('a871b730-cc8a-11eb-a78c-b9f05e289d42')
new_dir=os.getcwd()
os.chdir(curdir)
os.system('python3 cli.py --command quality --file nlx-4d2f5480-cc8b-11eb-aefd-7de9011dbebd.wav --dir %s > test.txt'%(new_dir))
newfile=open('test.txt').read().split('--------------------------------------------------------------')
output=newfile[-2].replace('\n','')
os.remove('test.txt')
# now go through and see if all the strings are there
b=True
if output.find('brownfox_feature') < 0:
b=False
if output.find('animal_feature') < 0:
b=False
if output.find('caterpillar_feature') < 0:
b=False
if output.find('mandog_feature') < 0:
b=False
if output.find('tourbus_feature') < 0:
b=False
if output.find('bnt_feature') < 0:
b=False
if output.find('nonword_feature') < 0:
b=False
msg='failed the process of extracting quality metrics; please re-install dependencies with requirements.txt and try again.'
self.assertEqual(True, b, msg)
class test_references(unittest.TestCase):
'''
REFERENCES TEST API
Get sample reference values to see if everything is working / database is queryable.
'''
def test_table_by_feature(self, curdir=curdir):
file='tablebyfeature.txt'
os.system('python3 cli.py --command reference --vtype table_by_feature --agegender TwentiesMale > %s'%(file))
contents=open(file).read()
expected=open(curdir+'/tests/'+file).read()
if contents == expected:
b=True
else:
b=False
msg='Unexpected reference range.'
os.remove(file)
self.assertEqual(True, b, msg)
def test_table_by_embedding(self, curdir=curdir):
file='tablebyembedding.txt'
os.system('python3 cli.py --command reference --vtype table_by_embedding --agegender TwentiesMale > %s'%(file))
contents=open(file).read()
expected=open(curdir+'/tests/'+file).read()
if contents == expected:
b=True
else:
b=False
msg='Unexpected reference range.'
os.remove(file)
self.assertEqual(True, b, msg)
def test_table_across_tasks(self, curdir=curdir):
file='tableacrosstasks.txt'
os.system('python3 cli.py --command reference --vtype table_across_tasks --agegender TwentiesMale > %s'%(file))
contents=open(file).read()
expected=open(curdir+'/tests/'+file).read()
if contents == expected:
b=True
else:
b=False
msg='Unexpected reference range.'
os.remove(file)
self.assertEqual(True, b, msg)
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
class test_settings(unittest.TestCase):
'''
SETTINGS TEST API
Make sure all settings are proper values.
'''
settings=json.load(open('settings.json'))
def test_transcription_engine(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
transcript_option=settings['TranscriptEngine']
transcript_options=json.load(open('transcriptengine_options.json'))['TranscriptEngineOptions']
if transcript_option in transcript_options:
b=True
else:
b=False
msg='%s not in transcription options: \n%s'%(transcript_option, transcript_options)
self.assertEqual(True, b, msg)
def test_feature_embedding(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
data=json.load(open('feature_options.json'))
feature_embedding=settings['FeatureEmbedding']
feature_embeddings=list(json.load(open('feature_options.json')))
if feature_embedding in feature_embeddings:
b=True
else:
b=False
msg='%s not in possible list of feature embeddings: \n%s'%(feature_embedding, str(feature_embeddings))
self.assertEqual(True, b, msg)
def test_featuretype(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
feature_embedding=settings['FeatureEmbedding']
featuretype=settings['FeatureType']
featuretypes=json.load(open('feature_options.json'))[feature_embedding]
if featuretype in featuretypes:
b=True
else:
b=False
msg='%s not in list of possible feature types (in the %s embedding).'%(featuretype, feature_embedding)
self.assertEqual(True, b, msg)
def test_task(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
task_option=settings['Task']
task_options=json.load(open('task_options.json'))['AllTasks']
if task_option in task_options:
b=True
else:
b=False
msg='%s not in task options: \n%s'%(task_option, task_options)
self.assertEqual(True, b, msg)
def test_cleanaudio(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
option=settings['CleanAudio']
options=[True, False]
if option in options:
b=True
else:
b=False
msg='%s not in options: \n%s'%(option, options)
self.assertEqual(True, b, msg)
def test_agegender(self, curdir=curdir, settings=settings):
os.chdir(curdir)
os.chdir('data')
os.chdir('options')
option=settings['DefaultAgeGender']
options=json.load(open('agegender_options.json'))['AgeGenderOptions']
if option in options:
b=True
else:
b=False
msg='%s not in options: \n%s'%(option, options)
self.assertEqual(True, b, msg)
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
class test_visualization(unittest.TestCase):
'''
VISUALIZATION API TESTS
'''
def test_visualize_bar(self, curdir=curdir):
# delete all visualizations
os.chdir(curdir)
os.chdir('data')
os.chdir('visualizations')
visual_dir=os.getcwd()
listdir=os.listdir()
for i in range(len(listdir)):
if listdir[i].endswith('.png'):
os.remove(listdir[i])
os.chdir(curdir)
os.system('python3 cli.py --command visualize --agegender FourtiesMale --vtype bar --agegender ThirtiesMale --verbosity False')
os.chdir(visual_dir)
listdir=os.listdir()
if 'OpensmileFeatures_F0semitoneFrom27.5Hz_sma3nz_amean.png' in listdir:
b=True
else:
b=False
msg='visualization does not work, check matplotlib installation.'
self.assertEqual(True, b, msg)
def test_visualize_bar_cohort(self, curdir=curdir):
# delete all visualizations
os.chdir(curdir)
os.chdir('data')
os.chdir('visualizations')
visual_dir=os.getcwd()
listdir=os.listdir()
for i in range(len(listdir)):
if listdir[i].endswith('.png'):
os.remove(listdir[i])
os.chdir(curdir)
os.system('python3 cli.py --command visualize --agegender FourtiesMale --vtype bar_cohorts --agegender ThirtiesMale --verbosity False')
os.chdir(visual_dir)
listdir=os.listdir()
if 'OpensmileFeatures_F0semitoneFrom27.5Hz_sma3nz_amean_FourtiesMale_ThirtiesMale.png' in listdir:
b=True
else:
b=False
msg='visualization does not work, check matplotlib installation.'
self.assertEqual(True, b, msg)
if __name__ == '__main__':
unittest.main()