| Reference | Downloads | Github

Display images from two lists based on loop condition


I loaded two condition lists that contain the image locations in the Begin experiment code component. Then shuffled them.

I want the image to display from either of this two list based on my condition file:
Within the loop, the display should sequentially go through all the conditions in this condition file.

Unfortunately, I cannot use the built in loop to random select the image, since I have complicated nested loops.

This is the code I have so far

###In the each frame
#Count the number of blocks and event.
n = currentLoop.thisN
n_imageselect_1 = Imageselect_1.thisN
n_eventcond_1 = eventcond_1.thisN
n_blocks = blocks.thisN

trialnow = n_blocks*8 +n_eventcond_1*4 + n_imageselect_1-1

if Imageselect_1.trialList[n_imageselect_1] == "Emotion":
    event_images = emotion_list[trialnow]
elif Imageselect_1.trialList[n_imageselect_1] == "Neutral":
    event_images = neutral_list[trialnow]
event_image = event_images[n]

I have a lot of counter for trial, event and block numbers, since the right image should choose based on how many images have been shown. Therefore, I have event_images = emotion_list[trialnow] for getting the image from the correct row of the list.

I am also not sure whether event_image = event_images[n] can help me display my images.

I got this error

I must have more errors than only this one.

Thanks in advance!!

When using if A == B: you need to use a double =

Hello Wakecarter,

That is very true. Unfortunately, now it says I didn’t define event_images.

I have the code event_images = emotion_list[trialnow]. I am very confused.

Is event_images supposed to be a list or a single item?

event_images = emotion_list[trialnow] suggests that is is a single item

So why do you need event_image = event_images[n]

Online you would need to put event_images = ‘’ or [] before the if clause, but offline you normally don’t need to. However, are you tring to use it in a “constant” field?

You are right. event_images = emotion_list[trialnow] is a single item.

I thought event_image = event_images[n] helps to pass event_images to the setImage(). As the event_image should contain the info for selected image for specific trial, it will display image as I needed.

Yes, eventually this task will be run online. Thank you for telling me in advance.

I put my code in the wrong tab. It should be in the begin routine tab, not the each frame tab.

My current code that worked (below). But can someone point to me how I can store the neutral and emotional list that showed in each trial? I have tried Imageselect_1.addData(event_images). The event_images is each images that being displayed for each trial (see code below).

### Begin experiment
import pandas as pd

# Read csv, access first col, make Python list
# Do this only once at beginning of exp. No need to read from file over and over again.
neutral = np.array(pd.read_csv('neutral.csv').iloc[:,0]) # Read csv, choose first col, and convert to list
emotion = np.array(pd.read_csv('emotion.csv').iloc[:,0])

#shuffle their order

# Dispatcher for lists of image filenames
imgs_select = {'emotion.csv': emotion, 'neutral.csv': neutral}

### Begin routine
n = currentLoop.thisN

if n == 0: # Only on first iteration of each event
    currentlist = imgs_select[ImgS] 

n_imageselect_1 = Imageselect_1.thisN
n_eventcond_1 = eventcond_1.thisN
n_blocks = blocks.thisN

trialnow = n_blocks*8 +n_eventcond_1*4 + n_imageselect_1-1

event_images = currentlist[trialnow]

Hello Wakecarter,

From reading your shared google doc for tips for online experiments. I feel like my trial and block counter (.thisN) may not work online. Do you have any suggestions about how to modify it. I know how to count for the most inner-loop trial number. But failed to find a way to count the outer loops.


In that case you should create your own counter. I tend to put something like loopIdx = -1 in Begin Experiment and loopIdx += 1 in Begin Routine of the first routine in the loop.

Thank you wakecarter! I can never solve this problem without your generous help!