I have been having a problem for a while now, so I would like to ask for your help. I need to present to a participant a total of 20 images within one block (1 loop). Within that one block I have an image component, a code component and a key component to collect .participant’s answers so that in the next routine he can get feedback. I let the images be loaded from a .csv file. Unfortunately, I can’t randomize the stimuli so that I can present the participant with 10 images of one type and 10 images of another type, with the expectation that in total that given loop will run 20 times. I want to do that randomization using the code component. Can you help me? My code for randomization:
if Block1.trialList:
happy_faces = [c for c in Block1.trialList if c.get("emotion") == "happy"]
sad_faces = [c for c in Block1.trialList if c.get("emotion") == "sad"]
if len(happy_faces) >= 10 and len(sad_faces) >= 10:
selected_happy = random.sample(happy_faces, 10)
selected_sad = random.sample(sad_faces, 10)
selected_conditions = selected_happy + selected_sad
random.shuffle(selected_conditions)
Block1.trialList = selected_conditions
else:
print("lack of stimuli ")
else:
print("trialList is blank!")
I need to display 20 images in one block. 10 happy and 10 sad.
I plan to run the experiment only locally on PC.
What doesn’t work? It is not displaying 10 sad and 10 happy pictures, for a total of 20 visual stimuli in a given block. Sometimes there are more happy, sometimes sad images.
What do you mean that the trialList must be available before the loop starts?
ok, in given block there are sometimes more positive than negative images. I assume that your emotion coding is correct?
The demo code creates a list of 400 entries, hallf of them are odd numbers, half of them are even. It selects 10 of each category and combines them in a new list.
Begin experiment tab
import random
allImages = list(range(400))
posImages = []
negImages = []
#separate even and odd numbers
for num in allImages:
if num & 1:
posImages.append(num)
else:
negImages.append(num)
posSelection = random.sample(posImages, 10)
negSelection = random.sample(negImages, 10)
selImages = posSelection + negSelection
shuffle(selImages)
If you print out this list, you will see that there are no repeats.
As you can see, this very much resembles your code. The disadvantage of this approach is, that number (aka images) will be repeated acrosss block because they are not deleted from the list. Do you delete images from your list?
The following code reduces the list length by 10 for each parity ,“deletes” numbers from the list.
import random
allImages = list(range(400))
posImages = []
negImages = []
selImages = []
#separate even and odd numbers
for num in allImages:
if num & 1:
posImages.append(num)
else:
negImages.append(num)
shuffle(posImages)
shuffle(negImages)
for x in range(10):
selImages.append(posImages.pop())
selImages.append(negImages.pop())
shuffle(selImages)
I finally solved it by creating a separate excel file for each condition with 20 images of 10 sets and 10 happy and used a random function in a loop. Thank you for your help and your time!