End Routine Based on

I am looking to do two simple things, but their interaction is currently causing my experiment to crash.

The first thing: have a button change color when it is pressed and then end the routine .2 seconds after the color change. To do this, I made it so clicking the button does not end the routine, then added a mouse component, and then added code in the “Begin Routine” tab that says:

timer = core.Clock()
clicked = False
buttonPractice.color = 'white'

and code in the “Each Frame” tab that says:

if mouse.isPressedIn(buttonPractice):
    buttonPractice.color = 'green'
    clicked = True
    endTime = timer.getTime()
    
currentT = timer.getTime()

if clicked and currentT > endTime + .2:
    continueRoutine = False

This is working fine on its own.

At the same time, I have a video playing and when the video ends, the routine should end. This is also working fine on its own, as I have “End Routine” selected under the video component.

Generally, these two functions work fine. However, it is likely that a participant will click the button very close to the time the video is ending. When this happens, it crashes the program. My guess is that the program doesn’t know if it should end the routine with the video time or the code component time, so when they overlap, it goes haywire. However, I tried to fix that by de-selecting the “End Routine” choice in the video component and changing the last part of the “Every Frame” code to say:

if clicked and currentT > endTime + .2 or currentT >= VideoDur:
    continueRoutine = False

but that didn’t fix the issue - it seemed like nothing changed. At this point, I assume the issue is the way that I’ve written the code, but I’m not sure how to tell PsychoPy to end the routine at the video duration (VideoDur) or the endTime + .2, whichever comes first. Any suggestions are appreciated - thanks!

Hi @cpat3, do you get any error messages in the Runner? What exatcly does PsychoPy do when it “goes haywire”?

In my view, the cleanest way to do this would be

if (clicked and currentT > endTime + .2) or YourMovieStimName.isFinished:
    continueRoutine = False

No idea, if this fixes anything.

Thanks for the response! I tried that code and it still crashes sometimes. The frustrating thing is that there is no error code, so it’s really difficult to figure out exactly what is happening.

When the program crashes, it goes either to a fully black or fully white screen and the mouse turns into the blue spinning circle. Sometimes, I can get the experiment to continue by pressing the spacebar, but other times a little window appears that says “Python is not responding. What do you want to do?” and I can either force quit the program or wait for the program to respond (which never seems to happen). I haven’t figured out why sometimes the program will continue and other times it shuts down completely.

The runner window shows " Experiment ended with exit code 3489660927 [pid:8888]" but I haven’t been able to figure out what that means. Otherwise, it just lists off the files it has imported and such.

I think I need to take back some details on the issue. I went back to an older version of the experiment without the color changing button and ran into a similar crashing issue. It seems more difficult to replicate when the button isn’t changing color, but it does still happen. So I guess the issue is coming from somewhere besides the button code, but there still isn’t an error code to help me figure out what’s going on.

Do you by chance get a .log file out of the run of the experiment? This way, you could see what was happening last just before the crash.

I don’t think I’m getting a log file. I looked around online and can’t seem to find how to get the log file output. I found this info, but when I added that code it didn’t change the output. Are you able to help me access a log file?

The other issue is that when it crashes, no data is saved. There’s a text document for the participant number that’s blank, but no .csv or .PSYDAT file (which are both saved when the experiment runs properly). I don’t need the data from a crashed experiment, but it seems odd that it isn’t writing the data as the trials progress.

If the log file is not there in the data directory it’s probably just not written just like the data files, because the experiment crashes.

Were you able to pinpoint which line of code is related to the crashes in the meantime?

In case anyone comes across this in the future - I rebuilt the experiment from scratch and it stopped crashing. Not a clue what the problem was, but it’s gone now.

1 Like