test_paradigm2.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. '''
  2. description: testing speller
  3. author: Ioannis Vlachos
  4. date: 19.09.18
  5. Copyright (c) 2018 Ioannis Vlachos.
  6. All rights reserved.'''
  7. import importlib
  8. import logging
  9. import sys
  10. import time
  11. from random import randint
  12. import munch
  13. import pyaudio
  14. from colorlog import ColoredFormatter
  15. import aux
  16. from aux import log
  17. from paradigms import colorSpeller, exploration, question, trainingColorSpeller, feedback
  18. from multiprocessing import Array
  19. importlib.reload(trainingColorSpeller)
  20. importlib.reload(colorSpeller)
  21. importlib.reload(aux)
  22. paradigm_type = aux.args.speller
  23. # paradigm_type = 'question'
  24. params = aux.load_config()
  25. try:
  26. if paradigm_type == 'training_color':
  27. sp = trainingColorSpeller.TrainingColorSpeller(pyttsx_rate = params.speller.pyttsx_rate, params=params)
  28. elif paradigm_type == 'color':
  29. sp = colorSpeller.ColorSpeller(pyttsx_rate = params.speller.pyttsx_rate, params=params)
  30. elif paradigm_type == 'exploration':
  31. sp = exploration.Exploration(params=params)
  32. elif paradigm_type == 'question':
  33. sp = question.Question(params=params)
  34. elif paradigm_type == 'feedback':
  35. audio_fb_target = Array('d', 3)
  36. sp = feedback.Feedback(audio_fb_target, params=params)
  37. else:
  38. log.error('Speller does not exist')
  39. sys.exit(1)
  40. except Exception as e:
  41. log.error(e)
  42. log.error('Speller creation failed')
  43. raise(e)
  44. sys.exit(1)
  45. # for ii in range(10): # used to test refcounts to speller objects
  46. # log.info(f'index {ii}')
  47. # # print(sys.getrefcount(sp))
  48. # sp = exploration.Exploration()
  49. # # print(sys.getrefcount(sp))
  50. # time.sleep(.1)
  51. finish = False
  52. cnt = 0
  53. # answers = [0,0,0,1,1,1,1,1,1,0,1]
  54. # # answers = [0,1,-1,0,1,0,1,1]
  55. # answers = [1,1,0,0,0,1,1,1,0,1,1,0,1,0,1]
  56. # answers = [1,1,1,1,0,1,1,1,1,0,1] # program beenden
  57. # answers = [0,0]
  58. while not finish:
  59. # print('Current state: ' + sp.current_response)
  60. if hasattr(sp, 'current_selection'):
  61. current_selection = sp.current_selection
  62. else:
  63. current_selection = ''
  64. # Present the stimulus
  65. try:
  66. t1 = time.time()
  67. stim = sp.present_stimulus(audio=params.speller.audio)
  68. t2 = time.time()
  69. time.sleep(0.01)
  70. # print(f'Time elapsed: {t2 - t1}')
  71. # log.info(f'stim: {sp.current_stimulus}')
  72. if hasattr(sp, 'current_selection'):
  73. log.info(f'current state: {sp.current_selection}')
  74. if hasattr(sp, 'current_stimulus'):
  75. log.info(f'current state: {sp.current_stimulus}')
  76. if hasattr(sp, 'get_vocabulary'):
  77. log.info(f'current probable words: {sp.get_vocabulary()}')
  78. except Exception as e:
  79. log.error(e)
  80. # log.error('Speller failed')
  81. # sys.exit(1)
  82. if sp.mode == 'Validation' or sp.mode == 'Free' : # simulate decoder output
  83. print(cnt)
  84. # if 1:
  85. # answer = randint(0, 1)
  86. # if (cnt>=1 and cnt<2) or (cnt>=4 and cnt<=5):
  87. # answer = params.decision.unclassified
  88. # else:
  89. # answer = answers[cnt]
  90. user_answer = input(stim + '? ')
  91. if user_answer is 'y':
  92. answer = aux.decision.yes
  93. elif user_answer is 'q':
  94. break
  95. else:
  96. answer = aux.decision.no
  97. # answer = 0
  98. # if answer==0:
  99. # decision = params.decision.yes
  100. # else:
  101. # decision = params.decision.no
  102. else:
  103. answer = aux.decision.unclassified
  104. # log.info(f'decision: {params.decision(answer).name}')
  105. # log.info(sp.current_stimulus)
  106. finish = sp.process_result(aux.decision(answer).value)
  107. print(finish)
  108. # print(f'cnt: {cnt}, current selection: {current_selection}, decision: {params.decision(answer).name} , Current state: {sp.get_current_state()[0]}, {sp.get_current_state()[1]}, current string: {sp.current_string}')
  109. cnt += 1
  110. if hasattr(sp, 'current_string'):
  111. sp._say(sp.current_string.lower(), sp.trigger[0], sp.trigger[1])
  112. # Close the paradigm
  113. sp.close()
  114. matrix.terminate()