N-back test in 1.77

1,029 views
Skip to first unread message

Ricky Madhavan

unread,
Sep 10, 2013, 3:48:36 PM9/10/13
to psychop...@googlegroups.com
Hello all,

I'm relatively new to Psychopy and find it quite convenient and flexible. I've made a simple 2-back test in Builder mode for an fMRI study. Currently, the test has 4 conditions with 5 parameters each (4 sets of 5 numbers). The test randomly shows each string of numbers and records the participants' responses as well as before and after ratings.  

I want to be able to test a paradigm while they are in the scanner. The issue is that I have no efficient way of making sure they are actually trying to respond correctly while in the scanner; I have no answer grading system. Currently, the way I can see this is looking at at a csv and excel file to compare their responses to the presented numbers. 

I'm not proficient enough with the software to code a system which actually counts 2-back and compares that to the user's response. I'm considering using a predetermined string of numbers and a "corrAns" column in the conditions file. From there, there could possibly be a smiley or frowny face displayed on the screen in order to motivate the participant and allow me to see if they are answering correctly. Still, I thought I'd try the user-group in case anyone had a better suggestion.

My Question: Does anyone have any experience/access with a more complex n-back test that can evaluate response in real-time? 

Thanks,

Ricky

Michael MacAskill

unread,
Sep 10, 2013, 5:53:16 PM9/10/13
to psychop...@googlegroups.com
Hi Ricky,

We recently added something to make that easier, but it is in version 1.78, so you might want to upgrade.

See the API here:
<http://www.psychopy.org/api/data.html>
Each loop in the Builder creates something called a TrialHandler object, which is effectively a list of details for your trials which gets cycled through. If your loop is called "trials", then you can access details from a previous trial like this:
prevTrial = trials.getEarlierTrial(-2) # for a 2-back task

By definition, this won't be valid until the third trial (i.e. it will return "None" on the first and second trials).

This function lets you access an earlier trial even when the loop is a random one: this is what wouldn't be possible to do within your csv file. i.e. you couldn't construct a "corrAns" column in advance, as you can't know which row will end up being two trials before the current one, as the order will change on each run.

Let's say you have one routine which displays the number and collects the key response. The following routine contains two image components, one for a smiley face, one for a frown. In the opacity field for each, put:

showSmile

and

showFrown

respectively. Add a code component, put it at the top of your list so it gets executed before the images. In that code component we will calculate the values of the variables showSmile and showFrown to be 0 or 1 as required.

*Bear in mind that I've never coded an n-back task, and that this code is completely untested.*

The code below assumes that you have a keyboard component called "response", a loop called "trials", and that the variable from your csv file for displaying the string of numbers is called "number". Replace "y" with whatever your actual "they match" response is:

prevTrial = trials.getEarlierTrial(-2)
if prevTrial == None:
# don't show any feedback on first two trials
showSmile = 0
showFrown = 0
else:
# see if the number on this trial matched the earlier one:
trialMatches = thisTrial["number"] == prevTrial["number"] # True if matches, False if doesn't

# see if the subject thinks they matched
answer = response.keys() == "y" # True if "y", False if any other response

# set the visibility of the stimuli accordingly:
if trialMatches == answer:
showSmile = 1
showFrown = 0
else:
showSmile = 0
showFrown = 1

Hopefully that helps?

Regards,

Michael

Ricky Madhavan

unread,
Sep 11, 2013, 11:14:11 AM9/11/13
to psychop...@googlegroups.com
Michael,
Thanks a lot for your help. I see how your code can do what I need. However, I have little experience with the code view and wouldn't know where to start with this. I've attached the current 2-back test I've written with the conditions file (ratings removed). As you can see, it's very simple. I have yet to upgrade to 1.78 as it is a hassle to update/install anything on my university computers (hope that doesn't complicate things).
 
Let me know if this gives you a better idea of what I'm working with.
 
Regards,
 
Ricky 
2backtestnorate.psyexp
nbackConds.xlsx

Michael MacAskill

unread,
Sep 11, 2013, 5:55:30 PM9/11/13
to psychop...@googlegroups.com
Dear Ricky,

The solution posted wasn't for the Coder view. It was designed to be used within Builder, using the Code component there. As stated in the previous message, just add a code component as you would for any other stimulus. Paste the given code into the "Begin routine" box so it will be executed at the beginning of the feedback routine.

The ability to safely look back at previous trials without causing an error was added in v 1.78. PsychoPy is an evolving piece of software (I added that little function precisely to cater for requests like yours). So if you want to take advantage of this capability, you really should upgrade. There could be a workaround, but it would require good understanding of your task.

Having said that, I took a quick look at your task and it's not what I envisaged. My solution was posted for what I thought of as a standard n-back task, where you have a single loop advancing through a randomised list of stimuli, and the subject responds whether each stimulus does or does not match the one n back. Your task seems to consist of a small series of discrete loops, with the response only gathered after each loop has finished, and is a "what was the stimulus" rather than a "yes/no" response. So the code I proposed would need to be modified substantially.

Regards,

Mike

onyeu...@belmonthill.org

unread,
Oct 18, 2013, 10:13:44 PM10/18/13
to psychop...@googlegroups.com
Hello,
        I had trouble following your instructions. When I put showSmile and show Frown respectively into the Opacity fields, this came up when i tried to run the game:

Error when generating experiment script:
The C++ part of the StdOutRich object has been deleted, attribute access no longer allowed.

What do you recommend I do?

Thank you,
James

On Tuesday, September 10, 2013 5:53:16 PM UTC-4, Michael MacAskill wrote:
Reply all
Reply to author
Forward
0 new messages