psychopy.org | Reference | Downloads | Github

Building a n-back test

Almost all manipulations that I tried maintained me stuck after the presentation of the first image. Im basically trying to use this in a code component:

# Begin Experiment
msg = ''

# Begin Routine
if trialsPractice.thisN == 0:
    one_back = 'Not applicable'
if stim == one_back and respPrac == 'y':
    msg= "Correct! RT=" + resp.rt
elif stim != one_back and not respPrac:
     msg= "Correct!"
elif stim != one_back and respPrac == 'y':
    msg="Incorrect"
elif stim == one_back and not respPrac:
    msg="You miss that one"

#End routine
one_back = stim

I think that I haven’t been successful when trying to communicate the variables of the image routine whith the feedback routine.

Hi @setegonz, try the following in relevant tabs:

# Begin Routine
if trialsPractice.thisN == 0:
    one_back = 'Not applicable'

# End Routine
if stim == one_back and respPrac.keys == 'y':
    msg= "Correct! RT=" + respPrac.rt
elif stim != one_back and not respPrac.keys:
     msg= "Correct!"
elif stim != one_back and respPrac.keys == 'y':
    msg="Incorrect"
elif stim == one_back and not respPrac.keys:
    msg="You miss that one"
one_back = stim

As a sumarry I have a loop composed of two routines

image

I’d tried two options:

  1. Following the order of your Navon Task:
  • Practice routine- I tried leaving here only the image presentation and response.

  • Feedback routine -

# Begin Routine
if trialsPractice.thisN == 0:
   one_back = 'Not applicable'

# End Routine
if stim == one_back and respPrac.keys == 'y':
   msg= "Correct! RT=" + respPrac.rt
elif stim != one_back and not respPrac.keys:
    msg= "Correct!"
elif stim != one_back and respPrac.keys == 'y':
   msg="Incorrect"
elif stim == one_back and not respPrac.keys:
   msg="You miss that one"
one_back = stim

Error:
text=msg,
NameError: name ‘msg’ is not defined

  1. I also tried
  • Practice routine-
# Begin Routine 
if trialsPractice.thisN == 0:
     one_back = 'Not applicable'

# End Routine 
if stim == one_back and respPrac.keys == 'y':
     msg= "Correct! RT=" + respPrac.rt 
elif stim != one_back and not respPrac.keys:
      msg= "Correct!" 
elif stim != one_back and respPrac.keys == 'y':
     msg="Incorrect" 
elif stim == one_back and not respPrac.keys:
     msg="You miss that one" 
one_back = stim
  • Feedback routine - only text to print $msg

Error:
text=msg,
NameError: name ‘msg’ is not defined

Ok, you need to define msg first at the beginning of the experiment as above, I left it out unintentionally.

# Begin Experiment
msg = ''

Now I’m obtaining the feedback, but delayed by one trial. For example, Image x was presented(trial 1), then image x is presented again so I press the correct key (trial 2), then image z is presented (trial 3) and after image z I’m receiving the feedback for the correct response given at the second trial.

@dvbridges My bad…Moving part of the code to the “begin routine tab” makes it work. But now my question is…In order to have the possibility to check the clock at any time, do I need to have the same clock code in the two routines?

If you want to check your time at any point in either routines, its easy enough. I would create my clocks at the beginning of the experiment (e.g., myClock = core.Clock() , reset the clock at the beginning of the time critical period using myClock.reset(), and just retrieve and present time whenever required using $myClock.getTime() in a text component.

Ok! But the issue is that I want to present the clock for 1s only if a certain key (‘q’) was pressed. I think that I will need an “if” because I only want the text to be presented IF the ‘q’ was pressed. My doubt is…Do I need to add an individual code component in each routine?

Yes you can do that. Based on your schematic above, if the participant pushes the ‘q’ key to view the time, it must be presented for 1 second, therefore must be presented at the beginning of the next trial for 1 second. Is that correct?

That’s correct. But if it is necessary to change the clock presentation duration time to make it work I don’t have any problems.

Its ok to have it presented on the next trial, if that is what you want. To do this add a new text component to your “practice” routine. Call the new text component “timer”, set the position to the top left of the screen, add $timeMsg to the text box and set the text box to update on every repeat. In your code component in the practice routine, add the following to the relevant tabs:

# Begin Experiment - add this after your other code that is in there
myClock = core.Clock()
presentNextTrial = False

# Begin Routine - replace all code with this
msg = ''
timeMsg = ''

if trials.thisN == 0:
    one_back = 'Not applicable'
    myClock.reset()  # Only reset clock at beginning of first trial

if presentNextTrial:
    timeMsg = round(myClock.getTime(), 2)
    timer.setText(timeMsg)
    presentNextTrial = False

# End routine add the following to the existing code
if 'q' == respPrac.keys:
    presentNextTrial = True

I’m having the following error:

text=timeMsg,
NameError: name ‘timeMsg’ is not defined

Code component of my practice routine:

# Begin Experiment - add this after your other code that is in there
myClock = core.Clock()
presentNextTrial = False

# Begin Routine - replace all code with this
msg = ''
timeMsg = ''

if trials.thisN == 0:
    one_back = 'Not applicable'
    myClock.reset()  # Only reset clock at beginning of first trial

if presentNextTrial:
    timeMsg = round(myClock.getTime(), 2)
    timer.setText(timeMsg)
    presentNextTrial = False

# End routine add the following to the existing code
if 'q' == respPrac.keys:
    presentNextTrial = True

Code component of my feedback routine:

# Begin Experiment 
msg = ''

# Begin Routine 
if trialsPractice.thisN == 0:
     one_back = 'Not applicable'
if stim == one_back and respPrac.keys == 'space':
     msg= "Correct! RT=%.3f" %(respPrac.rt)
elif stim != one_back and not respPrac.keys:
      msg= "Correct!" 
elif stim != one_back and respPrac.keys == 'space':
     msg="Incorrect" 
elif stim == one_back and not respPrac.keys:
     msg="You miss that one" 

#End Routine
one_back = stim

Can you post a picture of your practice routine?

Of course!
image

The code component contents for each of the routines that are within the practiceLoop are in my previous response.

I mean the actual routine, where your components are laid out. Its ok, have you set your timer text component text box to update on every repeat?

By setting the text component to update on every repear it started to work, partially. That is, if I press the timer key when an image is being displayed I have to wait until the feedback finish and it’s not until the next image that I get to see the timer. If I press the timer key when seeing the feedback nothing happens.
practice routine

feedback routine

Ok, I mentioned this would happen. The thing is, immediate feedback will not be displayed for exactly 1 second, because it will mess up your trial timing if it goes over your trial duration. I can give you some code that will give you immediate feedback, and it will last until the end of the routine. You can adapt that code to get feedback in your feedback screen as well:

Roll back everything from the last post, and remove the timer text component as well. From whats left, add the following to the existing code. This will give immediate timing feedback in the trial routine, but not the feedback routine. For that, you can adapt the code:

# Begin experiment
drawTime = False

timer = visual.TextStim(win=win, name='text',
    text='',
    font='Arial',
    pos=(-.25, 0.25));

# Every frame
if 'q' == respPrac.keys and drawTime != True:
    timeMsg = round(myClock.getTime(), 2)
    drawTime = True

if drawTime:
    if myClock.getTime() < (timeMsg + 1):
        timer.setText(timeMsg)
        timer.draw()
    elif myClock.getTime() >= (timeMsg + 1):
        timer.setText('')
        drawTime = False

# End routine 
drawTime = False

1 Like

Thanks for your help. Now it is possible to see the clock at both routines. But I want to know store the time when the clock key was pressed. Right now I’m storing the RT of the clock key.

Dear Michael and others,

I tried this code in my n-back task within one trial:

begin experiment:
oneback = ‘Not applicable’
begin routine:
if “letter” == oneback
correctResponse = ‘space’
else:
correctResponse = ‘None’
end routine:
oneback = “letter”

‘letter’ is de column with the letters of the n-back task, correctResponse is defined in the key_resp (also see below).

However, output shows only 1 (correct) under key resp cor when space is pressed; regardless of the previous letter matches the current letter and gives a correctReponse column with ‘none’ in all responses.

Any idea what I am missing?
Best regards, Suzanne

Screen Shot 2020-08-20 at 2.18.58 PM

I also tried this code in the begin routine (deleted the other code), but the experiment will stop after the instruction screen. I feel I am doing something wrong with the correctResponse variable and ‘incorrect’ and ‘correct’ but I am not sure what is going wrong.

Best, Suzanne

prevTrial = trials.getEarlierTrial(-2)
trialMatches = thisTrial[“letter”] == prevTrial[“letter”]
answer = response.keys() == “space”
if trialMatches == answer:
correctResponse = ‘correct’
else:
correctResponse = ‘incorrect’