Package topo :: Package base :: Module sheetview
[hide private]
[frames] | no frames]

Source Code for Module topo.base.sheetview

  1   
  2  """ 
  3  Topographica SheetView objects and its subclasses. 
  4   
  5  For use with the Topographica plotting and data analysis mechanisms. 
  6  A Sheet object has its internal data which remains hidden, but it will 
  7  create views of this data depending on the Sheet defaults or the 
  8  information requested.  This way there can be multiple views recorded 
  9  for a single sheet, and a view can be passed around independent of the 
 10  originating source object. 
 11   
 12  $Id: sheetview.py 10672 2009-10-28 01:00:50Z ceball $ 
 13  """ 
 14  __version__='$Revision: 10672 $' 
 15   
 16  import types 
 17  import operator 
 18   
 19  import param 
 20   
 21  import sheet 
 22   
 23  ### JABHACKALERT! 
 24  ### 
 25  ### Based on the comments, this file is not yet completed.  E.g. are 
 26  ### any of these operators implemented?  They should either be tested 
 27  ### or removed.  Similarly for other features below -- they need to be 
 28  ### either completed or removed. 
 29   
 30  # Valid operations that a SheetView should support for the list of 
 31  # sheets passed in.  There are other things that would be useful 
 32  # such as masking which will require a bit more work.  !!UNTESTED!! 
 33  ADD      = 'ADD' 
 34  SUBTRACT = 'SUB' 
 35  MULTIPLY = 'MUL' 
 36  DIVIDE   = 'DIV' 
 37  # These 'operator' functions require 2 parameters when using 'apply'. 
 38  operations = {ADD : operator.add, 
 39                SUBTRACT : operator.sub, 
 40                MULTIPLY : operator.mul, 
 41                DIVIDE : operator.truediv} 
 42   
 43   
44 -class SheetView(param.Parameterized):
45 """ 46 A SheetView is constructed from a matrix of values, a bounding box 47 for that matrix, and a name. There are two major ways to create a 48 SheetView: one is from a single matrix of data from a single 49 sheet, the other is by combining the matrices from multiple 50 matrices or SheetViews. 51 """ 52 53 cyclic = param.Boolean(default=False,doc= 54 """Whether or not the values in this View's matrix represent a cyclic dimension.""") 55 56 # CB: Should call this cyclic_range, I think. Unless norm_factor 57 # is ever going to be anything but 1.0 for non-cyclic 58 # quantities...but I'm not sure it makes sense to store anything 59 # but the cyclic range here. See ALERT in featureresponses.py. 60 norm_factor = param.Parameter(None,doc="""If cyclic is True, this value is the cyclic range.""") 61 62 ### JCALERT! term_1 and term_2 should be more explicit... 63 ### the 3 cases described in the doc, are they really useful? 64 ### shouldn't it be simplified?
65 - def __init__(self, (term_1, term_2), src_name=None, precedence = 0.0, timestamp = -1, row_precedence = 0.5, **params):
66 """ 67 For ``__init__(self, input_tuple, **params)``, there are three 68 types of input_tuples:: 69 70 (matrix_data, matrix_bbox) 71 72 This form locks the value of the sheetview to a single matrix. 73 Terminating case of a composite SheetView. 74 75 (operation, [tuple_list]) 76 77 'operation' is performed on the matrices collected from 78 tuple_list. See the list of valid operations in 79 operations.keys(). 80 81 Each tuple in the tuple_list is one of the following:: 82 83 (SheetView, None) 84 Another SheetView may be passed in to create nested plots. 85 (matrix_data, bounding_box) 86 Static matrix data complete with bounding box. 87 (Sheet, sheet_view_name) 88 This gets sheet_name.sheet_view(sheet_view_name) each time 89 the current SheetView has its data requested by .view(). 90 91 (Sheet, sheet_view_name) 92 93 Degenerate case that will pull data from another SheetView 94 and not do any additional processing. Don't yet know a 95 use for this case, but documented for possible future use. 96 """ 97 super(SheetView,self).__init__(**params) 98 99 self.src_name = src_name 100 self.precedence = precedence 101 self.row_precedence = row_precedence 102 self.timestamp = timestamp 103 104 # Assume there's no such thing as an operator that can be mistaken 105 # for a matrix_data element. This is true as long as the real 106 # values of the operator keys are strings. 107 if term_1 not in operations.keys(): 108 self.operation = ADD 109 self._view_list = [(term_1, term_2)] 110 else: 111 self.operation = operations[term_1] 112 self._view_list = term_2 113 if not isinstance(self._view_list,types.ListType): 114 self._view_list = [self._view_list]
115 116
117 - def view(self):
118 """ 119 Return the requested view as a (matrix, bbox) tuple. 120 121 If the constructor was given multiple maps, the view must be 122 built before being returned, which may lock in new views of 123 data from the specified sheets. 124 125 Inputs are in the variable self._view_list which is a list of 126 tuples, with each tuple being a matrix and a bounding box, or 127 a sheet and a map name. The sequence cannot be dumped into 128 maps just once because the raw maps may have changed, so other 129 sheets must be queried for the data repeatedly. 130 """ 131 maps = [] 132 for tup in self._view_list: 133 (term_1, term_2) = tup 134 if isinstance(term_1,sheet.Sheet): 135 maps.append(term_1.sheet_views[term_2]) 136 elif isinstance(term_1,SheetView): 137 maps.append(term_1.view()) # Don't care about term_2 138 else: 139 maps.append((term_1, term_2)) # Assume it is a matrix 140 141 # Convert the list of (matrix, bbox) tuples into a single 142 # matrix and another bounding box. 143 return self.sum_maps(maps)
144 145
146 - def sum_maps(self,maps):
147 """ 148 Convert the list of (matrix, bbox) tuples into a single matrix 149 and another bounding box. Not a protected function as it could 150 prove useful to other areas of the simulator. 151 152 THIS MUST BE EXPANDED IN THE FUTURE TO MAKE PROPER USE OF THE 153 BOUNDING BOX INFORMATION. CURRENT (8/04) IMPLEMENTATION 154 PASSES THE FIRST MAP IN THE LIST AS THE BOUNDING BOX FOR THE 155 CONSTRUCTED VIEW. 156 157 WOULD HAVE DONE AN ADD/INTERSECTION/UNION, BUT BOUNDINGREGION 158 DOES NOT YET SUPPORT SUCH OPERATIONS. 159 """ 160 result = maps.pop(0) 161 for m in maps: 162 # Needs to be changed 163 result = (apply(self.operation, (result[0], m[0])), result[1]) 164 return result
165 166 167
168 -class UnitView(SheetView):
169 """ 170 SPRING 2005: Currently does not extend any functions, but can do 171 so to add outlines, and other interesting features. 172 173 Consists of an X,Y position for the unit that this View is 174 created for. Subclasses the SheetView class. 175 176 UnitViews should be stored in Sheets via a tuple 177 ('Weights',Sheet,Projection,X,Y). The dictionary in Sheets can be 178 accessed by any valid key. 179 """ 180 181 ### JCALERT! UnitView (as well as SheetView have to be reviewed.
182 - def __init__(self, term_tuple, x, y, projection, timestamp, **params):
183 """ 184 Subclass of SheetView. Contains additional x,y member data. 185 """ 186 super(UnitView,self).__init__(term_tuple, projection.src.name, projection.src.precedence, timestamp = timestamp, row_precedence = projection.src.row_precedence, **params) 187 self.x = x 188 self.y = y 189 self.projection = projection
190 191
192 -class ProjectionView(SheetView):
193 """ 194 ProjectionViews should be stored in Sheets via a tuple 195 ('Weights',Sheet,Projection). 196 """ 197
198 - def __init__(self, term_tuple,projection,timestamp,**params):
199 """ 200 Subclass of SheetView. 201 """ 202 super(ProjectionView,self).__init__(term_tuple, projection.src.name, projection.src.precedence, timestamp, row_precedence = projection.src.row_precedence, **params) 203 self.projection = projection
204