From 7a9ebe427ec03e9c54a768138e9ac7a27848b7a2 Mon Sep 17 00:00:00 2001 From: BeritJanssen Date: Tue, 28 Nov 2023 09:44:07 +0100 Subject: [PATCH] correct offsets for round indications; fix tests --- backend/experiment/rules/eurovision_2020.py | 10 +++---- backend/experiment/rules/hooked.py | 28 ++++++++----------- backend/experiment/rules/huang_2022.py | 11 +++----- backend/experiment/rules/tests/test_hooked.py | 7 ++--- backend/experiment/rules/thats_my_song.py | 4 +-- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/backend/experiment/rules/eurovision_2020.py b/backend/experiment/rules/eurovision_2020.py index 2952268ea..78ad356f8 100644 --- a/backend/experiment/rules/eurovision_2020.py +++ b/backend/experiment/rules/eurovision_2020.py @@ -130,11 +130,11 @@ def next_heard_before_action(self, session): # Get section. section = None if round_number <= len(songs) and round_number <= len(tags): - section = \ - session.playlist.get_section( - {'tag': str(tags[round_number])}, - [songs[round_number]] - ) + section = session.playlist.get_section( + {'tag': str(tags[round_number])}, + [songs[round_number]] + ) + if not section: print("Warning: no heard_before section found") section = session.playlist.get_section() diff --git a/backend/experiment/rules/hooked.py b/backend/experiment/rules/hooked.py index b1aeafd46..8aa9623b4 100644 --- a/backend/experiment/rules/hooked.py +++ b/backend/experiment/rules/hooked.py @@ -176,22 +176,18 @@ def final_score_message(self, session): n_old_new_correct = 0 for result in session.result_set.all(): - json_data = result.load_json_data() - try: - if json_data['view'] == 'SONG_SYNC': - if json_data['result']['type'] == 'recognized': - n_sync_guessed += 1 - sync_time += json_data['result']['recognition_time'] - if result.score > 0: - n_sync_correct += 1 - else: - if result.expected_response == 'old': - n_old_new_expected += 1 - if result.score > 0: - n_old_new_correct += 1 - except KeyError as error: - print('KeyError: %s' % str(error)) - continue + if result.question_key == 'recognize': + if result.given_response == 'yes': + n_sync_guessed += 1 + json_data = result.load_json_data() + sync_time += json_data.get('decision_time') + if result.score > 0: + n_sync_correct += 1 + else: + if result.expected_response == 'old': + n_old_new_expected += 1 + if result.score > 0: + n_old_new_correct += 1 score_message = "Well done!" if session.final_score > 0 else "Too bad!" if n_sync_guessed == 0: diff --git a/backend/experiment/rules/huang_2022.py b/backend/experiment/rules/huang_2022.py index d4ddfcfd4..3c2aee36b 100644 --- a/backend/experiment/rules/huang_2022.py +++ b/backend/experiment/rules/huang_2022.py @@ -148,7 +148,7 @@ def next_round(self, session): else: # Load the heard_before offset. - heard_before_offset = len(plan['song_sync_sections']) + 1 + heard_before_offset = len(plan['song_sync_sections']) # show score score = self.get_score(session, round_number) @@ -163,10 +163,10 @@ def next_round(self, session): actions.append(self.heard_before_explainer()) actions.append( self.next_heard_before_action(session)) - elif heard_before_offset < round_number <= total_rounds: + elif heard_before_offset < round_number < total_rounds: actions.append( self.next_heard_before_action(session)) - elif round_number == total_rounds + 1: + else: questionnaire = self.get_questionnaire(session) if questionnaire: actions.extend([Explainer( @@ -175,11 +175,8 @@ def next_round(self, session): steps=[], step_numbers=True, button_label=_("Let's go!")), *questionnaire]) - session.increment_round() else: - actions.append(self.finalize(session)) - else: - return [self.finalize(session)] + return [self.finalize(session)] return actions def finalize(self, session): diff --git a/backend/experiment/rules/tests/test_hooked.py b/backend/experiment/rules/tests/test_hooked.py index 330ee6577..dcd763c87 100644 --- a/backend/experiment/rules/tests/test_hooked.py +++ b/backend/experiment/rules/tests/test_hooked.py @@ -65,7 +65,7 @@ def test_thats_my_song(self): for i in range(0, experiment.rounds): actions = rules.next_round(session) - if i == experiment.rounds: + if i == experiment.rounds + 1: assert len(actions) == 2 assert actions[1].ID == 'FINAL' elif i == 0: @@ -113,9 +113,8 @@ def test_thats_my_song(self): assert actions[2].feedback_form.form[0].key == 'heard_before' else: assert len(actions) == 3 - assert actions[1].feedback_form.form[0].key == 'heard_before' - assert actions[2].feedback_form.form[0].key in musicgen_keys - session.increment_round() + assert actions[1].feedback_form.form[0].key in musicgen_keys + assert actions[2].feedback_form.form[0].key == 'heard_before' def test_hooked_china(self): experiment = Experiment.objects.get(name='Hooked-China') diff --git a/backend/experiment/rules/thats_my_song.py b/backend/experiment/rules/thats_my_song.py index 698c98e0a..812de9dff 100644 --- a/backend/experiment/rules/thats_my_song.py +++ b/backend/experiment/rules/thats_my_song.py @@ -64,7 +64,7 @@ def next_round(self, session): # If the number of results equals the number of experiment.rounds, # close the session and return data for the final_score view. - if round_number == session.experiment.rounds: + if round_number == session.experiment.rounds + self.round_modifier: # Finish session. session.finish() @@ -73,7 +73,7 @@ def next_round(self, session): # Return a score and final score action. social_info = self.social_media_info(session.experiment, session.final_score) return [ - self.get_score(session, round_number), + self.get_score(session, round_number - self.round_modifier), Final( session=session, final_text=self.final_score_message(session) + " For more information about this experiment, visit the Vanderbilt University Medical Center Music Cognition Lab.",