-
Notifications
You must be signed in to change notification settings - Fork 0
/
stroop-device.lisp
437 lines (353 loc) · 12 KB
/
stroop-device.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
;;; ------------------------------------------------------------------
;;; STROOP-DEVICE.LISP
;;; ------------------------------------------------------------------
;;; A class that provide an ACT-R GUI interface for a modified
;;; version of the Simon Task.
;;; ------------------------------------------------------------------
(defparameter *using-swank* t)
;;; ----------------------------------------------------------------
;;; ACT-R Functions
;;; ----------------------------------------------------------------
(defun act-r-loaded? ()
"Cheap hack to check whether ACTR is loaded"
(and (fboundp 'run-n-events)
(fboundp 'start-environment)))
(defparameter *d1* 1)
(defparameter *d2* 1)
(defparameter *error-penalty* -1)
(defun bg-reward-hook (production reward time)
(declare (ignore time))
(let* ((pname (symbol-name production))
(start (subseq pname 0 4)))
(cond ((string-equal start "PROC")
(* *d1* reward))
((string-equal start "DONT")
(* *d2* reward))
(t
0.0))))
;; ---------------------------------------------------------------- ;;
;; Some utilities
;; ---------------------------------------------------------------- ;;
(defun pick (lst)
"Picks up an element from a list"
(when (listp lst)
(elt lst (random (length lst)))))
(defun scramble (lst &optional (sofar nil))
"Scrambles a list of different elements"
(if (null lst)
sofar
(let ((picked (pick lst)))
(scramble (remove picked lst) (cons picked sofar)))))
(defun scramble* (lst)
"Scrambles any list of objects"
(let ((l (length lst))
(pos nil))
(dotimes (i l)
(push i pos))
(mapcar #'(lambda (x) (elt lst x)) (scramble pos))))
(defun mean (&rest nums)
(when (every #'numberp nums)
(/ (reduce #'+ nums)
(length nums))))
(defun divide-into-pairs (lst &optional (partial nil) (open nil))
"Recursively divides a list into pairs"
(cond ((null lst)
(append partial open))
((= (length (car open)) 2)
(divide-into-pairs (rest lst)
(append partial open)
(list (list (first lst)))))
((= (length (car open)) 1)
(divide-into-pairs (rest lst)
partial
(list (list (caar open)
(first lst)))))
(t
(divide-into-pairs (rest lst)
partial
(list (list (first lst)))))))
;;; A stroop-trial of the form (shape circle location left)
(defparameter *default-stroop-rule* 'color)
(defparameter *responses* '((f . left) (j . right)))
(defparameter *default-stroop-congruent-stimuli* '((color blue word blue)
(color red word red)))
(defparameter *default-stroop-incongruent-stimuli* '((color blue word red)
(color red word blue)))
(defun stroop-stimulus? (lst)
(and (consp lst)
(evenp (length lst))))
(defun stimulus-correct-response (stimulus &optional (rule *default-stroop-rule*))
(when (stroop-stimulus? stimulus)
(second (assoc rule (divide-into-pairs stimulus)))))
(defun stimulus-congruent? (stimulus)
(let* ((chunk (divide-into-pairs stimulus))
(col (second (assoc 'color chunk)))
(wrd (second (assoc 'word chunk))))
(when (equalp col wrd)
t)))
(defun stimulus-incongruent? (stimulus)
(let* ((chunk (divide-into-pairs stimulus))
(col (second (assoc 'color chunk)))
(wrd (second (assoc 'word chunk))))
(unless (equalp col wrd)
t)))
(defun stimulus-type (stimulus)
(if (stimulus-congruent? stimulus)
'congruent
'incongruent))
(defun make-stroop-trial (stim)
(let ((trial (list stim 0 0 (stimulus-correct-response stim) nil nil)))
(set-trial-type trial (stimulus-type stim))
trial))
(defun trial-stimulus (trial)
(nth 0 trial))
(defun set-trial-stimulus (trial stimulus)
(when (stroop-stimulus? stimulus)
(setf (nth 0 trial) stimulus)))
(defun trial-onset-time (trial)
(nth 1 trial))
(defun set-trial-onset-time (trial tme)
(setf (nth 1 trial) tme))
(defun trial-response-time (trial)
(nth 2 trial))
(defun set-trial-response-time (trial tme)
(setf (nth 2 trial) tme))
(defun trial-correct-response (trial &optional (task 'color))
(let* ((stim (trial-stimulus trial))
(pairs (divide-into-pairs stim)))
(cadr (assoc task pairs))))
(defun set-trial-correct-response (trial response)
(setf (nth 3 trial) response))
(defun trial-actual-response (trial)
(nth 4 trial))
(defun set-trial-actual-response (trial response)
(setf (nth 4 trial) response))
(defun trial-type (trial)
(nth 5 trial))
(defun set-trial-type (trial typ)
(setf (nth 5 trial) typ))
(defun trial-congruent? (trial)
(equalp (trial-type trial) 'congruent))
(defun generate-stimuli ()
(let ((result nil))
(dolist (stimulus *default-stroop-congruent-stimuli*)
(dotimes (i 75)
(push (copy-seq stimulus) result)))
(dolist (stimulus *default-stroop-incongruent-stimuli*)
(dotimes (i 25)
(push (copy-seq stimulus) result)))
result))
(defun generate-trials (stim-list)
(mapcar #'make-stroop-trial stim-list))
(defun trial-rt (trial)
(- (trial-response-time trial)
(trial-onset-time trial)))
(defun trial-accuracy (trial &optional (task 'color))
(let ((act-string (format nil "~A" (trial-actual-response trial)))
(cor-string (format nil "~A" (trial-correct-response trial task))))
(if (search act-string cor-string)
1
0)))
(defclass stroop-task ()
((phase :accessor task-phase
:initform nil)
(index :accessor index
:initform nil)
(trials :accessor trials
:initform (generate-trials (generate-stimuli)))
(current-trial :accessor current-trial
:initform nil)
(experiment-log :accessor experiment-log
:initform nil))
(:documentation "A manager for the PSS task"))
(defmethod init ((task stroop-task))
"Initializes the PSS task manager"
(unless (null (trials task))
(setf (index task) 0)
(setf (experiment-log task) nil)
(setf (trials task) (scramble* (trials task)))
(setf (current-trial task)
(nth (index task) (trials task)))
(setf (task-phase task) 'stimulus)))
(defmethod respond ((task stroop-task) response)
"Records a response in the PSS task"
(unless (null (current-trial task))
(let* ((trial (current-trial task)))
(set-trial-actual-response trial response)
(when (act-r-loaded?)
(set-trial-response-time (current-trial task)
(mp-time))
(let ((goal (chunk-slot-value-fct (car (buffer-chunk-fct '(goal)))
'focus)))
(if (= 1 (trial-accuracy (current-trial task) goal))
(trigger-reward 1)
(trigger-reward -1)))
(schedule-event-relative 0 #'next :params (list task))))))
(defmethod next ((task stroop-task))
"Moves to the next step in a Stroop Task timeline"
(cond ((equal (task-phase task) 'stimulus)
(setf (task-phase task) 'pause)
(push (current-trial task) (experiment-log task))
(setf (current-trial task) nil)
(when (act-r-loaded?)
(schedule-event-relative 1 'next :params (list task))))
((equal (task-phase task) 'pause)
(incf (index task))
(cond ((>= (index task) (length (trials task)))
(setf (task-phase task) 'done))
(t
(setf (task-phase task) 'stimulus)
(setf (current-trial task) (nth (index task)
(trials task)))
(when (act-r-loaded?)
(set-trial-onset-time (current-trial task)
(mp-time)))))))
(when (act-r-loaded?)
(schedule-event-relative 0 'proc-display :params nil)))
(defmethod device-handle-keypress ((task stroop-task) key)
"Does nothing"
(declare (ignore task))
nil)
(defmethod device-handle-click ((task stroop-task))
"Does nothing"
(declare (ignore task))
nil)
(defmethod device-move-cursor-to ((task stroop-task) pos)
"Does nothing"
(declare (ignore task))
nil)
(defmethod device-speak-string ((task stroop-task) str)
"Responds to the model's vocal answers"
(respond task (intern (string-upcase (format nil "~a" str)))))
(defmethod get-mouse-coordinates ((task stroop-task))
"Does nothing"
(declare (ignore task))
(vector 0 0))
(defmethod cursor-to-vis-loc ((task stroop-task))
"Does nothing"
(declare (ignore task))
nil)
(defmethod build-vis-locs-for ((task stroop-task) vismod)
(if (equalp (task-phase task) 'stimulus)
(build-vis-locs-for (trial-stimulus (current-trial task))
vismod)
(build-vis-locs-for (task-phase task)
vismod)))
(defmethod build-vis-locs-for ((trial list) vismod)
(let ((results nil))
(push `(isa stroop-stimulus-location
kind stroop-stimulus
value stimulus
screen-x 0
screen-y 0
height 400
width 400
,@trial)
results)
(define-chunks-fct results)))
(defmethod build-vis-locs-for ((phase symbol) vismod)
(let ((results nil))
(push `(isa stroop-stimulus-location
kind screen
value ,phase
color black
screen-x 0
screen-y 0
height 400
width 400)
results)
(define-chunks-fct results)))
(defmethod vis-loc-to-obj ((task stroop-task) vis-loc)
"Transforms a visual-loc into a visual object"
(let ((new-chunk nil)
(phase (task-phase task))
(stimulus (trial-stimulus (current-trial task))))
(if (equal phase 'stimulus)
(setf new-chunk (vis-loc-to-obj stimulus vis-loc))
(setf new-chunk (vis-loc-to-obj phase vis-loc)))
(fill-default-vis-obj-slots new-chunk vis-loc)
new-chunk))
(defmethod vis-loc-to-obj ((stimulus list) vis-loc)
"Transforms a stimulus into a visual object"
(first (define-chunks-fct
`((isa stroop-stimulus
kind stroop-stimulus
,@stimulus
)))))
(defmethod vis-loc-to-obj ((phase symbol) vis-loc)
"Transforms a stimulus into a visual object"
(first (define-chunks-fct
`((isa stroop-screen
kind stroop-screen
value ,phase
)))))
;;; ------------------------------------------------------------------
;;; STATS
;;; ------------------------------------------------------------------
(defun analyze-log (log &optional (task 'color))
"Analyzes the log of a single run"
(let* ((incong (remove-if #'trial-congruent? log))
(cong (remove-if-not #'trial-congruent? log))
(correct-incong (remove-if-not #'(lambda (x) (= (trial-accuracy x task) 1))
incong))
(correct-cong (remove-if-not #'(lambda (x) (= (trial-accuracy x task) 1))
cong)))
(if (or (null correct-incong)
(null correct-cong))
;; If we don't have enough trials, return NA
;;'((:congruent :na) (:incongruent :na))
'(:na :na :na :na)
;; Otherwise, compute accuracies and RTs (on correct trials)
(let* ((cong-acc (apply #'mean (mapcar #'(lambda (x) (trial-accuracy x task)) cong)))
(incong-acc (apply #'mean (mapcar #'(lambda (x) (trial-accuracy x task)) incong)))
(cong-rt (apply #'mean (mapcar #'trial-rt correct-cong)))
(incong-rt (apply #'mean (mapcar #'trial-rt correct-incong))))
(list cong-acc cong-rt incong-acc incong-rt)))))
(defun result? (lst)
"A list is a result IFF it's made of at least four numbers"
(and (>= (length lst) 4)
(every #'(lambda (x) (or (numberp x)
(keywordp x)))
lst)))
(defun result-congruent-accuracy (res)
(nth 0 res))
(defun result-congruent-rt (res)
(nth 1 res))
(defun result-incongruent-accuracy (res)
(nth 2 res))
(defun result-incongruent-rt (res)
(nth 3 res))
(defun average-results (results)
"Averages values across a list of results"
(if (every #'result? results)
(let* ((meanres nil)
(n (length (first results))))
(dotimes (i n (reverse meanres))
(let ((avg
(float
(apply 'mean
(remove-if-not #'numberp
(mapcar #'(lambda (x) (nth i x))
results))))))
(push avg meanres))))
(progn
(format t " Not every results is a result")
(setf *results* results))))
(defparameter *results* nil)
(defun stroop-reload ()
"Precise reload"
(reload)
(install-device (make-instance 'stroop-task))
(init (current-device))
(proc-display))
(defun simulate (n &optional (params nil))
"Simulate N trials with specific PARAMS"
(let ((results nil))
(dotimes (j n results)
(reload)
(when params
(sgp-fct params))
(sgp :v nil) ;; Ensures no verbose output
(no-output (run 1000))
(let ((partial (analyze-log (experiment-log (current-device)))))
(push partial results)))))