Re: [psychopy-users] Key press to end a loop

1,488 views
Skip to first unread message

Michael MacAskill

unread,
Nov 12, 2012, 7:58:11 PM11/12/12
to psychop...@googlegroups.com
Hi Mitch,

I'm not an expert in Builder, but although it does allow for the ending of a routine, it doesn't seem at first glance to allow for the conditional ending of a loop, which is what you want.

You could add a code component at the top of the elements in your routine, and put something like this in the "Each Frame" box:

if event.getKeys(["space"]):
trials.addData('detectedOnRepetition', trials.thisRepN)
while trials.nRemaining > 0:
trials.next() # end the loop by instantly cycling though it

This is kludgy, as it mucks up the data storage more than a bit.

There is probably a better way from someone who knows Builder better… (This sort of thing would be very straightforward in the Coder view, as you could make the loop and individual stimuli conditional on keypresses exactly as required, and terminate when necessary).

Regards,

Mike


On 13 Nov, 2012, at 09:43, Mitch LaPointe <mitchla...@gmail.com> wrote:

> Hi All,
>
> I've been trying to create a change detection experiment using psychopy. I've created a loop where stimuli 'A' is presented, followed by a blank screen, followed by stimuli 'B', followed by another blank screen. I've placed a loop around this sequence to alternate up to 20 times, however, I've also like to have participants hit the spacebar when they see the change, thus ending the trial. Unfortunately, what is occurring is the loop will go through one cycle and then wait for the spacebar press in order to advance to the next cycle of the loop. Does anyone recognize this problem? I've made sure to click the 'Force End Routine' button with no success. Any advice would be helpful.
>
> Thanks!












Jonathan Peirce

unread,
Nov 13, 2012, 4:49:45 AM11/13/12
to psychop...@googlegroups.com
Actually you can end a loop prematurely from builder using the line
nameOfLoop.finished=True

Jon

On 13/11/2012 00:58, Michael MacAskill wrote:
> Hi Mitch,
>
> I'm not an expert in Builder, but although it does allow for the ending of a routine, it doesn't seem at first glance to allow for the conditional ending of a loop, which is what you want.
>
> You could add a code component at the top of the elements in your routine, and put something like this in the "Each Frame" box:
>
> if event.getKeys(["space"]):
> trials.addData('detectedOnRepetition', trials.thisRepN)
> while trials.nRemaining> 0:
> trials.next() # end the loop by instantly cycling though it
>
> This is kludgy, as it mucks up the data storage more than a bit.
>
> There is probably a better way from someone who knows Builder better� (This sort of thing would be very straightforward in the Coder view, as you could make the loop and individual stimuli conditional on keypresses exactly as required, and terminate when necessary).
>
> Regards,
>
> Mike
>
>
> On 13 Nov, 2012, at 09:43, Mitch LaPointe<mitchla...@gmail.com> wrote:
>
>> Hi All,
>>
>> I've been trying to create a change detection experiment using psychopy. I've created a loop where stimuli 'A' is presented, followed by a blank screen, followed by stimuli 'B', followed by another blank screen. I've placed a loop around this sequence to alternate up to 20 times, however, I've also like to have participants hit the spacebar when they see the change, thus ending the trial. Unfortunately, what is occurring is the loop will go through one cycle and then wait for the spacebar press in order to advance to the next cycle of the loop. Does anyone recognize this problem? I've made sure to click the 'Force End Routine' button with no success. Any advice would be helpful.
>>
>> Thanks!
>
>
>
>
>
>
>
>
>
>
>

--
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk


This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

Mitch LaPointe

unread,
Nov 13, 2012, 9:26:20 AM11/13/12
to psychop...@googlegroups.com
Thank you, both!

Michael MacAskill

unread,
Nov 13, 2012, 2:15:46 PM11/13/12
to psychop...@googlegroups.com

On 13 Nov, 2012, at 22:49, Jonathan Peirce <jonatha...@nottingham.ac.uk> wrote:

> Actually you can end a loop prematurely from builder using the line
> nameOfLoop.finished=True
>
> Jon

Ahh, simple as that. I'd tried setting nameOfLoop.nRemaining to zero with no joy and without looking at the code, assumed .finished would be effectively read-only too. Good to know.

Cheers,

Mike

ShoinExp

unread,
Jan 28, 2015, 7:32:52 AM1/28/15
to psychop...@googlegroups.com
Hi,
     Following up on this, I have a Builder (1.80.03) experiment in which I repeat the same Routine 10 times in a single loop (and that loop then cycles indefinitely).  N.B. This is 10 copies of the same Routine in a row on the timeline.  I want the subject to hit a key when they notice a change (as Mitch did); so I added a code component (checking each frame) to end the loop when the subject hits a button.  It works fine, but unfortunately it finishes out the current loop before ending, so if the subject is on the first of the 10 Routines and hits the button to end the loop, the remaining 9 Routines are shown.  Is there anyway to get Psychopy to end the Loop (and all remaining Routines in that loop) immediately?
Thanks!
Mark

P.S. [TECHNICAL POINT] I know the immediate solution would be to replace those 10 copies of the same Routine with a loop cycling through the routine 10 times.  However, the movement from routine to routine creates an audio rhythm that has to go quite fast for the experiment, and using a loop does not cycle through routines fast enough to produce the necessary rhythm (the loop is kinda sluggish - I can't get it to go fast enough).  Neighbouring routines cycle faster, so I used multiple copies as a workaround to get the necessary speed.

Michael MacAskill

unread,
Jan 28, 2015, 9:19:28 PM1/28/15
to psychop...@googlegroups.com
Hi,

My default response was "nest two loops" but I see you find that doesn't work for you… 

It pains me to think of ten repeated routines :-) but if there is no other way,  I think you could just do a check within each of those routines in their "Start routine" tabs to kill them before they start, e.g. something like:

if nameOfLoop.finished: continueRoutine = False

Cheers,

Mike

ShoinExp

unread,
Jan 29, 2015, 1:55:50 AM1/29/15
to psychop...@googlegroups.com
Hi Mike,
    You're a life saver!  That worked perfectly (had to put the "if" command on the "each frame" tab, but other than that, worked perfectly).
    Your solution to my other problem also worked perfectly (though the cycling through loops is still not fast enough to avoid the multiple copies workaround described above).
    Thanks!
    Mark
Reply all
Reply to author
Forward
0 new messages