Exemple #1
0
    def test_min_inquiries(self):
        """Test min inquiries criteria."""

        alphabet = ['a', 'b', 'c', 'd']
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq=2)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq=10),
                ProbThresholdCriteria(threshold=0.8)
            ])

        stimuli_agent = NBestStimuliAgent(alphabet=alphabet, len_query=2)

        decision_maker = mf.DecisionMaker(stimuli_agent=stimuli_agent,
                                          stopping_evaluator=stopping_criteria,
                                          state='',
                                          alphabet=['a', 'b', 'c', 'd'])

        # Initialize with initial (language model) probabilities for each letter.
        lm_prior = np.array([0.1, 0.1, 0.1, 0.1])
        decision_maker.decide(lm_prior)

        likelihood = np.array([0.05, 0.05, 0.09, 0.81])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertFalse(decision, "Not enough inquiries presented")

        likelihood = np.array([0.05, 0.05, 0.09, 0.81])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertTrue(decision)
        self.assertEqual('d', decision_maker.displayed_state)
Exemple #2
0
    def test_decision_maker(self):
        """Test default behavior"""
        alphabet = ['a', 'b', 'c', 'd']
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq=1)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq=10),
                ProbThresholdCriteria(threshold=0.8)
            ])

        stimuli_agent = NBestStimuliAgent(alphabet=alphabet, len_query=2)

        decision_maker = mf.DecisionMaker(stimuli_agent=stimuli_agent,
                                          stopping_evaluator=stopping_criteria,
                                          state='',
                                          alphabet=['a', 'b', 'c', 'd'])

        likelihood = np.array([0.2, 0.2, 0.2, 0.4])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertFalse(decision)
        self.assertEqual('.', decision_maker.state)
        self.assertEqual('', decision_maker.displayed_state)

        likelihood = np.array([0.05, 0.05, 0.1, 0.8])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertFalse(decision, "Must exceed threshold")
        self.assertEqual('..', decision_maker.state)

        likelihood = np.array([0.05, 0.05, 0.09, 0.81])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertTrue(decision)
        self.assertEqual('..d', decision_maker.state)
        self.assertEqual('d', decision_maker.displayed_state)
Exemple #3
0
    def __init__(
        self,
        state='',
        alphabet=list(string.ascii_uppercase) + ['<'] + [SPACE_CHAR],
        is_txt_stim=True,
        stimuli_timing=[1, .2],
        inq_constants=None,
        stopping_evaluator=CriteriaEvaluator.default(min_num_inq=2,
                                                     max_num_inq=10,
                                                     threshold=0.8),
        stimuli_agent=RandomStimuliAgent(
            alphabet=list(string.ascii_uppercase) + ['<'] + [SPACE_CHAR])):
        self.state = state
        self.displayed_state = self.form_display_state(state)
        self.stimuli_timing = stimuli_timing

        # TODO: read from parameters file
        self.alphabet = alphabet
        self.is_txt_stim = is_txt_stim

        self.list_series = [{
            'target': None,
            'time_spent': 0,
            'list_sti': [],
            'list_distribution': [],
            'decision': None
        }]
        self.time = 0
        self.inquiry_counter = 0

        # Stopping Criteria
        self.stopping_evaluator = stopping_evaluator

        # Stimuli Agent
        self.stimuli_agent = stimuli_agent

        self.last_selection = ''

        # Items shown in every inquiry
        self.inq_constants = inq_constants
Exemple #4
0
    def setUp(self):
        """Set up decision maker object for testing """
        alphabet = list(string.ascii_uppercase) + ['<'] + [SPACE_CHAR]
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq=1)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq=10),
                ProbThresholdCriteria(threshold=0.8)
            ])

        stimuli_agent = NBestStimuliAgent(alphabet=alphabet, len_query=10)
        self.decision_maker = DecisionMaker(
            stimuli_agent=stimuli_agent,
            stopping_evaluator=stopping_criteria,
            state='',
            alphabet=alphabet,
            is_txt_stim=True,
            stimuli_timing=[1, .2],
            inq_constants=None)

        self.evidence_fusion = EvidenceFusion(list_name_evidence=['A', 'B'],
                                              len_dist=2)
Exemple #5
0
    def test_max_inquiries(self):
        """Test max inquiries criteria."""

        alphabet = ['a', 'b', 'c', 'd']
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq=1)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq=3),
                ProbThresholdCriteria(threshold=0.8)
            ])

        stimuli_agent = NBestStimuliAgent(alphabet=alphabet, len_query=2)

        decision_maker = mf.DecisionMaker(stimuli_agent=stimuli_agent,
                                          stopping_evaluator=stopping_criteria,
                                          state='',
                                          alphabet=['a', 'b', 'c', 'd'])

        # Initialize with initial (language model) probabilities for each letter.
        lm_prior = np.array([0.1, 0.1, 0.1, 0.1])
        decision_maker.decide(lm_prior)

        likelihood = np.array([0.2, 0.2, 0.4, 0.2])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertFalse(decision)

        likelihood = np.array([0.2, 0.2, 0.4, 0.2])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertFalse(decision)

        likelihood = np.array([0.2, 0.2, 0.4, 0.2])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertTrue(
            decision,
            "Should make a decision after max inquiries are reached even if max value is below threshold"
        )
        self.assertEqual('c', decision_maker.displayed_state)
Exemple #6
0
    def test_decision_maker_threshold(self):
        """Threshold should be configurable"""
        alphabet = ['a', 'b', 'c', 'd']
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq=1)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq=10),
                ProbThresholdCriteria(threshold=0.5)
            ])

        stimuli_agent = NBestStimuliAgent(alphabet=alphabet, len_query=2)

        decision_maker = mf.DecisionMaker(stimuli_agent=stimuli_agent,
                                          stopping_evaluator=stopping_criteria,
                                          state='',
                                          alphabet=['a', 'b', 'c', 'd'])
        # Initialize with initial (language model) probabilities for each letter.
        lm_prior = np.array([0.1, 0.1, 0.1, 0.1])
        decision_maker.decide(lm_prior)

        likelihood = np.array([0.2, 0.6, 0.1, 0.1])
        decision, _arg = decision_maker.decide(likelihood)
        self.assertTrue(decision, "Value should be above configured threshold")
        self.assertEqual('b', decision_maker.displayed_state)
    def __init__(self,
                 min_num_inq,
                 max_num_inq,
                 signal_model=None,
                 fs=300,
                 k=2,
                 alp=None,
                 evidence_names=['LM', 'ERP'],
                 task_list=[('I_LOVE_COOKIES', 'I_LOVE_')],
                 lmodel=None,
                 is_txt_stim=True,
                 device_name='LSL',
                 device_channels=None,
                 stimuli_timing=[1, .2],
                 decision_threshold=0.8,
                 backspace_prob=0.05,
                 backspace_always_shown=False,
                 filter_high=45,
                 filter_low=2,
                 filter_order=2,
                 notch_filter_frequency=60):

        self.conjugator = EvidenceFusion(evidence_names, len_dist=len(alp))

        inq_constants = []
        if backspace_always_shown and BACKSPACE_CHAR in alp:
            inq_constants.append(BACKSPACE_CHAR)

        # Stimuli Selection Module
        stopping_criteria = CriteriaEvaluator(
            continue_criteria=[MinIterationsCriteria(min_num_inq)],
            commit_criteria=[
                MaxIterationsCriteria(max_num_inq),
                ProbThresholdCriteria(decision_threshold)
            ])

        # TODO: Parametrize len_query in the future releases!
        stimuli_agent = NBestStimuliAgent(alphabet=alp, len_query=10)

        self.decision_maker = DecisionMaker(
            stimuli_agent=stimuli_agent,
            stopping_evaluator=stopping_criteria,
            state=task_list[0][1],
            alphabet=alp,
            is_txt_stim=is_txt_stim,
            stimuli_timing=stimuli_timing,
            inq_constants=inq_constants)

        self.alp = alp
        # non-letter target labels include the fixation cross and calibration.
        self.nonletters = ['+', 'PLUS', 'calibration_trigger']
        self.valid_targets = set(self.alp)

        self.signal_model = signal_model
        self.sampling_rate = fs
        self.downsample_rate = k
        self.filter_high = filter_high
        self.filter_low = filter_low
        self.filter_order = filter_order
        self.notch_filter_frequency = notch_filter_frequency

        self.mode = 'copy_phrase'
        self.task_list = task_list
        self.lmodel = lmodel
        self.channel_map = analysis_channels(device_channels, device_name)
        self.backspace_prob = backspace_prob