Package Vision :: Module StandardNodes
[hide private]
[frames] | no frames]

Source Code for Module Vision.StandardNodes

   1  ######################################################################### 
   2  # 
   3  # Date: Nov 2001 Authors: Michel Sanner, Daniel Stoffler 
   4  # 
   5  #    sanner@scripps.edu 
   6  #    stoffler@scripps.edu 
   7  # 
   8  #       The Scripps Research Institute (TSRI) 
   9  #       Molecular Graphics Lab 
  10  #       La Jolla, CA 92037, USA 
  11  # 
  12  # Copyright: Michel Sanner, Daniel Stoffler and TSRI 
  13  # 
  14  ######################################################################### 
  15   
  16  import warnings 
  17  import types 
  18  import Numeric 
  19  import string, types, re 
  20  from UserList import UserList 
  21  from NetworkEditor.items import NetworkNode 
  22   
23 -class Generic(NetworkNode):
24 """A node prototype. The user needs to add input-, and output ports, 25 and edit the compute function.""" 26
27 - def __init__(self, name='Generic', **kw):
28 kw['name'] = name 29 apply( NetworkNode.__init__, (self,), kw )
30 31
32 -class ATEST(NetworkNode):
33 """A node prototype. The user needs to add input-, and output ports, 34 and edit the compute function.""" 35
36 - def __init__(self, name='Generic', **kw):
37 kw['name'] = name 38 apply( NetworkNode.__init__, (self,), kw ) 39 ip = self.inputPortsDescr 40 ip.append(datatype='float(0)', name='singleFloat', required=False) 41 ip.append(datatype='float(3)', name='float3', required=False) 42 ip.append(datatype='float(>3)', name='floatmore3', required=False) 43 ip.append(datatype='float(4,4)', name='float44', required=False) 44 ip.append(datatype='float(>3 and <6)', name='float4or5', required=False) 45 46 code = """def doit(self, singleFloat=None, float3=None, 47 floatmore3=None, float44=None, float4or5=None): 48 print singleFloat, float3, floatmore3, float44, float4or5 49 """ 50 self.setFunction(code)
51 52
53 -class TestDefaultValues(NetworkNode):
54
55 - def __init__(self, name='ATEST', **kw):
56 kw['name'] = name 57 apply( NetworkNode.__init__, (self,), kw ) 58 ip = self.inputPortsDescr 59 ip.append(datatype='None', name='required') 60 ip.append(datatype='None', required=False, name='optNodefault') 61 ip.append(datatype='string', required=False, name='opt1') 62 ip.append(datatype='float', required=False, name='opt2') 63 64 code = """def doit(self, required, optNodefault, opt1='a', opt2=3.14): 65 print 'required', required 66 print 'optNodefault',optNodefault 67 print 'opt1',opt1 68 print 'opt2',opt2 69 """ 70 self.setFunction(code)
71 72 73
74 -class HasNewData(NetworkNode):
75 """ Node to evaluate if an input port gets new data 76 77 Input Ports 78 in1: allows to pass a Python object and determine if it is new data. 79 Output Ports 80 result: the input if new data 81 """ 82
83 - def __init__(self, name='HasNewData', **kw):
84 kw['name'] = name 85 apply( NetworkNode.__init__, (self,), kw ) 86 87 code = """def doit(self, in1): 88 if self.inputPorts[0].hasNewData(): 89 self.outputData(result=in1) 90 #else: 91 # return 'stop' 92 """ 93 94 if code: self.setFunction(code) 95 96 ip = self.inputPortsDescr 97 ip.append(datatype='None', required=True, name='in1') 98 99 self.outputPortsDescr.append(datatype='None', name='result')
100 101
102 -class ListOf(NetworkNode):
103 """Creates a list of Python objects from the ones sent into the node. 104 Each new object is added using a new uinput port. New input ports are 105 created on the fly when a connection is made to the first input port 106 called 'object'. 107 108 Input: 109 object: connect to this port to create a new input port for a Python 110 object to be added to the list 111 Output: 112 datalist: list of Python objects 113 """
114 - def __init__(self, name='LitsOf', **kw):
115 kw['name'] = name 116 apply( NetworkNode.__init__, (self,), kw ) 117 118 # define a function that creates a new port each time a connection 119 # to this port is made 120 codeAfterConnect = """def afterConnect(self, conn): 121 # self refers to the port 122 # conn is the connection that has been created 123 op = conn.port1 124 node = self.node 125 newport = {'name':op.name, 'datatype':op.datatypeObject['name'], 126 'afterDisconnect':self.node.codeAfterDisconnect, 127 '_modified': True} 128 ip = apply( node.addInputPort, (), newport ) 129 # create the connection to the new port 130 newconn = self.network.connectNodes( op.node, node, op.name, ip.name ) 131 # delete the connection to the port 132 self.network.deleteConnections([conn]) 133 # update the signature of the function 134 self.node.updateCode(node) 135 return newconn 136 """ 137 # define a function to be bound to a newly created port (see above) 138 # which will delete the port if the connection is deleted 139 # to this port is created 140 self.codeAfterDisconnect = """def afterDisconnect(self, p1, p2): 141 # self refers to the port 142 node = p2.node 143 node.deletePort(p2) 144 """ 145 ip = self.inputPortsDescr 146 ip.append(datatype='None', name='object', required=False, 147 balloon="connect data to be added to the list\nA new port will be created", 148 afterConnect=codeAfterConnect) 149 150 op = self.outputPortsDescr 151 op.append(datatype='list', name='datalist') 152 153 code = """def doit(self, newport): 154 vlist = [] 155 for p in self.inputPorts[1:]: 156 vlist.append(p.getData()) 157 self.outputData(datalist=vlist) 158 """ 159 self.setFunction(code)
160 161
162 -class Zip(ListOf):
163 """Zips together multiple lists of objects using the zip() Python function 164 165 New lists are added by connecting them to the the first port. Doing so will 166 create a new input ports on the fly. 167 168 Input: 169 object: connect to this port to create a new input port for a Python 170 object to be added to the list 171 followed by any number of ports (one per list) created on the fly 172 173 Output: 174 datalist: a single list whgere each element is a list contining 1 element 175 fro each of the input lists 176 """
177 - def __init__(self, name='zip', **kw):
178 kw['name'] = name 179 apply( ListOf.__init__, (self,), kw ) 180 181 code = """def doit(self, newport): 182 lists = [ p.getData() for p in self.inputPorts[1:] ] 183 if len(lists): 184 self.outputData(datalist=apply(zip, tuple(lists) ) ) 185 """ 186 self.setFunction(code)
187 188
189 -class Select(ListOf):
190 """Takes multiple input ports and outputs values from the ports presenting 191 new data 192 193 New input ports are added by connecting output ports to the the first port. 194 Doing so will create a new input ports on the fly. 195 196 Input: 197 object: connect to this port to create a new input port for a Python 198 object to be added to the list 199 followed by any number of ports (one per list) created on the fly 200 201 Output: 202 datalist: a single list whgere each element is a list contining 1 element 203 fro each of the input lists 204 """
205 - def __init__(self, name='select', **kw):
206 kw['name'] = name 207 apply( ListOf.__init__, (self,), kw ) 208 209 code = """def doit(self, newport): 210 values = [] 211 for p in self.inputPorts[1:]: 212 if p.hasNewData(): 213 values.append( p.getData() ) 214 if len(values): 215 self.outputData(datalist=values) 216 """ 217 self.setFunction(code)
218 219 220
221 -class DelayBuffer(NetworkNode):
222 """A node that receives data and buffers it a user specified number of executions. This canbe used to delay data in a a network. 223 224 Input Ports 225 value: next value to be added to the queue 226 delay: number of executions between the time data enters and is output 227 228 Output Ports 229 value: first value from the queue 230 """
231 - def __init__(self, name='DelayBuffer', **kw):
232 kw['name'] = name 233 apply( NetworkNode.__init__, (self,), kw ) 234 235 self.widgetDescr['delay'] = { 236 'class':'NEThumbWheel', 'master':'node', 237 'width':80, 'height':20, 'type':'int', 'wheelPad':1, 238 'initialValue':1, 239 'labelGridCfg':{'sticky':'w'}, 240 'widgetGridCfg':{'sticky':'w', 'columnspan':2}, 241 'labelCfg':{'text':'Qlen:'}, 242 } 243 244 ip = self.inputPortsDescr 245 ip.append(datatype='None', name='value', 246 balloon='valueto be appended to the queue') 247 ip.append(datatype='int', name='delay') 248 249 op = self.outputPortsDescr 250 op.append(datatype='None', name='value') 251 252 self.queue = [] 253 254 code = """def doit(self, value, delay): 255 self.queue.append(value) 256 if len(self.queue)<=delay: 257 self.outputPorts[0].data = None 258 else: 259 val = self.queue[0] 260 self.queue = self.queue[1:] 261 self.outputData(value=val) 262 """ 263 self.setFunction(code)
264 265
266 -class Filename(NetworkNode):
267 """A node to generate filenames with an integer value 268 Input Ports 269 format: a string use to create the filename is 'frame%08d.png' 270 number: number to be printed using format 271 272 Output Ports 273 filenamee: the resulting filename 274 """
275 - def __init__(self, name='Entry', **kw):
276 kw['name'] = name 277 apply( NetworkNode.__init__, (self,), kw ) 278 self.inNodeWidgetsVisibleByDefault = True 279 280 self.widgetDescr['format'] = { 281 'class':'NEEntry', 'master':'node', 'labelCfg':{'text':'format:'}, 282 'initialValue':'file%08d.png'} 283 284 ip = self.inputPortsDescr 285 ip.append(datatype='string', balloon='format string used to create a filename', name='format') 286 ip.append(datatype='int', name='number') 287 288 op = self.outputPortsDescr 289 op.append(datatype='string', name='filename') 290 291 code = """def doit(self, format, number): 292 self.outputData(filename=format%number) 293 """ 294 self.setFunction(code)
295 296
297 -class Filelist(NetworkNode):
298 """A node to generate a list filenames matching a string 299 Input Ports 300 matchstring: a string used by glob 301 302 Output Ports 303 filenames: the resulting list 304 """
305 - def __init__(self, name='Entry', **kw):
306 kw['name'] = name 307 apply( NetworkNode.__init__, (self,), kw ) 308 self.inNodeWidgetsVisibleByDefault = True 309 310 self.widgetDescr['match_str'] = { 311 'class':'NEEntry', 'master':'node', 'labelCfg':{'text':'match_str:'}, 312 'initialValue':'*.pdb*'} 313 314 ip = self.inputPortsDescr 315 ip.append(datatype='string', 316 balloon='string used to select filenames', name='match_str') 317 318 op = self.outputPortsDescr 319 op.append(datatype='list', name='filelist') 320 321 code = """def doit(self, match_str): 322 import glob 323 values = glob.glob(match_str) 324 self.outputData(filelist=values) 325 """ 326 self.setFunction(code)
327 328 329
330 -class EntryNE(NetworkNode):
331 """A Tkinter Entry widget. 332 Double-clicking on the node opens the entry widget. 333 334 Input Ports 335 button: (bound to checkbutton widget) 336 337 Output Ports 338 value: a integer describing the status of the checkbutton (1 on, 0 off) 339 """ 340
341 - def __init__(self, name='Entry', **kw):
342 kw['name'] = name 343 apply( NetworkNode.__init__, (self,), kw ) 344 self.inNodeWidgetsVisibleByDefault = True 345 346 self.widgetDescr['entry'] = { 347 'class':'NEEntry', 'master':'node', 'width':14, 348 'labelCfg':{'text':''}, 'lockedOnPort':True } 349 350 self.inputPortsDescr.append(datatype='string', name='entry') 351 352 self.outputPortsDescr.append(datatype='string', name='string') 353 354 code = """def doit(self, entry): 355 if len(str(entry))!=0: 356 self.outputData(string=entry) 357 """ 358 359 self.setFunction(code)
360 361
362 -class FileBrowserNE(NetworkNode):
363 """A Tkinter Filebrowser. Double-clicking into the entry opens the 364 filebrowser.""" 365
366 - def __init__(self, name='File Browser', **kw):
367 kw['name'] = name 368 apply( NetworkNode.