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

# Source Code for Module VisionEgg.PyroApps.FlatGratingServer

      1  #!/usr/bin/env python
      2  #
      3  # The Vision Egg: FlatGratingServer
      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 sinusoidal gratings (server-side)"""
     15
     16  import VisionEgg, string
     17  __version__ = VisionEgg.release_name
     18  __cvs__ = string.split('$Revision$')[1]
     19  __date__ = string.join(string.split('$Date$')[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.Gratings
     26  import VisionEgg.PyroHelpers
     27  import Pyro.core
     28
     29  from VisionEgg.PyroApps.FlatGratingGUI import FlatGratingMetaParameters
     30
     31 -class FlatGratingExperimentMetaController( Pyro.core.ObjBase ):
     32 -    def __init__(self,screen,presentation,stimuli):
     33
     34          # get stimulus
     35          assert( stimuli[0][0] == '2d_overlay')
     36          grating = stimuli[0][1]
     37
     38          Pyro.core.ObjBase.__init__(self)
     39          self.meta_params = FlatGratingMetaParameters()
     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(grating,VisionEgg.Gratings.SinGrating2D):
     45              raise ValueError("Expecting instance of VisionEgg.Gratings.SinGrating2D")
     46          self.p = presentation
     47          self.stim = grating
     48
     49          screen.parameters.bgcolor = (0.5, 0.5, 0.5, 0.0)
     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 -    def __del__(self):
     56          self.p.remove_controller(self.stim,'on')
     57          Pyro.core.ObjBase.__del__(self) # call base class
     58
     59 -    def on_function_during_go(self,t):
     60          """Compute when the grating is on"""
     61          if t <= self.meta_params.pre_stim_sec:
     62              return 0 # not on yet
     63          elif t <= (self.meta_params.pre_stim_sec + self.meta_params.stim_sec):
     64              return 1 # on
     65          else:
     66              return 0 # off again
     67
     68 -    def on_function_between_go(self):
     69          """Compute when the grating is off"""
     70          return 0 # off again
     71
     72 -    def get_parameters(self):
     73          return self.meta_params
     74
     75 -    def set_parameters(self, new_parameters):
     76          if isinstance(new_parameters, FlatGratingMetaParameters):
     77              self.meta_params = new_parameters
     78          else:
     79              raise ValueError("Argument to set_parameters must be instance of FlatGratingMetaParameters")
     80          self.update()
     81
     82 -    def update(self):
     83          stim_params = self.stim.parameters # shorthand
     84          meta_params = self.meta_params # shorthand
     85          stim_params.contrast = meta_params.contrast
     86          stim_params.orientation = meta_params.orient
     87          stim_params.spatial_freq = meta_params.sf
     88          stim_params.temporal_freq_hz = meta_params.tf
     89          stim_params.size = (meta_params.size_x, meta_params.size_y)
     90          stim_params.position = (meta_params.center_x, meta_params.center_y)
     91          self.p.parameters.go_duration = ( meta_params.pre_stim_sec + meta_params.stim_sec + meta_params.post_stim_sec, 'seconds')
     92
     93 -    def go(self):
     94          self.p.parameters.enter_go_loop = 1
     95
     96 -    def quit_server(self):
     97          self.p.parameters.quit = 1
     98
     99 -def get_meta_controller_class():
    100      return FlatGratingExperimentMetaController
    101
    102 -def make_stimuli():
    103      stimulus = VisionEgg.Gratings.SinGrating2D( spatial_freq=1.0/100.0, # wavelength = 100 pixels
    104                                                  temporal_freq_hz = 1.0,
    105                                                  anchor='center',
    106                                                  )
    107      return [('2d_overlay',stimulus)]
    108
    109 -def get_meta_controller_stimkey():
    110      return "flat_grating_server"
    111
    112  # Don't do anything unless this script is being run
    113  if __name__ == '__main__':
    114
    115      pyro_server = VisionEgg.PyroHelpers.PyroServer()
    116
    117      screen = VisionEgg.Core.Screen.create_default()
    118
    119      # get Vision Egg stimulus ready to go
    120      stimuli = make_stimuli()
    121      stimulus = stimuli[0][1]
    122      viewport = VisionEgg.Core.Viewport(screen=screen,stimuli=[stimulus])
    123      p = VisionEgg.FlowControl.Presentation(viewports=[viewport])
    124
    125      # now hand over control of grating and mask to FlatGratingExperimentMetaController
    126      meta_controller = FlatGratingExperimentMetaController(screen,p,stimuli)
    127      pyro_server.connect(meta_controller,get_meta_controller_stimkey())
    128
    129      # get listener controller and register it
    130      p.add_controller(None,None, pyro_server.create_listener_controller())
    131
    132      # enter endless loop
    133      p.run_forever()
    134