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

# Source Code for Module VisionEgg.PyroApps.SphereGratingServer

      1  #!/usr/bin/env python
      2  """Handle perspective-distorted sinusoidal gratings (server-side)"""
      3
      4  # Copyright (c) 2002-2003 Andrew Straw.  Distributed under the terms
      5  # of the GNU Lesser General Public License (LGPL).
      6
      7  import VisionEgg, string
      8  __version__ = VisionEgg.release_name
      9  __cvs__ = string.split('$Revision$')[1]
     10  __date__ = string.join(string.split('$Date$')[1:3], ' ')
     11  __author__ = 'Andrew Straw <astraw@users.sourceforge.net>'
     12
     13  import sys, os, math
     14  import VisionEgg.Core
     15  import VisionEgg.FlowControl
     16  import VisionEgg.SphereMap
     17  import VisionEgg.PyroHelpers
     18  import Pyro.core
     19
     20  from VisionEgg.PyroApps.ScreenPositionServer import ScreenPositionMetaController
     21  from VisionEgg.PyroApps.ScreenPositionGUI import ScreenPositionParameters
     22  from VisionEgg.PyroApps.SphereGratingGUI import SphereGratingMetaParameters
     23
     24 -class SphereGratingExperimentMetaController( Pyro.core.ObjBase ):
     25 -    def __init__(self,screen,presentation,stimuli):
     26
     27          # get stimuli
     28          assert( stimuli[0][0] == '3d_perspective')
     29          assert( stimuli[1][0] == '3d_perspective')
     30          sphere_grating = stimuli[0][1]
     31          sphere_window = stimuli[1][1]
     32
     33          Pyro.core.ObjBase.__init__(self)
     34          self.meta_params = SphereGratingMetaParameters()
     35          if not isinstance(screen,VisionEgg.Core.Screen):
     36              raise ValueError("Expecting instance of VisionEgg.Core.Screen")
     37          if not isinstance(presentation,VisionEgg.FlowControl.Presentation):
     38              raise ValueError("Expecting instance of VisionEgg.FlowControl.Presentation")
     39          if not isinstance(sphere_grating,VisionEgg.SphereMap.SphereGrating):
     40              raise ValueError("Expecting instance of VisionEgg.SphereMap.SphereGrating")
     41          if not isinstance(sphere_window,VisionEgg.SphereMap.SphereWindow):
     42              raise ValueError("Expecting instance of VisionEgg.SphereMap.SphereWindow")
     43          self.p = presentation
     44          self.stim = sphere_grating
     45          self.window = sphere_window
     46
     47          screen.parameters.bgcolor = (0.5, 0.5, 0.5, 0.0)
     48
     49          self.p.add_controller(self.stim,'on',VisionEgg.FlowControl.FunctionController(
     50              during_go_func=self.on_function_during_go,
     51              between_go_func=self.on_function_between_go))
     52
     53 -    def __del__(self):
     54          self.p.remove_controller(self.stim,'on')
     55          Pyro.core.ObjBase.__del__(self) # call base class
     56
     57 -    def on_function_during_go(self,t):
     58          """Compute when the grating is on"""
     59          if t <= self.meta_params.pre_stim_sec:
     60              return 0 # not on yet
     61          elif t <= (self.meta_params.pre_stim_sec + self.meta_params.stim_sec):
     62              return 1 # on
     63          else:
     64              return 0 # off again
     65
     66 -    def on_function_between_go(self):
     67          """Compute when the grating is off"""
     68          return 0 # off again
     69
     70 -    def get_parameters(self):
     71          return self.meta_params
     72
     73 -    def set_parameters(self, new_parameters):
     74          if isinstance(new_parameters, SphereGratingMetaParameters):
     75              self.meta_params = new_parameters
     76          else:
     77              raise ValueError("Argument to set_parameters must be instance of SphereGratingMetaParameters")
     78  #        self.meta_params = new_parameters
     79          self.update()
     80
     81 -    def update(self):
     82          stim_params = self.stim.parameters # shorthand
     83          window_params = self.window.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_cpd = meta_params.sf
     88          stim_params.temporal_freq_hz = meta_params.tf
     89          stim_params.grating_center_azimuth = meta_params.window_az
     90          stim_params.grating_center_elevation = meta_params.window_el
     91          self.p.parameters.go_duration = ( meta_params.pre_stim_sec + meta_params.stim_sec + meta_params.post_stim_sec, 'seconds')
     92          window_params.window_shape = meta_params.window_func
     93          window_params.window_shape_radius_parameter = meta_params.window_radius
     94          window_params.window_center_azimuth = meta_params.window_az
     95          window_params.window_center_elevation = meta_params.window_el
     96
     97 -    def go(self):
     98          self.p.parameters.enter_go_loop = 1
     99
    100 -    def quit_server(self):
    101          self.p.parameters.quit = 1
    102
    103 -def get_meta_controller_class():
    104      return SphereGratingExperimentMetaController
    105
    106 -def make_stimuli():
    107      stimulus = VisionEgg.SphereMap.SphereGrating(radius=1.0,
    108                                                   spatial_freq_cpd=1.0/9.0,
    109                                                   temporal_freq_hz = 1.0)
    110      mask = VisionEgg.SphereMap.SphereWindow(radius=0.95)
    111      return [('3d_perspective',stimulus),('3d_perspective',mask)]
    112
    113 -def get_meta_controller_stimkey():
    114      return "sphere_grating_server"
    115
    116  # Don't do anything unless this script is being run
    117  if __name__ == '__main__':
    118
    119      pyro_server = VisionEgg.PyroHelpers.PyroServer()
    120
    121      screen = VisionEgg.Core.Screen.create_default()
    122
    123      # get Vision Egg stimulus ready to go
    124      stimuli = make_stimuli()
    125      stimulus = stimuli[0][1]
    126      mask = stimuli[1][1]
    127
    128      temp = ScreenPositionParameters()
    129
    130      left = temp.left
    131      right = temp.right
    132      bottom = temp.bottom
    133      top = temp.top
    134      near = temp.near
    135      far = temp.far
    136      projection = VisionEgg.Core.PerspectiveProjection(left,
    137                                                        right,
    138                                                        bottom,
    139                                                        top,
    140                                                        near,
    141                                                        far)
    142      viewport = VisionEgg.Core.Viewport(screen=screen,stimuli=[stimulus,mask],projection=projection)
    143      p = VisionEgg.FlowControl.Presentation(viewports=[viewport])
    144
    145      # now hand over control of projection to ScreenPositionMetaController
    146      projection_controller = ScreenPositionMetaController(p,projection)
    147      pyro_server.connect(projection_controller,"projection_controller")
    148
    149      # now hand over control of grating and mask to SphereGratingExperimentMetaController
    150      meta_controller = SphereGratingExperimentMetaController(screen,p,stimuli)
    151      pyro_server.connect(meta_controller,get_meta_controller_stimkey())
    152
    153      # get listener controller and register it
    154      p.add_controller(None,None, pyro_server.create_listener_controller())
    155
    156      # enter endless loop
    157      p.run_forever()
    158