Administrator approval is now required for registering new accounts. If you are registering a new account, and are external to the University, please ask the repository owner to contact ServiceLine to request your account be approved. Repository owners must include the newly registered email address, and specific repository in the request for approval.

Commit d1563291 authored by Ed Rogers's avatar Ed Rogers
Browse files

Add saving of data to file

parent 07613564
......@@ -2,8 +2,7 @@ import time
import numpy as np
import random
from SoundLibrary import SoundLibrary
from typing import List
# from HearingTestGUI import HearingMplCanvas
class SoundRecord:
def __init__(self, freq, volume, time):
......@@ -52,22 +51,22 @@ class HearingTest:
for i, freq in enumerate(self.freqs):
choices.extend([(freq, v) for v in
np.arange(self.lower_bounds[i], self.upper_bounds[i], self.get_block_size())])
freq, volume = random.choice(choices)
self.library.play(freq, volume)
played_time = time.time()
next_sleep_time = random.uniform(self.max_response_time*1.2, self.max_response_time*3)
self.sounds.append(SoundRecord(freq, volume, played_time))
test_finished = np.all(self.finished_freqs())
self.check_for_not_heard(played_time)
return test_finished, freq, volume, played_time, next_sleep_time
return self.test_finished, freq, volume, played_time, next_sleep_time
@property
def test_finished(self):
return np.all(self.finished_freqs)
@property
def finished_freqs(self):
return self.upper_bounds - self.lower_bounds < self.get_block_size() * 1.2
return self.upper_bounds - self.lower_bounds < self.get_block_size() * 0.9
def check_for_not_heard(self, event_time):
for sound in reversed(self.sounds):
......@@ -87,3 +86,13 @@ class HearingTest:
def set_upper_bound(self, freq, volume):
self.upper_bounds[self.freqs == freq] = volume
self.canvas.update_result(self)
@property
def thresholds(self) -> np.ndarray:
if not self.test_finished:
raise ValueError
return mean_two_arrays(self.lower_bounds, self.upper_bounds)
def mean_two_arrays(a: np.ndarray, b: np.ndarray):
return np.mean(np.array([a, b]), axis=0)
......@@ -77,7 +77,7 @@ class HearingMplCanvas(FigureCanvas):
def update_result(self, result: HearingTest) -> None:
self.set_bar_heights(self.ul_bar, result.upper_bounds-self.baseline)
self.set_bar_heights(self.ll_bar, result.lower_bounds-self.baseline)
self.set_bar_colors([self.ul_bar, self.ll_bar], result.finished_freqs())
self.set_bar_colors([self.ul_bar, self.ll_bar], result.finished_freqs)
self.draw()
@staticmethod
......@@ -126,7 +126,7 @@ class TestWindow(QMainWindow):
def __init__(self, library):
self.library = library
self.result = None
self.test = None
self.testing_thread = None
self.test_running = False
......@@ -175,7 +175,7 @@ class TestWindow(QMainWindow):
if event.key() == Qt.Qt.Key_Escape:
self.testing_thread.abort = True
key_press_time = time.time()
self.result.handle_key_press(key_press_time)
self.test.handle_key_press(key_press_time)
self.record_key_press(event, key_press_time)
def record_key_press(self, event: QtGui.QKeyEvent, _time):
......@@ -185,8 +185,8 @@ class TestWindow(QMainWindow):
self.log.setText('')
self.title.setText('Test running')
self.log.append('Starting...')
self.result = HearingTest(self.library, self.graph)
self.testing_thread = HearingTestThread(self.result)
self.test = HearingTest(self.library, self.graph)
self.testing_thread = HearingTestThread(self.test)
self.testing_thread.played_sound.connect(self.sound_played)
self.testing_thread.finished.connect(self.test_finished)
self.testing_thread.aborted.connect(self.aborted)
......@@ -203,10 +203,15 @@ class TestWindow(QMainWindow):
self.test_running = False
self.title.setText("Press any key to start...")
self.log.append("Finished test")
self.log.append("Writing test result to file")
thresholds = self.test.thresholds
thresholds = thresholds.reshape(thresholds.shape[0], -1) # convert to 2d
with open('data.csv', 'ba') as file:
np.savetxt(file, thresholds.T, fmt='%.2f', delimiter=',')
# TODO store test
# TODO calculate score
# TODO display test
self.result = None
self.test = None
def main():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment