#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# loop freezing over mutiple trials of image presentation and keyboard response

Hi, we're running a fairly basic image presentation as a cue to response over two loops of 320 trials each. The experiment will freeze in place randomly (resumes by hitting esc and resuming, but that is not a feasible solution as it would break mri scanner timing). Code for object generation:

```number_of_trials = 320  #320 actual 80 for debug

shapes = ["red_square"]*(int(0.3*number_of_trials))	#adding the frequency of red squares proportional to .3?

for colour in ["yellow", "blue", "green"]:

shapes += ["%s_square"%colour]*(int((0.175/3) * number_of_trials))

shapes += ["yellow_square", "blue_square"]

for shape in ["star", "circle", "triangle"]:

shapes += ["red_%s"%shape]*(int((0.175/3) * number_of_trials))

shapes += ["red_star", "red_circle"]

for colour in ["yellow", "blue", "green"]:

for shape in ["star", "circle", "triangle"]:

shapes += ["%s_%s"%(colour,shape)]*(int((0.35/(3*3) * number_of_trials)))

shapes += ["green_triangle", "blue_triangle", "green_circle", "blue_star"]

lenShapes = len(shapes)/4

print(lenShapes)

timings = []

while lenShapes > 0:

timings += [1000]

timings += [1500]

timings += [2000]

timings += [2500]

lenShapes -= 1

random.shuffle(shapes)

random.shuffle(timings)

print("len of shapes: " + str(len(shapes)))

print("len of timings: " + str(len(timings)))
```

Code for the loop (first python script) prepare:

```global shapes
global timings
shape = shapes.pop()
ISI = timings.pop()

self.experiment.set("trial_shape", shape)
self.experiment.set("trial_ISI", ISI)
```

second python script prepare:

```from openexp.canvas import canvas
canvas_shape = canvas(exp)
canvas_blank = canvas(exp)
path = exp.get_file("stimuli/%s.png"%exp.get("trial_shape"))
canvas_shape.image(path)
```

second python script run:

```from openexp.keyboard import keyboard

# Durations for the two displays
duration_shape = 100
duration_blank = self.experiment.get('trial_ISI')

# Displays are copied from sketchpads. To avoid these
# sketchpads from being shown twice (i.e. once in the
# normal way, and once in this inline_script), you can set
# their 'Run-if' statement to 'never'.

# Create a keyboard object with a zero timeout to allow
# for continuous polling.
my_keyboard = keyboard(exp, timeout=0)
my_keyboard.flush()
key = None		#changed from None to 1 to see if this controls allowed responses? perhaps not

# Show the first canvas, and poll the keyboard for the
# specified duration.
show_time_shape = canvas_shape.show()
exp.set('onset_time_shape', show_time_shape)		#trying to get onset time of shape
while self.time() - show_time_shape < duration_shape:
if not key:
key, time = my_keyboard.get_key(keylist=[1])

# Show the second canvas, and poll the keyboard for the
# specified duration.
show_time_blank = canvas_blank.show()
while self.time() - show_time_blank < duration_blank:
if not key:
key, time = my_keyboard.get_key(keylist=[1])

exp.set('response', key)
if key:
exp.set('response_time', int(round(time-show_time_shape)))
else:
exp.set('response_time', 0)
```

followed by a logger.

My guess is that the repeated get_file() calls are causing the issue, so we've tried running with legacy and xpyriment back-ends with garbage collection enabled and disabled, but unsuccessful in getting consistent reliability. If anyone has insight into this issue much app

• If you get stuck in a while loop, you get the impression of freezing. So, possible it lies somehow there?

What about putting key=None before the second while loop?

Not sure, whether this helps you out, but worth a try.

Best,

Stephan

• Hi @kazald,

Could you upload the scripts embedded in the experiment you use? Perhaps with irrelevant parts removed? On the first glance I can't see obvious mistakes, but also code that could be improved.

First part of the debugging process would be to identify at which line exactly the experiment stalls. The easiest way would be to include regular print statements in your experiment and observe when a delay occurs.

Eduard

• Hi Eduard, thank you for your reply. Here is a copy of the experiment, per my notes in the other posted question referencing this problem, the issue seems to lie with the while loop on line 9 of the run tab for the origshowandcollect python script within the new_loop. This was determined by the experiment freezing while showing a shape, so it is not within the second while loop which just shows a blank canvas as the stimulus interval. Commenting out the keyboard response collection relevant code results in the same stoppage, our lab runs on macs, so we're seeing CPU usage of python via the activity monitor.

• Hi,

attached a streamlined version of your experiment. Perhaps that helps. However, as I said earlier, I couldn't find anything intrinsically wrong with your code and I also have not experienced any freezing. Perhaps this is related to your setup? Try to pilot the experiment at the scanner and maybe it will run there just fine.

Eduard

• Hi Eduard, the streamlining of the code does seem to have helped, project isn't freezing any more. Thank you for your help, I must admit I am confused as to why altering the if/then statements outside the loop fixed the problem. I guess if anyone else is referencing this for a similar problem, I would suggest checking logic statements for possible cases where those statements aren't getting resolved.