psychopy.org | Reference | Downloads | Github

Using a counter to set a number of trials before proceeding

Hi all,

I’m attempting to code an experiment for an animal touch screen task and wanted to have a fixed ratio of 20:1, where the animal is rewarded after successfully touching the presented stimulus in 20 trials. I was attempting to use a while loop with an initialized counter that adds upon mouse press but was having issues with the mouse.getPressed() function because it returns true on any frame during which the mouse is pressed, and therefore my count is not accurately updating.

I’ve managed to write code that successfully updates the count when I want it to but I’m now running into the problem that, if there is no mouse click, the program proceeds directly to the reinforcement block. I wanted to include an elif statement that is contingent upon the mouse not getting pressed, but am not really sure how to do that successfully. I’ve tried a couple things, but always get stuck because getPressed() returns false when the mouse is not pressed and there will always be at least a couple frames where it isn’t pressed but I have no way of knowing what that reaction time will be.

Here’s what I have so far:

# Set up the window
win = visual.Window([800,600], fullscr=False, monitor="testMonitor", color = '#ffffff')

# make the shape for the left triangle
left_triangle = visual.ShapeStim(
    win=win, name='left_triangle',
    vertices=[[-(0.5, 0.5)[0]/2.0,-(0.5, 0.5)[1]/2.0], [+(0.5, 0.5)[0]/2.0,-(0.5, 0.5)[1]/2.0], [0,(0.5, 0.5)[1]/2.0]],
    ori=0, pos=(0, 0),
    lineWidth=1, lineColor='#f9d70b',
    fillColor='#f9d70b',
    opacity=1, depth=0.0, interpolate=True)

#make reinforcement cue
overlay_1 = visual.ShapeStim(
    win=win, name='overlay_1',
    vertices=[[-(0.75, 0.75)[0]/2.0,-(0.75, 0.75)[1]/2.0], [+(0.75, 0.75)[0]/2.0,-(0.75, 0.75)[1]/2.0], [0,(0.75, 0.75)[1]/2.0]],
    ori=0, pos=(0, 0),
    lineWidth=1, lineColor='#32df17',
    fillColor= None, fillColorSpace='rgb',
    opacity=1, depth=-2.0, interpolate=True)
   
#Mouse
mouse = event.Mouse(win=win)

left_clock = core.Clock()

# left lever presses
left_triangle_count = 0 # Initialize a count for the trials
old_count = 1
left_clock.reset()
while left_clock.getTime() < 5 and left_triangle_count <=20:
    left_triangle.draw()
    win.flip()
    if mouse.getPressed()[0]:
        left_triangle_count +=1
        print left_triangle_count
        win.flip()
        core.wait(1.0)
## here's where I lose it!
    elif not mouse.getPressed()[0]:
        left_triangle.draw()
        win.flip()

#reinforcement cue
left_triangle.draw()
overlay_1.draw()
win.flip()
core.wait(2.0)

win.close()
core.quit()

Please note that it’s still fairly sparse since I wanted to figure this loop out before working on anything related to output (measuring RT and saving that to a csv file, etc.).

Sorry in advance if this is a bit dense, I am happy to elaborate on anything that is unclear. If anyone can think of a way to do this that doesn’t rely on a counter, that would also be appreciated! I’m just really stuck!

Thanks,

Margaux

Hi Margaux,

You need to keep track of some flag which indicates if you have already detected this mouse press, because as you note, the button can stay down for some period. eg create a variable mouse_already_down, which is initialised to False. Then incorporate that into your logic:

if mouse.getPressed():
    if not mouse_already_down:
        # do stuff (only once)
        mouse_already_down = True
else: 
    # do stuff?
    mouse_already_down = False

Hi Michael,

Thanks so much for your response! I tried using the flag and wasn’t able to fix it that way. However, I managed to use a frame count to fix the issue - I’ve pasted the code below in case others are having a similar problem. Essentially, I initialized a frame count at the beginning of the loop and added to it for each loop through. To solve the problem of presenting the stimulus again, I reset the frame counter at the end of the loop so that it starts again if no press is detected during stimulus presentation.

frameN = 0
left_triangle_count = 0
missed_trials = 0

while frameN <=250 and left_triangle_count <=4:
    left_triangle.draw()
    win.flip()
    if mouse.getPressed()[0]:
        left_triangle_count +=1
        win.flip()
        core.wait(1.0)
    elif not mouse.getPressed()[0] and frameN <=249:
        frameN +=1
    elif not mouse.getPressed()[0] and frameN == 250:
        frameN = 0
        missed_trails += 1 
        win.flip()
        core.wait(1.0)

This is my first time with PsychoPy so I’m sure there’s a more elegant solution but this one should work for me. Thanks again for taking the time to help!

Margaux