Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

importing many pictures as canvas - python crashes - garbage collection?

When reading in 100 pictures and building up 400 canvas for an experiment, i get many python crashes. Sometimes i get error messages like 'out of memory' without changing the code. I found the garbage_collection discussion. However, i am not familiar with this function and i do not know where i need to set the gc.enable() into my code.
Any suggestions?

Here is the code:

#import gc

tar = canvas()
path2 = pool['targets.tif']

rotate = [0,90,180,270]
COR_list = []
for i in range(38): #number of correct stim
    for j in range(4): #4 types of rotations
        COR_list[i+j] = canvas()
        path = pool['Folie'+str(i+1)+'.tif']
        #rotate the image to have all 4 directions
        COR_list[i+j].image(path2, y=400)       

FAL_list = []
for i in range(69): #number of false stim
    for j in range(4): #4 types of rotations
        FAL_list[i+j] = canvas()
        path = pool['Folie'+str(i+40)+'.tif']
        #rotate the image to have all 4 directions
        FAL_list[i+j].image(path2, y=400)   

I know i need to reduce the number of stimuli. But i would like to select the most suited by using human data.


  • Hi,

    The problem here is that you're preloading all Canvas objects. But I suspect that this isn't necessary. What exactly is the paradigm? Is it not possible to selectively prepare the Canvas that you want to show, as you normally would in a trial-based experiment?


    There's much bigger issues in the world, I know. But I first have to take care of the world I know.

  • I now created a list of names. And based on this list i kind of overwrite the same canvas object 214 times (I have 214 images). However, it always get's stuck at i=115. Any idea why?

    Here is part of the code:

    #prepare the picture (more is not possible to store in the memory of OS)
    var.COR_TOTAL = 0
    import re
    for i in range(214): 
        #eventually have a break
        if (i+1)%20 == 0:
            key, time = space.get_key()
        #load the stimulus
        strings = re.findall('\d+', present_list[i]) #make a list of all numbers in the name
        present = canvas()
        path = pool['Folie'+strings[0]+'.tif']
        if strings[1] == '1':       #rotate the image to have all 4 directions
        elif strings[1] == '2':
        present.image(path2, y=400) #answer buttons in picture
    #present the fixation dot and the stimulus
        #clock.sleep#(995) #forum does not like clocksleep :)
        stim_time =
        #get response
        key, time = target.get_key()
        var.Stim = str(present_list[i])
        print var.Resp, var.Stim
        if   'COR' in present_list[i] and key=='x':
            var.Cor = 1
        elif 'COR' in present_list[i] and key=='m':
            var.Cor = 0 
        elif 'FAL' in present_list[i] and key=='m':
            var.Cor = 1
        elif 'FAL' in present_list[i] and key=='x':
            var.Cor = 0
        var.COR_TOTAL = var.COR_TOTAL + var.Cor
        var.COR_PERC = float(var.COR_TOTAL) / float(i+1) *float(100)
        var.RT = time - stim_time
        print var.COR_TOTAL, var.Resp, var.COR_PERC
  • Stuck means: Nothing goes. I need to open the task manager to shut down OpenSesame. :|
    Not even Escape button works.

  • Hello,

    I will not probably be able to help but I am curious:

    • if you separate your loop in two loops same issues? Like for 0 to 110 then from 110 to the end?
    • if you use xrange instead of range same issue?


  • Unfortunately, it did not help.
    With xrange it stopped after i=58
    With two seperate loops range(107) and range(107,214) it stopped after i=111
    I tested the original range(214) twice and it stopped after i=115
    For the new runs i closed OpenSesame and opened the experiment again.

  • Right, I see now. By default, OpenSesame forces garbage collection after each sequence, to avoid unpredictable delays due to the garbage collection kicking in unexpectedly. However, in your case this is no good, because everything happens inside one big script. If you re-enable the automatic garbage collection in the general properties tab, does the issue go away?
    Thanked by 1DahmSF

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.

  • Nice, this works.

Sign In or Register to comment.