Conditional Branching for Picture Naming Trials

Hi,

The PsychoPy version I am using is v3.0.0b12 standalone on Windows 7 Professional.

The barebones setup for the study I am trying to set up is as follow:

(1) Show participant a picture (e.g., ‘chocolate’) for naming
(2) If participant is able to name (e.g., ‘chocolate’): Proceed to next picture trial (e.g., ‘chair’) for naming
(3) If participant is unable to name (e.g., ‘chocolate’): Provide prompts (e.g., audio + written) to help naming that picture (‘chocolate’), then proceeding to next naming trial irrespective of whether naming was correct or not.

The experimenter will be at the side controlling button presses for steps (2) and (3) to indicate to the program whether participant had named correctly or not, so there is nil need for voice recognition.

So, far this is what I have done:


For code2, this is what I have typed:

  • Under ‘Begin Routine’ tab:
    stimuli = [image_prac]

  • Under ‘Each Frame’ tab:

    if key_resp_prac== corrAns: # correct response
            continueRoutine = False
            trials.finished = True
    else: # incorrect response
            sound_prac_incorr.play()
            ortho_prac_incorr.draw()
            continueRoutine = False
    break # stop checking after the first detected stimulus (in case of overlaps)

What specifically went wrong when you tried that?:
The problem is that the entire program runs and immediately ends, without displaying anything. There are no error messages either.

I am completely new to PsychoPy and programming. Please assist. Thanks!

Try thinking like a computer. They only do exactly what you tell them to do.

On your code that runs on every frame (typically 60 times per second), you check if the response is correct and then do something. This is where you fall at the first hurdle: if key_resp_prac== corrAns: doesn’t check if the response was corrAns: it checks whether your keyboard object was corrAns, which is always going to be untrue. With computer code, you must very precisely ask the right question. By analogy, in the real world, it doesn’t make sense to ask "is my computer keyboard the letter n"? So the right question to ask here is something like:

if corrAns in key_resp_prac.keys:

i.e. you’re not asking if the keyboard is a letter, you’re asking if the store of keypresses attached to the keyboard contains the letter you’re interested in.

But also, the else clause doesn’t imply that the subject made an incorrect response, it only implies that absolutely anything else happened, including making no response. So what could happen is that the subject does absolutely nothing, which you are checking for 60 times a second and ending the trial accordingly. So with a study designed to last 20 trials, say, the entire experiment would finish in one third of a second. There would be no errors, because according to your code, absolutely nothing went wrong. The experiment completed as described.

Thank you, Michael, for explaining.

I have adjusted the codes.

Under ‘Begin Routine’ tab, it now reads:

stimuli = [image_prac]

for stimulus in stimuli:
    if corrAns in key_resp_prac.keys: # correct response
            continueRoutine = False
            trials.finished = True
    elif corrAns not in key_resp_prac.keys: # incorrect response
            sound_prac_incorr.play()
            ortho_prac_incorr.draw()
            continueRoutine = True
    break # stop checking after the first detected stimulus (in case of overlaps)

The program runs but not in the intended manner. Instead of showing the picture without prompts (e.g., audio + written) first, the prompts are now shown together with the picture right from the start in the first trial. This continues regardless of whether the response is correct or incorrect.

I suspect my ‘if’ and ‘elif’ codings are still incorrect. Please assist. Thanks!

OK, I’ve gone back to your first post and think we need to restructure things quite a bit, which should make things work much more easily for you.

I think you should split your trial into two routines. On the first routine, put the fixation cross, the image, and the initial keyboard response.

On the next routine, put the audio and text prompt and the second keyboard component. Each component is set to “Force end of routine”.

Now all you need to do is stop the second routine appearing if the response on the first one was correct. Simply insert a code component on the second routine and put this in the “begin routine” tab:

if key_resp_prac.corr: # if correct response on prev routine, 
    continueRoutine = False # don't show this one

As far as I can see, you just need one loop (connected to your conditions file).

Dear Michael,

Thank you so much. It now works.
Apologies though if the queries were elementary, but huge thanks!

I just re-run using the revised code and realised that the issue is not entirely fixed. The program runs but still not in the intended manner. While the prompts are now shown only when the response is incorrect, the picture is not shown together with the prompts.

To recap, the intended experiment flow is as follow:
(1) Show participant a picture (e.g., ‘chocolate’) for naming
(2) If participant is able to name (e.g., ‘chocolate’): Proceed to next picture trial (e.g., ‘chair’) for naming
(3) If participant is unable to name (e.g., ‘chocolate’): Provide prompts (e.g., audio + written) to help naming that picture (‘chocolate’), then proceeding to next naming trial irrespective of whether naming was correct or not.

Making modifications only made things worse, so I reverted back. Currently, the setup looks as follow (i.e., the non-intended scenario where the picture is unfortunately not shown with the prompts):

Under the “Begin Routine” tab in code_2, the code reads:

stimuli = [image_prac]

for stimulus in stimuli:
    if key_resp_prac.corr: # if correct response on prev routine, 
        continueRoutine = False # don't show this one

Pls assist. Thanks!

Where did that code come from? It doesn’t really make any sense.

The only code you need is as stated above:

if key_resp_prac.corr: # if correct response on prev routine, 
    continueRoutine = False # don't show this one

Go back to that, and explain exactly what you mean by “While the prompts are now shown only when the response is incorrect, the picture is not shown together with the prompts.” We can’t help unless we have a precise description of

  1. what you intend to happen, and
  2. what actually happens.

Hi Michael,

Really appreciate you helping out. I have rerun the code based on your suggestion, but this error message popped up.
“NameError: name ‘stimuli’ is not defined.”

For that reason, I have to use the code:

# this list should could just be created once per trial, in the 'begin routine' tab:
stimuli = [image_prac]

for stimulus in stimuli:
    if key_resp_prac.corr: # if correct response on prev routine, 
        continueRoutine = False # don't show this one

However, while this code runs, it does not perform as intended.

What we intend to happen is as follow:
(1) Show participant a picture (e.g., ‘chocolate’) for naming
(2) If participant is able to name (e.g., ‘chocolate’): Proceed to next picture trial (e.g., ‘chair’) for naming
(3) If participant is unable to name (e.g., ‘chocolate’): Program provides prompts (e.g., audio + written) together with the picture to help in naming (‘chocolate’), then proceeding to next naming trial irrespective of whether naming was correct or not.

However:
The code runs well, except that the picture does not appear in step (3). Only the prompts appear.

Hope I am making sense. Do assist. Thanks!

The code I suggested does not include any variable named stimuli. Please just use that code as suggested. The code you are using with with this made-up variable just doesn’t make any sense.

If the last image you have posted above is correct, the reason you don’t see any image on the second routine is simply because you don’t have an image component on that routine. Just insert an image component there with exactly the same settings as you have on the other routine. A computer will only do what it is told to do.

Thanks, Michael. Followed your instructions and all fixed!

1 Like