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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. col_base = _G[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. col_votes = {}
  52. do_debug = false
  53. end
  54. -- this function is called when the box is uninitialized
  55. function uninitialize(box)
  56. end
  57. function process(box)
  58. -- loops until box is stopped
  59. while box:keep_processing() do
  60. -- first, parse the timeline stream
  61. for stimulation = 1, box:get_stimulation_count(2) do
  62. -- gets the received stimulation
  63. local identifier, date, duration = box:get_stimulation(2, 1)
  64. -- discards it
  65. box:remove_stimulation(2, 1)
  66. if identifier == segment_start then
  67. if do_debug then
  68. box:log("Info", string.format("Trial start"))
  69. box:log("Info", string.format("Clear votes"))
  70. end
  71. -- zero the votes
  72. col_votes = {}
  73. row_votes = {}
  74. target_fifo = List.new()
  75. -- fixme fixed 20
  76. for i = 0,20 do
  77. col_votes[i] = 0
  78. row_votes[i] = 0
  79. end
  80. segment_status = 1
  81. end
  82. -- Does the identifier code a flash? if so, put into fifo
  83. if segment_status == 1 and identifier >= row_base and identifier <= OVTK_StimulationId_LabelEnd then
  84. -- assume rows before cols
  85. if identifier < col_base then
  86. local t = {"row", identifier - row_base}
  87. List.pushright(target_fifo,t)
  88. if do_debug then
  89. box:log("Info", string.format("Push row target %d", identifier - row_base ))
  90. end
  91. else
  92. local t = {"col", identifier - col_base}
  93. List.pushright(target_fifo,t)
  94. if do_debug then
  95. box:log("Info", string.format("Push col target %d", identifier - col_base ))
  96. end
  97. end
  98. end
  99. if identifier == segment_stop then
  100. if do_debug then
  101. box:log("Info", string.format("Trial stop"))
  102. end
  103. segment_status = 2
  104. end
  105. end
  106. -- then parse the classifications
  107. for stimulation = 1, box:get_stimulation_count(1) do
  108. -- gets the received stimulation
  109. local identifier, date, duration = box:get_stimulation(1, 1)
  110. -- discards it
  111. box:remove_stimulation(1, 1)
  112. -- Is it an in-class prediction?
  113. if identifier == OVTK_StimulationId_Target then
  114. local t = List.popleft(target_fifo)
  115. if do_debug then
  116. box:log("Info", string.format("Pred fifo %s %d is target", t[1], t[2]))
  117. end
  118. if t[1]=="row" then
  119. row_votes[t[2]] = row_votes[t[2]] + 1
  120. else
  121. col_votes[t[2]] = col_votes[t[2]] + 1
  122. end
  123. end
  124. if identifier == OVTK_StimulationId_NonTarget then
  125. local t = List.popleft(target_fifo)
  126. if do_debug then
  127. box:log("Info", string.format("Pred fifo %s %d is nontarget", t[1], t[2]))
  128. end
  129. end
  130. end
  131. if segment_status == 2 and List.isempty(target_fifo) then
  132. -- output the vote after the segment end when we've matched all predictions
  133. local maxRowIdx, maxRowValue = arrayMax(row_votes)
  134. local maxColIdx, maxColValue = arrayMax(col_votes)
  135. if maxRowValue == 0 and maxColValue == 0 then
  136. box:log("Warning", string.format("Classifier predicted 'no p300' for all flashes of the trial"));
  137. end
  138. if do_debug then
  139. local rowVotes = 0
  140. local colVotes = 0
  141. for ir, val in pairs(row_votes) do
  142. rowVotes = rowVotes + val
  143. end
  144. for ir, val in pairs(col_votes) do
  145. colVotes = colVotes + val
  146. end
  147. box:log("Info", string.format("Vote [%d %d] wt [%d,%d]", maxRowIdx+row_base, maxColIdx+col_base, maxRowValue, maxColValue))
  148. box:log("Info", string.format(" Total [%d %d]", rowVotes, colVotes))
  149. end
  150. local now = box:get_current_time()
  151. box:send_stimulation(1, maxRowIdx + row_base, now, 0)
  152. box:send_stimulation(2, maxColIdx + col_base, now, 0)
  153. segment_status = 0
  154. end
  155. box:sleep()
  156. end
  157. end