psychopy.org | Reference | Downloads | Github

Breaking simple non trial loop conditionally online

I want a single cycle of 12 trials at first. However, if the participant has equal to or more than 8 correct responses, he should be able to end the loop and if not, he should enter the practice loop again. The practice loop should run for a maximum of 3 times.

nReps = 3

If you call “practice_rounds” “trials” then you’ll be able to use Auto translate Python code which will be easier to debug.

in my experiment,
i am defining a variable as

rept in begin experiment section.
then updating this variable after one block

but this update value is not goinf in
nResp space… i have mentioned “rept” variable here without $

nResp = rept in outer loop… updating of rept variable in innere loop… similar to above mentioned experiment…

Any suggestion

I’m pretty sure you can’t edit nReps once the loop has started. You can only end it early (using trials.finished=true; in JS)

use of any variable for nResp would not work?

any suggestion (relevant question) from forum would be really helpful.

Tried suggestion from this thread… i think they have used a variable for nResp

this is not working for me somehow.
:frowning:

You can use a variable for nReps but changing it once the loop has started has no effect.

may be i am wrong here but it seems this person is using similar concept in the given link

I solved my problem using the following:

  1. Inserting a check routine with a code component:

Begin Experiment:
score=0

Begin Routine:
if score/training_loop.nTotal > 0.90:
repeat_training_loop.finished = True
else:
score = 0

  1. Adding a code component in the probe routine

End Routine:
if trainingresp.corr:
score=score+1

Thank you for your help.

Is there any other option to solve such issue, please let me know, that would be really helpful.

Thank You

That code should work except that for online use you need to refer to the loop as trials, I.e. trials.finished=true; as per one of my previous posts

this code is not working offline for me…

trials.finished=true;
is this code for repeat loop?

suppose is condition got fulfilled that accuracy less than 80% then inner loop would repeat. is this correct?

and

trials.finished=false;
for getting out off outer loop

what is “trials” here name of loop or routine here in this case.

Thanks for helping.

Hi Surabhi,

I’m not sure why your experiment doesn’t run, but I think there’s two things, 1) the code to break out of a loop offline and online aren’t the same, 2) you shouldn’t need to set conditions or parameters for the outer loop.

I’m confident you should be able to get this loop to work for your code since I’ve applied it in multiple online experiments now.

This is what should work:

for the outer ‘practice_rounds’ loop:

  • looptype = sequential
  • untick ‘Is trials’
  • nReps = 3
  • everything else left blank, it should say ‘no parameters set’

then for your inner loop:

  • I have looptype as random (think you should be able to use other types, but maybe use this to start)
  • ‘Is trials’ is ticked
  • nReps = 1
  • conditions = a .csv file with your conditions and corrAns (which you need to break the loop, if you want to base this on correct answers). It should say you have x conditions with x parameters.

This is what my loop looks like (it’s big so the beginning is cut off):

So in the last box inside the inner pracLoop (called Ptrials_feedback), I have a code segment where I keep track of correct answers from the trials. In this code segment, in the ‘begin experiment’ part, for both the Python and the JS code I have PCorrs = 0

Then in the ‘begin Routine’ code segment I have:

for Python:

if key_resp.corr == 1:
feedbackCol = [0,1,0]
PCorrs = PCorrs + 1

For JS:

if (key_resp.corr == 1) {
PCorrs = PCorrs + 1;
}

Then in the outer loop, the single green box titled ‘BreakPLoop’ I display some text to say whether they will need to repeat the practice or not, and a code segment to break the outer loop. In this code segment, for ‘Begin experiment’, for both Python and JS code I have: ‘PLoopTally = 0’

Then in the ‘begin routine’ part I have:

For Python:
if PLoopTally >= 3:
Practice_rounds.finished = True
msg = “Well done!”
elif PCorrs >= 6:
Practice_rounds.finished = True
msg = “Well done!”
else:
PLoopTally = PLoopTally + 1
PCorrs = 0
msg = “Let’s try that one more time!”

So this will quit the loop automatically if the outer loop has been repeated more than 3 times, or it will quit the loop immediately if at least 6 correct answers were given during the practice trials. the text after ‘msg’ is just what I display with this screen for 2 seconds.

For JS I have:

if (PLoopTally >= 3) {
msg = “Well done!”;
trials.finished = true;
} else if (PCorrs >= 6) {
msg = “Well done!”;
trials.finished = true;
} else {
trials.finished = false;
PLoopTally = PLoopTally + 1;
PCorrs = 0;
msg = “Let’s try that one more time!”;
}

So I guess maybe just check in your code that you use trials in your inner loop, that you use the correct break commands to get out of your loops, and then it should work!

Also just one more thing, if you get issues with JS not being able to initialise, check if there are quotation marks in your .csv file you use for trials. JS won’t initialise if you have any quotation marks in there.

1 Like

Hi xai,

I’m not sure if I completely understand your question, it would be helpful if you could post your code and a screenshot of your experimental loops.

Maybe just check my reply above, but ‘trials.finished = true’ will quit you out of a loop online. I’m not sure what ‘trials’ refers to, I think it’s a placeholder for ‘repetitions of the loop’ and the trials.finished = true command should break you out of any loop online. for Python (so offline), you should use the name of the loop you want to quit out of, e.g. my outer loop was called ‘Practice_rounds’ but if your loop is called ‘training_loop’ then for Python you would break out of it by saying ‘training_loop.finished = True’

thanks for your response.

it means i have to put some maximum number in outer loop nResp

then i would quit as required criteria matched.

these are some screenshot hope it would be helpful to understand my problem.
code is not working, loop is working for one time only.

prac_block is outer loop, i have given rept variable in nResp section.

Thanks for you response.

Hi zai,

I don’t think your screenshots loaded before you sent the message, as I can’t see them. It might also be easier to actually send both your Python and JS code as text.

Could you maybe just rephrase what your actual problem is and what you are trying to do?

If you want to quit an outer loop based on a criteria in some way, it’s easiest to put a placeholder value at number of repetitions for the (outer) loop. For example, say nRep = 10, and then try to conditionally quit earlier, as in my previous example as well. Why do you need to give a variable in nResp? I am not entirely sure of what you want to do.

added screenshots now…

i am adding this code in “acc” routine in js only to check the desired condition then repeat or not…

this time in outer loop i kept 10 in nResp

if (omm >= 4)
{
msg = “Let’s try that one more time! Press SPACE key to practice again”;
trials.finished = false;
}

if (comm >= 6){
else{

msg = 'Well done.. Now ready for main experiment';
trials.finished=True;
}

after this code,

now it is generating .js file… after running online
it is freezing at initializing the experiment…

Thank You

trials.finished=false; doesn’t do anything.

You have single quotes in one msg statement and double in the other.

Using the auto translate function helps you to avoid simple errors.

Hi zai,

having trials.finished = false and single quotes for one string and double quotes for another shouldn’t be causing any issues.

However, the loop you printed below wouldn’t run. if statement two is true comm >= 6 then statement 1 would also be true comm >= 4.

So first your conditions aren’t right. you should maybe instead do something like, if comm < 6 and else if comm >= 6.

Question1: does your code does what you want it do to offline?

Question2: where do you define ‘comm’? During your trials, you need to define a correct answer in some way. It might be better to use a trial file with correct answers, and then if key_resp.corr == 1 to say ‘comm = comm + 1’ for example.

I would recommend for you to start a new experiment, and just try to only get the practice loop working, offline and online. You could just use this approach:

for the outer ‘practice_rounds’ loop:

  • looptype = sequential
  • untick ‘Is trials’
  • nReps = 3
  • everything else left blank, it should say ‘no parameters set’

then for your inner loop:

  • I have looptype as random (think you should be able to use other types, but maybe use this to start)
  • ‘Is trials’ is ticked
  • nReps = 1
  • conditions = a .csv file with your conditions and corrAns (which you need to break the loop, if you want to base this on correct answers). It should say you have x conditions with x parameters.

This is what my loop looks like (it’s big so the beginning is cut off):

So in the last box inside the inner pracLoop (called Ptrials_feedback), I have a code segment where I keep track of correct answers from the trials. In this code segment, in the ‘begin experiment’ part, for both the Python and the JS code I have PCorrs = 0

Then in the ‘begin Routine’ code segment I have:

for Python:

if key_resp.corr == 1:
feedbackCol = [0,1,0]
PCorrs = PCorrs + 1

For JS:

if (key_resp.corr == 1) {
PCorrs = PCorrs + 1;
}

Then in the outer loop, the single green box titled ‘BreakPLoop’ I display some text to say whether they will need to repeat the practice or not, and a code segment to break the outer loop. In this code segment, for ‘Begin experiment’, for both Python and JS code I have: ‘PLoopTally = 0’

Then in the ‘begin routine’ part I have:

For Python:
if PLoopTally >= 3:
Practice_rounds.finished = True
msg = “Well done!”
elif PCorrs >= 6:
Practice_rounds.finished = True
msg = “Well done!”
else:
PLoopTally = PLoopTally + 1
PCorrs = 0
msg = “Let’s try that one more time!”

So this will quit the loop automatically if the outer loop has been repeated more than 3 times, or it will quit the loop immediately if at least 6 correct answers were given during the practice trials. the text after ‘msg’ is just what I display with this screen for 2 seconds.

For JS I have:

if (PLoopTally >= 3) {
msg = “Well done!”;
trials.finished = true;
} else if (PCorrs >= 6) {
msg = “Well done!”;
trials.finished = true;
} else {
trials.finished = false;
PLoopTally = PLoopTally + 1;
PCorrs = 0;
msg = “Let’s try that one more time!”;
}

Hi Claire,

Thank you for your detailed response. I followed the same steps as you have described, but unfortunately, I still can’t initialize the experiment online.

Could you please check what is going wrong here?

Please set your experiment to public if you’d like someone to look at it.

Hi,

I am sorry for forgetting to do that. Made it public now.
Here is the link to the gitlab project:

Hi Surabhi,

I am not sure what’s going wrong with your experiment. I feel if JS doesn’t initialise it usually means there is some kind of error in your code (could be as silly as missing a bracket somewhere).

I would suggest you:

  1. change your excel list for trials (e.g.‘LL_main.xlsx’) to a csv file. Then check if there are any quotation marks in there at all. I also see you’re using image files on some of the trials, but left this blank on others. I would replace any trial where you’re not using images with a transparent image, e.g. named ‘blank.png’, which you could just make in a free software like GIMP. I would also maybe recommend using either .jpg files or .png files, but not sure if that would cause any issues. It could be that the program doesn’t know what to do for your other trials where you just left the image blank,e.g. after line 26. You can also quickly trial if this is the problem by changing that excel/csv file to just use the first 5 trials. If it initialises then, you know that was causing the problem.

What might also be causing the problem is if you’re trying to change the location where a stimulus is shown dynamically online. I am not sure what the letters refer to in your excel file, but if you’re trying to use that as a location variable that might not work.

  1. if above doesn’t change anything, start an entirely new experiment with just these practice loops in the most basic form (e.g. only show some text as a trial, very simple excel file or .csv file as trial list (no images)). Then try to recreate the practice loop you want step by step and trial it online.

Generally I would just recommend you try to get your task online step-by-step. Offline Psychopy tasks and functions don’t translate directly to online tasks. So don’t expect to be able to program your whole task offline and then for it to run perfectly online. It’s better to build and add to your task iteratively, e.g. set something up offline, then trial if that part works online. That will also allow you to figure out when and what exactly is going wrong. That’s the best advice I can give you. Good luck!