| Reference | Downloads | Github

Psychopy halts in the middle of the experiment


I’ve been working with Psychopy to develop a self-paced reading task these days. You can actually see that my few posts all relate to this. Almost everything now works well but one thing.

The experiment somehow crashes and stops in the middle of the experiment. In my case, there are 320 sentence stimuli to be processed and it always halts around 199th or 200th stimuli with error depicted below.

Since similar things happend before with totally different task, I guess it is something to do with memory problem or somehow Psychopy thinks it’s too much. Until now, I was splitting one experimental file into two so that I can change the file while participant take rest. When I split stimuli, say 320 sentences into 160 each, halting never happens. It is possible because I know that it stopped near 200 when I tested.

This bothers me when I need randomization of whole list of stimuli. If I split the stimuli and experiment into two, I can’t fully randomize with one command in the Psychopy. I have to randomly assign stimuli into two groups first, and than feed each group to two different experiment files.

Is there any good solution for this?

Thank you for reading.


(1) What version of PsychoPy are you using? There used to be a memory leak for text stimuli (caused by a bug in the underlying pyglet library that PsychoPy uses) but as far as I know, that should have been fixed.

(2) You might benefit from using the TextBox class rather than a TextStim to display your text. There isn’t a graphical Builder component for that, but as I recall, most of your stimuli manipulation is done in code, so it shouldn’t take more than a couple of extra lines to shift to the other stimulus.

If you go to the Coder view and look through the demos menu, you’ll see a few related to using the TextBox class.

The version is 1.85.2
That’s what is shown at the Help-About page.

I tried to convert a piece of TextStim of my code to TextBox but I couldn’t nicely convert to TextBox. The error messages keep showing. I am not familiar with TextBox I guess. I compared the code for TextStim and how I changed it to TextBox, and the error message I get.

  1. Original code with TextStim

# Initialize components for Routine "trial"
trialClock = core.Clock()
text = visual.TextStim(win=win, name='text',
    text='default text',
    pos=(0, 0), height=0.1, wrapWidth=2, ori=0, 
    color='white', colorSpace='rgb', opacity=1,
  1. Changed code with TextBox
# Initialize components for Routine "trial"
trialClock = core.Clock()
text = visual.TextBox(window=win, name='text',
    text='default text',
    font_size = 32,
    font_color = (255, 255, 255),
    background_color = None,
    border_color = None,
    pos=(0, 0),
    color_space='rgb', opacity=1,
  1. The error message I get.

I think it happened because the other part of the code is based on a stimuli that was once textstim.

Hi Kyung,

You may be right. Have you removed the graphical text components from the Builder window? You don’t need to (and actually shouldn’t be) editing the generated .py file: that could lead to errors because we are changing the initial definition of text from one sort of stimulus to another, but later in the Builder-generated code, it assumes that text is still a TextStim.

Instead, remove the relevant graphical Text component. This text stimulus should exist only in your code. So define the TextBox stimulus as you have, but in the Begin Experiment tab of a code component (don’t edit the generated .py file). Then later in other parts of the code component, you refer to that text stimulus as needed (e.g. updating its content). Builder doesn’t know it exists, so there is no conflict.

As Builder doesn’t know it exists, though, you also need to take responsibility for drawing it on screen. Either call text.draw() on each screen refresh, or set text.autoDraw = True at the beginning of the routine.

Thank you for replying but I still get the same error message.

I did as you said, deleting the relevant text component and put the code underneath in the ‘Begin Experiment’ tab and put ‘text.autoDraw = True’ in ‘Begin Routine’ tab.

The error message:

Maybe I did something wrong with defining TextBox.

And more to this, even if I successfully define TextBox in the Code component, how can I set the text of this stimulus as columns in the EXCEL file I’m referring to? In the Builder environment, while setting text component, I could write ‘$sentence(the name of the column that has stimuli in it)’ in the ‘text’ but in the code above I wrote ‘default text’. I wrote that because that’s what it looked like in the .py file. I think I need to know how to make TextBox in the Builder view…