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