0

here is my code:

from psychopy import visual, event, gui
import random, os
from random import shuffle
from PIL import Image
import glob

# import images sequences and randomize in the same order
a = glob.glob("DDtest/targetimagelist1/*")
b = glob.glob("DDtest/distractorimagelist1/*")
c = glob.glob("DDtest/targetimagelist2/*")
d = glob.glob("DDtest/distractorimagelist3/*")
indices = random.sample(range(len(a)), len(a))
a = map(a.__getitem__, indices)
b = map(b.__getitem__, indices)
c = map(c.__getitem__, indices)
d = map(d.__getitem__, indices)

def loop():
    # randomizes the location of the stimuli
    loc = [1, 2]
    location = random.choice(loc)
    if location == 1:
        pos1 = [-.05,-.05]
        pos2 = [.05, .05]
    else:
         pos1 = [.05, .05]
         pos2 = [-.05, -.05]

    # randomizes the image lists
    type = [1,2]
    trialtype = random.choice(type)
    if trialtype == 1:
        target = a
        distractor = b
    else:
        target = c
        distractor = d

     # Create window and stimuli. 
    win = visual.Window(size=(1280, 800), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1]) # removed a default value
    targetstim = visual.ImageStim(win=win, pos=pos2, size=[0.5,0.5])
    targetstim.autoDraw = True
    distractorstim = visual.ImageStim(win=win, pos=pos1, size=[0.5,0.5])
    distractorstim.autoDraw = True

    distractorstim.image = distractor[i]
    targetstim.image = target[i]

    # Display and wait for answer 
    win.flip()
    event.waitKeys(keyList = ['space']) 

# loop
for i in range(len(a)):
    loop()

So here is my problem: I have 64 images in each file. The current program terminates when the 64 images have been displayed (length is based on the amount of images in 'a'). What I would like is for the file to terminate when all images have been displayed (128 trials). Is there anyway to do this? I will appreciate any help or guidance. :)

Edit:

I tried doing this to the loop:

 # loop
    for i in range(len(a)*2):
        loop()

What happens when I do this, the images cycle as they did before except when I get a little past 64 (65-67) it tries to call an image that is outside the range, and this causes an "IndexError:list index out of range." Basically I need some way to index one list from 1-64, and the other 65-128, and then just randomly generate the order while ensuring that the index for list a and b are the same.

Brionius
  • 13,858
  • 3
  • 38
  • 49
y3trgfhsfgr
  • 467
  • 8
  • 17

1 Answers1

2

Here's how I would recommend addressing this - create a separate counter for each trial type. Every time you use an image set from one of the trial types, use the counter for that trial type as the index, then increment that counter. When you select the trial type, select it at random unless one of the trial type image sets has been exhausted, in which case pick the other one.

Here's the code - there are three places where modifications are necessary:

    type = [1,2]
    if trialImageCounters[1] == len(a):
        trialtype = 2  # If the maximum number of type-1 images have been used the trial is automatically type-2.
    elif trialImageCounters[2] == len(a):
        trialtype = 1  # If the maximum number of type-2 images have been used the trial is automatically type-1.
    else:
        trialtype = random.choice(type)  # If neither type-1 or type-2 images are all used up, pick a random type.
    if trialtype == 1:
        target = a
        distractor = b
    else:
        target = c
        distractor = d

...

# The image index is taken from the counter for the selected type of trial
distractorstim.image = distractor[trialImageCounters[trialtype]]  
targetstim.image = target[trialImageCounters[trialtype]]

trialImageCounters[trialtype] += 1

...

# loop
trialImageCounters = {1:0, 2:0}  # Create a different image counter for each trial type
for i in range(len(a)*2):
    loop()
Brionius
  • 13,858
  • 3
  • 38
  • 49
  • The image display code is in the "create window and stimuli" section. I tried doing this, the problem is that the values that the index come from are out of range once it goes beyond 64. :( – y3trgfhsfgr Feb 25 '15 at 23:28
  • 1
    Ok, well you need to post **that** code if you want help with that. I was just guessing how you might have set things up. – Brionius Feb 26 '15 at 00:08
  • It is all posted. The above code is exactly what I am using. – y3trgfhsfgr Feb 26 '15 at 00:32
  • 1
    You said: *I tried doing this, the problem is that the values that the index come from are out of range once it goes beyond 64*. If you can show us **that** code, then we can help you fix it. I don't know exactly how you want the program to work, so I can't write the looping and display code for you, but I could help you fix it if you gave it a try then showed us what went wrong. – Brionius Feb 26 '15 at 01:17
  • Sorry I misunderstood. I'll post above. – y3trgfhsfgr Feb 26 '15 at 01:34
  • Actually, it was my fault - I didn't notice that your code box was truncated with a scroll bar - sorry. I understand the problem now - I'll think about the best way to do this. – Brionius Feb 26 '15 at 02:12