Skip to content

Commit 703af9f

Browse files
committed
Merge pull request #115 from Eric89GXL/start-stimuli
ENH: Change to start_stimulus (tested. works. merging.)
2 parents f7d673b + ae3bc87 commit 703af9f

File tree

8 files changed

+38
-29
lines changed

8 files changed

+38
-29
lines changed

examples/eyetracking_experiment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
targ_circ.draw()
4747
screenshot = ec.screenshot()
4848
ec.identify_trial(ec_id='Circle', ttl_id=[0], el_id=[0])
49-
ec.flip_and_play() # automatically stamps to EL
49+
ec.start_stimulus() # automatically stamps to EL
5050
if not el.wait_for_fix(fix_pos, 1., max_wait=5., units='deg'):
5151
print('Initial fixation failed')
5252
for ii, (x, y) in enumerate(zip(x_pos[1:], y_pos[1:])):

examples/simple_experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
ec.clear_buffer()
114114
ec.load_buffer(wavs[stim_num])
115115
ec.identify_trial(ec_id=stim_num, ttl_id=[0, 0])
116-
ec.flip_and_play()
116+
ec.start_stimulus()
117117
pressed, timestamp = ec.wait_one_press(max_resp_time, min_resp_time,
118118
live_keys)
119119
ec.stop() # will stop stim playback as soon as response logged
@@ -149,7 +149,7 @@
149149
ec.load_buffer(concat_wavs)
150150
ec.write_data_line('multi-tone trial', [x + 1 for x in mass_trial_order])
151151
ec.identify_trial(ec_id='multi-tone', ttl_id=[0, 1])
152-
ec.flip_and_play()
152+
ec.start_stimulus()
153153
ec.wait_secs(len(concat_wavs) / float(ec.stim_fs))
154154
ec.screen_text('<center>Go!</center>')
155155
ec.flip()

examples/sync_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
screenshot = None
3939
while pressed != '8': # enable a clean quit if required
4040
ec.draw_background_color('white')
41-
t1 = ec.flip_and_play(start_of_trial=False) # skip checks
41+
t1 = ec.start_stimulus(start_of_trial=False) # skip checks
4242
ec.draw_background_color('black')
4343
t2 = ec.flip()
4444
diff = round(1000 * (t2 - t1), 2)

expyfun/_experiment_controller.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,17 @@ def draw_background_color(self, color='black'):
460460
rect.draw()
461461
return rect
462462

463-
def flip_and_play(self, start_of_trial=True):
464-
"""Flip screen, play audio, then run any "on-flip" functions.
463+
def start_stimulus(self, start_of_trial=True, flip=True):
464+
"""Play audio, (optionally) flip screen, run any "on_flip" functions.
465465
466466
Parameters
467467
----------
468468
start_of_trial : bool
469469
If True, it checks to make sure that the trial ID has been
470470
stamped appropriately. Set to False only in cases where
471471
``flip_and_play`` is to be used mid-trial (should be rare!).
472+
flip : bool
473+
If False, don't flip the screen.
472474
473475
Returns
474476
-------
@@ -477,22 +479,30 @@ def flip_and_play(self, start_of_trial=True):
477479
478480
Notes
479481
-----
480-
Order of operations is: screen flip, audio start, additional functions
481-
added with ``on_next_flip``, followed by functions added with
482-
``on_every_flip``.
482+
Order of operations is: screen flip (optional), audio start, then
483+
(only if ``flip=True``) additional functions added with
484+
``on_next_flip`` and ``on_every_flip``.
483485
"""
484486
if start_of_trial:
485487
if not self._trial_identified:
486488
raise RuntimeError('Trial ID must be stamped before starting '
487489
'the trial')
488490
self._trial_identified = False
489-
logger.exp('Expyfun: Flipping screen and playing audio')
491+
extra = 'flipping screen and ' if flip else ''
492+
logger.exp('Expyfun: Starting stimuli: {0}playing audio'.format(extra))
490493
# ensure self._play comes first in list, followed by other critical
491494
# private functions (e.g., EL stamping), then user functions:
492-
self._on_next_flip = ([self._play] + self._ofp_critical_funs +
493-
self._on_next_flip)
494-
flip_time = self.flip()
495-
return flip_time
495+
if flip:
496+
self._on_next_flip = ([self._play] + self._ofp_critical_funs +
497+
self._on_next_flip)
498+
stimulus_time = self.flip()
499+
else:
500+
funs = [self._play] + self._ofp_critical_funs
501+
self._win.dispatch_events()
502+
stimulus_time = self._clock.get_time()
503+
for fun in funs:
504+
fun()
505+
return stimulus_time
496506

497507
def play(self):
498508
"""Start audio playback
@@ -733,9 +743,8 @@ def flip(self):
733743
734744
Notes
735745
-----
736-
Order of operations is: screen flip, audio start, additional functions
737-
added with ``on_every_flip``, followed by functions added with
738-
``on_next_flip``.
746+
Order of operations is: screen flip, functions added with
747+
``on_next_flip``, followed by functions added with ``on_every_flip``.
739748
"""
740749
call_list = self._on_next_flip + self._on_every_flip
741750
self._win.dispatch_events()
@@ -964,8 +973,8 @@ def load_buffer(self, samples):
964973
def _play(self):
965974
"""Play the audio buffer.
966975
"""
967-
logger.debug('Expyfun: playing audio')
968976
self._ac.play()
977+
logger.debug('Expyfun: started audio')
969978
self.write_data_line('play')
970979

971980
def stop(self):

expyfun/_trigger_controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ParallelTrigger(object):
1515
"""Parallel port and dummy triggering support
1616
1717
IMPORTANT: When using the parallel port, note that calling
18-
ec.flip_and_play() will automatically invoke a stamping of
18+
ec.start_stimulus() will automatically invoke a stamping of
1919
the 1 trigger, which will cause a delay equal to that of
2020
high_duration.
2121

expyfun/codeblocks/_pupillometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def find_pupil_dynamic_range(ec, el, prompt=True, verbose=None):
9797
bgrect.set_fill_color(np.ones(3) * lev)
9898
bgrect.draw()
9999
fix.draw()
100-
ec.flip_and_play()
100+
ec.start_stimulus()
101101
ec.wait_secs(settle_time)
102102
ec.check_force_quit()
103103
ec.trial_ok()
@@ -233,7 +233,7 @@ def find_pupil_tone_impulse_response(ec, el, bgcolor, prompt=True,
233233
ec.load_buffer(sweep_stim if targ else tone_stim)
234234
ec.identify_trial(ec_id='TONE_{0}'.format(int(targ)),
235235
el_id=[int(targ)], ttl_id=[int(targ)])
236-
flip_times.append(ec.flip_and_play())
236+
flip_times.append(ec.start_stimulus())
237237
presses.append(ec.wait_for_presses(isi))
238238
ec.stop()
239239
ec.trial_ok()

expyfun/tests/test_experiment_controller.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,24 @@ def test_ec(ac=None, rd=None):
214214
assert_equal(len(w), 3)
215215

216216
ec.stop()
217-
ec.call_on_every_flip(dummy_print, 'called on flip and play')
218-
assert_raises(RuntimeError, ec.flip_and_play)
219-
ec.flip_and_play(start_of_trial=False) # should work
217+
ec.call_on_every_flip(dummy_print, 'called start stimuli')
218+
assert_raises(RuntimeError, ec.start_stimulus)
219+
ec.start_stimulus(start_of_trial=False) # should work
220220
assert_raises(KeyError, ec.identify_trial, ec_id='foo') # need ttl_id
221221
# only binary for TTL
222222
assert_raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
223223
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
224224
ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
225225
assert_raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
226-
ec.flip_and_play()
226+
ec.start_stimulus(flip=False)
227227
ec.flip()
228228
ec.estimate_screen_fs()
229229
ec.play()
230230
ec.call_on_every_flip(None)
231231
ec.call_on_next_flip(ec.start_noise())
232-
ec.flip_and_play(start_of_trial=False)
232+
ec.start_stimulus(start_of_trial=False)
233233
ec.call_on_next_flip(ec.stop_noise())
234-
ec.flip_and_play(start_of_trial=False)
234+
ec.start_stimulus(start_of_trial=False)
235235
ec.get_mouse_position()
236236
ec.toggle_cursor(False)
237237
ec.toggle_cursor(True, True)

expyfun/tests/test_eyelink_controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def test_eyelink_methods():
3838
# missing el_id
3939
assert_raises(KeyError, ec.identify_trial, ec_id='foo', ttl_id=[0])
4040
ec.identify_trial(ec_id='foo', ttl_id=[0], el_id=[1])
41-
ec.flip_and_play()
41+
ec.start_stimulus()
4242
ec.identify_trial(ec_id='foo', ttl_id=[0], el_id=[1, 1])
43-
ec.flip_and_play()
43+
ec.start_stimulus()
4444
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[0],
4545
el_id=[1, dict()])
4646
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[0],

0 commit comments

Comments
 (0)