psychopy.org | Reference | Downloads | Github

Randomly pulling from two sets of images in a single trial

Hi all!

I’m programming a version of the affect misattribution procedure (Payne et al., 2004), and I’m stuck with one aspect. I have two sets of stimuli - primes and targets. There are 12 primes and 162 targets. Each trial displays a prime followed by a target, and there are 324 trials total - this means that each prime is displayed 27 times and each target is displayed 2 times. The primes and targets should be randomly paired for each trial (in the sense that any single participant won’t necessarily see the same pair), but each prime needs to be displayed 27 times, and each target needs to be displayed twice.

I currently have a function that will pull from one list contain both the target file names and the primes file names. Consequently, the images are not being randomly paired - they’re paired as they are in the csv file used to create the list. Relevant snippets of code below.


pracCSV = "praclist.csv"
pracList = pd.read_csv(pracDir + pracCSV)

#create stimuli that are constant for entire experiment
pracprimeImage = visual.ImageStim(win, image = pracDir + '1220.JPG', pos = (0, 0), size = (primeImageSizeX, primeImageSizeY))
practargetImage = visual.ImageStim(win, image = pracDir + 'pic81.bmp', pos = (0, 0), size = (targetImageSizeX, targetImageSizeY))

def runBlock(taskName='amp', blockType='', reps = 2, saveData=True, practiceTrials=6):
    trialsInBlock = pracList #for prac trials
    trialsInBlock = pd.concat([trialsInBlock] * reps, ignore_index = True) #repeat each trial/row in dataframe reps number of times
    trialsDf = trialsInBlock.reindex(np.random.permutation(trialsInBlock.index)) #random shuffle trials
    trialsDf = trialsDf.reset_index(drop = True) #reset row index
    for i, thisTrial in trialsDf.iterrows():
        #draw stim
        pracprimeImage.setImage(pracDir + thisTrial['pracprimeImage'])
        practargetImage.setImage(pracDir + thisTrial['practargetImage'])

Do I need to import two csvs, one with prime file names and one with target filenames? I feel like that’s the answer but I’m not sure how to proceed with creating a trial list from there (i.e., getting it all into my trialsDf object). Any help would be greatly appreciated!

…figured it out :slight_smile: solution below. Thanks!

pracCSV = "praclist.csv" #list of primes
pracList = pd.read_csv(pracDir + pracCSV)
pracCSVtargets = "praclist_targets.csv" #list of targets
pracListtargets = pd.read_csv(pracDir + pracCSVtargets)

#create stimuli that are constant for entire experiment
pracprimeImage = visual.ImageStim(win, image = pracDir + 'prime1.JPG', pos = (0, 0), size = (primeImageSizeX, primeImageSizeY))
practargetImage = visual.ImageStim(win, image = pracDir + 'target1.bmp', pos = (0, 0), size = (targetImageSizeX, targetImageSizeY))

def runBlock(taskName='amp', blockType='', reps = 2, saveData=True, practiceTrials=6):
    primetrialsInBlock = pracList #list of primes
    targettrialsInBlock = pracListtargets.sample(frac = 1).reset_index(drop=True) #randomize target list
    trialsforBlock = pd.concat([primetrialsInBlock, targettrialsInBlock], axis = 1) #combine prime and target dfs
    trialsDf = trialsInBlock.reindex(np.random.permutation(trialsInBlock.index)) #random shuffle trials
    trialsDf = trialsDf.reset_index(drop = True) #reset row index
    for i, thisTrial in trialsDf.iterrows():
        #draw stim
        pracprimeImage.setImage(pracDir + thisTrial['pracprimeImage'])
        practargetImage.setImage(pracDir + thisTrial['practargetImage'])

Hi Amanda, could you share your psychopy code for the AMP with us? Trying to implement a task like this but never did it in psychopy…Thanks!!

Hi @KateYang, you can create the AMP using Builder. In the attached example, I created a version of the AMP (based on Payne et al., 2005) without images, but words to describe the stimuli (e.g., prime = pleasant1, target= target1). You can replace the words with pictures.

To randomise the prime and target pairings, I have added a few lines of code in a code component which shuffles the prime stimuli, and a new conditions file is created (called “newCond.xlsx”) which is fed to the loop handler:

import pandas as pd 

# Get condition file
dat = pd.read_excel("cond.xlsx", index=False)
# Shuffle Prime column
dat.loc[:, 'Prime'] = np.random.permutation(dat['Prime'].values)
# save shuffled dataframe to new spreadsheet ready for loop
dat.to_excel("newCond.xlsx", index=False)

Here are the files to demostrate the task:
cond.xlsx (8.0 KB) amp.psyexp (14.3 KB)

If you are unfamiliar with Builder, I recommend the Stroop tutorial to get you familiar with Builder concepts.

thank you very much! it was very helpful!!