# Selecting images using Mouse

**URL:** <https://discourse.psychopy.org/t/selecting-images-using-mouse/1478>\
**Category:** Builder\
**Tags:** randomization, screen\
**Created:** [November 16, 2016, 2:30pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478 "2016-11-16T14:30:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [November 16, 2016, 2:30pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/1 "2016-11-16T14:30:38Z")

</div>

Dear all  
I have four images (image\_1, image\_2, image\_3, image\_4) on screen and one of them is target. I want the trial to if one of the four images is selected (using mouse). And if the response selected is correct (corrAns), it gives score of 1 or score of 0.  
…  
for stimulus in [image\_1, image\_2, image\_3, image\_4]:

```
# check if the mouse is pressed within the current one:
if mouse.isPressedIn(stimulus):

    # Yes, so store the reaction time in the data:
    thisExp.addData('RT', t)

    # check if the stimulus' image filename matches the correct answer:
    if stimulus.image == eval(corrAns):
         thisExp.addData('correct_responses', 1)
    else:
         thisExp.addData('correct_responses', 0)

    # end the trial once done here:
    continueRoutine = False

    # stop any further checking:
    break

```

…  
It runs but the problem is …  
It skips through some items in my ‘condition’ file. For example I have 47 items/trials in my condition file but it plays only 15 or so items.I have a feeling that it skips through the items.

Interestingly:

Note the same experiment works perfectly, if I remove the code component and set the mouse click to ‘end routine’. So I understand that there is something wrong with the code that is causing this problem. I need the code so I can register the correct response.  
Can anybody figure out what the problem is  
[conditions.xlsx](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/a78446b0b740670c83861188356285e7461771ad.xlsx) (11.5 KB)  
[Routine1,Recall, Mouse.psyexp](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/ce84b7cb68ce1e9d3e0354dab8fab55adeb3d7eb.psyexp) (13.2 KB)

I am also attaching the program

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 16, 2016, 10:45pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/2 "2016-11-16T22:45:17Z")

</div>

> [@kuppuslp](#):
>
> I have a feeling that it skips through the items.

Rather than having a feeling, how about looking at the data files and seeing which trials are completed?

Are the same ones always missed? Are they correct or incorrect trials? This should help isolate the problem

---

<div class="post-metadata">

**Author:** ![matschmitz](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@matschmitz](https://discourse.psychopy.org/u/matschmitz)\
**Post date:** [November 17, 2016, 11:08am UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/3 "2016-11-17T11:08:12Z")

</div>

I had a similar problem. I believe that when you click one of the images your mouse is recording like 5-10 responses (depending on how long your “click” is). To avoid this, I added a short pause at the end of each trial (e.g. `core.wait(0.3)`). I’m not sure it is the right way to proceed though…

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [November 17, 2016, 11:19am UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/4 "2016-11-17T11:19:35Z")

</div>

Hi Michael  
I had a look at the data file (added couple of them).  
1.No, it is not the same ones that is always missed.  
2. I am not able comment on if it is the correct or incorrect trials that is missed. I have responded for all the trials that was shown (say 15 out of 46). In the data sheet I see that all the correct ones are given score of ‘1’ which I wanted and the ones those were skipped are given the score of ‘0’.

Hi Matscmitz, yes it worked with (core.wait(0.3) inserted after every frame. Thanks for that. But seems to create a delay before it takes the mouse click. So I reduced it to (core.wait(0.01) which seem to have no effect, but (core wait.1) does seem to get the task to run every trial as I wanted, but still the delay is annoying.

Could there be another way around it? [\_Recall\_2016\_Nov\_17\_1112.csv](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/96e48aa18202547ef9cec2264e761ea8b8623d7f.csv) (842 Bytes)  
[\_Recall\_2016\_Nov\_17\_1111.csv](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/4eef37e34eb370be14b2597f277a7c1cf65a18c9.csv) (1.3 KB)

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 18, 2016, 2:07am UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/5 "2016-11-18T02:07:29Z")

</div>

It would be useful to have a more detailed explanation of what you mean by trials being “skipped”. The data seems to show that all trials are completed in order, and each actually gets a reaction time.

In the absence of a fuller explanation, I guess what you mean is that some trials have very short reaction times (between 7 and 50 ms). So probably the trial runs, but a very early response is detected and so it might not _appear_ to have run at all.

My guess is that the participant might not have released the mouse button before the next check for the mouse press is made. What probably needs to happen is that the next trial doesn’t run until a check has been made that the mouse button has actually been released. You’d need to explain the sequence of your trial and how such a check period could be incorporated.

Or it might be as simple as calling `mouse.clickReset()` at the start of each trial, I’m not sure:  
[http://www.psychopy.org/api/event.html](http://www.psychopy.org/api/event.html)

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [November 18, 2016, 6:17pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/6 "2016-11-18T18:17:01Z")

</div>

Hi Michael  
Thanks for the clarifications. I tried the following first

Inserted **mouse.clickReset()** in the code component right at the top of every frame section, so the code component looked like this…

…  
mouse.clickReset()

for stimulus in [image\_1, image\_2, image\_3, image\_4]:

```
# check if the mouse is pressed within the current one:
if mouse.isPressedIn(stimulus):

    # Yes, so store the reaction time in the data:
    thisExp.addData('RT', t)

    # check if the stimulus' image filename matches the correct answer:
    if stimulus.image == eval(corrAns):
         thisExp.addData('correct_responses', 1)
    else:
         thisExp.addData('correct_responses', 0)

    # end the trial once done here:
    continueRoutine = False

    # stop any further checking:
    break

```

…

That didn’t seem to help. So I feel your first explanation is more suitable.

\*\*'My guess is that the participant might not have released the mouse \*\*  
\*\*button before the next check for the mouse press is made. What probably \*\*  
\*\*needs to happen is that the next trial doesn’t run until a check has \*\*  
\*\*been made that the mouse button has actually been released. You’d need \*\*  
\*\*to explain the sequence of your trial and how such a check period could \*\*  
**be incorporated.’**

The sequence of my trial is as follows, (I hope this is what you are expecting)

Four pictures appear on the screen from time 0 to infinite.  
After a 250ms a sound is played,  
After the sound is completed playing, the mouse response has to start. The trial will end if mouse was clicked on one of the pictures.  
A click on one of the pictures completes the trials.

So, I would assume that the **check for the mouse button release** should be made just before a new trial and it has to be set as ‘release’ by default?

Thank you for your time again.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 20, 2016, 7:24am UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/7 "2016-11-20T07:24:41Z")

</div>

> [@Michael](#):
>
> it might be as simple as calling mouse.clickReset() at the start of each trial

i.e. do this just once, in the `Begin routine` tab of the code component.

Resetting at the beginning of each frame would wipe any mouse click before you can actually check it.

---

<div class="post-metadata">

**Author:** ![lindeloev](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/lindeloev/32/15_2.png) [@lindeloev](https://discourse.psychopy.org/u/lindeloev)\
**Post date:** [November 20, 2016, 9:58pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/8 "2016-11-20T21:58:46Z")

</div>

You could listen to mouse release instead of mouse down. So insert a separate routine in the end of the trial with a code component that contains this:

```
# Waits until no mouse buttons are pressed
while any(mouse.getPressed()):
    pass

```

This will continue immediately upon mouse release. If you need to have everything on screen until the release, you could also insert this into your stimulus routine, but then have another variable (say `response_recorded`) which is False on trial start but True when a response has been recorded to this trial and then:

```
while response_recorded and any(mouse.getPressed()):
    pass
```

---

<div class="post-metadata">

**Author:** ![kuppuslp](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kuppuslp](https://discourse.psychopy.org/u/kuppuslp)\
**Post date:** [December 9, 2016, 3:36pm UTC](https://discourse.psychopy.org/t/selecting-images-using-mouse/1478/9 "2016-12-09T15:36:02Z")

</div>

Dear Michael and Lindenov.

I tried what Michael suggested and it worked like how I wanted. I did not try Lindeloev’s method, though it looks reasonable. Thanks you two for your time.
