Select Rows from a larger trialfile according to the value of a condition

Hi, I am new to Builder as well as python (using PsychoPy 2024.2.4 under Win 11). I have large trialfile (variables: block/set, image, complete imagepath etc.). In each block/set (named e.g. BuPa, DaBa) need to present only certain rows of the trialfile - the images that belong to that specific block/set (i.e for which block/set variable has the same value → their name starts with the block/set name e.g. BuPa01). The block order should be randomized, the image order within a block should be fixed. I use a code snippet at the beginning of the block routine (row_select routine, on the picture) to find and index the first and the last row of the sequence that is to be presented in that block (and give it in SelectedRows of the trial routine that uses the large trialfile). I managed to make it work but I wonder whether there is a simplier/better solution that e.g. does not require creating and saving a randomized excel file for each participant (the block order). (Since, the name is the same for everyone, it can mess up things.). As a beginner, I think I also managed to overcomplicate things :slight_smile:

This is the structure of the task:


Note: The different image sequences are in different subfolders in the images folder (e.g. images/DoDo, images/BuBa) and the images witin a sequence have the same name just different number (BuBa01…BuBa15).

The code:
Begin Experiment:
import pandas as pd
import random
from copy import deepcopy

df_tot = pd.read_excel(‘pilot_trialfile.xlsx’, sheet_name =‘Sheet1’)

setlist = pd.read_excel(‘pilot_blockfile.xlsx’)[‘imagesets’].tolist()
setlist_rand=deepcopy(setlist)
random.shuffle(setlist_rand)

blockdata = {‘sets’: setlist_rand}
df = pd.DataFrame(blockdata)
df.to_excel(‘my_randsets.xlsx’, index=False) #to use this as a condition file for the block loop
#not sure I actually need to save this and reference it in the block loop (??)

nReps=0
firstrow_num =0
lastrow_num=0

Begin Routinte (row_select routine):

actualset=setlist_rand[nReps] # nReps increases by 1 at the end of each block routine
firstpic = str(actualset) + ‘01.jpg’
firstrow = df_tot[df_tot[‘picture’] == firstpic].index
firstrow_num=firstrow.tolist()[0]
lastrow_num = firstrow_num+14
imagerange=str(firstrow_num) + ‘:’ + str(lastrow_num)

end of the Delay routine:
nReps=nReps+1
firstrow_num =0
lastrow_num=0

I edited my original post (which asked for help, how to solve the issue). I posted this parially to get feedback and advices for a nicer solution, partially to help others.

Why are you using read_excel and dataframes rather than PsychoPy TrialHandler (either in code or as a loop)?

So the first part of the code is actually unnecessary. I simply have to set the block loop to random and link in the original (nonrandomized pilot_blockfile.xlsx file that contains the names of the sets in the imagesets variable).
Then, in the code snippet
Begin Experiment:
df_tot = pd.read_excel(‘pilot_trialfile.xlsx’, sheet_name =‘Sheet1’)
nReps=0
firstrow_num =0
lastrow_num=0

Begin Routinte (row_select routine):

actualset=imagesets #var name in the pilot_blockfile.xlsx
firstpic = str(actualset) + ‘01.jpg’
firstrow = df_tot[df_tot[‘picture’] == firstpic].index
firstrow_num=firstrow.tolist()[0]
lastrow_num = firstrow_num+14
imagerange=str(firstrow_num) + ‘:’ + str(lastrow_num)

end of the Delay routine:
nReps=nReps+1
firstrow_num =0