Adding breaks between trials and how many trials they have left

OS (e.g. Win10)
PsychoPy version (e.g. 1.84.x)

Hi guys,

I have created a loop within my experiment that presents 3 of my experimental trials (which includes responding to sound stimuli) in a random order and a text stimulus that tells my participant to take a short break after each of the three trials. This loop works fine as is.

However, I want to add to each “break” message a part that tells them how many trials they have left. For example, the first break message would say “Take a short break. You have 2 more trials to go”. The next one would say “Take a short break. You have 1 more trials to go”.

I know an easy way to fix this would be to get rid of the loop and create 3 routines including the sound stimuli (my experimental trials) and 2 break messages between them. However, I need the experimental trials to be randomized in order, which is why I have them in a loop.

Is there any way to add the a countdown of how many trials my participants have left within the break message?

Thanks.

Hi There!

You can use the information stored in your loop (which is a trialHandler) to create this feedback in your text component. Try adding this to your text component where you want the feedback

$'you have completed %s of %s trials!'%(trials.thisN, len(trials.trialList)*trials.nReps)

Where ‘trials’ is replaced with the name of the loop you feed your conditions file into :slight_smile:
Basically this is accessing the parameters of your loop (thisN = the current trial numer (starting at 0 because it is python), trialList = list of trials you feed in as conditions, nReps = how many times you are repeating the loop psychopy.data - functions for storing/saving/analysing data — PsychoPy v2021.2).

To demonstrate. Here is a builder fileblockFeedback.psyexp (10.1 KB)
and corresponding conditions file conditions.xlsx (8.5 KB)

Hope this helps,
Becca

1 Like

Thanks Becca!

I actually found a thread giving a very similar suggestion as you and it worked!

fantastic - pleased you found a solution!

Hi Becca,

Your code worked perfectly on Psychopy, but I’m having an issue running it online on Pavlovia. I think it is having trouble translating len() from Python to Javascript, but I am not certain. So I was wondering if you had any suggestions?

Here is my error message on Pavlovia:

Screen Shot 2020-09-16 at 4.02.02 PM

Thank you,
Mike

Hi Mike,

Ok sorry I didn’t realise this was for online - there are a few more steps to think about when getting online so I’ll out line those here - hopefully it helps!.

As you rightly point out, when getting online, your python code is translated to JS code to run in the browser. Most errors like the one you have there show us there has been an error in translation. Here, as you say, the JS side of the code can’t figure out what len is (because the function for the two isn’t the same in both languages). A way to figure out what’s wrong is to add a code component and check out what the ‘missing’ code/function translates to in JS (note the Auto->JS in code type):

So here we can see that actually in JS len(a) is implemented slightly differently - a.length.

For use online, I would highly recommend that rather than adding code directly to component fields (i.e. above we have this code in the ‘text’ field of our text component’ ) we enter a variable name, e.g. $feedbackText, into the field and set that variable using a code component at the beginning of our routine (this way psychopy will handle most of the translation for us).

Another thing I noticed for online use is that we might need to create the feedback string using ‘+’ instead of ‘%’. So, for online use, add a code component, then add this code to your ‘Begin Routine’ tab:

feedbackText = 'you have completed' + trials.trialList.length +' of' + trials.nReps +' trials!'

Then in the text component set your text to be $feedbackText

Let us know how you get on,
Becca

Hi Becca,

Thank you again for your help!

I implemented all of your suggestions and I got the message to work online.

However, it seems that trials.nReps always yields the same variable. After my first trial, it (correctly) says “You have completed 1 of 3 trials!”, but after my 2nd and 3rd trial it says the exact same message. See below:

After looking at my loop settings, trials.nReps seems to correspond to the number entered into “nReps $”. When I changed this number, the number in my break message also changed. However, this wasn’t the case in the builder view where it would update every trial (perhaps Javascript interprets it differently?)

Screen Shot 2020-09-17 at 11.29.47 AM

I’m wondering if I should implement “trials.thisN”, but I’m not sure if it can also be used in the online version of Psychopy.

Thanks,
Mike

Hey,

The stages you have taken so far to figure out the issue are exactly correct and checking the docs of the trialHandler (the function that handles the loop) (https://www.psychopy.org/api/data.html#psychopy.data.TrialHandler) I would say you might actually want thisRepN - have you given this a go online? I think it should work OK.

Becca