Generating an Accuracy After Each Block

806 views
Skip to first unread message

Graham

unread,
Apr 20, 2011, 12:13:45 PM4/20/11
to psychopy-users
Hi All,

I have what I hope is a fairly simple problem: I've been wondering if
there is a way to generate a message after each block of stimuli that
would report the percentage of correct responses entered in that
block.

Thanks,
Graham

Dave Britton

unread,
Apr 20, 2011, 3:21:02 PM4/20/11
to psychop...@googlegroups.com
Are you using custom code or the psychopy GUI?

Graham

unread,
Apr 20, 2011, 3:40:07 PM4/20/11
to psychopy-users
I'm working in the GUI for simplicity's sake (up until now there has
been no need to customize any of the code), but I'm open to using
custom code if that would be the only way to get it done.

Dave Britton

unread,
Apr 20, 2011, 7:38:17 PM4/20/11
to psychop...@googlegroups.com
I don't know the GUI at all, since I've always been using handwritten
code, but what I do to get this effect is to re-calculate the running
accuracy and average RT for the block after each trial (during the
block, and based on the subject's response, of course) and paint a
feedback message at the start of the next block. At the start of each
block the subject is told, "on the previous block you got X correct out
of Y. Your average response time was Z". I imagine you could prepare a
routine that does this calculation and add it to the GUI.

Graham

unread,
Apr 20, 2011, 8:55:15 PM4/20/11
to psychopy-users
Thanks for the help!
I'm feeling a little bit stupid, but coding in python is not my strong
suit: I know how to code a routine such that it will report reaction
time and correct vs incorrect response for the previous trial, but I'm
not clear on how to code a routine in order to report the mean of
those values for an entire block of trials.

Graham

unread,
Apr 20, 2011, 9:01:25 PM4/20/11
to psychopy-users
Sorry, for correct vs incorrect responses I meant to refer to totaling
the percentage of correct responses rather than taking the mean

Dave Britton

unread,
Apr 20, 2011, 11:40:52 PM4/20/11
to psychop...@googlegroups.com
Graham, here's some pseudocode for how I do it:

# Before the blocks start:
# initialize a total-holding variable and trial and block counters
totcorrect=0
ntrials=0
blocknum=0
# initialize a message
totmsg=''

for each block:
# at the start of the block, print the feedback message
if totmsg > '': # don't paint an empty message, if no blocks are completed
# display the total message
print totmsg
# or more likely, draw it onto the Window
# set the text of a textStim to totmsg
# then draw the textStim and flip the window.

# now for each trial:
# do the trial and collect the subject's answer
# .... get subjectanswer....
# now still inside the block, at the end of each trial:
if subjectanswer==correctanswer: totcorrect+=1 # add 1 to counter
of correct answers
ntrials +=1 # add 1 to counter of how many trials

# now inside the block but after all trials are done,
# and before the next block starts, set the message to display the
totals and percent:
blocknum += 1 # increment the block counter of how many blocks are done
totmsg="In block %d just completed you got %d correct out of %d, which
is a score of %3.2f percent"%(blocknum,
totcorrect,ntrials,(float(totcorrect)/float(ntrials))*100.0 )
# zero out the counters for the next block
totcorrect=0
ntrials=0
# and now continue to the start of the next block where this new
totmsg will be displayed

I guess you have to figure out how to insert the counter initialization
and increment commands into the GUI-generated code, or turn them into
little functions that you can call in the GUI

I hope this helps.
-Dave

Jonathan Peirce

unread,
Apr 21, 2011, 5:45:27 AM4/21/11
to psychop...@googlegroups.com
If you're using the Builder then the way to do this is with a code
component to generate an appropriate message (and then a textstim to
present that message). You can see the concept in the demo
ExtendedStroop, which gives feedback on every trial of the practice
trials based on the last response.

In your case, however, the feedback routine would need to come after the
loop (the block of trials) and the message needs to use the stored data
in that loop. Accessing the data from a loop is not well documented but
totally possible.

if you had a loop called 'trials', and within it a keybooard component
called 'resp' then you will have a python variable in your experiment
called::

trials.data['resp.keys'] #numpy array storing all the keys pressed
in different trials

If you used the 'Store Correct' feature of the Keyboard Component (and
told psychopy what the correct answer was) you will also have a variable::

trials.data['resp.corr'] #numpy array storing all the keys pressed
in different trials

You can take the sum of correct answers with::

nCorr = trials.data['resp.corr'].sum() #numpy arrays also have
methods like std(), mean()
msg = "You got %i trials correct" %(nCorr) #formatted strings in
python (see link below)

So you need to use code like the two lines above in a Code Component,
and then set a Text Component to have text=$msg (and remember that this
should be set to change every repeat!)

The attached experiment gives an example. It won't actually run because
it doesn't have a trial list file, but you can copy the code 'feedback'
Routine from the experiment (see the experiment menu) and paste it into
your own experiment. Obviously you'll need to adapt the Code Component
in that routine to fit the names of your loop and Keyboard Component. I
hope at some stage to add template routines for things like feedback
into Builder.

enjoy,
Jon

--
Dr. Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk/


This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

blockFeedback.psyexp
Reply all
Reply to author
Forward
0 new messages