How to use psychopy create interruption task

Hi, I am new to Psychopy, and I don’t have any knowledge or experience in programming.

I have created 13 Email tasks that participants have to type to answer. The email task (primary task) seems to run fine. However, I want to add an interruption task. The idea is an interruption task will pop up in the third, fourth, and seventh trials. There are two conditions: (1) participants must do an interruption task before returning to the primary task. (2) an interruption task will pop up, but participants can choose whenever they want to do an interruption task; there will be a question that asks participants whether they want to do an interruption task at the moment or not. The problem is I have no idea how to create an interruption task and make it return to the main task.

I think my experiment is similar tohttps://discourse.psychopy.org/t/task-switching-in-builder-view/6790

Any help would be very appreciated.

MainTaskRoutine (Begin routine)

if interrupt == 1:  # Check if this trial needs an interrupt task
    trials.finished = True  # Stop the main task loop
    continueRoutine = False  # Skip remaining part of this trial

InterruptTaskRoutine (Begin routine)

continueRoutine = True  # Ensure the interrupt task starts properly

InterruptTaskRoutine (End routine)

print("Interrupt task finished. Returning to main task.")  # Debugging message

interruptLoop.finished = True  # Stop the interrupt task loop
trials.finished = False  # Restart the main task loop
currentLoop = trials  # Ensure the main loop is set as active
thisExp.nextEntry()  # Move to the next trial properly

Window 10
PsychoPy version 2024.2.4

Put the interruption task inside the main loop and skip it based on a variable (e.g. skipInterruption) which you set as True except for when you want to show it.

Thank you very much for your answer, and I am sorry for the late reply.
Your advice seems to work, but there are still some slight issues. The task finished before it should have been, and I met with this error:

remainingIndices.pop(remainingIndices.index(thisTrial.thisIndex))
ValueError: 7 is not in list

Do you have any advice for this problem?

Are you using .pop(7)? Why? I normally just use .pop() to remove the last value or exceptionally .pop(0) to remove the first (which doesn’t work online)

I think I didn’t use any pop(), and I don’t even know what pop() is. (Sorry for my lack of knowledge, but I am very new to Psychopy)

Are you using the selected rows parameter of a loop?

I didn’t use the selected rows parameter of a loop, but I want the interruption task to pop up on the third, fourth, and seventh trials. So, I made an interrupt column that marks when an interrupt task should appear (interrupt = 1 interruption popups, interrupt = 0 no interruption). Could this somehow be related to this error?

I don’t think this does what you think it does. If your flow is still as per your screenshot then you can’t go back from the second loop to the first. I would have two routines within a single loop (as previously mentioned).

Thank you so much. The error is gone now, but there are some problems that I need advice on. (sorry if I bother you too much)

The interruption task still occurs randomly, And I forgot to mention I want an interruption task to pop up after starting the third, fourth, and seventh tasks for 35 seconds. Do you have any advice?

How are you deciding when to interrupt? If it’s based on a spreadsheet variable, then the loop needs to be sequential. If the loop is random, then it needs to be based on the value of trials.thiaN.

I want to randomize the task variables but fix the time that interrupts occur.

Hi

The problems I mentioned in the previous message have been solved, but I have just noticed a new problem. When I finish the interruption task and return to the primary task, It will skip to the next trial. For example, when I finish a secondary task that pops up on the third trial, It will return to the primary task on the fourth trial. But I want to return to the third trial. Is there any way to solve this problem?

Any advice would be very appreciated.

Locally you could try this new feature.

Online you might need to have a loop within the main loop so the trial can be repeated, or a second copy of the trial routine which gets skipped when there is no interruption.

Thank you for your kindness. I have tried but it didn’t work. It returns to the first trial. I wonder if I did something wrong.

This is my experiment file (Condition (2)) If you want to check what’s wrong with my experiment.
exp_2.psyexp (69.4 KB)
main_conditions.xlsx (13.8 KB)
interrupt_conditions.csv (115 Bytes)

I meant something like this

Where intr is skipped based on a variable skipIntr which is set in main based on the values of trials.thisN and repeats.thisN. If intr is to be skipped then repeats.finished=True will break out of that loop and move to the next trial.

I can’t help with your actual psyexp file however, because of the amount of AI generated code. You have a code component called JScode but the rest of your code is Python only implying that you don’t want to run the experiment online.

Thank you for your answer (and sorry for a lot of AI-generated code, I use them because I have no experience with Python code). I am still a little confused with repeats.thisN and repeats loop. I don’t know where should I put repeats.thisN in. And should I put the condition file in the repeat loop? Because when I put the condition file in the repeat loop, the interruption task won’t pop up.

If you remove redundant code and set up two loops as per my diagram with the repeat loop having two repetitions then I’d be happy to add the code to only play the interrupt routine on certain iterations of trials.

I greatly appreciate your help.
I am not sure I delete all redundant code, but I hope I delete all of them.

imgonnacry.psyexp (56.6 KB)
task_condition.xlsx (10.2 KB)

Sorry for interrupting your weekend. When I was trying to create experiment condition (1), I noticed that the code component looked different from the one I sent you earlier, and I also tried to use trials.rewindTrials()(with my poor code skills). This time kinda works, but there is only one problem: it rewinds the interruption work too, so I can’t go to the next trial because it keeps rewinding the interrupted task and interruption task. If you could check and give me some advice would be very appreciated.

Thank you

exp_Con1_V.2.psyexp (64.8 KB)
main_conditions.xlsx (14.7 KB)

The last one you uploaded wouldn’t run for me at all. I’ll take a look at this new one tomorrow.