Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

exploration.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Jul 24 16:16:05 2018
  5. @author: ale
  6. """
  7. import os
  8. import random
  9. from aux import log
  10. from paradigm import Paradigm
  11. class Exploration(Paradigm):
  12. '''
  13. Paradigm to discriminate between several mental states. Different stimuli
  14. are presented based on the configuration file.
  15. '''
  16. def __init__(self, **kwargs):
  17. '''
  18. Initilalize the paradigm
  19. Variables:
  20. states_idx: string (index of the selected states)
  21. states: string (name of the selected states and of the corresponding wav files)
  22. audio_path: string (path where the audio files are stored)
  23. num_stim: int (number of stimuli presented for each state)
  24. mode: string (mode of the paradigm)
  25. list_of_index: list[int] (indexes of stimuli that will be presented)
  26. list_of_stimuli: list[string] (stimuli that will be presented)
  27. current_stimulus: string (current stimulus)
  28. current_idx: int (index of current stimulus)
  29. history: list[(int,str)] (history of the stimulus presentation: index, stimulus)
  30. '''
  31. super().__init__(**kwargs)
  32. # read configuration file
  33. config = self.params.paradigms.exploration
  34. all_states = config.states
  35. self.states_idx = config.selected_states
  36. self.states = [all_states[idx] for idx in self.states_idx]
  37. self.audio_path = config.audio_path
  38. self.num_stim = config.number_of_stim
  39. self.mode = config.mode[config.selected_mode]
  40. self.list_of_index = self._create_list()
  41. self.list_of_stimuli = [all_states[idx] for idx in self.list_of_index]
  42. self.current_stimulus = self.list_of_stimuli.pop(0)
  43. self.current_idx = self.list_of_index.pop(0)
  44. self.start_triger = [110, 111]
  45. self.history = []
  46. def _create_list(self):
  47. '''
  48. Create the list of stimuli randomizing the states
  49. Return:
  50. all_idx: list (list of randomizing states)
  51. '''
  52. all_idx = self.states_idx * self.num_stim
  53. random.shuffle(all_idx)
  54. return all_idx
  55. def process_result(self, decision=None, audio=False):
  56. '''
  57. Append the last index and string stimulus to history.
  58. Update the current_idx and current_stimulus variables popping from corresponding lists
  59. Parameters:
  60. decision: boolean (value of the last decision)
  61. Return:
  62. finish: boolean (True if the paradigm is finished, False otherwise)
  63. '''
  64. self.history.append([self.current_idx,self.current_stimulus])
  65. if self.list_of_stimuli:
  66. self.current_stimulus = self.list_of_stimuli.pop(0)
  67. self.current_idx = self.list_of_index.pop(0)
  68. finish = False
  69. else:
  70. finish = True
  71. self.current_stimulus = ''
  72. log.info('Stimulus list is empty.')
  73. return finish
  74. def present_stimulus(self, audio=True):
  75. '''
  76. Play auditorly the current stimulus using the play function defined in
  77. the abstract class
  78. Return:
  79. current_stimulus: string (the stimulus that is presented)
  80. '''
  81. if audio:
  82. stim_path = self.audio_path+os.sep+self.current_stimulus+'.wav'
  83. try:
  84. self._play_sound(stim_path,self.current_idx,self.current_idx+100)
  85. except Exception as e:
  86. raise e
  87. return self.current_stimulus
  88. def present_start(self, audio=True):
  89. '''
  90. Play auditorly the start stimulus using the play function defined in
  91. the abstract class
  92. Return:
  93. None
  94. '''
  95. if audio:
  96. stim_path = self.audio_path.split('/exploration')[0] + os.sep + 'beginning.wav'
  97. try:
  98. self._play_sound(stim_path, self.start_triger, self.start_triger)
  99. except Exception as e:
  100. raise e
  101. return None
  102. def get_current_state(self):
  103. '''
  104. Return the current state
  105. Return:
  106. current_idx: str (index of the current state)
  107. current_stimulus str (current state)
  108. '''
  109. return str(self.current_idx), self.current_stimulus
  110. def get_mode(self):
  111. '''
  112. Return the mode of the used paradigm
  113. Return:
  114. mode: string (mode of the paradigm)
  115. '''
  116. return self.mode
  117. def save_log(self):
  118. '''
  119. Save the log
  120. '''
  121. pass