End loop if specific key press

725 views
Skip to first unread message

Vyoma Shah

unread,
Jul 13, 2016, 1:31:26 PM7/13/16
to psychopy-users


Hi all,

I'm trying to build an experiment (on the Builder view) where if the first key press (key_resp_1) is "b", that routine ends and it moves on to the follow up phase of the trial. If the first key press is "n", it should end the whole loop and start a new trial. Any ideas?

I've set the allowed keys as "b" and "n" and it's only storing first key. 

I tried inserting the code component and adding something like this to the Begin Routine part but it doesn't seem to end the loop:

if key_resp_2.keys == "n":
      continueRoutine = False
      loopname.finished = True


Thanks!

Michael MacAskill

unread,
Jul 13, 2016, 5:38:47 PM7/13/16
to psychop...@googlegroups.com

On 14/07/2016, at 05:31, Vyoma Shah <vs...@lbl.gov> wrote:

I tried inserting the code component and adding something like this to the Begin Routine part but it doesn't seem to end the loop:

if key_resp_2.keys == "n":
      continueRoutine = False
      loopname.finished = True

At the "begin routine" point, the response hasn't yet been received (assuming key_resp_2 is on the current routine). Shouldn't this be in the "End routine" tab? But it is hard to tell, as your description of the overall situation is a little hard to parse.

Regards,

Michael

-- 
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Senior Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph:   +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill

Vyoma Shah

unread,
Jul 13, 2016, 5:46:58 PM7/13/16
to psychopy-users
Ah good point! I tried putting it in end routine too but it doesn't seem to end the routine! Sorry, I'll try to be clearer and re-explain here:

The task has ~60 trials, and it loops from one trial to the next. Each trial is such that there are 4 routines - each one is a question and allows the subject to give an answer. If the subject answers yes (the key "b") for the first question it ends that routine, then shows them the next 3 routines/questions. This part works fine.
However, if the subject answers "no" on the first question, I want it to end that routine, skip the next 3 questions for that loop and move on to the next trial.

So far, I just have a regular Builder key response that goes through all 4 routines. Tried writing something to end that trial in the loop but it didn't work :( Any ideas?

Thanks for all the help!

Michael MacAskill

unread,
Jul 13, 2016, 6:32:22 PM7/13/16
to psychop...@googlegroups.com
Right, that is much more useful.

I suspect that the "loop.finished" setting doesn't take effect until the end of the current iteration (in your case, after all four routines have finished).

So you will also need to terminate each of the following three routines to get an immediate termination of the current trial. In an upcoming release of PsychoPy, this will be much easier to do (by setting continueRoutine to be False in the "Begin Routine" tab of a code component.

But at the moment, that doesn't have any effect, so we need a bit of Frankenstein solution:

(1) Around *each* of the last three routines in the trial *individually*, put a loop. They are not connected to a conditions file, and in the nReps field of each, put a variable name, say "runOrNot".

(2) In the "begin routine" tab on the first routine, put this:

runOrNot = 1

This means that the subsequent routines will run by default.

(3) In your code to check the responses, as well as terminating the first routine, do this:

runOrNot = 0

This means that the next three questions will not be presented on this iteration.

Actually, then it is very likely not desirable to end the loop: that would also prevent the subsequent 59 trials. Is that what you want?

Regards,

Michael



On 14/07/2016, at 09:46, Vyoma Shah <vs...@lbl.gov> wrote:

Ah good point! I tried putting it in end routine too but it doesn't seem to end the routine! Sorry, I'll try to be clearer and re-explain here:

The task has ~60 trials, and it loops from one trial to the next. Each trial is such that there are 4 routines - each one is a question and allows the subject to give an answer. If the subject answers yes (the key "b") for the first question it ends that routine, then shows them the next 3 routines/questions. This part works fine.
However, if the subject answers "no" on the first question, I want it to end that routine, skip the next 3 questions for that loop and move on to the next trial.

So far, I just have a regular Builder key response that goes through all 4 routines. Tried writing something to end that trial in the loop but it didn't work :( Any ideas?

Thanks for all the help!

Vyoma Shah

unread,
Jul 13, 2016, 6:46:26 PM7/13/16
to psychopy-users
Ah I see what you're thinking...nice approach!

No, that's also something I'm not sure how to deal with. I don't want to end the remaining 59 trials if they respond no to the first question, just want it to move on to the next trial (next first routine). Any way to end *just* this specific trial (end this routine, skip the next 3 routines and start the first routine for the next trial). Sorry, I'm still a novice to this and figuring out what might be the best way to approach this. Maybe just a chunk of code and no Builder key response at all?

Michael MacAskill

unread,
Jul 13, 2016, 10:41:40 PM7/13/16
to psychop...@googlegroups.com

On 14/07/2016, at 10:46, Vyoma Shah <vs...@lbl.gov> wrote:

No, that's also something I'm not sure how to deal with. I don't want to end the remaining 59 trials if they respond no to the first question, just want it to move on to the next trial (next first routine). Any way to end *just* this specific trial (end this routine, skip the next 3 routines and start the first routine for the next trial). Sorry, I'm still a novice to this and figuring out what might be the best way to approach this. Maybe just a chunk of code and no Builder key response at all?

Yes, just do exactly as I suggested but leave out the line where you try to finish the loop, as that is not necessary or desired.

i.e. in response to the the keypress on the first routine:

(1) set continueRoutine to False: this has immediate effect in ending the first routine.

(2) set runOrNot to 0: This will prevent any of the three subsequent routines running on this trial. Therefore the loop will go straight to the next iteration, starting at the first routine again.

Make sense?

Regards,

Michael

Vyoma Shah

unread,
Jul 15, 2016, 4:19:50 PM7/15/16
to psychop...@googlegroups.com
Awesome, I just tested this and it worked perfect. Thank you so much!!

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/PaEiLrqmMTM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/452A7C77-6880-4DAA-B98C-3388394B4811%40nzbri.org.

For more options, visit https://groups.google.com/d/optout.

Vyoma Shah

unread,
Jul 25, 2016, 5:41:53 PM7/25/16
to psychopy-users
Hi again!

I had to change my experiment so that we no longer had any key presses - just rating scales. So my first question (the answer to which determines whether or not to ask the next 3 questions/run those routines) is now a Builder rating scale with the options "Yes" and "No". If the participant clicks the marker on "Yes", I want it to continue the rest of the experiment versus if they choose "No" I want it to skip the follow up 3 questions and move on to the next trial.

I tried keeping the RunOrNot variable by doing the following:
1) At 'Begin Routine' for Trial 1/Routine 1 I set RunOrNot = 1
2) At 'End Routine' I said:
    if rating_yesno.response == "No":
        RunOrNot = 0
        continueRoutine = False

However, I get this error:
Traceback (most recent call last):
  File "/Users/Vyoma/Desktop/MemoryTask/memorytask_v4_lastrun.py", line 233, in <module>
    if rating_yesno.response == "No":
AttributeError: 'RatingScale' object has no attribute 'response'

Any ideas on how I could fix this?

Thanks again,
V

On Friday, July 15, 2016 at 1:19:50 PM UTC-7, Vyoma Shah wrote:
Awesome, I just tested this and it worked perfect. Thank you so much!!
On Wed, Jul 13, 2016 at 7:41 PM, Michael MacAskill <michael....@nzbri.org> wrote:

On 14/07/2016, at 10:46, Vyoma Shah <vs...@lbl.gov> wrote:

No, that's also something I'm not sure how to deal with. I don't want to end the remaining 59 trials if they respond no to the first question, just want it to move on to the next trial (next first routine). Any way to end *just* this specific trial (end this routine, skip the next 3 routines and start the first routine for the next trial). Sorry, I'm still a novice to this and figuring out what might be the best way to approach this. Maybe just a chunk of code and no Builder key response at all?

Yes, just do exactly as I suggested but leave out the line where you try to finish the loop, as that is not necessary or desired.

i.e. in response to the the keypress on the first routine:

(1) set continueRoutine to False: this has immediate effect in ending the first routine.

(2) set runOrNot to 0: This will prevent any of the three subsequent routines running on this trial. Therefore the loop will go straight to the next iteration, starting at the first routine again.

Make sense?

Regards,

Michael

-- 
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Senior Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph:   +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/PaEiLrqmMTM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-users+unsubscribe@googlegroups.com.
To post to this group, send email to psychopy-users@googlegroups.com.

Michael MacAskill

unread,
Jul 25, 2016, 10:32:54 PM7/25/16
to psychop...@googlegroups.com

> On 26/07/2016, at 09:41, Vyoma Shah <vs...@lbl.gov> wrote:
>
> if rating_yesno.response == "No":
> AttributeError: 'RatingScale' object has no attribute 'response'
>
> Any ideas on how I could fix this?

— Look at the documentation for the rating scale and see what attributes it actually does have (which naturally are different from the keyboard):
<http://www.psychopy.org/api/visual/ratingscale.html>
Note that it returns a list, even if there is just one response.

— And read through the rating scale demos under the demos menu in the Coder view to see how to use its responses.

— And if that doesn't help, cheat and try something like:

if 'No' in rating_yesno.getRating():

Vyoma Shah

unread,
Jul 25, 2016, 11:05:13 PM7/25/16
to psychopy-users
This is a really helpful link, thank you - I'll play around with it!

Still a novice at this, so another basic question: if I want to call the 'textColor' attribute for the category choices how would I do that? I want it to be black instead of the default light gray. I tried a few things on the lines of 'rating_yesno.textColor = 'Black' in the 'Begin Experiment' section but that doesn't work.

Michael MacAskill

unread,
Jul 25, 2016, 11:32:53 PM7/25/16
to psychop...@googlegroups.com

> On 26/07/2016, at 15:05, Vyoma Shah <vs...@lbl.gov> wrote:
>
> that doesn't work

Tell us what the actual problem is. I can guess, but a specific error message would help (us and you).

Vyoma Shah

unread,
Jul 25, 2016, 11:44:41 PM7/25/16
to psychop...@googlegroups.com
Got the conditional loop end working - thank you so much!

For the text color, it doesn't spit out any error message - it runs fine with the color still remaining light gray!

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/PaEiLrqmMTM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/DE721704-1A25-4A2C-9A16-AFF775FB235D%40nzbri.org.

Michael MacAskill

unread,
Jul 25, 2016, 11:58:41 PM7/25/16
to psychop...@googlegroups.com

> On 26/07/2016, at 15:44, Vyoma Shah <vs...@lbl.gov> wrote:
>
> For the text color, it doesn't spit out any error message - it runs fine with the color still remaining light gray!

Try putting

textColor='black'

in the "custom" tab of the rating scale dialog box instead.

Vyoma Shah

unread,
Jul 26, 2016, 12:34:57 AM7/26/16
to psychop...@googlegroups.com
Quite funnily, that changes the color but actually affects with the current setup. The yes/no scale changes to a scale from 1-7 and my other settings (single click, disappear=True) etc all get undone when I do this - maybe I should code up the whole rating scale section and mention each setting?

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/PaEiLrqmMTM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages