psychopy.org | Reference | Downloads | Github

Random prime-target pair & type of trials by creating lists and merging them with pandas

Hello everybody !

As I’m novice in PsychoPy and Python language I hope I will explain my problem correctly.

I want to conduct an affective priming task but with a complete randomization of prime-target pairs (instead of fixed rows). I don’t want to just write all the combination in my excel sheet and randomize their presentation because I want to follow some recommendation in the affective priming field. Indeed, I want to present 10 of each of my 4 types of trial in each of the 2 blocks.of my experiment.

More precisely, in a prejudice affective priming task I have 4 types of trials depending on the comination of Outgroup Prime, Ingroup Prime, Positive Target and Negative Target.
In the first block I want 10 Outgroup/Positive, 10 Outgroup/Negative, 10 Ingroup/Positive and 10 Ingroup/Negative trials randomized for each of my participants.
In the second block, I want again 10 Outgroup/Positive, 10 Outgroup/Negative, 10 Ingroup/Positive and 10 Ingroup/Negative trials randomized for each of my participants and different of those in the first block.

I’ve try to create 4 types of list (one for each type of trial) for each block (so 8 types of list) at the begining of the experiment by randomizing the prime-target pairs. And then I try to merge this 4 lists in one for each block. I plan to call this lists in each of my Loop condition (block 1 and 2).

That’s the code component I’ve used in my experiment :

In this script I use a csv file to define my variables as opposed to explicity defining them in my code.
I then use the participant’s number and the session number to create personalized order files, in case that would be helpful

import random 
import csv

Now, I am going to use the original_list.csv file to define my words instead of doing it explicitly
first, I open the file called ‘original_list.csv’ and read its contents into the variable ‘stimuli’

Stimuli=list(csv.reader(open('Original_list.csv',"rU"),delimiter=';'))

This original_list file lokks like that (for simplification : M is my Outgroup Primes, C my ingroup ones, N my negative targets and P my positive ones

M1	C1	N1	P1
M2	C2	N2	P2
M3	C3	N3	P3
M4	C4	N4	P4
M5	C5	N5	P5
M6	C6	N6	P6
M7	C7	N7	P7
M8	C8	N8	P8
M9	C9	N9	P9
M10	C10	N10	P10

Next I will create two ‘empty’ variables into which I will put the word lists

AmorceMg=[]
AmorceCau=[]
TargetN=[]
TargetP=[]

Next I will loop through the ‘stimuli’ object and place the appropriate words in their respective objects

for i in range(len(Stimuli)):
    AmorceMg.append(Stimuli[i][0])
    AmorceCau.append(Stimuli[i][1])
    TargetN.append(Stimuli [i][2])
    TargetP.append(Stimuli [i][3])

Now we take out the header line


AmorceMg=AmorceMg[1:]
AmorceCau=AmorceCau[1:]
TargetN=TargetN[1:]
TargetP=TargetP[1:]

Then I randomize them

AmorceMgRand1=random.sample(AmorceMg,len(AmorceMg)) 
AmorceCauRand1=random.sample(AmorceCau,len(AmorceCau)) 
TargetNRand1=random.sample(TargetN,len(TargetN)) 
TargetPRand1=random.sample(TargetP,len(TargetP))

this will create my 4 randomized word lists for the second block of trials

AmorceMgRand2=random.sample(AmorceMg,len(AmorceMg)) 
AmorceCauRand2=random.sample(AmorceCau,len(AmorceCau)) 
TargetNRand2=random.sample(TargetN,len(TargetN)) 
TargetPRand2=random.sample(TargetP,len(TargetP))

Now I do the writing to file part twice, once for the first block of stimuli, once for the second
First though, Idefine the ‘subjectID’ and ‘sessionID’ variables for naming the order files

subjectID=expInfo['participant']
sessionID=expInfo['session']

Now I create 4 file names based on compatibility for the first block


CompMgFile1=subjectID + '_' + sessionID + '_CompMg1.csv'
IncMgFile1=subjectID + '_' + sessionID + '_IncMg1.csv'
CompCauFile1=subjectID + '_' + sessionID + '_CompCau1.csv'
IncCauFile1=subjectID + '_' + sessionID + '_IncCau1.csv'

with open(CompMgFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand1)): 
    writer.writerow([AmorceMgRand1[i],TargetNRand1[i]]) 

with open(IncMgFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand1)): 
    writer.writerow([AmorceMgRand1[i],TargetPRand1[i]]) 

with open(CompCauFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand1)): 
    writer.writerow([AmorceCauRand1[i],TargetPRand1[i]]) 

with open(IncCauFile1,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand1)): 
    writer.writerow([AmorceCauRand1[i],TargetNRand1[i]])

And for the second block


CompMgFile2=subjectID + '_' + sessionID + '_CompMg2.csv'
IncMgFile2=subjectID + '_' + sessionID + '_IncMg2.csv'
CompCauFile2=subjectID + '_' + sessionID + '_CompCau2.csv'
IncCauFile2=subjectID + '_' + sessionID + '_IncCau2.csv'

with open(CompMgFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand2)): 
    writer.writerow([AmorceMgRand2[i],TargetNRand2[i]]) 

with open(IncMgFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceMgRand2)): 
    writer.writerow([AmorceMgRand2[i],TargetPRand2[i]]) 

with open(CompCauFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand2)): 
    writer.writerow([AmorceCauRand2[i],TargetPRand2[i]]) 

with open(IncCauFile2,'wb') as w: 
  writer=csv.writer(w) 
  writer.writerow(['Amorce','Target']) 
  for i in range(len(AmorceCauRand2)): 
    writer.writerow([AmorceCauRand2[i],TargetNRand2[i]])

Now I create two file names based on precedent variables for each blocks

Block1=subjectID + '_' + sessionID + '_order1.csv'
Block2=subjectID + '_' + sessionID + '_order2.csv'

Now I merge all files for each blocks

import pandas as pd

CompMgFile1 = pd.DataFrame(CompMgFile1,columns=['Amorce', 'Target'])
IncMgFile1 = pd.DataFrame(IncMgFile1,columns=['Amorce', 'Target'])
CompCauFile1 = pd.DataFrame(CompCauFile1,columns=['Amorce', 'Target'])
IncCauFile1 = pd.DataFrame(IncCauFile1,columns=['Amorce', 'Target'])
CompMgFile2 = pd.DataFrame(CompMgFile2,columns=['Amorce', 'Target'])
IncMgFile2 = pd.DataFrame(IncMgFile2,columns=['Amorce', 'Target'])
CompCauFile2 = pd.DataFrame(CompCauFile2,columns=['Amorce', 'Target'])
IncCauFile2 = pd.DataFrame(IncCauFile2,columns=['Amorce', 'Target'])

merged = pd.concat([CompMgFile1, IncMgFile1, CompCauFile1,IncCauFile1])
merged.to_csv(Block1, index=None)

merged = pd.concat([CompMgFile2, IncMgFile2, CompCauFile2,IncCauFile2])
merged.to_csv(Block2, index=None)

Now I put $Block1 and $Block2 in the respective conditionFile fields in the trials and trials_2 loop dialog boxes

But unfortunately, it doesn’t work.

Is anybody have an idea of the problem ?
Thank you very munch in advance for any help !
If informations are missing do not hesitate to ask me.
However, if my post is not well suited I apologize.

Have a nice week-end !

Ivane

@Ivane
Hi Ivane, I’m facing the same issue here, and we have a very similar experimental design. Since there was no reply to your post, may I know if you solved it eventually? And would you like to share it with me? I would be grateful.