Package topo :: Package sheet :: Module composer
[hide private]
[frames] | no frames]

Source Code for Module topo.sheet.composer

  1  """ 
  2  A Sheet class for composing activity from different sheets into a 
  3  single activity matrix.  Primarily a simple example of how to make 
  4  a sheet class, but can also be useful. 
  5   
  6  $Id: composer.py 8988 2008-08-25 00:57:14Z ceball $ 
  7  """ 
  8  __version__='$Revision: 8988 $' 
  9   
 10  from numpy.oldnumeric import zeros 
 11   
 12  from topo.base.sheet import Sheet 
 13  from topo.misc.util import Struct, NxN 
 14   
15 -class Composer(Sheet):
16 """ 17 A Sheet that combines the activity of 2 or more other sheets into 18 a single activity matrix. When connecting a sheet to a composer, 19 you can specify the location at which that sheet's input will be 20 mapped into the composer by adding the 'origin' argument to the 21 connect() call e.g.: 22 23 sim.connect(input_sheet.name,composer.name,delay=1, origin=(0.25,0.25)) 24 25 will cause (0,0) on input sheet's activity to map to (0.25,0.25) 26 on composer's activity. 27 28 """ 29 30 dest_ports=None # Allows connections to come in on any port 31
32 - def __init__(self,**params):
33 super(Composer,self).__init__(**params) 34 self.inputs = {} 35 self.__dirty = False
36
37 - def port_configure(self,port,**config):
38 """ 39 Configure a specific input port. 40 41 origin = (default (0,0)) The offset in the output matrix where 42 this port's input should be placed. 43 """ 44 if not port in self.ports: 45 self.ports[port] = {} 46 47 for k,v in config.items(): 48 self.ports[port][k] = v
49
50 - def _dest_connect(self,proj,origin=(0,0)):
51 super(Composer,self)._dest_connect(proj) 52 self.inputs[(proj.src.name,proj.src_port)] = Struct(origin=origin)
53
54 - def process_current_time(self):
55 if self.__dirty: 56 self.send_output(data=self.activity) 57 self.activity = zeros(self.activity.shape)+0.0 58 self.__dirty=False
59
60 - def input_event(self,conn,data):
61 62 self.verbose("Received %s input from %s." % (NxN(data.shape),conn.src)) 63 64 self.__dirty = True 65 66 in_rows, in_cols = data.shape 67 68 # compute the correct position of the input in the buffer 69 start_row,start_col = self.sheet2matrixidx(*self.inputs[(conn.src.name,conn.src_port)].origin) 70 row_adj,col_adj = conn.src.sheet2matrixidx(0,0) 71 72 self.debug("origin (row,col) = "+`(start_row,start_col)`) 73 self.debug("adjust (row,col) = "+`(row_adj,col_adj)`) 74 75 start_row -= row_adj 76 start_col -= col_adj 77 78 # the maximum bounds 79 max_row,max_col = self.activity.shape 80 81 self.debug("max_row = %d, max_col = %d" % (max_row,max_col)) 82 self.debug("in_rows = %d, in_cols = %d" % (in_rows,in_cols)) 83 84 end_row = start_row+in_rows 85 end_col = start_col+in_cols 86 87 # if the input goes outside the activity, clip it 88 left_clip = -min(start_col,0) 89 top_clip = -min(start_row,0) 90 right_clip = max(end_col,max_col) - max_col 91 bottom_clip = max(end_row,max_row) - max_row 92 93 start_col += left_clip 94 start_row += top_clip 95 end_col -= right_clip 96 end_row -= bottom_clip 97 98 self.debug("start_row = %d,start_col = %d" % (start_row,start_col)) 99 self.debug("end_row = %d,end_col = %d" % (end_row,end_col)) 100 self.debug("left_clip = %d" % left_clip) 101 self.debug("right_clip = %d" % right_clip) 102 self.debug("top_clip = %d" % top_clip) 103 self.debug("bottom_clip = %d" % bottom_clip) 104 self.debug("activity shape = %s" % NxN(self.activity.shape)) 105 106 self.activity[start_row:end_row, start_col:end_col] += data[top_clip:in_rows-bottom_clip, 107 left_clip:in_cols-right_clip]
108