| Reference | Downloads | Github

Custom randomising stimuli

I have psychopy v3.1.2.

I have my trials set up in a .csv file and everything works fine. I need to randomise the presentation of stimuli for each participant prior to experimentation, but unfortunately PsychoPy’s randomisation is not sufficient since I have to apply some restrictions. I have written some python code that imports the csv file using pandas and randomises it with my desired constraints, so everything is in place. I’m just wondering how best to apply it - I could run the code and output/overwrite the csv, and then run the experiment, but that feels a bit clunky to me. I haven’t really made much use of the coder view, but is it possible to paste in my code to there and have it execute each time the experiment runs?

You don’t need to use the Coder view. In Builder, just insert a Code Component (from the “custom” component panel). In the “begin experiment” tab of that component, paste your custom code. It needs to be in that tab so that the randomisation will be completed before any loop that needs to use it.

The advantage of this approach is that you can continue to use the graphical Builder view to develop your experiment, while also getting the benefit of custom code.

It probably is easiest just to write your newly-shuffled conditions to a new .csv file, and use the name of that file in the relevant loop dialog (set to “sequential”, so you don’t undo your careful randomisation of course).

1 Like

That’s great! Thanks for the suggestion.

One last question - the randomisation code takes a small amount of time to find a solution. Is it possible to somehow display a text component with a timer while that code is running?

Not if it is in the “begin experiment” tab, because the stimuli and routines haven’t even been created at that stage.

You would need to insert a dedicated routine for text display that occurs before all the other routines and insert the code into say, the “begin routine” tab of a code component on that routine.

You would then take responsibility for updating a text stimulus while your code runs. If you are updating the screen, you should use win.flip(waitBlanking = False) to not slow down your code by waiting for the next screen refresh.

1 Like