How to use only those rows from the Conditions file where the participant_ID value matches the participant number entered at the beginning of the experiment?

I am preparing an experiment in Builder, where I would like to run a trial 12 times per participant with the information from the Conditions file. According to that information the text and picture stimuli are chosen. So far, this idea works.

Due to several reasons (e.g., the complexity of the pseudo-randomisation and my better knowledge in writing R-code), I decided to prepare the pseudo-randomisation in R. As I don’t want to have one excel-file for every participant, I decided to have all trials (for all possible participant IDs) in one excel file. Within this file I have one column with the “participant_ID” (ranges from 1 to 100) and another column with the “trial_number” (ranges from 1 to 12). For each of the trials I have one text (“text_stim”) and one picture stimulus (“face_stim”) and I want to present both together. To present the face and the text stimuli together, I added the excel file as Conditions file to my trials-loop and everything runs smoothly. My only problem is that I just want to present those text/face stimuli which are assigned to the specific participant_ID.

Thus, I would like to specify the “Selected Rows” to just use those excel rows, where the “participant_ID”-column has the same value as the participant-ID entered at the beginning of the experiment in the pop-up window (specified by using: expInfo[‘participant’] ). As participant_ID I could either use numeric values (from 1 to 100) or strings where the numbers are padded with leading 0s (3 character strings).

I know one way to get there if I use a code component at the beginning of the experiment, but then I would need to specify with another code component which information I would like to add to the experiment handler (and, thus, to the final data sheet per trial). As I really like the convenience of “all information of the Conditions file is added to the final data sheet per participant” I would like to go via the “Selected Rows”-Option in the loop (which is around my trial routine) or via one code component within this loop. Furthermore, I would like to use the loopType: “sequential” (as I already (pseudo)randomised the order within my R-code for the excel file).

Summary or main question: How could I use only those rows from my Conditions file where the participant_ID column has the same value as the expInfo[‘participant’] ? Preferably, I would like to specify this in the “Selected Rows” option or in a code component within my trials-loop.

I am pretty sure that there is a more or less easy option to do so and I would appreciate suggestions how to get there. I hope I explained everything in an understandable way - if not, please let me know.

Thanks in advance,

Hannah

One (according to my opinion not so nice) option is working:

Adding a code component at the beginning of my trial-routine with the following input in the “begin experiment”-tab

lower = (int(expInfo['participant'])-1)*12
upper = int(expInfo['participant'])*12

lower_upper = np.arange(lower,upper)

Afterwards I can use $lower_upper in the Selected Rows option.

The result is what I wanted, but I am sure there is a better way to go there - especially because I would love to use the “check in a column of my Conditions file for a specific value” in other contexts as well.

I think one solution is that to create a csv file for each participants based on its number at the beginning of the experiment and assign that file as the condition file. Something like this:

# At the begin experiment tab

import pandas as pd
original_file= pd.read_csv('the_path_to_your_file.csv')

# Filter rows with values similar to the participants id
condition_file =  original_file ['participant_ID']== int(expInfo['participant'])

# write your condition file as a csv file
condition_file .to_csv('condition_file.csv')

Then, in your loop, you can use condition_file.csv as your file. Hope this helps.

1 Like

Thanks a lot @Omidrezaa! That is exactly what I was looking for.

I just made some minor changes:

  • with condition_file = original_file[‘participant_ID’] == int(expInfo[‘participant’]) I’ve only got a column with TRUE/FALSE values. Adding original_file.loc[ xxx ] around it helped (see complete code line below)
  • and instead of csv I wanted to use excel.

My changes can be found below (just in case there is anyone looking for a solution for the same problem).

# in the begin experiment tab:

import pandas as pd
original_file = pd.read_excel('the_path_to_your_file.xlsx')

# Filter rows with values similar to the participants id
condition_file = original_file.loc[ original_file['participant_ID'] == int(expInfo['participant']) ]

# write your condition file as a csv file
condition_file.to_excel('condition_file.xlsx', index = None, header=True)

2 Likes