Merge pull request #139 from MycroftAI/bugfix/unittests

Make Unittests runnable
pull/153/head
Åke 2020-04-09 07:34:18 +02:00 committed by GitHub
commit 88dc92e20a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 72 additions and 11 deletions

1
MANIFEST.in Normal file
View File

@ -0,0 +1 @@
include precise/data/activate.wav

View File

@ -80,8 +80,7 @@ def play_audio(filename: str):
def activate_notify():
audio = 'data/activate.wav'
audio = abspath(dirname(abspath(__file__)) + '/../' + audio)
audio = join(dirname(abspath(__file__)), audio)
play_audio(audio)

View File

@ -69,8 +69,9 @@ setup(
'precise-calc-threshold=precise.scripts.calc_threshold:main',
]
},
include_package_data=True,
install_requires=[
'numpy',
'numpy==1.16',
'tensorflow>=1.13,<1.14', # Must be on piwheels
'sonopy',
'pyaudio',

10
test/Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:3.7-slim
ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get -y install git python3-scipy cython libhdf5-dev python3-h5py portaudio19-dev swig libpulse-dev libatlas-base-dev
ADD . mycroft-precise
WORKDIR mycroft-precise
RUN pip install .
RUN pip install pytest
ENV PYTHONPATH /mycroft-precise
ENTRYPOINT ["pytest"]

13
test/__init__.py Normal file
View File

@ -0,0 +1,13 @@
# Copyright 2019 Mycroft AI Inc.
#
# 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.

13
test/scripts/__init__.py Normal file
View File

@ -0,0 +1,13 @@
# Copyright 2019 Mycroft AI Inc.
#
# 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.

View File

@ -35,8 +35,14 @@ class DummyAudioFolder:
return min + (max - min) * np.random.random() * pr.buffer_t
def generate_samples(self, folder, name, value, duration):
"""Generate sample file.
The file is generated in the specified folder, with the specified name,
dummy value and duration.
"""
for i in range(self.count):
save_audio(join(folder, name.format(i)), np.array([value] * int(duration * pr.sample_rate)))
save_audio(join(folder, name.format(i)),
np.array([value] * int(duration * pr.sample_rate)))
def subdir(self, *parts):
folder = self.path(*parts)

View File

@ -26,18 +26,27 @@ def read_content(filename):
def test_combined(train_folder, train_script):
"""Test a "normal" development cycle, train, evaluate and calc threshold.
"""
train_script.run()
params_file = train_folder.model + '.params'
assert isfile(train_folder.model)
assert isfile(params_file)
EvalScript.create(folder=train_folder.root, models=[train_folder.model]).run()
EvalScript.create(folder=train_folder.root,
models=[train_folder.model]).run()
# Ensure that the graph script generates a numpy savez file
out_file = train_folder.path('outputs.npz')
graph_script = GraphScript.create(folder=train_folder.root, models=[train_folder.model], output_file=out_file)
graph_script = GraphScript.create(folder=train_folder.root,
models=[train_folder.model],
output_file=out_file)
graph_script.run()
assert isfile(out_file)
# Esure the params are updated after threshold is calculated
params_before = read_content(params_file)
CalcThresholdScript.create(folder=train_folder.root, model=train_folder.model, input_file=out_file).run()
CalcThresholdScript.create(folder=train_folder.root,
model=train_folder.model,
input_file=out_file).run()
assert params_before != read_content(params_file)

View File

@ -36,6 +36,10 @@ class FakeStdout:
def test_engine(train_folder, train_script):
"""
Test t hat the output format of the engina matches a decimal form in the
range 0.0 - 1.0.
"""
train_script.run()
with open(glob.glob(join(train_folder.root, 'wake-word', '*.wav'))[0], 'rb') as f:
data = f.read()

View File

@ -22,15 +22,20 @@ from test.scripts.dummy_audio_folder import DummyAudioFolder
class DummyTrainFolder(DummyAudioFolder):
def __init__(self, count=10):
super().__init__(count)
self.generate_samples(self.subdir('wake-word'), 'ww-{}.wav', 1.0, self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('not-wake-word'), 'nww-{}.wav', 0.0, self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('test', 'wake-word'), 'ww-{}.wav', 1.0, self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('test', 'not-wake-word'), 'nww-{}.wav', 0.0, self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('wake-word'), 'ww-{}.wav', 1.0,
self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('not-wake-word'), 'nww-{}.wav', 0.0,
self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('test', 'wake-word'), 'ww-{}.wav',
1.0, self.rand(0, 2 * pr.buffer_t))
self.generate_samples(self.subdir('test', 'not-wake-word'),
'nww-{}.wav', 0.0, self.rand(0, 2 * pr.buffer_t))
self.model = self.path('model.net')
class TestTrain:
def test_run_basic(self):
"""Run a training and check that a model is generated."""
folders = DummyTrainFolder(10)
script = TrainScript.create(model=folders.model, folder=folders.root)
script.run()