Way to save extra data when experiment is interrupted within builder plugin


we are currently working on a plugin and trying to figure out if there is a way to get custom data saved in case the experiment is interrupted. Something like “def writeExperimentEndCode” is for the end of an experiment and compatible with the new builder compile setup.

Greetings, Florian

If I understand you correctly, this isn’t something that PsychoPy itself provides, but Python has a couple of options for you:

  • garbage collection can be used for this so that when an object is deleted it runs its __del__ method. That’s important for some of our classes (hardware that may need to gracefully disconnect, or opengl stimuli that need to clear themselves from the graphics card memory)
  • if it really is just once on exit that you need the code then use Python’s atexit module

If those don’t do the job then we could think about something new for ExperimentHandler (using it’s __del__ function as above)

Oh, no I didn’t mean this extreme case. What I meant is if the user exits the experiment using ‘q’ or the escape key.
In that case my within writeExperimentEndCode defined data handling will not be executed, right? On the other hand PsychoPy Builder experiments still save data. Is this done in saveData? Is there a convenient way to add lines there if needed? Btw, is there a complete list of functions to add lines to the code via a component or routine, like writeExperimentEndCode, writeStartCode or writeRoutineEndCode?
I hope it’s much clearer now what I intended for and I will for sure look into your suggestions :slight_smile:

On the other hand PsychoPy Builder experiments still save data

Actually the way that the main PsychoPy data files are written is exactly using these methods. ExperimentHandler has a __del__ method which calls self.close() which saves any data to csv. Logfiles (logging targets) also have a __del__ method that makes sure they get all their inputs with a flush()

The good thing about these methods is that they also allow data to be saved if, for example, the final line of a 2 hours experiment has an undefined variable - these __del__ calls still get made as the Python instance cleans up a crash (if it isn’t a segfault!)