def grid(self, rows=11, cols=11, lbrt=None, situated=False, **kwargs): xdensity, ydensity = self.dest.xdensity, self.dest.ydensity l, b, r, t = self.dest.bounds.lbrt() half_x_unit = ((r - l) / xdensity) / 2. half_y_unit = ((t - b) / ydensity) / 2. if lbrt is None: l, b, r, t = (l + half_x_unit, b + half_y_unit, r - half_x_unit, t - half_y_unit) else: l, b = self.dest.closest_cell_center(lbrt[0], lbrt[1]) r, t = self.dest.closest_cell_center(lbrt[2], lbrt[3]) x, y = np.meshgrid(np.linspace(l, r, cols), np.linspace(b, t, rows)) coords = zip(x.flat, y.flat) grid_items = {} for x, y in coords: grid_items[x, y] = self.view(x, y, situated=situated, **kwargs) grid = GridSpace(grid_items, label=' '.join([self.dest.name, self.name]), group='CFs') grid.metadata = AttrDict(info=self.name, proj_src_name=self.src.name, proj_dest_name=self.dest.name, timestamp=self.src.simulation.time(), **kwargs) return grid
def update_sheet_activity(sheet_name, force=False): """ Update the '_activity_buffer' ViewMap for a given sheet by name. If force is False and the existing Activity Image isn't stale, the existing view is returned. """ name = 'ActivityBuffer' sheet = topo.sim.objects(Sheet)[sheet_name] view = sheet.views.Maps.get(name, False) time = topo.sim.time() metadata = AttrDict(precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name, shape=sheet.activity.shape, timestamp=time) if not view: im = Image(np.array(sheet.activity), sheet.bounds) im.metadata = metadata view = HoloMap((time, im), key_dimensions=[Time]) view.metadata = metadata sheet.views.Maps[name] = view else: if force or view.range('Time')[1] < time: im = Image(np.array(sheet.activity), sheet.bounds) im.metadata = metadata view[time] = im return view
def _cf_grid(self, shape=None, **kwargs): "Create GridSpace with the correct metadata." grid = GridSpace({}) grid.metadata = AttrDict(timestamp=self.src.simulation.time(), info=self.name, proj_src_name=self.src.name, proj_dest_name=self.dest.name, **kwargs) return grid
def __getitem__(self, coords): metadata = AttrDict(precedence=self.precedence, row_precedence=self.row_precedence, timestamp=self.simulation.time()) image = Image(self.activity.copy(), self.bounds, label=self.name, group='Activity')[coords] image.metadata = metadata return image
def __getitem__(self, coords): metadata = AttrDict(precedence=self.precedence, row_precedence=self.row_precedence, timestamp=self.simulation.time()) sv = Matrix(self.activity.copy(), self.bounds, label=self.name + ' Activity', value='Activity')[coords] sv.metadata = metadata return sv
def view(self, sheet_x, sheet_y, timestamp=None, situated=False, **kwargs): """ Return a single connection field Image, for the unit located nearest to sheet coordinate (sheet_x,sheet_y). """ if timestamp is None: timestamp = self.src.simulation.time() time_dim = Dimension("Time", type=param.Dynamic.time_fn.time_type) (r, c) = self.dest.sheet2matrixidx(sheet_x, sheet_y) cf = self.cfs[r, c] r1, r2, c1, c2 = cf.input_sheet_slice situated_shape = self.src.activity.shape situated_bounds = self.src.bounds roi_bounds = cf.get_bounds(self.src) if situated: matrix_data = np.zeros(situated_shape, dtype=np.float64) matrix_data[r1:r2, c1:c2] = cf.weights.copy() bounds = situated_bounds else: matrix_data = cf.weights.copy() bounds = roi_bounds sv = CFView(matrix_data, bounds, situated_bounds=situated_bounds, input_sheet_slice=(r1, r2, c1, c2), roi_bounds=roi_bounds, label=self.name, group='CF Weight') sv.metadata = AttrDict(timestamp=timestamp) viewmap = HoloMap((timestamp, sv), kdims=[time_dim]) viewmap.metadata = AttrDict(coords=(sheet_x, sheet_y), dest_name=self.dest.name, precedence=self.src.precedence, proj_name=self.name, src_name=self.src.name, row_precedence=self.src.row_precedence, timestamp=timestamp, **kwargs) return viewmap
def projection_view(self, timestamp=None): """Returns the activity in a single projection""" if timestamp is None: timestamp = self.src.simulation.time() im = Image(self.activity.copy(), self.dest.bounds, label=self.name, group='Activity') im.metadata=AttrDict(proj_src_name=self.src.name, precedence=self.src.precedence, proj_name=self.name, row_precedence=self.src.row_precedence, src_name=self.dest.name, timestamp=timestamp) return im
def projection_view(self, timestamp=None): """Returns the activity in a single projection""" if timestamp is None: timestamp = self.src.simulation.time() sv = Matrix(self.activity.copy(), self.dest.bounds, label='Activity', title='%s {label}' % self.name) sv.metadata=AttrDict(proj_src_name=self.src.name, precedence=self.src.precedence, proj_name=self.name, row_precedence=self.src.row_precedence, src_name=self.dest.name, timestamp=timestamp) return sv
def __getitem__(self, coords): metadata = AttrDict(precedence=self.precedence, row_precedence=self.row_precedence, timestamp=self.simulation.time()) if self._channel_data: arr = np.dstack(self._channel_data) else: arr = self.activity.copy() im = Image(arr, self.bounds, label=self.name+' Activity', group='Activity')[coords] im.metadata=metadata return im
def topo_metadata_fn(input_names=[], output_names=[]): """ Return the shapes of the specified GeneratorSheets and measurement sheets, or if none are specified return all that can be found in the simulation. """ metadata = AttrDict() metadata['timestamp'] = topo.sim.time() generator_sheets = topo.sim.objects(GeneratorSheet) all_sheets = dict((n, s) for n, s in topo.sim.objects(Sheet).items()) measurement_sheets = dict((n, s) for n, s in topo.sim.objects(Sheet).items() if hasattr(s, 'measure_maps') and s.measure_maps) projections = dict((conn.name, conn) for conn in topo.sim.connections()) if input_names == []: input_names = generator_sheets.keys() metadata['inputs'] = {} for i in input_names: if i in generator_sheets: gs = generator_sheets[i] metadata['inputs'][i] = { 'bounds': gs.bounds, 'precedence': gs.precedence, 'row_precedence': gs.row_precedence, 'shape': gs.shape, 'src_name': gs.name } else: topo.sim.warning('Input sheet {0} not found.'.format(i)) if output_names == []: output_names = measurement_sheets.keys() metadata['outputs'] = {} for o in output_names: if o in all_sheets: s = all_sheets[o] metadata['outputs'][o] = { 'bounds': s.bounds, 'precedence': s.precedence, 'row_precedence': s.row_precedence, 'shape': s.shape, 'src_name': s.name } elif o in projections: p = projections[o] metadata['outputs'][o] = { 'bounds': p.dest.bounds, 'precedence': p.dest.precedence, 'row_precedence': p.dest.row_precedence, 'shape': p.dest.shape, 'dest_name': p.dest.name, 'src_name': p.src.name } else: topo.sim.warning('Output sheet {0} not found.'.format(o)) return metadata
def setUp(self): ### Simple case: we only pass a dictionary to Plot() ### that does not belong to a Sheet: views = {} time = 0 metadata = AttrDict(timestamp=time) ### SheetView1: ### Find a way to assign randomly the matrix. self.matrix1 = np.zeros((10,10),dtype=np.float) + np.random.random((10,10)) self.bounds1 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = Matrix(self.matrix1, self.bounds1) sv.metadata=metadata self.sheet_view1 = NdMapping((None, sv)) self.sheet_view1.metadata = AttrDict(src_name='TestInputParam', precedence=0.1, row_precedence=0.1, cyclic_range=None, timestamp=time) self.key1 = 'SV1' views[self.key1] = self.sheet_view1 ### SheetView2: ### Find a way to assign randomly the matrix. self.matrix2 = np.zeros((10,10),dtype=np.float) + 0.3 self.bounds2 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = Matrix(self.matrix2, self.bounds2) sv.metadata=metadata self.sheet_view2 = NdMapping((None, sv)) self.sheet_view2.metadata = AttrDict(src_name='TestInputParam', precedence=0.2, row_precedence=0.2, cyclic_range=None, timestamp=time) self.key2 = 'SV2' views[self.key2] = self.sheet_view2 ### SheetView3: ### Find a way to assign randomly the matrix. self.matrix3 = np.zeros((10,10),dtype=np.float) + np.random.random((10,10)) self.bounds3 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = Matrix(self.matrix3, self.bounds3) sv.metadata=metadata self.sheet_view3 = NdMapping((None, sv)) self.sheet_view3.metadata = AttrDict(src_name='TestInputParam', precedence=0.3, row_precedence=0.3, cyclic_range=None, timestamp=time) self.key3 = 'SV3' views[self.key3] = self.sheet_view3 ### SheetView4: for testing clipping + different bounding box ### Find a way to assign randomly the matrix. self.matrix4 = np.zeros((10,10),dtype=np.float) + 1.6 self.bounds4 = BoundingBox(points=((-0.7,-0.7),(0.7,0.7))) sv = Matrix(self.matrix4, self.bounds4) sv.metadata=metadata self.sheet_view4 = NdMapping((None, sv)) self.sheet_view4.metadata = AttrDict(src_name='TestInputParam', precedence=0.4, row_precedence=0.4, cyclic_range=None, timestamp=time) self.key4 = 'SV4' views[self.key4] = self.sheet_view4 self.view_dict = {'Strength': views, 'Hue': views, 'Confidence': views} ### JCALERT! for the moment we can only pass a triple when creating plot ### adding more sheetView to test when plot will be fixed for accepting ### as much as you want. # plot0: empty plot + no sheetviewdict passed: error or empty plot? ### JCALERT! It has to be fixed what to do in this case in plot.. ### disabled test for the moment. #self.plot0 = Plot((None,None,None),None,name='plot0') ### CATCH EXCEPTION plot_channels1 = {'Strength':None,'Hue':None,'Confidence':None} # plot1: empty plot self.plot1 = make_template_plot(plot_channels1,self.view_dict,density=10.0,name='plot1') plot_channels2 = {'Strength':self.key1,'Hue':None,'Confidence':None} # plot2: sheetView 1, no normalize, no clipping self.plot2 = make_template_plot(plot_channels2,self.view_dict,density=10.0,name='plot2') plot_channels3 = {'Strength':self.key1,'Hue':self.key2,'Confidence':None} # plot3: sheetView 1+2, no normalize, no clipping self.plot3 = make_template_plot(plot_channels3,self.view_dict,density=10.0,name='plot3') plot_channels4 = {'Strength':self.key1,'Hue':self.key2,'Confidence':self.key3} # plot4: sheetView 1+2+3, no normalize , no clipping self.plot4 = make_template_plot(plot_channels4,self.view_dict,density=10.0,name='plot4') plot_channels5 = {'Strength':self.key1,'Hue':None,'Confidence':self.key3} # plot5: sheetView 1+3, no normalize, no clipping self.plot5 = make_template_plot(plot_channels5,self.view_dict,density=10.0,name='plot5') plot_channels6 = {'Strength':None,'Hue':self.key2,'Confidence':self.key3} # plot6: sheetView 2+3, no normalize , no clipping self.plot6 = make_template_plot(plot_channels6,self.view_dict,density=10.0,name='plot6') plot_channels7 = {'Strength':self.key4,'Hue':self.key2,'Confidence':self.key3} # plot7: sheetView 1+2+3, no normalize , clipping self.plot7 = make_template_plot(plot_channels7,self.view_dict,density=10.0,name='plot7') plot_channels8 = {'Strength':self.key1,'Hue':self.key2,'Confidence':self.key3} # plot8: sheetView 1+2+3, normalize , no clipping self.plot8 = make_template_plot(plot_channels8,self.view_dict,density=10.0,normalize=True,name='plot8') ### JCALERT! FOR THE MOMENT I TAKE THE DEFAULT FOR NORMALIZE. ### WE WILL SEE IF IT REMAINS IN PLOT FIRST. ### also makes a sheet to test realease_sheetviews self.sheet = Sheet() self.sheet.views.Maps[self.key1]=self.sheet_view1 self.sheet.views.Maps[self.key2]=self.sheet_view2 self.sheet.views.Maps[self.key3]=self.sheet_view3 self.sheet.views.Maps[self.key4]=self.sheet_view4 plot_channels9 = {'Strength':self.key1,'Hue':self.key2,'Confidence':self.key3} self.plot9 = make_template_plot(plot_channels9,self.sheet.views.Maps,density=10.0,name='plot9')