How to connect "correct/wrong" response to an Image?

Hi, I new to programming and I am trying to code my very first experiment.

The problem I am having is that I don’t know how to “link” the correct response (keyboard “left” and “right”) to the images (arrows pointing to the left or to the right).

With the code that I wrote (please see below) it seems that random correct responses are being linked to the pictures. How could I fix this? Thanks in advance :slight_smile:

code:


from psychopy.visual import Window, ImageStim, TextStim
from psychopy.core import wait
from psychopy import gui, visual, core, data, event, logging, clock
from psychopy.event import waitKeys
import random

# Create a window
disp = Window (size = (1440, 900), units="pix", fullscr=True) 

#Target locations
TARLOCS = ["upcongLeft","upcongRight","upincongLeft","upincongRight"] 

#Potential targets
TARGETS = ["left","right"] 

#Potential SOAs
SOAS = [0.1,0.9]

#Fixation time at the start of a trial
FIXTIME = 1.5

#Duration of the cue Screen
CUETIME = 0.1

#Duration of the feedback Screen
FEEDBACKTIME = 1

# number of times to repeat training mode
TRAININGREPEATS = 1

#All possible target stimulli
tarstim = {}

tarstim ["upcongLeft"]= ImageStim(disp, size=(300,300), image="upcongLeft.png")
tarstim ["upcongRight"]= ImageStim(disp, size=(300,300), image="upcongRight.png")
tarstim ["upincongLeft"]= ImageStim(disp, size=(300,300), image="upincongLeft.png")
tarstim ["upincongRight"] = ImageStim(disp, size=(300,300), image="upincongRight.png")

#Draw incorrect Feedback
fbstim = {}
fbstim[0] = TextStim (disp, text="Incorrect!", height=24,\
    color = (1, -1, -1))
#Draw correct Feedback
fbstim[1] = TextStim (disp, text="Correct!", height=24,\
    color = (-1, 1, -1))

# Create an empty list to contain the training trial
training = []
# Loop
for cueside in CUELOCS:
    for tarside in TARLOCS:
        for soa in SOAS:
            for tar in TARGETS:
                # Training dictionary
                trialtraining = {"cueside":cueside, "tarside":tarside,\
                    "target":tar, "soa":soa}
                # add the trial dictionary to the list
                training.extend (TRAININGREPEATS * [trialtraining])
# Randomise
random.shuffle (training)


# Loop
for trialtraining in training:

    # Draw a target stimullus
    tarstim[trialtraining ["tarside"]].draw()
    # Update the monitor
    taronset = disp.flip()

    # Wait for a response
    resplist = waitKeys (maxWait=float("inf"), keyList = ["left", "right"],\
        timeStamped = True)
    # Select the first response from the response list
    response, presstime = resplist[0]

    # Check if the response was correct
    if response == trialtraining ["target"]:
        correct = 1
    else:
        correct = 0
    
    # Show feedback
    fbstim[correct].draw()
    disp.flip()
    wait (FEEDBACKTIME)
    
disp.close () 

To help us (and yourself) find the error, it would probably be useful if you provided a minimally working example, i.e. reduce the complexity of your code to what we need to know to help you with your problem. For example you could just use four images, leave out the instructions and the fixation mark, but include the imports and the definition of all relevant variables (e.g. TARGETS) so that we can actually run your code.

I can see that you compare response with target and it look like target is tar and it looks like tar is TARGETS but I can’t see where you set TARGETS to [‘left’,’right’]

Hi Lukas, I just edited the code now . thanks for your suggestions.

Hi wakecarter, I edited the code and put all the relevant informations. Thank you very much

CUELOCS is still missing in your code above.

So I’m not entirely sure, what you tried to do with some parts of the code, but I think my code may help you anyway, because you can adjust it and still profit from the logging, I introduced.

from psychopy.visual import Window, ImageStim, TextStim
from psychopy.core import wait, quit
from psychopy import gui, visual, core, data, event, logging, clock
from psychopy.event import waitKeys, getKeys
import random

from psychopy import logging
from PIL import Image, ImageDraw

# output logging to the console
logging.console.setLevel(logging.WARNING)

#Target locations
TARLOCS = ["upcongLeft","upcongRight","upincongLeft","upincongRight"] 

# create four images for debugging
for fn in TARLOCS:
    img = Image.new('RGB', (200, 200))
    
    # write the filename on the image
    d = ImageDraw.Draw(img)
    d.text((10,10), fn, fill=(255,255,0))
    
    img.save(fn + '.png')

# Create a window
disp = Window (size = (1440, 900), units="pix", fullscr=True) 

#Potential targets
TARGETS = ["left","right"] 

#Potential SOAs
SOAS = [0.1,0.9]

#Fixation time at the start of a trial
FIXTIME = 1.5

#Duration of the cue Screen
CUETIME = 0.1

#Duration of the feedback Screen
FEEDBACKTIME = 1

# number of times to repeat training mode
TRAININGREPEATS = 1

#All possible target stimuli
tarstim = {}

# For every stimulus, also store the correct answer as an attribute
tarstim["upcongLeft"]               = ImageStim(disp, size=(300,300), image="upcongLeft.png")
tarstim["upcongLeft"].correctAns    = 'left'
tarstim["upcongRight"]              = ImageStim(disp, size=(300,300), image="upcongRight.png")
tarstim["upcongRight"].correctAns   = 'right'
tarstim["upincongLeft"]             = ImageStim(disp, size=(300,300), image="upincongLeft.png")
tarstim["upincongLeft"].correctAns  = 'left'
tarstim["upincongRight"]            = ImageStim(disp, size=(300,300), image="upincongRight.png")
tarstim["upincongRight"].correctAns = 'right'

#Draw incorrect Feedback
fbstim = {}
fbstim[0] = TextStim (disp, text="Incorrect!")
#Draw correct Feedback
fbstim[1] = TextStim (disp, text="Correct!")

# textstim to monitor responses
debugtxt = TextStim(disp, pos = (0, 0.3), units = 'norm')

# Create an empty list to contain the training trial
training = []
# Loop
for tarside in TARLOCS:
    for soa in SOAS:
        for tar in TARGETS:
            # Training dictionary
            trialtraining = {"tarside": tarside, "target":tar, "soa":soa}
            # add the trial dictionary to the list
            training.extend (TRAININGREPEATS * [trialtraining])

# Randomise
random.shuffle (training)


# Loop
for trialtraining in training:

    # Draw a target stimullus
    tarstim[trialtraining ["tarside"]].draw()
    # Update the monitor
    taronset = disp.flip()

    # Wait for a response
    resplist = waitKeys (maxWait=float("inf"), keyList = ["left", "right"], timeStamped = True)
    if 'escape' in getKeys(): quit()
    
    # Select the first response from the response list
    response, presstime = resplist[0]

    # Check if the response was correct
    if response == tarstim[trialtraining ["tarside"]].correctAns:
        correct = 1
    else:
        correct = 0
    
    # log the response, the correct answer and the value of the variable correct
    debugstr = f'response: {response}; correctAns: {tarstim[trialtraining ["tarside"]].correctAns}; correct: {correct}'
    logging.warn(debugstr)
    debugtxt.text = debugstr
    debugtxt.draw()
    
    # Show feedback
    fbstim[correct].draw()
    disp.flip()
    wait (FEEDBACKTIME)
    
disp.close () 

I removed the part about CUELOCS and I’m not sure what trialtraining['target'] is for. If you have questions let me know.

1 Like

Thank you very much, Lukas. It worked :slight_smile:

1 Like