Images appearing as black boxes after many trials
Hi I am hoping someone can help me with a rather weird problem. I am working on an experiment that is >1000 trials. The experiment uses a "mouse contingent window" in which the subjects should move the mouse around a gray screen to locate an image "hidden" below the mask. This part of the code is working fine. However I am running into a problem consistently after (about) 900 trials: the image appears as a black box. There also distractors (just circles) drawn, but they are not affected.
I have tried switching the backend from PsychoPy to xpyriment | legacy, but with the later two I immediately get an AttributeError 'pygame.Surface object has no attribute 'units'. I have copied the critical part of the code below, any help would be greatly appreciated!
import psychopy
from psychopy import visual
import numpy
import pygame
from PIL import Image
from openexp.mouse import mouse
my_mouse = mouse(exp)
# the filename of the image
# the images were previously in the file pool, but I moved them thinking this might help... it didnt..
fpath = 'C:\\Users\\Allgm1\\Desktop\\DejansExpra\\stimuli\\'
fname = fpath+ var.object + var.version + '.jpg'
# some of this I took from something I found online before.. not sure where from... I would bet Edwins stuff
maskImageSize = 256
meshx,meshy = numpy.meshgrid(range(-maskImageSize/2,maskImageSize/2),range(-maskImageSize/2,maskImageSize/2))
imgArray = numpy.ones((maskImageSize,maskImageSize,4),numpy.uint8)*128
maskimage = Image.fromarray(imgArray,mode='RGBA')
# target
stim = psychopy.visual.ImageStim(win,fname,pos=var.stimpos[0])
# distractors
distractor1 = psychopy.visual.Circle(win,radius=75, pos=var.d1pos, fillColor = [1,0,1])
distractor2 = psychopy.visual.Circle(win,radius=75, pos=var.d2pos, fillColor = [0,1,1])
distractor3 = psychopy.visual.Circle(win,radius=75, pos=var.d3pos, fillColor = [1,-1,-1])
# mask
mask = psychopy.visual.ImageStim(win,maskimage,interpolate='linear')
mask.setSize((max(win.size)*2,max(win.size)*2))
windowSize = 10.0
t0 = clock.time()
while True:
# Get MOUSE position
(x, y), timestamp = my_mouse.get_pos()
distance = ((var.txpos-x)**2 + (var.typos2-y)**2)**0.5
if distance < 50:
t1 = clock.time()
break
# ... and draw a mouse-contingent window
imgArray[:,:,3] = 255*(1-numpy.exp(-(meshx/windowSize)**2-(meshy/windowSize)**2))
maskimage = Image.fromarray(imgArray,mode='RGBA')
mask.setImage(maskimage)
maskcenter = (x,-y)
mask.setPos(maskcenter)
distractor1.draw()
distractor2.draw()
distractor3.draw()
stim.draw()
mask.draw()
win.flip()
# show the target again
stim.draw()
win.flip()
# decide with left or right mouse button
while True:
# until mouse is clicked
button = my_mouse.get_pressed()
if button != (0,0,0):
t2 = clock.time()
break
# search time
var.search_time = t1-t0
# RT
var.reaction_time = t2-t1
# Accuracy
# left is 1, right is 3
# create buttoname variable for correct answer comparison
if button[0] == 1:
buttonname = "left_button"
elif button[2] == 1:
buttonname = "right_button"
elif button[1] == 1:
buttonname = "scroll"
# checks if the button pressed corresponds to the correct answer
if var.correct_response == buttonname:
var.correct = 1
else:
var.correct = 0
what I have tried:
1. let it run without drawing the mask - same error
2. draw 10 targets instead of 1 to see if it is some sort of memory problem - this leads to the images incrementally going black. so again after approx. 800-900 trials, 4 out of the 10 images consistently turn black at every trial. After x more trials it is 6 and so on until at some point all of them turn black at every trial. again, the distractor circles are not affected..
Any idea what is going on? Or do I need to specify something to make the problem more clear?
Thanks in advance!
Comments
Hi,
This is a problem with PsychoPy that I've experienced myself as well on a few occasions. I don't think that there's anything you can do about it, although you could ask around on the PsychoPy users group. I think it's a memory corruption of sorts.
That's because you are using PsychoPy code directly, in which case you must use the
psycho
backend. But there's nothing in your code that couldn't be done just as easily using OpenSesame's native Python libraries, which are backend-independent, or Expyriment, which requires thexpyriment
backend.Is there any particular reason that you need PsychoPy?
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
thanks for the fast response!
It is quite unfortunate that PsychoPy shows this issue! I will definately raise it in the users group and see what people think.
About using canvas(): I agree that it would be no problem to draw the stimuli with it, but I am a bit unsure/confused about drawing the mask and making it mouse contingent.
First, it seems to be disagreeing with the
maskimage
I am feeding it:<PIL.Image.Image image mode=RGBA size=256x256 at 0x61ECF30>' is not a supported image format
Second, it seems that I can't really set a size of the canvas, like I do here in PsychoPy:
mask.setSize((max(win.size)*2,max(win.size)*2))
Do you have any quick suggestions about how to best implement the mask with canvas()?
Thanks!!!
Hi,
I actually misread your code: I thought you were just showing an image at the gaze location. But even a gaze-contingent window can be done in pure PyGame. It's maybe even easier than in PsychoPy, or at least the way you're doing it now.
Here's an example of a mouse-contingent window, which you can easily modify to a gaze-contingent window. This requires the legacy backend or the xpyriment backend with OpenGL set to 'no.'
Another simple solution would just be to run your experiment in two shorter sessions, so that the memory corruption issue in PsychoPy doesn't come up.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
thanks for the detailed answer and code!
I did think about spliting my experiments into 2 sessions, but this would really only be my last choice!
I played around with your code today, but the fact that the pygame servers were down and I could not get a hand on their documentation pretty much limited my efforts!
I get a mask which only covers the object, rather than the whole screen, when using your code, even though this is set:
mask = pygame.Surface( (var.width, var.height) )
.If I want to add distractors to the screen, can I then just draw them to
win
?I hope you have some suggestions! pygame being offline makes this
blit
method a bit mysterious!Thanks!
What do you mean exactly? My script shows a mouse-contingent window (to be adapted for gaze). That's what you want, right?
That would probably work, but it would be more common to blit the distractors to
win_img
and then blitwin_img
towin
.Look what I found after 1 microsecond of Googling!
Check out SigmundAI.eu for our OpenSesame AI assistant!