psychopy.org | Reference | Downloads | Github

How to Efficiently Draw Multiple Images

Hello,

I’m drawing a series of vertical rectangles, and changing their size over a certain time period. I draw the shapes using this:

    while True:
        mask1.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask2.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask3.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask4.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask5.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask6.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        mask7.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2)
        
        image.draw()
        mask1.draw()
        mask2.draw()
        mask3.draw()
        mask4.draw()
        mask5.draw()
        mask6.draw()
        mask7.draw()
        win.flip()

My issue though is that at any given point, the size of mask1 is larger than mask7. Is there a more efficient way of drawing and updating shape sizes such that their sizes are the same?

Thank you.

Hi,
how about using list comprehension?

    # all masks in a list
    masks = [mask1, mask2, mask3, mask4, mask5, mask6, mask7]
    while True:
        [m.size = ((unmaskLength - current_time)*(avg_shade_width/unmaskLength)*2,2) for m in masks]
        image.draw()
        [m.draw() for m in masks]
        win.flip()

for-loop would do the job too, but I personally prefer list comprehension.

Hope that helps!
Yuhan

You could also use elementArrayStim to draw many items efficiently. See http://www.psychopy.org/api/visual/elementarraystim.html

Do the masks all have the same stimulus class? You can draw multiple shapes using a single stimulus instance, sort of like drawing with a stamp.

maskSizes = [...]
maskColors = [...]

for i in range(numMasks):
    mask.size = maskSizes[i]
    mask.col = maskColors[i]
    mask.draw()

win.flip()


Attributes like color, size and orientation cost computationally little to change on the fly.