VisionEgg.PyroApps.TargetServer

Package VisionEgg :: Package PyroApps :: Module TargetServer |
[frames] | no frames]

# Source Code for Module VisionEgg.PyroApps.TargetServer

      1  #!/usr/bin/env python
      2  #
      3  # The Vision Egg: TargetServer
      4  #
      5  # Copyright (C) 2001-2003 Andrew Straw.
      6  # Author: Andrew Straw <astraw@users.sourceforge.net>
      7  # URL: <http://www.visionegg.org/>
      8  #
      9  # Distributed under the terms of the GNU Lesser General Public License
     10  # (LGPL). See LICENSE.TXT that came with this file.
     11  #
     12  # $Id$
     13
     14  """Handle small targets gratings (server-side)"""
     15
     16  import VisionEgg
     17  __version__ = VisionEgg.release_name
     18  __cvs__ = '$Revision$'.split()[1]
     19  __date__ = ' '.join('$Date$'.split()[1:3])
     20  __author__ = 'Andrew Straw <astraw@users.sourceforge.net>'
     21
     22  import sys, os, math
     23  import VisionEgg.Core
     24  import VisionEgg.FlowControl
     25  import VisionEgg.MoreStimuli
     26  import VisionEgg.PyroHelpers
     27  import Pyro.core
     28
     29  from VisionEgg.PyroApps.TargetGUI import TargetMetaParameters
     30
     31 -class TargetExperimentMetaController( Pyro.core.ObjBase ):
     32 -    def __init__(self,screen,presentation,stimuli):
     33          Pyro.core.ObjBase.__init__(self)
     34
     35          # get stimulus
     36          assert( stimuli[0][0] == '2d_overlay')
     37          target = stimuli[0][1]
     38
     39          self.meta_params = TargetMetaParameters()
     40          if not isinstance(screen,VisionEgg.Core.Screen):
     41              raise ValueError("Expecting instance of VisionEgg.Core.Screen")
     42          if not isinstance(presentation,VisionEgg.FlowControl.Presentation):
     43              raise ValueError("Expecting instance of VisionEgg.FlowControl.Presentation")
     44          if not isinstance(target,VisionEgg.MoreStimuli.Target2D):
     45              raise ValueError("Expecting instance of VisionEgg.MoreStimuli.Target2D")
     46
     47          self.screen = screen
     48          self.p = presentation
     49          self.stim = target
     50
     51          self.p.add_controller(self.stim,'on',VisionEgg.FlowControl.FunctionController(
     52              during_go_func=self.on_function_during_go,
     53              between_go_func=self.on_function_between_go))
     54
     55          self.p.add_controller(self.stim,'position',VisionEgg.FlowControl.FunctionController(
     56              during_go_func=self.center_during_go,
     57              between_go_func=self.center_between_go))
     58
     59          self.update() # set stimulus parameters to initial defaults
     60
     61 -    def __del__(self):
     62          self.p.remove_controller(self.stim,'on')
     63          self.p.remove_controller(self.stim,'position')
     64          Pyro.core.ObjBase.__del__(self) # call base class
     65
     66 -    def on_function_during_go(self,t):
     67          if t <= self.meta_params.pre_stim_sec:
     68              return 0 # not on yet
     69          elif t <= (self.meta_params.pre_stim_sec + self.meta_params.stim_sec):
     70              return 1 # on
     71          else:
     72              return 0 # off again
     73
     74 -    def on_function_between_go(self):
     75          return 0
     76
     77 -    def center_during_go(self,t):
     78          t_adjusted = t - self.meta_params.pre_stim_sec
     79          distance = self.meta_params.velocity_pps * t_adjusted
     80          x_offset = math.cos(self.meta_params.direction_deg / 180.0 * math.pi)*distance
     81          y_offset = math.sin(self.meta_params.direction_deg / 180.0 * math.pi)*distance
     82
     83          return (self.meta_params.start_x + x_offset,
     84                  self.meta_params.start_y + y_offset)
     85
     86 -    def center_between_go(self):
     87          return (0.0, 0.0) # doesn't matter -- it's off
     88
     89 -    def get_parameters(self):
     90          return self.meta_params
     91
     92 -    def set_parameters(self, new_parameters):
     93          if isinstance(new_parameters, TargetMetaParameters):
     94              self.meta_params = new_parameters
     95          else:
     96              raise ValueError("Argument to set_parameters must be instance of TargetMetaParameters")
     97          self.update()
     98
     99 -    def update(self):
    100          stim_params = self.stim.parameters # shorthand
    101          meta_params = self.meta_params # shorthand
    102
    103          # colors
    104          stim_params.color = meta_params.color
    105          self.screen.parameters.bgcolor = meta_params.bgcolor
    106
    107          # size and orientation
    108          stim_params.size = (meta_params.width, meta_params.height)
    109          stim_params.orientation = meta_params.orientation_deg
    110
    111          self.p.parameters.go_duration = ( meta_params.pre_stim_sec + meta_params.stim_sec + meta_params.post_stim_sec, 'seconds')
    112
    113 -    def go(self):
    114          self.p.parameters.enter_go_loop = 1
    115
    116 -    def quit_server(self):
    117          self.p.parameters.quit = 1
    118
    119 -def get_meta_controller_class():
    120      return TargetExperimentMetaController
    121
    122 -def make_stimuli():
    123      stimulus = VisionEgg.MoreStimuli.Target2D(anchor='center')
    124      return [('2d_overlay',stimulus)]
    125
    126 -def get_meta_controller_stimkey():
    127      return "target_server"
    128
    129  # Don't do anything unless this script is being run
    130  if __name__ == '__main__':
    131
    132      pyro_server = VisionEgg.PyroHelpers.PyroServer()
    133
    134      screen = VisionEgg.Core.Screen.create_default()
    135
    136      # get Vision Egg stimulus ready to go
    137      stimuli = make_stimuli()
    138      stimulus = stimuli[0][1]
    139      viewport = VisionEgg.Core.Viewport(screen=screen,stimuli=[stimulus])
    140      p = VisionEgg.FlowControl.Presentation(viewports=[viewport])
    141
    142      # now hand over control of grating and mask to FlatGratingExperimentMetaController
    143      meta_controller = TargetExperimentMetaController(screen,p,stimuli)
    144      pyro_server.connect(meta_controller,get_meta_controller_stimkey())
    145
    146      # get listener controller and register it
    147      p.add_controller(None,None, pyro_server.create_listener_controller())
    148
    149      # enter endless loop
    150      p.run_forever()
    151