Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stoppage Criteria: max incorrect #351

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bcipy/helpers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,22 @@ def generate_targets(alp, stim_number):
targets = [target for sublist in lists for target in sublist]

return targets


def consecutive_incorrect(target_text: str, spelled_text: str) -> int:
"""Function that computes the number of consecutive symbols that
are incorrectly spelled.

>>> consecutive_incorrect('WORLD', 'H')
1
>>> consecutive_incorrect('WORLD', 'W')
0
>>> consecutive_incorrect('WORLD', 'WOHL')
2
"""
if not target_text:
return len(spelled_text)
for i, character in enumerate(spelled_text):
if character != target_text[i]:
return len(spelled_text[i:])
return 0
26 changes: 19 additions & 7 deletions bcipy/helpers/tests/test_task.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import unittest

from typing import List
from collections import Counter
from mockito import unstub, mock, when, verify, verifyStubbedInvocationsAreUsed
from typing import List

import numpy as np
import psychopy
from mockito import mock, unstub, verify, verifyStubbedInvocationsAreUsed, when

from bcipy.acquisition import LslAcquisitionClient
from bcipy.acquisition.record import Record
from bcipy.task.exceptions import InsufficientDataException

from bcipy.helpers.task import (_float_val,
calculate_stimulation_freq, construct_triggers,
from bcipy.helpers.task import (_float_val, calculate_stimulation_freq,
consecutive_incorrect, construct_triggers,
generate_targets, get_data_for_decision,
get_key_press, target_info)
from bcipy.task.exceptions import InsufficientDataException


class TestCalculateStimulationFreq(unittest.TestCase):
Expand Down Expand Up @@ -319,5 +317,19 @@ def test_get_data_for_decision_throws_insufficient_data_error_if_data_query_out_
get_data_for_decision(inquiry_timing, self.daq)


class TestUtils(unittest.TestCase):
"""Tests for utility functions"""

def test_consecutive_incorrect(self):
"""Test calculation of consecutive incorrect"""
self.assertEqual(
0, consecutive_incorrect(target_text='', spelled_text=''))
self.assertEqual(0, consecutive_incorrect('WORLD', ''))
self.assertEqual(0, consecutive_incorrect('WORLD', 'W'))
self.assertEqual(0, consecutive_incorrect('WORLD', 'WORLD'))
self.assertEqual(1, consecutive_incorrect('WORLD', 'H'))
self.assertEqual(2, consecutive_incorrect('WORLD', 'WOHL'))


if __name__ == '__main__':
unittest.main()
8 changes: 8 additions & 0 deletions bcipy/parameters/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,14 @@
"recommended_values": "",
"type": "int"
},
"max_incorrect": {
"value": "5",
"section": "bci_config",
"readableName": "Maximum Number of Incorrect Selections",
"helpTip": "The maximum number of consecutive incorrect selections for copy/spelling tasks. The task will end if this number is reached.",
"recommended_values": "",
"type": "int"
},
"decision_threshold": {
"value": "0.8",
"section": "bci_config",
Expand Down
15 changes: 13 additions & 2 deletions bcipy/task/paradigm/rsvp/copy_phrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from bcipy.helpers.session import session_excel
from bcipy.helpers.stimuli import InquirySchedule, StimuliOrder
from bcipy.helpers.symbols import BACKSPACE_CHAR, alphabet
from bcipy.helpers.task import (construct_triggers, fake_copy_phrase_decision,
from bcipy.helpers.task import (consecutive_incorrect, construct_triggers,
fake_copy_phrase_decision,
get_device_data_for_decision, get_user_input,
relative_triggers, target_info,
trial_complete_message)
Expand Down Expand Up @@ -94,7 +95,8 @@ class RSVPCopyPhraseTask(Task):
'font', 'fixation_color', 'trigger_type',
'filter_high', 'filter_low', 'filter_order', 'notch_filter_frequency', 'down_sampling_rate', 'prestim_length',
'is_txt_stim', 'lm_backspace_prob', 'backspace_always_shown',
'decision_threshold', 'max_inq_len', 'max_inq_per_series', 'max_minutes', 'max_selections', 'min_inq_len',
'decision_threshold', 'max_inq_len', 'max_inq_per_series', 'max_minutes', 'max_selections', 'max_incorrect',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a reminder to myself, we need to add this parameter to MatrixCopyPhrase in the orchestrator PR. I have since separated them.

'min_inq_len',
'show_feedback', 'feedback_duration',
'show_preview_inquiry', 'preview_inquiry_isi', 'preview_inquiry_error_prob',
'preview_inquiry_key_input', 'preview_inquiry_length', 'preview_inquiry_progress_method',
Expand Down Expand Up @@ -416,6 +418,15 @@ def check_stop_criteria(self) -> bool:
'(configured with the max_selections parameter)')
return False

if consecutive_incorrect(
target_text=self.copy_phrase,
spelled_text=self.spelled_text) >= self.parameters.get(
'max_incorrect', 3):
self.logger.info(
'Max number of consecutive incorrect selections reached '
'(configured with the max_incorrect parameter)')
return False

return True

def next_target(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions bcipy/task/tests/paradigm/rsvp/test_copy_phrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setUp(self):
'max_minutes': 20,
'min_inq_len': 1,
'max_selections': 50,
'max_incorrect': 10,
'notch_filter_frequency': 60.0,
'preview_inquiry_isi': 1.0,
'preview_inquiry_key_input': 'space',
Expand Down
Loading