diff --git a/HearingTest.py b/HearingTest.py index 85988221f7f28d07dd2459d0bd28353c59fcf337..5a2382fe862aa6688c6166800220839166a2062c 100644 --- a/HearingTest.py +++ b/HearingTest.py @@ -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) diff --git a/HearingTestGUI.py b/HearingTestGUI.py index 3ad9434f0190fe0a05b00dc7121e71cd22a2a355..f5cd21ea7950561c52d4b6e20e2e8b38561e3181 100644 --- a/HearingTestGUI.py +++ b/HearingTestGUI.py @@ -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():