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

Source Code for Module topo.tkgui.templateplotgrouppanel

  1  """ 
  2  class TemplatePlotGroupPanel 
  3  Panel for displaying preference maps and activity plot groups. 
  4   
  5  $Id: templateplotgrouppanel.py 11310 2010-07-27 16:56:14Z ceball $ 
  6  """ 
  7  __version__='$Revision: 11310 $' 
  8   
  9   
 10  import copy 
 11   
 12  from Tkinter import DISABLED, NORMAL 
 13  from tkFileDialog import asksaveasfilename 
 14   
 15  import param 
 16  from param import tk,normalize_path 
 17   
 18  import topo.command.pylabplot 
 19  from topo.plotting.plotgroup import TemplatePlotGroup 
 20   
 21  from plotgrouppanel import SheetPanel 
 22   
 23   
 24  ### CEBALERT: additional dynamic info/right-click problems: 
 25  # 
 26  # If I open an Activity plot and then measure an orientation map, 
 27  # the plot only shows Activity, but the dynamic info includes the or 
 28  # pref and selectivity.  That makes sense, given that the plot would 
 29  # show that if it were refreshed, but it's confusing.  
 30   
 31   
 32   
 33   
 34  # CEBALERT: should  be something in plot or wherever, or maybe I don't see how to get this 
 35  # info the easy way from it? Not sure I actually wrote this correctly, anyway. 
36 -def available_plot_channels(plot):
37 """ 38 Return the channels+names of the channels that have views. 39 """ 40 available_channels = {} 41 for name,channel in plot.channels.items(): 42 if plot.view_dict.has_key(channel): 43 available_channels[name]=channel 44 return available_channels
45 46 47 48 49 ### CB: I'm working here at the moment. 50
51 -class TemplatePlotGroupPanel(SheetPanel):
52 53 plotgroup_type = TemplatePlotGroup 54 55 strength_only = param.Boolean(default=False,doc="""If true, disables all but the Strength channel of each plot, 56 disabling all color coding for Strength/Hue/Confidence plots.""") 57 58 #################################################################### 59 # CEBALERT: Ugly hack! Basic idea for method (deleting conf & hue 60 # from plot_templates) copied from previous tkgui. Instead, we 61 # probably want a mechanism for doing this from the command line, 62 # probably a global 'monochrome' parameter that all plots respect, 63 # and then people can modify the plot templates directly if they 64 # need more control than that.
65 - def _strength_only_hack(self):
66 # same as superclass, except that if strength only is true 67 # it temporarily removes hue&conf from plot_templates 68 original_templates = self.plotgroup.plot_templates 69 self.plotgroup.plot_templates = copy.deepcopy(self.plotgroup.plot_templates) 70 if self.strength_only: 71 for name,template in self.plotgroup.plot_templates: 72 for c in ['Confidence','Hue']: 73 if c in template: 74 del template[c] 75 return original_templates
76
77 - def refresh_plots(self):
78 # same as superclass, except that if strength only is true 79 # it temporarily removes hue&conf from plot_templates 80 original_templates = self._strength_only_hack() 81 super(TemplatePlotGroupPanel,self).refresh_plots() 82 self.plotgroup.plot_templates = original_templates
83
84 - def redraw_plots(self):
85 # same as superclass, except that if strength only is true 86 # it temporarily removes hue&conf from plot_templates 87 original_templates = self._strength_only_hack() 88 super(TemplatePlotGroupPanel,self).redraw_plots() 89 self.plotgroup.plot_templates = original_templates
90 91 92 ## CB: update init args now we have no pgts.
93 - def __init__(self,master,plotgroup,**params):
94 95 super(TemplatePlotGroupPanel,self).__init__(master,plotgroup,**params) 96 97 self.pack_param('strength_only',parent=self.control_frame_1, 98 on_set=self.redraw_plots,side='right') 99 100 # Display any plots that can be done with existing data, but 101 # don't regenerate the SheetViews unless requested 102 if self.plotgroup.plot_immediately: 103 self.refresh_plots() 104 else: 105 self.redraw_plots() 106 self.display_labels() # should this be called for any redraw? genuinely needs to be 107 # called here because labels might never have been drawn. 108 109 110 #################### RIGHT-CLICK MENU STUFF #################### 111 self._sheet_menu.add_command(label="Save image as PNG", 112 command=self.__save_to_png) 113 114 115 self._sheet_menu.add_command(label="Save image as EPS", 116 command=self.__save_to_postscript) 117 118 119 self._unit_menu.add_command(label="Print info", 120 command=self.__print_info) 121 122 # JABALERT: Shouldn't be assuming SHC here; should also work 123 # for RGB (or any other channels). 124 channel_menus={} 125 for chan in ['Strength','Hue','Confidence']: 126 newmenu = tk.Menu(self._canvas_menu, tearoff=0) 127 self._canvas_menu.add_cascade(menu=newmenu,label=chan+' channel', indexname=chan) 128 129 # The c=chan construction is required so that each lambda has its own copy of the string 130 newmenu.add_command(label="Print matrix", command=lambda c=chan: self.__print_matrix(c)) 131 newmenu.add_command(label="Plot with axis labels", command=lambda c=chan: self.__plot_matrix(c)) 132 newmenu.add_command(label="Plot as 3D wireframe", command=lambda c=chan: self.__plot_matrix3d(c)) 133 newmenu.add_command(label="Fourier transform", command=lambda c=chan: self.__fft(c)) 134 newmenu.add_command(label="Histogram", command=lambda c=chan: self.__histogram(c)) 135 newmenu.add_command(label="Gradient", command=lambda c=chan: self.__gradient(c)) 136 channel_menus[chan]=newmenu
137 138 139 #self._sheet_menu.add_command(label="Print matrix values", 140 # command=self.__print_matrix) 141 ################################################################# 142 143 144 145 146 147 ### JABALERT: Should remove the assumption that the plot will be 148 ### SHC (could e.g. be RGB). 149 ### 150 ### Should add the ability to turn off any of the channels 151 ### independently (just as the Strength-only button does), and 152 ### eventually should allow the user to type in the name of any 153 ### SheetView to change the template as desired to visualize any 154 ### quantity. 155 ### 156 # CB: something about these methods does not seem to fit the PlotGroup hierarchy.
157 - def _canvas_right_click(self,event_info):
158 """ 159 Make whichever of the SHC channels is present in the plot available on the menu. 160 """ 161 super(TemplatePlotGroupPanel,self)._canvas_right_click(event_info,show_menu=False) 162 163 if 'plot' in event_info: 164 plot = event_info['plot'] 165 166 available_channels =available_plot_channels(plot) 167 168 for channel in ('Strength','Hue','Confidence'): 169 if channel in available_channels: 170 self._canvas_menu.entryconfig(channel, 171 label="%s channel: %s" % 172 (channel,str(plot.channels[channel])),state=NORMAL) 173 else: 174 self._canvas_menu.entryconfig(channel, 175 label="%s channel: None" % 176 (channel),state=DISABLED) 177 178 self._canvas_menu.tk_popup(event_info['event'].x_root, 179 event_info['event'].y_root)
180 181
182 - def __save_to_png(self):
183 plot = self._right_click_info['plot'] 184 filename = self.plotgroup.filesaver.filename(plot.label(),file_format="png") 185 PNG_FILETYPES = [('PNG images','*.png'),('All files','*')] 186 snapshot_name = asksaveasfilename(filetypes=PNG_FILETYPES, 187 initialdir=normalize_path(), 188 initialfile=filename) 189 190 if snapshot_name: 191 plot.bitmap.image.save(snapshot_name)
192 193 # based on routine in editor.py
194 - def __save_to_postscript(self):
195 plot = self._right_click_info['plot'] 196 canvas = self._right_click_info['event'].widget 197 filename = self.plotgroup.filesaver.filename(plot.label(),file_format="eps") 198 POSTSCRIPT_FILETYPES = [('Encapsulated PostScript images','*.eps'), 199 ('PostScript images','*.ps'),('All files','*')] 200 snapshot_name = asksaveasfilename(filetypes=POSTSCRIPT_FILETYPES, 201 initialdir=normalize_path(), 202 initialfile=filename) 203 204 if snapshot_name: 205 canvas.postscript(file=snapshot_name)
206 207 # CB: these methods assume channel has a view (the menu only displays those that do)
208 - def __fft(self,channel):
209 plot = self._right_click_info['plot'] 210 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 211 m=plot._get_matrix(channel) 212 topo.command.pylabplot.fftplot(m, title="FFT Plot: " + description)
213
214 - def __histogram(self,channel):
215 plot = self._right_click_info['plot'] 216 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 217 m=plot._get_matrix(channel) 218 topo.command.pylabplot.histogramplot(m,title="Histogram: "+ description)
219
220 - def __gradient(self,channel):
221 plot = self._right_click_info['plot'] 222 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 223 m=plot._get_matrix(channel) 224 view = plot.view_dict[plot.channels[channel]] 225 topo.command.pylabplot.gradientplot(m,title="Gradient: " + description, 226 cyclic=view.cyclic,cyclic_range=view.norm_factor)
227
228 - def __print_matrix(self,channel):
229 plot = self._right_click_info['plot'] 230 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 231 print ("#" + description) 232 m=plot._get_matrix(channel) 233 print m
234
235 - def __plot_matrix(self,channel):
236 plot = self._right_click_info['plot'] 237 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 238 m=plot._get_matrix(channel) 239 topo.command.pylabplot.matrixplot(m, title=description)
240
241 - def __plot_matrix3d(self,channel):
242 plot = self._right_click_info['plot'] 243 description = "%s %s at time %s" % (plot.plot_src_name, plot.name, topo.sim.timestr()) 244 m=plot._get_matrix(channel) 245 topo.command.pylabplot.matrixplot3d(m, title=description)
246 247 # CEBALERT: decide if and how to allow any of these functions to be used for getting as many 248 # channels' info as possible. 249 # e.g. in this one...
250 - def __print_info(self,channel=None):
251 plot = self._right_click_info['plot'] 252 (r,c),(x,y) = self._right_click_info['coords'] 253 description ="%s %s, row %d, col %d at time %s: " % (plot.plot_src_name, plot.name, r, c, topo.sim.timestr()) 254 255 channels_info = "" 256 if channel is None: 257 for channel,name in available_plot_channels(plot).items(): 258 m=plot._get_matrix(channel) 259 channels_info+="%s:%f"%(name,m[r,c]) 260 else: 261 m=plot._get_matrix(channel) 262 channels_info+="%s:%f"%(plot.channels[channel],m[r,c]) 263 264 print "%s %s" % (description, channels_info)
265 266 267 268 269
270 - def _dynamic_info_string(self,event_info,basic_text):
271 """ 272 Also print whatever other channels are there and have views. 273 """ 274 plot = event_info['plot'] 275 r,c = event_info['coords'][0] 276 277 info_string = basic_text 278 279 for channel,channel_name in available_plot_channels(plot).items(): 280 info_string+=" %s: % 1.3f"%(channel_name,plot._get_matrix(channel)[r,c]) 281 282 return info_string
283