Package topo :: Package tkgui :: Module testpattern
[hide private]
[frames] | no frames]

Source Code for Module topo.tkgui.testpattern

  1  """ 
  2  The Test Pattern window allows input patterns to be previewed. 
  3   
  4  $Id: testpattern.py 11310 2010-07-27 16:56:14Z ceball $ 
  5  """ 
  6  __version__='$Revision: 11310 $' 
  7   
  8  ## Notes: 
  9  # * need to remove audigen pgs 
 10  # * missing disparity flip hack (though see JABHACKALERT below) 
 11  # * values like pi are written over 
 12  # * need to sort the list of Pattern generators 
 13   
 14  ## Needs to be upgraded to behave how we want: 
 15  ### JABHACKALERT: Should use PatternPresenter (from 
 16  ### topo.command.analysis), which will allow flexible support for 
 17  ### making objects with different parameters in the different eyes, 
 18  ### e.g. to test ocular dominance or disparity. 
 19   
 20  # CBENHANCEMENT: add 'use for learning' to install current pattern 
 21  # (saving previous ones)? 
 22   
 23  from Tkinter import Frame 
 24   
 25  import param 
 26  from param import tk 
 27   
 28  import topo 
 29   
 30  from topo.base.functionfamily import PatternDrivenAnalysis 
 31  from topo.base.sheetview import SheetView 
 32  from topo.base.patterngenerator import PatternGenerator, Constant 
 33  from topo.misc.generatorsheet import GeneratorSheet 
 34  from topo.command.basic import pattern_present 
 35  from topo.plotting.plot import make_template_plot 
 36  from topo.plotting.plotgroup import SheetPlotGroup 
 37   
 38  from plotgrouppanel import SheetPanel 
 39   
 40   
 41  # CEBALERT: this uses make_template_plot(), so how is it not a 
 42  # TemplatePlotGroup?  
43 -class TestPatternPlotGroup(SheetPlotGroup):
44
45 - def sheets(self):
46 if hasattr(self,'_sheets'): 47 return self._sheets 48 else: 49 return super(TestPatternPlotGroup,self).sheets()
50
51 - def _generate_plots(self):
52 dynamic_plots = [] 53 for kw in [dict(sheet=sheet) for sheet in self.sheets()]: 54 sheet = kw['sheet'] 55 new_view = SheetView((sheet.input_generator(),sheet.bounds), 56 sheet.name,sheet.precedence,topo.sim.time()) 57 sheet.sheet_views['Activity']=new_view 58 channels = {'Strength':'Activity','Hue':None,'Confidence':None} 59 60 ### JCALERT! it is not good to have to pass '' here... maybe a test in plot would be better 61 dynamic_plots.append(make_template_plot(channels,sheet.sheet_views, 62 sheet.xdensity,sheet.bounds,self.normalize, 63 name='')) 64 65 return self._static_plots[:]+dynamic_plots
66 67 68
69 -class TestPattern(SheetPanel,PatternDrivenAnalysis):
70 71 sheet_type = GeneratorSheet 72 73 dock = param.Boolean(False) 74 75 edit_sheet = param.ObjectSelector(doc=""" 76 Sheet for which to edit pattern properties.""") 77 78 plastic = param.Boolean(default=False,doc=""" 79 Whether to enable plasticity during presentation.""") 80 81 duration = param.Number(default=1.0, softbounds=(0.0,10.0),doc=""" 82 How long to run the simulation for each presentation.""") 83 84 Present = tk.Button(doc="""Present this pattern to the simulation.""") 85 86 pattern_generator = param.ClassSelector(default=Constant(), class_=PatternGenerator, doc=""" 87 Type of pattern to present. Each type has various parameters that can be changed.""") 88 89 90
91 - def __init__(self,master,plotgroup=None,**params):
92 plotgroup = plotgroup or TestPatternPlotGroup() 93 94 super(TestPattern,self).__init__(master,plotgroup,**params) 95 96 self.auto_refresh = True 97 98 self.plotcommand_frame.pack_forget() 99 for name in ['pre_plot_hooks','plot_hooks','Fwd','Back']: 100 self.hide_param(name) 101 102 edit_sheet_param = self.get_parameter_object('edit_sheet') 103 edit_sheet_param.objects = self.plotgroup.sheets() 104 105 self.pg_control_pane = Frame(self) #,bd=1,relief="sunken") 106 self.pg_control_pane.pack(side="top",expand='yes',fill='x') 107 108 self.params_frame = tk.ParametersFrame( 109 self.pg_control_pane, 110 parameterized_object=self.pattern_generator, 111 on_modify=self.conditional_refresh, 112 msg_handler=master.status) 113 114 self.params_frame.hide_param('Close') 115 self.params_frame.hide_param('Refresh') 116 117 # CEB: 'new_default=True' is temporary so that the current 118 # behavior is the same as before; shoudl make None the 119 # default and mean 'apply to all sheets'. 120 self.pack_param('edit_sheet',parent=self.pg_control_pane,on_modify=self.switch_sheet,widget_options={'new_default':True,'sort_fn_args':{'cmp':lambda x, y: cmp(-x.precedence,-y.precedence)}}) 121 self.pack_param('pattern_generator',parent=self.pg_control_pane, 122 on_modify=self.change_pattern_generator,side="top") 123 124 present_frame = Frame(self) 125 present_frame.pack(side='bottom') 126 127 self.pack_param('plastic',side='bottom',parent=present_frame) 128 self.params_frame.pack(side='bottom',expand='yes',fill='x') 129 self.pack_param('duration',parent=present_frame,side='left') 130 self.pack_param('Present',parent=present_frame,on_set=self.present_pattern,side="right")
131 132
133 - def setup_plotgroup(self):
134 super(TestPattern,self).setup_plotgroup() 135 136 # CB: could copy the sheets instead (deleting connections etc) 137 self.plotgroup._sheets = [GeneratorSheet(name=gs.name, 138 nominal_bounds=gs.nominal_bounds, 139 nominal_density=gs.nominal_density) 140 for gs in topo.sim.objects(GeneratorSheet).values()] 141 self.plotgroup._set_name("Test Pattern")
142 143
144 - def switch_sheet(self):
145 if self.edit_sheet is not None: 146 self.pattern_generator = self.edit_sheet.input_generator 147 self.change_pattern_generator()
148 149
150 - def change_pattern_generator(self):
151 """ 152 Set the current PatternGenerator to the one selected and get the 153 ParametersFrameWithApply to draw the relevant widgets 154 """ 155 # CEBALERT: if pattern generator is set to None, there will be 156 # an error. Need to handle None in the appropriate place 157 # (presumably tk.py). 158 self.params_frame.set_PO(self.pattern_generator) 159 160 for sheet in self.plotgroup.sheets(): 161 if sheet==self.edit_sheet: 162 sheet.set_input_generator(self.pattern_generator) 163 164 self.conditional_refresh()
165 166
167 - def refresh(self):
168 """ 169 Simply update the plots: skip all handling of history. 170 """ 171 self.refresh_plots()
172 173
174 - def present_pattern(self):
175 """ 176 Move the user created patterns into the GeneratorSheets, run for 177 the specified length of time, then restore the original 178 patterns. 179 """ 180 topo.sim.run(0.0) # ensure EPs are start()ed 181 182 topo.sim.state_push() 183 for f in self.pre_presentation_hooks: f() 184 input_dict = dict([(sheet.name,sheet.input_generator) \ 185 for sheet in self.plotgroup.sheets()]) 186 pattern_present(input_dict,self.duration, 187 plastic=self.plastic,overwrite_previous=False) 188 topo.guimain.auto_refresh() 189 for f in self.post_presentation_hooks: f() 190 topo.sim.state_pop()
191