Howdy, Stranger!

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

Supported by

Images appearing as black boxes after many trials

edited July 2016 in OpenSesame

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,

    However I am running into a problem consistently after (about) 900 trials: the image appears as a black box.

    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.

    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'.

    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 the xpyriment backend.

    Is there any particular reason that you need PsychoPy?

    Cheers!
    Sebastiaan

    Buy Me A Coffee

  • edited July 2016

    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!!!

  • edited July 2016

    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.'

    import numpy as np
    import pygame
    from pygame.locals import *
    
    radius = 100
    source_img = 'image.jpg'
    xc = var.width/2
    yc = var.height/2
    white = pygame.Color('white')
    black = pygame.Color('black')
    my_mouse = mouse(timeout=0)
    
    # Load the image and create a mask, which is also an image
    img = pygame.image.load(pool[source_img])
    mask = pygame.Surface( (var.width, var.height) )
    
    time_list = []
    
    while True:
        # Loop until a button is clicked.
        button, pos, time = my_mouse.get_click()
        if button is not None:
            break
        # Keep track of time so that we can determine the framerate
        time_list.append(time)
        # Get the cursor position, and draw a white circle on an otherwise black
        # mask image. The coordinate transformation is necessary to go from
        # (0,0) = center to (0,0) = top-left.
        (x, y), time = my_mouse.get_pos()
        x = int(x+xc)
        y = int(y+yc)
        mask.fill(black)
        pygame.draw.circle(mask, white, (x, y), radius) 
        # Create a copy of the original image, and combine with the mask so that the
        # lowest color is copied (BLEND_MIN). In other words, wherever the mask
        # image is black, the copied image will become black as well. This gives you
        # a mouse-contingent window!
        win_img = img.copy()    
        win_img.blit(mask, (0,0), special_flags=BLEND_MIN)
        # Show the copied image!
        win.blit(win_img, (0,0))
        pygame.display.flip()
    
    # Check if the frame duration is short enough
    time_array = np.array(time_list)
    dur_array = time_array[1:] - time_array[:-1]
    print('Mean frame duration: %.2f' % (dur_array.mean()))
    

    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

    Buy Me A Coffee

  • 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!

  • 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) ).

    What do you mean exactly? My script shows a mouse-contingent window (to be adapted for gaze). That's what you want, right?

    If I want to add distractors to the screen, can I then just draw them to win?

    That would probably work, but it would be more common to blit the distractors to win_img and then blit win_img to win.

    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!

    Look what I found after 1 microsecond of Googling!

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games