If this template helps then use it. If not then just delete and start from scratch.
OS (Sierra 10.13.6): PsychoPy version : Psychopy 2 PY 3 1.90.1
Hello,
I developed a task on psycho-py where some stimuli are presented, and if the participant reaches a number bigger than one variable (e.g., total_correct), there is code added that ends the loop (End routine). It is working nice.
My question is: Is it possible to set temporal criteria to finish this loop? For example, after 60 seconds this trials started the loop will finish?
I bought the book recently released and search the manuals available, but I couldnāt find information about using the time elapsed in an experiment this way. Does anyone know where I could find information about the mechanics and correct codes to use the time elapsed in an experiment?
Hello Michael, thank you very much for your reply. I am sorry for responding only now, I was participating on a conference last week, and had no time on the computer.
The code you said worked perfectly thank you. However I would like to know if there is a variable that consider the entire time of the experiment and not only the time in a trial.
The way it is now, if there is any trial and it lasts for 60 seconds it ends. But is there any variable that I could show many different trials and it will consider the complete time since the experiment started? Or even it counts the time of my loop?
You could create one with any of the clock/time/core functions. For example, at the start of your experiment (or at any point where you want to start counting, you can put something like:
timeStart = psychopy.core.getTime()
Then in the every frame* of your routine bit, you could write:
timePassed = psychopy.core.getTime() - timeStart
# This gives you the time that has passed since you created the timeStart variable.
if timePassed >= 60.0:
continueRoutine = False
yourLoopName.finished = True
# This bit is borrowed from Michael's post above. I THINK that getTime() uses seconds rather than milliseconds, but if not, adapt accordingly.
*You might want to put it at āend routineā instead, so that a) the calculation doesnāt execute every frame and b) your current trial finished neatly. But thatās up to you!
Hey Cesco, thank you very much for your suggestion it worked really nice.
however when I put the code timeStart = psychopy.core.getTime() it was showing an error saying
Name error: name psychopy is not defined. I tried to remove the psychopy and use only the timeStart = core.getTime() and it worked great.
I would like to ask another question if you have any clue on how to do it. I have several stimuli that will be presented this way for 60 seconds. Even if I created different timeStarts (e.g., timeStart1, timeStart2, etc) and different timePassed (e.g. timePassed1, timePassed2, etc) the value of the core.getTime() seems to pick the time of the beginning of the experiment. What I am doing now is to use multiples of 60 (1st trial 60, 2nd trial 120, 3rd trial 180, etc). Do you know any way that the core.getTime() gets the time of the moment of the beginning of the routine and not the beginning of the experiment?
Additionally I noticed that the time criterion is working but only if a correct responses occurs. If there is no response the routine is not being finished. If I disable the force the end of routine in the key_resp that I inserted only the time criterion is not controlling the end of my loop. If you have any advice on this just let me know. Thanks so much! Sorry to bother!
timeStart = core.Clock() # to instantiate a clock object
timeStart.reset() # To reset time at your baseline e.g., start of trial, start of stimuli
timeStart.getTime() # to get the time
BTW, if you are using Builder, there a variable called āglobalClockā which is created at the beginning of the experiment. Thus, if you wanted a clock that considers the complete time since the experiment started, this is it. Just use globalClock.getTime() whenever you need it.
For controlling the end of the loop using a response, you can use something similar to what Michael has shown, in the Every Frame tab of the code component:
# Every Frame
if <yourKeyboard>.keys == corrAns: # where corrAns = 'space'
continueRoutine = False
yourLoopName.finished = True
Thank you very much for your reply. I will use the codes you posted and post the results later.
About the end of the loop, I might expressed unclearly (sorry, as you can see I am not a native English speaker) I would like to do the exact opposite, I would like that the only criterion is the time, so if the end routine code have a line, if time Passed >= 5.0, if 5.0 seconds are passed even without any response the loop should end.
however what is happening is that, the stimulus is presented on the screen and even if 5 seconds passed the routine only ends if I press the key. Just let me know if I explained correctly now. Thank you very much!!!
I would like some events occur after some time and a response. This is working well, however if no response happen I would also need that after some time only criteria the loop would end.
I did the code including time AND response and this part is fine. (e.g >=5 and a correct response).
However if I use also another code considering only time (e.g timePassed >= 10) it is not ending the loop. It stays active infinitely and it is only ending if I press the correct key.
I was thinking that if any of the statements were true they should work .
Thank you very much for your attention, with your advices (and also Cesco and Michael) I managed to finish my task and it is working really nice.
I couldnāt use the reset resource, but I figure out another way without using it. I donāt want to bother more but if you have any suggestion on written material about psychopy (other than the book and the manual) just let me know.
Hi Michael very helpful! I have one issue thou, the experiments ends when the routine hits the costume code. I have tried to just get data ātimeS = pschopy.core.getTime()ā with no decision making or operations and the experiment still ends when it gets to the costume code. Any thoughts **
Hi, probably best if you start a new topic and provide full details for your particular situation, including giving us all of your custom code, and a complete explanation of what you want to happen vs what is currently happening.
Hi Michael, I have started a new topic as you have sugested " Costume code malfunction"
This is basically 2 routines in a loop. The loop will go through as many iterations it can, in 60 sec and then will terminate. The issue is that it doesnāt terminated in 60 sec. Can you help?