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 b8f6cb73 authored by Ed Rogers's avatar Ed Rogers
Browse files

Add display of score

parent 8a4bbe57
......@@ -19,7 +19,7 @@ class HearingTest:
max_response_time = 1 # in seconds
@staticmethod
def get_block_size():
def block_size():
return (HearingTest.upper_lim - HearingTest.lower_lim) / HearingTest.n_blocks
def __init__(self, library: SoundLibrary, canvas):
......@@ -48,9 +48,12 @@ class HearingTest:
def play_next_sound(self):
# TODO allow tone outside lims with low prob?
choices = []
# TODO fix bug here meaning that some choices do not reduce bounds
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())])
np.arange(self.lower_bounds[i],
self.upper_bounds[i],
self.block_size())])
freq, volume = random.choice(choices)
self.library.play(freq, volume)
played_time = time.time()
......@@ -66,7 +69,7 @@ class HearingTest:
@property
def finished_freqs(self):
return self.upper_bounds - self.lower_bounds < self.get_block_size() * 0.9
return self.upper_bounds - self.lower_bounds < self.block_size() * 0.9
def check_for_not_heard(self, event_time):
for sound in reversed(self.sounds):
......@@ -93,6 +96,28 @@ class HearingTest:
raise ValueError
return mean_two_arrays(self.lower_bounds, self.upper_bounds)
def get_score_text(self):
freq_bands = [self.freqs < 800,
np.logical_and(self.freqs >= 800, self.freqs < 3000),
3000 <= self.freqs]
band_labels = ['Low frequencies: ', 'Mid frequencies: ', 'High frequencies: ']
scores = [np.mean(self.thresholds[inds]) for inds in freq_bands]
text_score = [self.score_to_text(s) for s in scores]
score_list = [lbl + ts for lbl, ts in zip(band_labels, text_score)]
return '\n'.join(score_list)
@staticmethod
def score_to_text(score):
if score < 0.25:
text = 'Excellent'
elif 0.25 <= score < 0.50:
text = 'Good'
elif 0.50 <= score < 0.75:
text = 'Normal'
else:
text = 'Poor'
return text
def mean_two_arrays(a: np.ndarray, b: np.ndarray):
return np.mean(np.array([a, b]), axis=0)
......@@ -66,7 +66,7 @@ class HearingMplCanvas(FigureCanvas):
self.ll_bar = self.axes.bar(inds, result.lower_bounds - self.baseline,
color=self.false_color, bottom=self.baseline)
xlim = self.axes.get_xlim()
for i in np.arange(result.lower_lim + self.baseline, result.upper_lim, result.get_block_size()):
for i in np.arange(result.lower_lim + self.baseline, result.upper_lim, result.block_size()):
self.axes.plot(self.axes.get_xlim(), np.ones(2)*i, color='k')
labels = [str(b) for b in result.freqs]
labels.insert(0, '')
......@@ -134,6 +134,7 @@ class TestWindow(QMainWindow):
self.setMinimumSize(QSize(640, 480))
self.setWindowTitle("Hearing Test")
self.setStyleSheet("QMainWindow { background-color: black }")
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
......@@ -141,9 +142,9 @@ class TestWindow(QMainWindow):
grid_layout = QGridLayout(self)
central_widget.setLayout(grid_layout)
self.setStyleSheet("QMainWindow { background-color: black }")
text_style = "QLabel { background-color: black; color: white; font: 24pt}"
self.title = QLabel("Press space bar to start...", self)
self.title.setStyleSheet("QLabel { background-color: black; color: white; font: 24pt}")
self.title.setStyleSheet(text_style)
self.title.setAlignment(QtCore.Qt.AlignCenter)
self.log = QTextEdit()
......@@ -153,9 +154,15 @@ class TestWindow(QMainWindow):
self.graph = HearingMplCanvas(parent=self)
self.score_label = QLabel('', self)
self.score_label.setStyleSheet(text_style)
self.score_label.setAlignment(QtCore.Qt.AlignLeft)
self.score_label.hide()
grid_layout.addWidget(self.title, 0, 0)
grid_layout.addWidget(self.log, 1, 0)
grid_layout.addWidget(self.graph, 2, 0)
grid_layout.addWidget(self.score_label, 3, 0)
def keyPressEvent(self, event: QtGui.QKeyEvent):
if event.key() == Qt.Qt.Key_D:
......@@ -190,6 +197,7 @@ class TestWindow(QMainWindow):
self.log.setText('')
self.title.setText('Test running')
self.log.append('Starting...')
self.score_label.hide()
self.test = HearingTest(self.library, self.graph)
self.testing_thread = HearingTestThread(self.test)
self.testing_thread.played_sound.connect(self.sound_played)
......@@ -215,6 +223,9 @@ class TestWindow(QMainWindow):
np.savetxt(file, thresholds.T, fmt='%.2f', delimiter=',')
# TODO calculate score
# TODO display score
score_str = self.test.get_score_text()
self.score_label.setText(score_str)
self.score_label.show()
self.test = None
......@@ -222,7 +233,7 @@ def main():
device = sd.query_devices(sd.default.device['output'])
fs = device['default_samplerate']
length = 0.5
f = [100, 200, 500, 1000, 2000, 5000, 10000]
f = [125, 250, 500, 750, 1000, 1500, 2000, 5000, 8000, 12000]
library = SoundLibrary(fs, length, f)
......
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