1

I am working on an experimental design and code for a psychopy/python course. I am very new to coding, so excuse any jargon mistakes.

Design: Where's Waldo search experiment. Between-subjects, IDV: manipulates the size of the image to reduce visual noise (i.e. cropped = smaller image size = smaller search area)

  1. Image size by group: G1: full-size; G2: 2/3 size; G3: 1/3 size
  2. DV Measures: time to find Waldo, click accuracy (True v. False guesses)

I have a test script that I am using to identify the (x,y) coordinates of 'click-points' so I can define the coordinates of Waldo’s hiding spot within each image. I will use image-coordinate pairings to define True response within experimental conditions.

  • Here: ‘click-point’ = Waldo's location in the image

Once I have these coordinates, I intend to write some For loops with an If Boolean statement code for the mouse clicks:

  • click == found (Waldo's coordinates in the image) == True == next trial

  • click == miss (not Waldo's coordinates) == False == try again

  • Here: ‘click-point’ = coordinates within the image related to the participant's guess

Currently, I have my test script collecting a mouse location. However, it is tracking location based on where the mouse's initial position is within the window instead of where I click.

Question: Do you know how to get it to track location at the ‘click-point’ instead of its first identified position within the window?

#figuring out how to define a mouse click location in an image object
#=====================
#IMPORT SETTINGS
#=====================
from psychopy import visual, event
import numpy as np
import os
#=====================
#PATH SETTINGS
#=====================
main_dir = os.getcwd()                                                          
data_dir = os.path.join(main_dir,'experimentData')                              
image_dir = os.path.join(main_dir, 'images')                                    
#=====================
#WINDOW SETTINGS
#=====================
win = visual.Window(units='norm', size= [1000,642], fullscr=None)  
#=====================
#STIMULUS SETTINGS
#=====================
waldoImage = visual.ImageStim(win, size=2, interpolate=True) #interpolate >> helping with blurry image??
waldoImage.image = os.path.join(image_dir, 'wallpaperflare.com_wallpaper (2).jpg') #original size = 1920 x 1233 px 

mouse = event.Mouse()

x = visual.TextStim(win, text='x', color='black')
findWaldo = np.array([]) #Waldo locations list

waldoImage.draw()
win.flip()
if mouse.getPressed(waldoImage):
# if mouse.isPressedIn(waldoImage):  #tried this instead, but did not work
    loc = mouse.getPos()  #location = mouse starting point in the window, not where moved to and clicked
    findWaldo = np.append(findWaldo, loc)
event.waitKeys() # just to help control flow for testing

# test coordinates of collected location 
waldoImage.draw()
x.pos = findWaldo
x.draw() # at collected mouse coordinates
win.flip()    
event.waitKeys() # just to help control flow for testing

win.close()

print(findWaldo) # testing coordinate collection is working

1 Answers1

1

In case anyone has a similar issue, I was able to find a solution with the help of my professor. This post was used to generate the solution.

Sample of code solution that I used:

  • Allows the experimenter to set a list of target coordinates that are then imported into the actual experiment.
  • Script is designed to mark the target and pause to ensure the location choice is accurate.
  • A click count could be added if an average or range of click locations was desired.
  • Any images containing a target could be used in place of Waldo photos.
#=====================
#IMPORT SETTINGS
#=====================
from psychopy import visual, event, core
import os
import pandas as pd

#=====================
#PATH SETTINGS
#=====================
main_dir = os.getcwd()                                                          
data_dir = os.path.join(main_dir,'experimentData')                              
image_dir = os.path.join(main_dir, 'images') 
if not os.path.exists(data_dir):                                                # raise Error: if the folder does not exist... 
   os.makedirs(data_dir)                                                        # create one
#=====================
#IMAGES AND TRIAL SETTINGS
#=====================
nTrials = 18

img = []                                                                        # list images names and extensions
counter = 0
for i in range(6):
    counter = counter + 1
    img.append('waldo' + str(counter).zfill(2) + '.jpg')                        # Condition: full size images
counter = 0
for i in range(6):
    counter = counter + 1
    img.append('waldo' + '10' + str(counter) + '.jpg')                          # Condition: 10% reduced images
counter = 0
for i in range(6):
    counter = counter + 1
    img.append('waldo' + '25' + str(counter) + '.jpg')                          # Condition: 25% reduced images

#=====================
#COLLECT INFO
#=====================
click_list = []
img_list = []
filename = ('waldoLocations.csv')                                               # filename variable 

#=====================
#STIMULUS SETTINGS
#=====================
win = visual.Window(units='norm', fullscr=True)                                 # units normalized to scale image across various window sizes = fullscreen
waldoImage = visual.ImageStim(win, size=2, interpolate=True)                    # interpolate >> helping with blurry image??
mouse = event.Mouse()
x = visual.TextStim(win, text='x', color='black')

mouseDetected = False                                                           # tracker for mouse-click depression/release 
                                                                                # (otherwise mouse.getPressed() = True, i.e. always depressed)

for trial in range(nTrials):
    waldoImage.image = os.path.join(image_dir, img[trial]) 

    waldoImage.draw()
    win.flip()
    
    while True:
        if mouse.getPressed()[0]:                                               # indexes 'left' mouse-click
            if not mouseDetected:                                               # initial mouse-click detection 
                loc = mouse.getPos()
                click_list.append(loc)
                img_list.append(img[trial])
                mouseDetected = True                                            # mouse press (depression) 
                waldoImage.draw()    
                x.pos = loc                                                     # save Waldo's location for experiment
                x.draw()                                                        # draw an 'X' and wait to verify accurate click
                win.flip()
                core.wait(.3)
                break
        else:
            mouseDetected = False                                               # mouse press (released) 

#=====================
#SAVE DATA SETTINGS
#=====================
waldoLoc_x = []                                                                 # index and save x-coordinates
for x in range(len(click_list)):
    waldoLoc_x.append(click_list[x][0])
waldoLoc_y = []                                                                 # index and save y-coordinates
for y in range(len(click_list)):
    waldoLoc_y.append(click_list[y][1])


df = pd.DataFrame(data={                                                        # use pandas to save a dataframe of waldo locations
  "Image_Name": img_list,                                                       # define column headers/lists 
  "waldoLoc_x": waldoLoc_x,
  "waldoLoc_y": waldoLoc_y                                                        
})
df.to_csv(os.path.join(data_dir, filename), sep=',', index=False)               # save the dataframe as .csv file using parameters            

#=====================
#END EXPERIMENT SESSION
#=====================
win.close()

The solution in the context of my experiment can be found here: Waldo Experiment.

Happy coding.

  • Links' contents should be summarized in stackoverflow answer. There are a few reasons for this. One of them is that the link might break in the future. – roeen30 Dec 19 '22 at 15:20