psychopy.org | Reference | Downloads | Github

Loop only running 1x instead of 8x before ending routine

I have a loop on a routine which draws an image from a xls with 8 images. The routine runs fine and displays an image from the csv properly. But the routine ends after only 1 image is displayed instead of randomly displaying all 8 images from the conditions file. How can I get the loop to re-run until it presents all images in the conditions file?

Hi @sgwallner,

could you show us how you structured your conditions file and what arguments you passed to the loop?

Definitely, thanks for the response to my ill posed question. Here are some details.

  • I’m running 3.2.4 on Windows.

  • the conditions file is an excel sheet with 1 column “DisplayImage” with 8 image paths

  • I would like the routine to randomly present all 8 images once

  • in the image properties, Image = $DisplayImage and is set to start on a condition when the participant’s mouse leaves a target region. That part works fine.

  • in the loop; loopType is Random and “Is trials” is checked. nReps$ is set to 1, and the Conditions is set to DisplayImage.xlsx, the name of the conditions file. Below this in trials properties it shows 8 conditions, with 1 parameter [DisplayImage]. This matches the conditions file.

  • after some digging I think the issue is with my use of continueRoutine in the code element.

  • continueRoutine is set to True in Begin Routine, is set to False in Each Frame after the participants mouse reaches a target region.

if (target_L.contains(mouse.getPos()) or target_R.contains(mouse.getPos())):
start_click = False
leave_start = False
reach_target = True

if reach_target == True:
continueRoutine = False
reach_target = False

  • I also tried to set continueRoutine = True in End Routine, but from what I’ve read online, continueRoutine makes no difference in Begin and End Routine.

  • I tried debugging with a blank image after the trial within the loop, this image was displayed correctly, and strangely this caused the program to sometimes present more than 1 triali before ending gracefully without errors. most of the times the program runs one trial then ends gracefully.

  • in the output code I noticed that it seems to be running through the loop as specified, but the trials themselves are not being presented with the correct image, and option for the participant to start a trial, and move the mouse to a target to end the trial. a screenshot of the output file is attached.

Let me know if you have other questions, or need clarification. Thanks again for your help. I really do appreciate it.

Could you surround your code with backticks to keep the formatting intact?

That wouldn’t have an effect because continueRoutine ends the routine and if you’re already at the end of the routine, you don’t need to end it :wink:

It is difficult to say from the information we have, but could it be, that the loops run through super fast?

if (target_L.contains(mouse.getPos()) or target_R.contains(mouse.getPos())):
    reach_target = True
if reach_target == True:
   continueRoutine = False

With this code, you’re not requiring a click; you’re just checking the position of the mouse. If the mouse hovers over the image position, the next trial might show so fast, that you wouldn’t notice, because it instantly fulfils the condition if (target_L.contains(mouse.getPos()) or target_R.contains(mouse.getPos()))

Could you make a screenshot of how you organize your loop? The logging file shows that you set your loop to 5 repetitions of your 8 images (= 40 trials). I think what you want is one repetition (= 8 trials).

Could you upload your entire conditions file and the logging file?

Thanks for the response Lukas, let me know if the images and attachments are what you are looking for. In digging a bit online I also found this https://github.com/psychopy/psychopy/pull/1144 which suggests using an alternative to continueRoutine.

Also, you noticed one important aspect of this experiment. We want the Response Time measured when the mouse enters the region and not on a participant click, because there is an additional delay to execute a click, and also would be manually controlling the mouse movement to stay inside the target region. We want participants to react quickly and naturally which is why I wrote in that the mouse just needs to enter the target region. But obviously that is causing problems if the mouse stays in that target region. Do you have a suggestion to keep the integrity of a natural quick response time without causing the problem that goes along with it right now? Do you think that is what is causing my problem and not the continueRoutine issue? Thanks again.

DisplayImage.xlsx (8.62 KB)

(Attachment _loglo_new_2020_May_04_2057.psydat is missing)

_loglo_new_2020_May_04_2057.csv (4.15 KB)

(Attachment _loglo_new_2020_May_04_2057.log is missing)

_loglo_new_2020_May_04_2057trials.csv (691 Bytes)

To make sure, the mouse position inside the window is not an issue, you could set the position of the mouse a the beginning of every trial with the mouse.setPos() function to a point on the screen outside of your targets: Here is the link for the documentation.

Cool, that makes sense. Do you have a suggestion about the continueRoutine issue?

Hey Lukas, hoping I can ask you a follow up question about providing visual feedback to the participant based on the correct/incorrect response.

Participants move their mouse into one of two target regions either on the left or right of the screen. Based on the image they see in each trial there is a correct target and incorrect. I would like to present one feedback (currently a png image) if they enter the correct target and the other feedback image if they enter the incorrect target. Our experiment randomly presents one of 8 images. Each image has its own set of correct or incorrect responses. Should I use a code element in the trial loop? How do I connect both images to each trial image, so that a feedback is given properly whether the participant enters the correct or incorrect target region on each trial.

Thanks for your continued help. Let me know if you need clarification.

What do you mean by that. Did setting the mouse position not solve the fast progression to the next trial? You didn’t upload your log file in your post above. Perhaps that could help to find the problem.

You could store what reaction is correct (e.g. a column correctImg filled with strings ‘right’ or ‘left’, assuming you don’t randomize the position of the response options) alongside the image paths in your conditions file. Then, you could access the variable in a code component (you could set the string variables at the beginning of the experiment and then check the mouse every frame):

txtCorrect = 'Correct'
txtIncorrect = 'Incorrect'

if target_L.contains(mouse.getPos()) and correctImg == 'left': 
    txtFeedback = txtCorrect
elif target_R.contains(mouse.getPos()) and correctImg == 'right': 
    txtFeedback = txtCorrect
elif target_L.contains(mouse.getPos()) or target_R.contains(mouse.getPos()):
    txtFeedback = txtIncorrect

The txtFeedback variable would then be the text attribute of a text component in the following routine.
I didn’t test this though.

I tried this but I get an error that txtFeedback is not defined.

That is not enough information to help you on. Could you post the error?

Is your code component in the same routine as your text component? Then make sure, the code component is above the text component.

Did you forget to set your text attribute to update on every repeat?

You could compile the code and go to the line where the error occurs. What does it say?

Sorry about that Lukas, I ended up trying a different way to accomplish this, maybe you can help me with that. I’ve copied in my code element below.

  • I created 2 image elements in builder ;

“feedback_resp_L” to be displayed if the participants mouse enters the Left target. and “feedback_resp_R” to be displayed if the participants mouse enters the Right target.

  • There are 3 columns in the conditions file. The first for the stimulus, and the second two for the R and L response feedback images. All stimuli images and feedback images are in the folder which is indicated by the file path in the conditions file.

  • In the conditions file I created two new columns; “resp_L” and “resp_R” and put the feedback images I created one for correct and one for incorrect in the proper column for each row corresponding to the stimuli. Each stimuli image has one correct response and one incorrect in the resp_L and resp_R columns.

  • The program initiates and runs, showing an instruction screen first. But when entering the first routine it kicks out with an error. “AttributeError: Couldn’t make sense of requested image.” Above that in the output, "AatributeError: ‘bool’ object has no attribute ‘copy’

  • In the image element properties; I set the start condition to be “resp_L==True” with no duration, and in the Image place I put $resp_L which corresponds to the column name in the conditions file and set for every repeat. I did the same thing for the second, “resp_R” image element.

BEGIN ROUTINE

start_click = False
leave_start = False
reach_target = False
continueRoutine = True

resp_L = False
resp_R = False

mouse_time = []
mouse_x = []
mouse_y = []

EACH FRAME

if mouse.isPressedIn(start_box):
start_click = True

if start_click == True:
mouse_x.append(mouse.getPos()[0])
mouse_y.append(mouse.getPos()[1])

if (start_click == True) and (start_box.contains(mouse.getPos()) == False):
leave_start = True

if (start_click == True) and (leave_start == True):
mouse_time.append(mouse.mouseMoveTime())

if (target_L.contains(mouse.getPos())):
start_click = False
leave_start = False
reach_target = True
resp_L = True

if (target_R.contains(mouse.getPos())):
start_click = False
leave_start = False
reach_target = True
resp_R = True

if reach_target == True:
continueRoutine = False
reach_target = False
mouse.setPos(newPos = (0,0))
resp_L = False
resp_R = False

END ROUTINE

thisExp.addData(‘mouse_time’, mouse_time)
thisExp.addData(‘mouse_x’, mouse_x)
thisExp.addData(‘mouse_y’, mouse_y)

I hope that is enough information to help you understand my problem. Let me know what you might suggest. Thanks again, I’m sure this can be quite frustrating, so I really appreciate your time and efforts.

DisplayImage_feedbackfix.xlsx (8.8 KB)

(Attachment _loglo_FEEDBACK_FIX_2020_May_12_0821.log is missing)

(Attachment _loglo_FEEDBACK_FIX_2020_May_12_0821.psydat is missing)

I’m sorry to be so pedantic, but could you use the formatting option?
In Python, indentation can make a huge difference.

Could you post the entire error, not just the message with “AttributeError”. In the other lines, printed out with the error, there is a trace-back, telling us where the error occurred.

Apparently Discourse doesn’t allow uploading log or psydat files. They are missing again in your post.

What do you mean by that? What exactly did you enter as “Start” in the image component?
Was it $(resp_L == True)?

If resp_L is your image path, then comparing it to True, as you did above, will always evaluate to true in python. And if resp_L is your image path, why do you set it to False at the beginning of the routine?

Am I mixing something up here?

No need to apologize. I’m hoping I can get you the info that you need. Maybe these screen shots will be helpful.

Here is the error.

Here is the code from builder.

image.png

image.png

Here is a shot of the conditions file.

Pick a different name for your second and third column of your conditions file (which you now call resp_L and resp_R).
In Builder experiments, Psychopy uses your column name as a variable name. If you then set resp_L to False or True (a boolean as your error says), you also change the variable which should contain your image path.