You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p300-tactile-accumulator.lua 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. function arrayMax(a)
  2. if #a == 0 then return nil, nil end
  3. local maxIdx, maxValue = 0, a[0]
  4. for i = 1, (#a -1 ) do
  5. if maxValue < a[i] then
  6. maxIdx, maxValue = i, a[i]
  7. end
  8. end
  9. return maxIdx, maxValue
  10. end
  11. -- For handling target fifo
  12. List = {}
  13. function List.new ()
  14. return {first = 0, last = -1}
  15. end
  16. function List.pushright (list, value)
  17. local last = list.last + 1
  18. list.last = last
  19. list[last] = value
  20. end
  21. function List.popleft (list)
  22. local first = list.first
  23. if first > list.last then
  24. error("list is empty")
  25. end
  26. local value = list[first]
  27. list[first] = nil -- to allow garbage collection
  28. list.first = first + 1
  29. return value
  30. end
  31. function List.isempty (list)
  32. if list.first > list.last then
  33. return true
  34. else
  35. return false
  36. end
  37. end
  38. -- this function is called when the box is initialized
  39. function initialize(box)
  40. dofile(box:get_config("${Path_Data}") .. "/plugins/stimulation/lua-stimulator-stim-codes.lua")
  41. row_base = _G[box:get_setting(2)]
  42. n_tactilos = box:get_setting(3)
  43. segment_start = _G[box:get_setting(4)]
  44. segment_stop = _G[box:get_setting(5)]
  45. -- 0 inactive, 1 segment started, 2 segment stopped (can vote)
  46. segment_status = 0
  47. -- the idea is to push the flash states to the fifo, and when predictions arrive (with some delay), they are matched in oldest-first fashion.
  48. target_fifo = List.new()
  49. -- box:log("Info", string.format("pop %d %d", id[1], id[2]))
  50. row_votes = {}
  51. do_debug = false
  52. end
  53. -- this function is called when the box is uninitialized
  54. function uninitialize(box)
  55. end
  56. function process(box)
  57. -- loops until box is stopped
  58. while box:keep_processing() do
  59. -- first, parse the timeline stream
  60. for stimulation = 1, box:get_stimulation_count(2) do
  61. -- gets the received stimulation
  62. local identifier, date, duration = box:get_stimulation(2, 1)
  63. -- discards it
  64. box:remove_stimulation(2, 1)
  65. if identifier == segment_start then
  66. if do_debug then
  67. box:log("Info", string.format("Trial start"))
  68. box:log("Info", string.format("Clear votes"))
  69. end
  70. -- zero the votes
  71. row_votes = {}
  72. target_fifo = List.new()
  73. -- fixme fixed 20
  74. for i = 0,20 do
  75. row_votes[i] = 0
  76. end
  77. segment_status = 1
  78. end
  79. -- Does the identifier code a flash? if so, put into fifo
  80. if segment_status == 1 and identifier >= row_base and identifier <= OVTK_StimulationId_LabelEnd then
  81. -- assume rows before cols
  82. if identifier < (row_base + n_tactilos) then
  83. local t = {"row", identifier - row_base}
  84. List.pushright(target_fifo,t)
  85. if do_debug then
  86. box:log("Info", string.format("Push row target %d", identifier - row_base ))
  87. end
  88. end
  89. end
  90. if identifier == segment_stop then
  91. if do_debug then
  92. box:log("Info", string.format("Trial stop"))
  93. end
  94. segment_status = 2
  95. end
  96. end
  97. -- then parse the classifications
  98. for stimulation = 1, box:get_stimulation_count(1) do
  99. -- gets the received stimulation
  100. local identifier, date, duration = box:get_stimulation(1, 1)
  101. -- discards it
  102. box:remove_stimulation(1, 1)
  103. -- Is it an in-class prediction?
  104. if identifier == OVTK_StimulationId_Target then
  105. local t = List.popleft(target_fifo)
  106. if do_debug then
  107. box:log("Info", string.format("Pred fifo %s %d is target", t[1], t[2]))
  108. end
  109. if t[1]=="row" then
  110. row_votes[t[2]] = row_votes[t[2]] + 1
  111. end
  112. end
  113. if identifier == OVTK_StimulationId_NonTarget then
  114. local t = List.popleft(target_fifo)
  115. if do_debug then
  116. box:log("Info", string.format("Pred fifo %s %d is nontarget", t[1], t[2]))
  117. end
  118. end
  119. end
  120. if segment_status == 2 and List.isempty(target_fifo) then
  121. -- output the vote after the segment end when we've matched all predictions
  122. local maxRowIdx, maxRowValue = arrayMax(row_votes)
  123. if maxRowValue == 0 then
  124. box:log("Warning", string.format("Classifier predicted 'no p300' for all flashes of the trial"));
  125. end
  126. if do_debug then
  127. local rowVotes = 0
  128. for ir, val in pairs(row_votes) do
  129. rowVotes = rowVotes + val
  130. end
  131. box:log("Info", string.format("Vote [%d] wt [%d]", maxRowIdx+row_base, maxRowValue))
  132. end
  133. local now = box:get_current_time()
  134. box:send_stimulation(1, maxRowIdx + row_base, now, 0)
  135. segment_status = 0
  136. end
  137. box:sleep()
  138. end
  139. end