Loop using a list as a parameter

**Standalone PsychoPy3 for Mac

Hi everyone,

I am working on an experiment using the builder. Ive included a loop (Loop is called Trials) around code + image stimuli, where the code explains the precise and detailed way to select each image (it is somewhat complex as I need to select a specific number of male and females images and a specific number of affiliated emotions, all of which are randomized). I am wondering if in the Loop Trials, I can give the parameter “selected_rows” a variable $im_files that contains a specific list of paths of the images selected in the code, which in turn used a broader excelfile.

For now, I keep getting the same error:
"New error :
trialList=data.importConditions(‘Experiement_inputs/Faces_pix.xlsx’, selection=im_files),
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/data/utils.py”, line 381, in importConditions
trialList.append(allConds[int(round(ii))])
TypeError: type str doesn’t define round method :

Hope this is clear. Thank you in advance!
G.

1 Like

It looks like ii isn’t what you need it to be. Try some (temporary) debugging code:

print(ii)
print(type(ii))

i.e. for this to work, remove the expression from the selected rows field to stop the error arising, and insert a code component from Builder’s custom component panel, and put the code above in the “begin routine” tab.

Hmmm, removing the input from the “select rows” parameter leads to a new slew of errors, without printing out the mysterious (ii).

File “/Users/PsychoPy/MEG_split_Task_short_lastrun.py”, line 384, in
Faces_still.setImage(im_files)
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/visual/image.py”, line 301, in setImage
setAttribute(self, ‘image’, value, log)
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/tools/attributetools.py”, line 141, in setAttribute
setattr(self, attrib, value)
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/tools/attributetools.py”, line 32, in set
newValue = self.func(obj, value)
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/visual/image.py”, line 288, in image
forcePOW2=False)
File “/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/psychopy/visual/basevisual.py”, line 832, in _createTexture
** raise AttributeError(msg)**
AttributeError: Couldn’t make sense of requested image.

OK, you need to show us exactly what you are doing, providing screenshots of your relevant components and the contents of your conditions file, and show us any custom code you are using.

Hi again,

So here is more detail about the experiment.

In this experiment, I present Faces that display different types of emotions (sad, happy, surprise, etc.). During the presentation, I randomly choose 20 faces out of my database of 120 faces. Half of these faces have to male, half female. In addition, I want half of the emotions to be positive, and half to be negative ( but this is not crucial at the moment)

.

My colleague helped write a piece of custom code to the Loop (pick_face) to help select the right number of emotions randomly.

pick_face.py (1.5 KB)

In the custom code, we created a variable (list) that contains the sub-selection of the image paths to be presented ( I have multiple trials of this task throughout the experiment).

In the loop “trials”, I give as condition, the excel file, and tried giving to the parameter “select rows” the name of the list variable “im_files”.

Hope this helps get insight! Thanks in advance for your help!

OK, it looks like you have a great collaborator, who can provide useful custom Python code. :+1:

I’ve only quickly scanned it but I think (hope) the only issue is that what that Python script returns is a list of file paths (from the Facefile column of your cognitions file). But what the “selected rows” field of a loop dialog requires are some indices (i.e. row numbers) of the file, not specific content from those rows.

i.e. the field simply needs to be provided with a list of row numbers, not a list of file names. Can you ask your colleague to amend that script accordingly?

We made the changes in order to give the indices of the path (i.e. row numbers of the shuffled images in the “select row” parameter of the loop), and it doesnt crash, but it only presents 1 of the 10 images that we asked it to show. It shows 1 images and nothing after (grey).

Thoughts on how to troubleshoot? Having trouble integrating this code with the gui.Thank you!

Insert some temporary debugging code in a code component. Eg in the “begin routine” tab, print the value of the image variable and the trial number.

Hi Michael!

I tried the debugging line you suggested and what I got back is a print of my variable im_indexes (a list of 12 row-numbers - total of images i want to show) and a print of the trial number is of 1.

The issue we are having is really linking up components of the custom code to parts of the built-in features of the builder.

As a reminder , this is the loop:

Pick_face contains only the custom code that generates a sub-list of emotions (out of 120 possibilities) so that there are an equivalent number of emotions and of genders.


im_indexes = list of row-numbers of sub-selected images
im_files = list of images paths of sub-selected images

Faces contains the image stimuli and a keyboard response. It is here that I feel we are not calling the right variable.


At the moment, the script runs if I put the name of the column in the excel file that contains the path name of all 120 images Facefile, but it crashes if I give it the variable im_files which contains the path of the sub-selection of images (from the code) (error = Couldn’t make sense of requested image). Strange because I feel this is what we should give it.

For the loop trials, we gave as “condition” the excel sheet (used in the code), and gave “selected rows” the variable created in the custom code im_indexes

. Changing loopType did not help, nor did changing the number of nRep.

We tried combining the code + faces in the same block, but no dice. I also tried having different parts of the custom code in begin routine and begin trial section of the window instead of begin experiment, but no help. We also tried turning the custom code into a function to call it - but nothing really worked.

At this point, the best we can get, without the script crashing, is that it runs for 1 picture (1 trial), and nothing appears after.

Thank you for your help, I feel we are close but really need a hand in combing raw code with the builtin features!

That is because this is exactly what you should be doing. :wink:
The Builder loop extracts one row of your conditions file to control each trial. The variable name Facefile will therefore contain just a single entry from that column. That is what the stimulus component needs (i.e. the name of specific image file to display).

It doesn’t matter that the column contains 120 filenames: the Builder loop extracts just one for each trial. That is the job of the loop, along with managing randomisation and selection.

Hopefully this now makes sense. The image field just wants a single filename (like dog.jpg. It has no idea of what to do with a list of values.

That seems right.