Skip to content

Commit

Permalink
Merge pull request #115 from Eric89GXL/start-stimuli
Browse files Browse the repository at this point in the history
ENH: Change to start_stimulus  (tested.  works.  merging.)
  • Loading branch information
drammock committed Apr 8, 2014
2 parents f7d673b + ae3bc87 commit 703af9f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/eyetracking_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
targ_circ.draw()
screenshot = ec.screenshot()
ec.identify_trial(ec_id='Circle', ttl_id=[0], el_id=[0])
ec.flip_and_play() # automatically stamps to EL
ec.start_stimulus() # automatically stamps to EL
if not el.wait_for_fix(fix_pos, 1., max_wait=5., units='deg'):
print('Initial fixation failed')
for ii, (x, y) in enumerate(zip(x_pos[1:], y_pos[1:])):
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
ec.clear_buffer()
ec.load_buffer(wavs[stim_num])
ec.identify_trial(ec_id=stim_num, ttl_id=[0, 0])
ec.flip_and_play()
ec.start_stimulus()
pressed, timestamp = ec.wait_one_press(max_resp_time, min_resp_time,
live_keys)
ec.stop() # will stop stim playback as soon as response logged
Expand Down Expand Up @@ -149,7 +149,7 @@
ec.load_buffer(concat_wavs)
ec.write_data_line('multi-tone trial', [x + 1 for x in mass_trial_order])
ec.identify_trial(ec_id='multi-tone', ttl_id=[0, 1])
ec.flip_and_play()
ec.start_stimulus()
ec.wait_secs(len(concat_wavs) / float(ec.stim_fs))
ec.screen_text('<center>Go!</center>')
ec.flip()
Expand Down
2 changes: 1 addition & 1 deletion examples/sync_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
screenshot = None
while pressed != '8': # enable a clean quit if required
ec.draw_background_color('white')
t1 = ec.flip_and_play(start_of_trial=False) # skip checks
t1 = ec.start_stimulus(start_of_trial=False) # skip checks
ec.draw_background_color('black')
t2 = ec.flip()
diff = round(1000 * (t2 - t1), 2)
Expand Down
37 changes: 23 additions & 14 deletions expyfun/_experiment_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,17 @@ def draw_background_color(self, color='black'):
rect.draw()
return rect

def flip_and_play(self, start_of_trial=True):
"""Flip screen, play audio, then run any "on-flip" functions.
def start_stimulus(self, start_of_trial=True, flip=True):
"""Play audio, (optionally) flip screen, run any "on_flip" functions.
Parameters
----------
start_of_trial : bool
If True, it checks to make sure that the trial ID has been
stamped appropriately. Set to False only in cases where
``flip_and_play`` is to be used mid-trial (should be rare!).
flip : bool
If False, don't flip the screen.
Returns
-------
Expand All @@ -477,22 +479,30 @@ def flip_and_play(self, start_of_trial=True):
Notes
-----
Order of operations is: screen flip, audio start, additional functions
added with ``on_next_flip``, followed by functions added with
``on_every_flip``.
Order of operations is: screen flip (optional), audio start, then
(only if ``flip=True``) additional functions added with
``on_next_flip`` and ``on_every_flip``.
"""
if start_of_trial:
if not self._trial_identified:
raise RuntimeError('Trial ID must be stamped before starting '
'the trial')
self._trial_identified = False
logger.exp('Expyfun: Flipping screen and playing audio')
extra = 'flipping screen and ' if flip else ''
logger.exp('Expyfun: Starting stimuli: {0}playing audio'.format(extra))
# ensure self._play comes first in list, followed by other critical
# private functions (e.g., EL stamping), then user functions:
self._on_next_flip = ([self._play] + self._ofp_critical_funs +
self._on_next_flip)
flip_time = self.flip()
return flip_time
if flip:
self._on_next_flip = ([self._play] + self._ofp_critical_funs +
self._on_next_flip)
stimulus_time = self.flip()
else:
funs = [self._play] + self._ofp_critical_funs
self._win.dispatch_events()
stimulus_time = self._clock.get_time()
for fun in funs:
fun()
return stimulus_time

def play(self):
"""Start audio playback
Expand Down Expand Up @@ -733,9 +743,8 @@ def flip(self):
Notes
-----
Order of operations is: screen flip, audio start, additional functions
added with ``on_every_flip``, followed by functions added with
``on_next_flip``.
Order of operations is: screen flip, functions added with
``on_next_flip``, followed by functions added with ``on_every_flip``.
"""
call_list = self._on_next_flip + self._on_every_flip
self._win.dispatch_events()
Expand Down Expand Up @@ -964,8 +973,8 @@ def load_buffer(self, samples):
def _play(self):
"""Play the audio buffer.
"""
logger.debug('Expyfun: playing audio')
self._ac.play()
logger.debug('Expyfun: started audio')
self.write_data_line('play')

def stop(self):
Expand Down
2 changes: 1 addition & 1 deletion expyfun/_trigger_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ParallelTrigger(object):
"""Parallel port and dummy triggering support
IMPORTANT: When using the parallel port, note that calling
ec.flip_and_play() will automatically invoke a stamping of
ec.start_stimulus() will automatically invoke a stamping of
the 1 trigger, which will cause a delay equal to that of
high_duration.
Expand Down
4 changes: 2 additions & 2 deletions expyfun/codeblocks/_pupillometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def find_pupil_dynamic_range(ec, el, prompt=True, verbose=None):
bgrect.set_fill_color(np.ones(3) * lev)
bgrect.draw()
fix.draw()
ec.flip_and_play()
ec.start_stimulus()
ec.wait_secs(settle_time)
ec.check_force_quit()
ec.trial_ok()
Expand Down Expand Up @@ -233,7 +233,7 @@ def find_pupil_tone_impulse_response(ec, el, bgcolor, prompt=True,
ec.load_buffer(sweep_stim if targ else tone_stim)
ec.identify_trial(ec_id='TONE_{0}'.format(int(targ)),
el_id=[int(targ)], ttl_id=[int(targ)])
flip_times.append(ec.flip_and_play())
flip_times.append(ec.start_stimulus())
presses.append(ec.wait_for_presses(isi))
ec.stop()
ec.trial_ok()
Expand Down
12 changes: 6 additions & 6 deletions expyfun/tests/test_experiment_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,24 @@ def test_ec(ac=None, rd=None):
assert_equal(len(w), 3)

ec.stop()
ec.call_on_every_flip(dummy_print, 'called on flip and play')
assert_raises(RuntimeError, ec.flip_and_play)
ec.flip_and_play(start_of_trial=False) # should work
ec.call_on_every_flip(dummy_print, 'called start stimuli')
assert_raises(RuntimeError, ec.start_stimulus)
ec.start_stimulus(start_of_trial=False) # should work
assert_raises(KeyError, ec.identify_trial, ec_id='foo') # need ttl_id
# only binary for TTL
assert_raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
assert_raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
ec.flip_and_play()
ec.start_stimulus(flip=False)
ec.flip()
ec.estimate_screen_fs()
ec.play()
ec.call_on_every_flip(None)
ec.call_on_next_flip(ec.start_noise())
ec.flip_and_play(start_of_trial=False)
ec.start_stimulus(start_of_trial=False)
ec.call_on_next_flip(ec.stop_noise())
ec.flip_and_play(start_of_trial=False)
ec.start_stimulus(start_of_trial=False)
ec.get_mouse_position()
ec.toggle_cursor(False)
ec.toggle_cursor(True, True)
Expand Down
4 changes: 2 additions & 2 deletions expyfun/tests/test_eyelink_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_eyelink_methods():
# missing el_id
assert_raises(KeyError, ec.identify_trial, ec_id='foo', ttl_id=[0])
ec.identify_trial(ec_id='foo', ttl_id=[0], el_id=[1])
ec.flip_and_play()
ec.start_stimulus()
ec.identify_trial(ec_id='foo', ttl_id=[0], el_id=[1, 1])
ec.flip_and_play()
ec.start_stimulus()
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[0],
el_id=[1, dict()])
assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[0],
Expand Down

0 comments on commit 703af9f

Please sign in to comment.