123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jul 24 13:32:51 2018
- @author: ale
- """
- # import logging
- from aux import log
- from paradigm import Paradigm
- class TrainingColorSpeller(Paradigm):
- '''
- Paradigm to train the user capability of using the color speller.
- Letters are grouped in different colors. Based on a training string and a
- confirmation method the algorithm will provide a list of the stimuli.
- '''
- def __init__(self, pyttsx_rate=100, **kwargs):
- '''
- Initialize the paradigm.
-
- Variables:
- trigger: list[int] (triggers for start and end of stimulus presentation)
- states: list[string] (structure of the grid with the letters)
- training_string: string (string that will be spelled)
- mode: string (mode of the paradigm)
- num_confirmation_yes: int (number of yes needed for selecting)
- num_confirmation_no: int (number of no needed for skipping)
- list_of_stimuli: list[str] (stimuli that will be presented)
- current_string = string (string currently written)
- current_selection = string (string of the current selection)
- history: list[string] (history of the presented stimuli)
- '''
- super().__init__(**kwargs)
-
-
- self.engine.setProperty('rate',pyttsx_rate)
- log.info(f'pyttsx3 rate is: {pyttsx_rate}')
- self.trigger = [1, 2]
-
- config = self.params.paradigms.training_color
-
- self.states = config.states
- self.training_string = config.training_string.upper()
- self.mode = config.mode[config.selected_mode]
- self.num_confirmation_yes = config.confirmation_yes
- self.num_confirmation_no = config.confirmation_no
- self.list_of_stimuli = []
- self.response_list = []
- self._create_list(self.training_string)
- self.current_string = ''
- self.current_selection = self.list_of_stimuli.pop(0)
- self.current_response = self.response_list.pop(0 )
-
- self.history = []
-
- def _create_list(self,string):
- '''
- Create the list of stimuli needed for spelling a string. The list will
- use the num_confirmation_yes and no for selecting or skipping.
-
- Parameters:
- string: str (string to spell)
-
- '''
- for train_letter in string:
- for row in self.states:
- if train_letter in row[1]:
- for i in range(self.num_confirmation_yes):
- self.list_of_stimuli.append(row[0])
- self.response_list.append('yes')
- for letter in row[1]:
- if train_letter == letter:
- for i in range(self.num_confirmation_yes):
- self.list_of_stimuli.append(letter)
- self.response_list.append('yes')
- break
- else:
- for i in range(self.num_confirmation_no):
- self.list_of_stimuli.append(letter)
- self.response_list.append('no')
-
- break
- else:
- for i in range(self.num_confirmation_no):
- self.list_of_stimuli.append(row[0])
- self.response_list.append('no')
-
-
- def process_result(self,decision=None):
- '''
- Append the last stimulus to history.
- Update the current_stimulus variable
-
- Parameters:
- decision: boolean (value of the last decision)
-
- Return:
- finish: boolean (True if the paradigm is finished, False otherwise)
-
- '''
-
- self.history.append([self.current_selection])
- if len(self.current_selection)==1 and (
- not self.list_of_stimuli or len(self.list_of_stimuli[0])>1):
- self.current_string += self.current_selection
-
- if self.list_of_stimuli:
- self.current_selection = self.list_of_stimuli.pop(0)
- self.current_response = self.response_list.pop(0)
- finish = False
- # log.info(f'History: {self.history}')
- else:
- finish = True
- self.current_selection = ''
- log.info('Stimulus list is empty.')
-
- return finish
-
-
- def present_stimulus(self, audio=True):
- '''
- Play auditorly the current stimulus using the say function defined in
- the abstract class
-
- Return:
- current_stimulus: string (the stimulus that is presented)
- '''
- '''stimulus = self.current_selection
-
- if audio is True:
-
- if not self.current_string:
- str2s = ''
- elif self.current_string[-1] == ' ' or len(self.list_of_stimuli)%10==0:
- str2s = 'Satz: '+self.current_string+'.\n Naechster: '
- else:
- str2s = ''
-
- stim2s = stimulus
-
- t2s = str2s + stim2s
-
- self._say(t2s.lower(),self.trigger[0],self.trigger[1])
-
-
- return stimulus'''
- if self.current_selection == '':
- log.warning('Stimulus list is empty!')
- return None
- stimulus = self.current_selection
- # log.info(f'Current stimulus: {stimulus}')
- if audio is True:
-
- if not self.current_string:
- str2s = ''
- stim2s = ''
- # elif self.current_string[-1] == ' ' or len(self.history)%10==0:
- elif self.current_string[-1] == ' ':
- # str2s = 'Satz: '+self.current_string
- str2s = self.current_string
- stim2s = 'Naechster: '
- else:
- str2s = ''
- stim2s = ''
-
- if stimulus == '<':
- stim2s += 'Loeschen'
- elif stimulus == ' ':
- stim2s += 'Anschlag'
- elif stimulus == '?':
- stim2s += 'Fragezeichen'
- elif stimulus == '^':
- stim2s += 'Zurueck zu den Farben?'
- elif stimulus == 'word':
- stim2s += 'Woerterbuch?'
- elif stimulus == 'end':
- stim2s += 'Programm beenden?'
- else:
- stim2s += stimulus
-
- if str2s:
- t2s = [str2s.lower(), stim2s.lower()]
- else:
- t2s = stim2s.lower()
-
- try:
- self._say(t2s, self.trigger[0], self.trigger[1])
- except:
- print('ERROR')
- raise
- return stimulus
-
-
- def get_current_state(self):
- '''
- Return the current state
-
- Return:
- current_string: str (current selected string)
- current_selection: str (current selection)
-
- '''
- return self.current_string, f'{self.current_selection}, {self.current_response}'
-
- def get_mode(self):
- '''
- Return the mode of the used paradigm
-
- Return:
- mode: string (mode of the paradigm)
-
- '''
- return self.mode
-
- def save_log(self):
- '''
- Save the log
-
- '''
- pass
|