Two independent routines?

Dear all,

I’ve been implementing an experiment in which the Subject_position can be moved across a grid by pressing the arrows, which force the end of the routine and begins another one with a new Subject_position. 10 seconds after the beginning of the experiment (or other conditions that are not worth mentioning here), a virtual predator appear in a corner of the grid. I did this by inserting in the “each_frame” of a code component the following code:

if if experiment_clock.getTime() >= 10: 
    Predator_on = True
    predator_clock = core.Clock()
else:
    Predator_on = False

and than, in the “Predator” component (a red triangle), I chose the “condition” option and $Predator_on == True.

The predator_clock in the exempt is because I needed that the predator started chasing the subject at a given velocity (e.g. 1 move every 3 seconds) starting 3 seconds after it appeared in the screen. I could give the predator the right directions by implementing a breadth first search algorithm and tried establish the chase start velocity by coding in a “every frame” tab of the code component the following:

if Predator_on == True and Subject_position != [-475, -225]:
    if predator_clock >= 3:
        Predator_position = list(reconstruct_path(came_from, start, goal)[1])
        predator_clock.reset()

The predator is moving to the correct position, but every new routine instead of in a predetermined velocity. I believe that’s because every new routine the code that make the predator_on is red again, but I can’t find a way of making this stop and start making the predator move under the control of the last code extract I copied. Also, the predator move immediately when the new routine begins, not waiting the 3 seconds I was planning.

Any idea of where is the problem? Or a solution? Please find the experiment attached if that helps.
@jderrfuss that knows the experiment a little?

Thanks in advance,

Felipe

I’m not sure that I really understand much of the issue here, but this aspect at least should be straightforward. In this code:

if predator_clock >= 3:

you are testing the value of the clock object itself (not really a meaningful thing to do here), rather than its current time. You need to do this:

if predator_clock.getTime() >= 3:

Also note that you can make your code a bit more concise when testing the values of booleans. Instead of this:

if Predator_on == True:

you can just do this:

if Predator_on:

Thanks so much, Michael.

I’ve made the changes, but still not working…

In the “Begin experiment” tab of my “Predator_updates” code component I create a clock: Predator_on_clock = core.Clock() (note that I changed the names of some variables) and in the “Each frame” tab of the same component is the following code:

if Predator_on_clock.getTime() >= 10: 
    Predator_on = True
    Predator_move_clock = core.Clock()
else:
    Predator_on = False


if Predator_on and Subject_position != [-475, -225]:
    if Predator_move_clock.getTime >= 3:
        Predator_position = list(reconstruct_path(came_from, start, goal)[1])
        Predator_move_clock.reset()

As you can see, after this if Predator_on_clock.getTime() >= 10: I create a new clock (Predator_move_clock) to start counting when Predator_on = True. I know that the Predator_on_clock is working because it appears in my screen after 10 seconds but the Predator_move_clock because the predator doesn’t wait the 3 seconds I was expecting to move, it move immediately (it actually appear in the position it should move to 3 seconds appearing). Am I doing something wrong by creating two clocks by the same method?

I’m uploading the updated version of the experiment if this helps…

Thanks again,

Felipe

No, you can have as many clocks as you want.

Do you reset/re-initialise everything at the beginning of the trial?

i.e. if you don’t re-initialise everything, at the start of the second trial, Predator_on will be True and Predator_move_clock.getTime() will likely be > 3 and Predator_on_clock.getTime() will be > 10, even on the very first frame.

Good to know I can….
As Predator_move_clock is conditioned to if Predator_on_clock.getTime() >= 10: I though it would start counting only after the Predator_on_clock.getTime() >= 10: reached 10s, is this not the case?
Thanks so much!
Felipe

Yes, but if you don’t reset Predator_on_clock, its time will be > 10 on the very first frame of the second trial, so the predator will be displayed immediately. A new Predator_move_clock will indeed be created at that point, but at time 0 s rather than the intended time of 10 s. So it would start moving at 3 s into the trial rather than 13 s.

But you are the only one who can really debug this. Try sprinkling some (temporary) print() statements thoughout your code so that you can review what is happening when and what the values of various variables are.

I understand, Michael, thanks!
Yes, I’ll debug this. Is that ok if I continue asking for your support? Please apologize, I just can’t think about another way around this…

I tried to print the time as you suggested and it seems the command is never passing the second “if statement”:

if predator_on_clock.getTime() >= 10: 
    Predator_on = True
    print predator_on_clock.getTime()
    predator_move_clock = core.Clock()
    print predator_move_clock.getTime()
    if predator_move_clock.getTime() >= 3:
        Predator_position = path[1]
        print predator_move_clock.getTime()

for the first print (print predator_on_clock.getTime()) I get increasing times > 10, which confirms that the first clock started counting in the beginning of the experiment and printing after the first “if statement” as requested.

The second print (print predator_move_clock.getTime()) returns me lots of times between 1.9 and 3.1 and something like 9.53674316406e-07. One for each frame. Example:

2.86102294922e-06
2.86102294922e-06
3.09944152832e-06
3.09944152832e-06
2.86102294922e-06
1.90734863281e-06
2.86102294922e-06
2.14576721191e-06
3.09944152832e-06
2.86102294922e-06
1.90734863281e-06
3.09944152832e-06
2.14576721191e-06
3.09944152832e-06
2.86102294922e-06
9.53674316406e-07
3.09944152832e-06
2.86102294922e-06
3.09944152832e-06
2.86102294922e-06
2.86102294922e-06
3.09944152832e-06

The third print is the same as the second, but after the second “if statement” and it never returns me any print. I though this could be happening because every frame the second clock could be restarting before the 3 seconds necessary to pass the second “if statement” so I tried to modify this to a much shorter time (if predator_move_clock.getTime() >= 0.5:), but the third print stillI tried the does not returns me anything. I also tried a much longer time (if predator_move_clock.getTime() >= 15:), in case the second clock could be starting from the beginning of the experiment, but, even so, no third print.

From these, I understand that the second “if statement” is never being passed, so the third print is never reached. Does that make sense? If so, would you think of a possible cause?

A also don’t understand the second sets of prints. Why does it seem to be reseted? I mean, why is it not increasing progressively as the first clock? (and why the first clock is not?)

Thanks and apologize again for the insistence.

Best,

Felipe

Because whenever predator_on_clock.getTime() >= 10, you create a new predator_move_clock. That process happens continually (i.e. not just once at t == 10 but on every screen refresh after that). So this clock is continually being reset. This is why its time value is always basically zero: you are always printing it immediately after the clock is created, so the time is never more than a few microseconds. It can certainly never count up to 3 s.

You need to add some additional checks using the value of Predator_on, maybe something like this:

# checking not Predator_on ensures we only do this once per trial:
if not Predator_on and predator_on_clock.getTime() >= 10:
    predator_move_clock = core.Clock()
    Predator_on = True

# checking Predator_on ensures we don't test predator_move_clock
# before it is created:
if Predator_on and predator_move_clock.getTime() >= 3:
    Predator_position = path[1]

Remember to set

Predator_on = False

in the Begin routine tab.

Hi Michael.

Unfortunately it didn’t work… Nor variations I’ve tried the whole day…

Is it possible that it’s never passing the second ‘if statement’ for a different reason? I tried to reduce the time to milliseconds (see below) and even so no print at all…

if predator_move_clock.getTime() >= 0.0001:
        print predator_move_clock.getTime()

Would you happen to have any alternative of timer that starts after the 10 initial seconds that I could use to move my predator every x seconds once it appears in the screen?

Thanks so much,

Felipe

That doesn’t give us much to go on…

That time duration is still 100 times longer than the durations being reported above, so no surprise the check is still not tripping. But that is kind of a pointless route to follow. What I suggested above (only creating the second clock once per trial) should ensure that it can count up to 3 s.

When you say “it didn’t work”, did you use exactly the same indentation? Note that my suggested code had a different structure (and hence indentation) than yours.

Hi Michael,

I had tried both ways and wasn’t working but now that you asked if I had texted exactly the same thing I realized that I hadn’t deleted the else: Predator_on = False I was using. Now that I did it worked…

I really appreciate your support and PsychoPy work as a whole. This is a fantastic tool and initiative. Thanks so much!

All the best,

Felipe