Rounding mouse click location

Hi all, I have a question regarding interpreting mouse locations. I have 6 images on the screen, each in a specified location (coordinates 0 or .5/-.5) In my output file, it’s very precise and I get values like "-0.331944444; 0.313333333"
But in order to interpret whether it’s correct, I’d like psychopy to round it up or down, either to 0 (if the value is between 0 and .25) or to .5 (if the value is between .25 and .5). - and then I’d like extra columns in my data file with these numbers.
I know there is a python function “round”, to deal with that, but I’m not sure how to implement it in psychopy - how do I combine “round” and “addData” into one command?

Thanks

Hi @agata,

From builder, you could either a) compile your script and add the following at the end of the routine, or b) add a code component and add the following in the tab for “End Routine”.

    x, y = mouse.getPos() # get mouse positions
    # Check whether x satisfies conditions, and write data
    if x < .25 and x >= 0:
        trials.addData('mouse.x_rounded', 0)
    elif x >= .25 and x <= .5:
        trials.addData('mouse.x_rounded', 0.5)
    else:
        trials.addData('mouse.x_rounded', round(x,2))
    # Check whether y satisfies conditions, and write data
    if y < .25 and y >= 0:
        trials.addData('mouse.y_rounded', 0)
    elif y >= .25 and y <= .5:
        trials.addData('mouse.y_rounded', 0.5)
    else:
        trials.addData('mouse.y_rounded', round(y,2))
        

Note, this is for positive values of x and y only, you will need to add code for the negative values e.g.,

if [condition] and [condition] or [condition] and [condition]:
    #DO THIS

I suspect you actually want to use your ImageStims’ .contains() function to directly test whether the mouse is in the bounds of the image:

http://www.psychopy.org/api/visual/imagestim.html#psychopy.visual.ImageStim.contains

That means you don’t have to do your own geometrical calculations.

Actually, the pre-release (testing-only) version of PsychoPy has this functionality (hit testing of stimuli) built in to the Builder Mouse component, so if you can wait for that to be released properly, you might even be able to do this with no code at all…

1 Like

Thanks both. I’m not sure I understand the function @Michael , but the position of the image is allocated randomly, so what I would need is something like:
if ‘mouse_rounded’ == positions[1]:
testpractice.addData(‘correct’, ‘True’)
else:
testpractice.addData(‘correct’, ‘False’)
Which unfortunately doesn’t work, probably because the response is coded in two columns in excel, and the location is coded as one, meaning that I always get “false” even when I click on the correct image. Any ideas how to go around it?

Nonetheless, @dvbridges your solution worked, thank you so much for your help!

This is an example from this thread Selecting images on screen using mouse in builder view using .isPressedIn(), instead of .contains(). It doesn’t care how the location of the stimuli is determined:

Thanks! I’m still not sure how to use this - what does the “stimulus.image” refer to? If I put my image filename or variable name e.g.:
if mouseresppractice.isPressedIn(‘distractor1’):
if distractor1 == corrAns
isn’t that a bit redundant? I know that’s the correct answer, what I need to know if whether the participant clicked on it.

either way, I’m getting this error:
Traceback (most recent call last):
File “/experiment code/memory_lastrun.py”, line 779, in
if mouseresppractice.isPressedIn(‘distractor1’):
File “/Applications/Psych Apps/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/event.py”, line 703, in isPressedIn
return any(wanted & pressed) and shape.contains(self)
AttributeError: ‘str’ object has no attribute ‘contains’

Any suggestions as to what this means?

Edit: I worked it out, I think, thank you

We can’t debug your code without seeing the indenting. Please do as the post above shows.

The error message tells you that you are trying to ask a string object (i.e. the string of letters 'distractor1') if it has has a mouse pointer in it, and that can never be true, since strings only exist in memory, not on your screen

I’m guessing that you have some sort of stimulus component named distractor1. Use that name directly: don’t surround it in quotes, which just makes it a series of letters.

I pointed you to some code that solved someone else’s similar problem. No doubt it needs to be adapted to suit what you require.

I will follow that in the future. I figured out what “stimulus” refers to now, so it all works. Thank you again for your help.

Hi There I am trying to save the mouse click location. Could you share how you made it work please ? Thanks

Hi, I don’t really have access to the whole code when working from home, but I found some notes - not sure if this is entirely correct but maybe will give you some idea. I think, but I’m not sure, that if you create a mouse click response, psychopy will save the coordinates automatically, so this is to round them to indicate which part of the screen it was - you may need a different strategy, I had 6 objects so divided the screen into 6. I’m also not sure how it’ll work with the new psychopy, I haven’t used the latest versions

x, y = mouseresppractice.getPos() # get mouse positions
# Check whether x satisfies conditions, and write data
if x < -.25 and x >= -.5:
testpractice.addData('mouse.x_rounded', -.5)
elif x > -.25 and x <= 0 or x < .25 and x >= 0:
testpractice.addData('mouse.x_rounded', 0)
elif x > .25 and x <= .5:
testpractice.addData('mouse.x_rounded', 0.5)
else:
testpractice.addData('mouse.x_rounded', round(x,2))
1 Like