Skip to content

Commit

Permalink
video: duration can be defined in time or frames
Browse files Browse the repository at this point in the history
  • Loading branch information
nzjrs committed Sep 9, 2020
1 parent 20ed2d1 commit 83db829
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
39 changes: 23 additions & 16 deletions flyvr/video/video_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ class VideoStim(object):

def __init__(self, **params):
self._id = params.pop('identifier', uuid.uuid4().hex)
self._duration = params.pop('duration', np.inf)

self._duration_frames = params.pop('duration_frames', np.inf)
self._duration_seconds = params.pop('duration_seconds', None)

self._show = params.pop('show', True)
self._log = logging.getLogger('flyvr.video.%s' % self.__class__.__name__)

self._fps = None

self.p = Dottable(params)
self.frame_count = 0

Expand All @@ -148,15 +153,18 @@ def identifier(self):
@property
def duration(self):
""" return the duration of this stimulus in frames """
return self._duration
return self._duration_frames

@property
def is_finished(self):
""" overridable property for stimuli to determine by other means when they are finished """
return self.frame_count > self.duration

def initialize(self, win, fps):
raise NotImplementedError
self._fps = fps
if np.isinf(self._duration_frames) and (self._duration_seconds is not None):
self._duration_frames = int(fps * self._duration_seconds)
self._log.debug('set duration for %d frames @ %f fps' % (self._duration_frames, fps))

def advance(self):
""" can return False when there is no further item to advance to. in the single stimulus case
Expand Down Expand Up @@ -200,9 +208,6 @@ class NoStim(VideoStim):
NAME = 'none'
NUM_VIDEO_FIELDS = 1

def initialize(self, win, fps):
pass

def update(self, win, logger, frame_num):
logger.log(self.log_name(),
np.array([frame_num]))
Expand Down Expand Up @@ -233,6 +238,7 @@ def __init__(self, sf=50, stim_size=5, stim_color=-1, bg_color=0.5, **kwargs):
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.GratingStim(win=win, size=self.p.stim_size,
pos=[0, 0], sf=self.p.sf,
color=self.p.stim_color, phase=0)
Expand Down Expand Up @@ -279,13 +285,12 @@ def __init__(self, radius=5, velx=1, offset=(0.2, 0),
off_time=float(off_time), bg_time=float(bg_time),
fps=float(fps), **kwargs)
self.screen = None
self._fps = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Circle(win=win,
radius=deg_to_px(self.p.radius), pos=[deg_to_px(self.p.init_pos),0],
lineColor=None, fillColor=self.p.fg_color)
self._fps = fps

@property
def is_finished(self):
Expand Down Expand Up @@ -322,6 +327,7 @@ def __init__(self, filename='pipStim.mat', offset=(0, 0), bg_color=0, fg_color=-
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Circle(win=win,
radius=0, pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
Expand Down Expand Up @@ -360,6 +366,7 @@ def __init__(self, filename='pipStim.mat', offset=(0, 0), bg_color=0, fg_color=-
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Circle(win=win,
radius=0, pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
Expand Down Expand Up @@ -402,6 +409,7 @@ def __init__(self, size=(0.25, 0.25), speed=(0.01, 0), offset=(0.2, -0.5),
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Rect(win=win,
size=self.p.size, pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
Expand Down Expand Up @@ -442,6 +450,7 @@ def __init__(self, filename='pipStim.mat', offset=(0.2, -0.5), bg_color=-1, fg_c
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Rect(win=win,
size=(0.25, 0.25), pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
Expand Down Expand Up @@ -478,6 +487,7 @@ def __init__(self, size_min=0.05, size_max=0.8, speed=0.01, offset=(0.2, -0.5),
self.screen = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Rect(win=win,
size=self.p.size_min, pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
Expand Down Expand Up @@ -506,23 +516,18 @@ class LoomingStimCircle(VideoStim):
NUM_VIDEO_FIELDS = 7

def __init__(self, size_min=0.05, size_max=0.8, rv=10, offset=(0, 0),
bg_color=0, fg_color=-1, time=1000, **kwargs):
bg_color=0, fg_color=-1, **kwargs):
super().__init__(size_min=float(size_min), size_max=float(size_max),
offset=[float(offset[0]), float(offset[1])],
rv=float(rv),time=float(time),
rv=float(rv),
bg_color=float(bg_color), fg_color=float(fg_color), **kwargs)
self.screen = None
self._fps = None

def initialize(self, win, fps):
super().initialize(win, fps)
self.screen = visual.Circle(win=win,
radius=self.p.size_min, pos=self.p.offset,
lineColor=None, fillColor=self.p.fg_color)
self._fps = fps

@property
def is_finished(self):
return (self.p.time - self.frame_count / self._fps * 1000) < 0

def update(self, win, logger, frame_num):
win.color = self.p.bg_color
Expand Down Expand Up @@ -565,6 +570,7 @@ def __init__(self, bg_color=(178 / 256) * 2 - 1, frame_start=10000, offset=(0.2,
top_y[frame_start:] - bottom_y[frame_start:]]

def initialize(self, win, fps):
super().initialize(win, fps)
self._imgs = [visual.ImageStim(win=win, image=Image.open(i).convert('L')) for i in self._image_names]
self.screen = self._imgs[0]

Expand Down Expand Up @@ -646,6 +652,7 @@ def __init__(self, bg_color=(178 / 256) * 2 - 1, offset=(0.2, -0.5), **kwargs):
[rx_p[ii:ii + 300] - lx_p[ii:ii + 300], ty_p[ii:ii + 300] - by_p[ii:ii + 300]]), axis=1)

def initialize(self, win, fps):
super().initialize(win, fps)
self._imgs = [visual.ImageStim(win=win, image=Image.open(i).convert('L')) for i in self._image_names]
self.screen = self._imgs[0]

Expand Down
5 changes: 3 additions & 2 deletions playlists/video2.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
playlist:
video:
- _options: {random_mode: 'random_walk', repeat: 3}
- v_loom_stim: {name: 'looming', duration: 100}
- v_move_sq: {name: 'moving_square', duration: 200}
- v_loom_stim: {name: 'looming', duration_frames: 300}
- v_move_sq: {name: 'moving_square', duration_frames: 200}
- v_grating: {name: 'grating', duration_seconds: 4.5}

0 comments on commit 83db829

Please sign in to comment.