End loop after 5 minutes

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2022.02.05
What are you trying to achieve?: Ending loop after 5 minutes

Hi.

My participants are shown 30 pictures and then are asked to recall them by describing each picture in each textbox. I made a loop that includes nRep of 30 textboxes, but I want this loop to end when it reaches 5 minutes.

I don’t know how to code, but here is what I’ve done so far with the help of other topic. (the name of the loop is trial)

Begin Experiment
newClock = core.Clock()

Begin Routine
newClock.reset()

Each Frame
if newClock.getTime() >= 300:
trial.finished = True

However, it seems like the loop is still ending when all the 30 textbox routines are done, not after 5 minutes.

Some help would be much appreciated!

Hi @hkim174,

The code you’ve written looks correct but might be in the wrong place. With clock.reset() in Begin Routine, your clock will reset with every trial and will never reach 300 seconds. You need two components, one to create & start the clock, the other to check the clock.

Component 1: Create and start the clock

  • this component should be in the routine immediately before your loop starts (e.g. your loop is trial so you want Component 1 to be in blank_1000ms

  • this routine should contain the following code:

    • Begin Experiment
    newClock = core.Clock()
    
    • End Routine
    newClock.reset()
    

    I recommend end routine to start the clock and match it closely with when the loop starts

Component 2: Check the clock

  • this component should be in the routine inside the loop (e.g. your loop is trial so you want Component 2 to be in Recall1
  • this trial should contain the following code:
    • Each Frame
    if newClock.getTime() >= 300:
    trial.finished = True
    

See also: Ending an entire experiment after a set time (60 minutes)

Hope that helps,
-shabkr

1 Like

Thank you so much, Shabkr! After three days of struggling, it finally worked!

An error message popped up regarding the indentation so I added an indent to the second line (trial.finished = True).

if newClock.getTime() >= 300:
trial.finished = True

Hi @ shabkr,

On top of the experiment I had, I added a question to make sure that the participants are still engaging in my task. (e.g., click 5 to indicate that you are still engaging in the task). So there are now two things in my bigger loop. (see the picture I attached)

  1. a loop (named trials_2) within a bigger loop(named trial_3)
  2. a routine(named attention) within the same bigger loop(named trials_3)

Like your previous explanation, I’ve added a code just before the bigger loop (blank_code) and put

Begin Experiment
newClock = core.Clock()

End Route
newClock.reset()

To Each Frame in both practicerecall and attention
I have added
if newClock.getTime() >= 300:
trials_3.finished = True

However, this does not work. Obviously, I don’t know what I am talking about here, but I would really really appreciate your help.

Hi @hkim174,

Right now your code will end the trials_3 loop, but will keep running the trials_2 loop, even if the participant goes over time. Try adding trials_2.finished = True in your if-statements to make sure both trial loops end.

-shabkr

Hi @shabkr

I added the if statements, however, the loop still continues after 300 seconds.
Does the number of nReps in each loop matter in this case?

I have 3 nReps for loop named trials_2
& have 2 nReps for loop named trials_3.

Thanks in advance.

The nReps shouldn’t matter, I tried building a minimal experiment in Psychopy 2022.2.5 and the loops correctly ended early.
Each Frame of both practicerecall and attention should have:

if newClock.getTime() >= 300:
    trials_2.finished = True
    trials_3.finished = True

It sounds like everything should be working, so without seeing the whole experiment I am not sure the issue might be.

Here is a small example, it is set up to end early after 14 seconds. Matching your case, the small loop has 3 nReps and the large loop has 2 nReps.

All routines in this example take 3 seconds, so it ends after completing 3 trial routines + 1 attention routine + 1 trial routine (the end early code is triggered here) + 1 attention routine. Extra code is required to skip/interrupt routines, which is why that last attention routine runs.
endloopearly.psyexp (18.5 KB)

Hi @shabkr,

Thanks for such a sophisticated reply. This is very thoughtful.
I did try your code from your sample experiment, however, it does not work on mine.

I played around with your sample experiment thinking about what would be the cause, and I think it is the time set for each routine that’s causing the error when it’s applied to my experiment. Your trials routines were set to 3 seconds (when I removed this, your sample experiment the screen would not move on)

However, for mine, participants have to type in texts and click a button for the screen to move on (for the loop to continue)

I attached a brief version of my experiment for you to see.
(fyi, the loop is not working either, and I am not sure why)

Some help would be much appreciated!
ending loop after X time.psyexp (18.6 KB)

Hi @hkim174,

Thanks for sharing your demo.

In the mouse component, make sure the new clicks only box is checked. This prevents of the previous trial from carrying over to the next trial (which causes it to immediately end).

After fixing the mouse component to be new clicks only, when I inserted the code into your demo experiment, the loop ends early as expected.
ending loop after X time.psyexp (23.5 KB)

In your main experiment are you receiving any specific error messages? What is specifically going wrong with the main experiment right now?

-shabkr

Hi @shabkr,

I think I might have figured out what the issues are here.

I have a “BIG loop” that I want to terminate after 14 seconds, regardless of which routines I am currently executing. As you know the two routines are the “response routine,” which consists of 3 nReps, and the “attention routine,” which has 2 nReps.

However, I noticed that when you spend more than a certain amount of time (e.g., say more than 14 seconds) in one of the “response routines”, and then click the next button, the routine transitions to the “attention routine” and then ends the BIG loop.

(I used the recent file that you attached to the thread)

Could it be possible that the code in the “Response routine” and the “Attention routine” are not compatible?

As always, thanks so much in advance!

Hi @hkim174,

Yes, this is intended behaviour with the current code. LOOP.finished = True will prevent a loop from repeating, but does not skip/interrupt any routines that still need to run. There are a couple options here depending on the behaviour you are looking for.

In both cases, the code looks like this:

# CLOCK_COMPONENT: the name of your clock component
# TIME: the max time you want the loops/routines to run for
# LOOP: the name of the loop that needs to end early
if CLOCK_COMPONENT.getTime() >= TIME:
    LOOP.finished = True #add this line for each loop that needs to end early
    continueRoutine = False #add this line to end the current routine early

Option 1: Skip the Routine

If you want to skip the attention routine before it starts you would add the code to Begin Routine in the attention routine’s code component. I recommend this option and have attached an example. (in the example, advance the trials with the spacebar)
endloopearly.psyexp (22.1 KB)

Option 2: Interrupt the Routine

If you want to stop the attention routine while it is running you would add your code to Each Frame in the attention routine’s code component.

Hope that helps,
-shabkr

I decided to go with option #1. Thanks for your patience and input on this. This helped me A LOT. I cannot thank you enough, @shabkr!

1 Like