def _generate_plots(self): dynamic_plots = [] for kw in [dict(sheet=sheet) for sheet in self.sheets()]: sheet = kw['sheet'] views = topo.sim.views[sheet.name].Maps sv = SheetView(sheet.input_generator(), bounds=sheet.bounds) sv.metadata=AttrDict(timestamp=topo.sim.time()) if 'Activity' not in views: views['Activity'] = NdMapping((topo.sim.time(), sv)) views['Activity'].metadata = AttrDict(precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name, timestamp=topo.sim.time()) else: views['Activity'][topo.sim.time()] = sv channels = {'Strength': 'Activity','Hue':None,'Confidence':None} view = topo.sim.views[sheet.name].Maps view_dict = {'Strength':view,'Hue':view,'Confidence':view} ### JCALERT! it is not good to have to pass '' here... maybe a test in plot would be better dynamic_plots.append(make_template_plot(channels, view_dict, sheet.xdensity,sheet.bounds,self.normalize, name='')) return self._static_plots[:]+dynamic_plots
def update_sheet_activity(sheet_name, force=False): """ Update the '_activity_buffer' SheetStack for a given sheet by name. If force is False and the existing Activity SheetView isn't stale, the existing view is returned. """ name = '_activity_buffer' sheet = topo.sim.objects(Sheet)[sheet_name] view = sheet.views.maps.get(name, False) time = topo.sim.time() metadata = AttrDict(bounds=sheet.bounds, precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name, shape=sheet.activity.shape, timestamp=time) if not view: sv = SheetView(np.array(sheet.activity), sheet.bounds, metadata=metadata) view = SheetStack((time, sv), dimensions=[Time], **metadata) sheet.views.maps[name] = view else: if force or view.timestamp < time: sv = SheetView(np.array(sheet.activity), sheet.bounds, metadata=metadata) view[time] = sv return view
def update_sheet_activity(sheet_name, force=False): """ Update the '_activity_buffer' SheetStack for a given sheet by name. If force is False and the existing Activity SheetView 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: sv = SheetView(np.array(sheet.activity), sheet.bounds) sv.metadata=metadata view = SheetStack((time, sv), dimensions=[Time]) view.metadata = metadata sheet.views.Maps[name] = view else: if force or view.dim_range('Time')[1] < time: sv = SheetView(np.array(sheet.activity), sheet.bounds) sv.metadata=metadata view[time] = sv return view
def _update_proj_cog(self, p, proj): """Measure the CoG of the specified projection and register corresponding SheetViews.""" sheet = proj.dest rows, cols = sheet.activity.shape xcog = np.zeros((rows, cols), np.float64) ycog = np.zeros((rows, cols), np.float64) for r in xrange(rows): for c in xrange(cols): cf = proj.cfs[r, c] r1, r2, c1, c2 = cf.input_sheet_slice row_centroid, col_centroid = centroid(cf.weights) xcentroid, ycentroid = proj.src.matrix2sheet( r1 + row_centroid + 0.5, c1 + col_centroid + 0.5) xcog[r][c] = xcentroid ycog[r][c] = ycentroid metadata = dict(precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name, dimension_labels=['Time'], key_type=[topo.sim.time.time_type]) timestamp = topo.sim.time() xsv = SheetView(xcog, sheet.bounds) ysv = SheetView(ycog, sheet.bounds) lines = [] hlines, vlines = xsv.data.shape for hind in range(hlines)[::p.stride]: lines.append(np.vstack([xsv.data[hind, :].T, ysv.data[hind, :]]).T) for vind in range(vlines)[::p.stride]: lines.append(np.vstack([xsv.data[:, vind].T, ysv.data[:, vind]]).T) xcog_stack = SheetStack((timestamp, xsv), **metadata) ycog_stack = SheetStack((timestamp, ysv), **metadata) contour_stack = SheetStack( (timestamp, SheetLines(lines, sheet.bounds)), **metadata) if 'XCoG' in sheet.views.maps: sheet.views.maps['XCoG'].update(xcog_stack) else: sheet.views.maps['XCoG'] = xcog_stack if 'YCoG' in sheet.views.maps: sheet.views.maps['YCoG'].update(ycog_stack) else: sheet.views.maps['YCoG'] = ycog_stack if 'CoG' in sheet.views.maps: sheet.views.maps['CoG'].update(contour_stack) else: sheet.views.maps['CoG'] = contour_stack return {'XCoG': xcog_stack, 'YCoG': ycog_stack, 'CoG': contour_stack}
def _set_sheet_views(instance, state): if state['simulation'] is None: return None name = state['_name_param_value'] if not hasattr(state['simulation'], 'views'): state['simulation'].views = AttrDict() if name not in state['simulation'].views: if hasattr(instance, 'views'): state['views'] = instance.views else: state['views'] = AttrDict() state['simulation'].views[name] = state['views'] views = state['views'] views['maps'] = AttrDict() views['cfs'] = AttrDict() views['rfs'] = AttrDict() views['curves'] = AttrDict() if 'sheet_views' in state: svs = state['sheet_views'] for key, sv in svs.items(): data, bounds = sv.view() new_sv = SheetView(data, bounds) metadata = dict(dimension_labels=['Time']) metadata_names = [ 'cyclic_range', 'precedence', 'row_precedence', 'src_name' ] for p in metadata_names: if hasattr(sv, p): metadata[p] = getattr(sv, p) state['views'].maps[key] = NdMapping((sv.timestamp, new_sv), **metadata) if 'curve_dict' in state: old_curves = state['curve_dict'] curves = views['curves'] for key, value in old_curves.items(): key = key.capitalize() for label, item in value.items(): labels = unit_value(label) label_name = labels[0].split(' ')[0] l_val = labels[-1] if key not in views['curves']: curves[key] = NdMapping(dimension_labels=['Time']) for f_val, old_sv in item.items(): timestamp = old_sv.timestamp curves[key][timestamp] = NdMapping( dimension_labels=[label_name]) if l_val not in curves[key][timestamp].keys(): curves[key][timestamp][l_val] = NdMapping( dimension_labels=[key], label=label, timestamp=old_sv.timestamp) data, bounds = old_sv.view() sv = SheetView(data, bounds) curves[key][timestamp][l_val][f_val] = sv state.pop('curve_dict', None) state.pop('sheet_views', None)
def test_init(self): sv1 = SheetView(self.s.activity, self.s.bounds) # s.sheet_view() returns a SheetView self.s.views.maps['sv1'] = sv1 sv2 = SheetView(self.s.activity, self.s.bounds) # s.sheet_view() returns a SheetView self.s.views.maps['sv2'] = sv2 # Define a type 1 SheetView, with matrix and bounding box. sv3 = SheetView(self.s.activity, self.s.bounds) sv4 = SheetView(self.s2.activity, self.s2.bounds)
def projection_view(self, timestamp=None): """Returns the activity in a single projection""" if timestamp is None: timestamp = self.src.simulation.time() sv = SheetView(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 _update_proj_cog(self, p, proj): """Measure the CoG of the specified projection and register corresponding SheetViews.""" sheet = proj.dest rows, cols = sheet.activity.shape xcog = np.zeros((rows, cols), np.float64) ycog = np.zeros((rows, cols), np.float64) for r in xrange(rows): for c in xrange(cols): cf = proj.cfs[r, c] r1, r2, c1, c2 = cf.input_sheet_slice row_centroid, col_centroid = centroid(cf.weights) xcentroid, ycentroid = proj.src.matrix2sheet( r1 + row_centroid + 0.5, c1 + col_centroid + 0.5) xcog[r][c] = xcentroid ycog[r][c] = ycentroid metadata = AttrDict(precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name) timestamp = topo.sim.time() xsv = SheetView(xcog, sheet.bounds, label='X CoG', title='%s {label}' % sheet.name) ysv = SheetView(ycog, sheet.bounds, label='Y CoG', title='%s {label}' % sheet.name) lines = [] hlines, vlines = xsv.data.shape for hind in range(hlines)[::p.stride]: lines.append(np.vstack([xsv.data[hind, :].T, ysv.data[hind, :]]).T) for vind in range(vlines)[::p.stride]: lines.append(np.vstack([xsv.data[:, vind].T, ysv.data[:, vind]]).T) cogmesh = Contours(lines, sheet.bounds, label='Center of Gravity') xcog_stack = SheetStack((timestamp, xsv), dimensions=[features.Time]) xcog_stack.metadata = metadata ycog_stack = SheetStack((timestamp, ysv), dimensions=[features.Time]) ycog_stack.metadata = metadata contour_stack = SheetStack((timestamp, cogmesh), dimensions=[features.Time]) contour_stack.metadata = metadata return {'XCoG': xcog_stack, 'YCoG': ycog_stack, 'CoG': contour_stack}
def update_rgb_activities(): """ Make available Red, Green, and Blue activity matrices for all appropriate sheets. """ for sheet in topo.sim.objects(Sheet).values(): metadata = AttrDict(src_name=sheet.name, precedence=sheet.precedence, row_precedence=sheet.row_precedence, timestamp=topo.sim.time()) for c in ['Red','Green','Blue']: # should this ensure all of r,g,b are present? if hasattr(sheet,'activity_%s'%c.lower()): activity_copy = getattr(sheet,'activity_%s'%c.lower()).copy() new_view = SheetView(activity_copy, bounds=sheet.bounds) new_view.metadata=metadata sheet.views.Maps['%sActivity'%c]=new_view
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() sv = SheetView(arr, self.bounds, label=self.name + ' Activity', value='Activity')[coords] sv.metadata = metadata return sv
def __getitem__(self, coords): metadata = AttrDict(precedence=self.precedence, row_precedence=self.row_precedence, timestamp=self.simulation.time()) return SheetView(self.activity.copy(), self.bounds, title=self.name, metadata=metadata)[coords]
def view_depth_map(self, mode='both'): """ Visualize the depth map using dataviews, including distribution histograms. Mode may be one of 'discrete', 'raw' or 'both': * The 'discrete' mode presents the depth map used by TemporalScatter, showing the latency at which TemporalScatter will propagate the input i.e. discrete, positive latencies in milliseconds. * The 'raw' mode shows the continuous distribution before discretization. This is typically a zero-mean, zero-centered distribution i.e a continuous, zero-centered latency distribution. * Both presents both of the above types together (default). """ views = [] if mode in ['raw', 'both']: views.append( SheetView(self.raw_depth_map, label='Pattern', name='Raw Depth map').hist()) if mode in ['discrete', 'both']: scaled_map = (self.depth_map * self.timestep) discrete_sv = SheetView(scaled_map, label='Pattern', name='Depth map') views.append( discrete_sv.hist(num_bins=self.depth, bin_range=(0, self.span))) return views[0] if len(views) == 1 else views[0] + views[1]
def _generate_plots(self): dynamic_plots = [] for kw in [dict(sheet=sheet) for sheet in self.sheets()]: sheet = kw['sheet'] views = topo.sim.views[sheet.name].Maps sv = SheetView(sheet.input_generator(), bounds=sheet.bounds) sv.metadata = AttrDict(timestamp=topo.sim.time()) if 'Activity' not in views: views['Activity'] = NdMapping((topo.sim.time(), sv)) views['Activity'].metadata = AttrDict( precedence=sheet.precedence, row_precedence=sheet.row_precedence, src_name=sheet.name, timestamp=topo.sim.time()) else: views['Activity'][topo.sim.time()] = sv channels = { 'Strength': 'Activity', 'Hue': None, 'Confidence': None } view = topo.sim.views[sheet.name].Maps view_dict = {'Strength': view, 'Hue': view, 'Confidence': view} ### JCALERT! it is not good to have to pass '' here... maybe a test in plot would be better dynamic_plots.append( make_template_plot(channels, view_dict, sheet.xdensity, sheet.bounds, self.normalize, name='')) return self._static_plots[:] + dynamic_plots
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 = zeros((10,10),Float) + RandomArray.random((10,10)) self.bounds1 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = SheetView(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 = zeros((10,10),Float) + 0.3 self.bounds2 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = SheetView(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',0,10) views[self.key2] = self.sheet_view2 ### SheetView3: ### Find a way to assign randomly the matrix. self.matrix3 = zeros((10,10),Float) + RandomArray.random((10,10)) self.bounds3 = BoundingBox(points=((-0.5,-0.5),(0.5,0.5))) sv = SheetView(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',0,'hello',(10,0)) views[self.key3] = self.sheet_view3 ### SheetView4: for testing clipping + different bounding box ### Find a way to assign randomly the matrix. self.matrix4 = zeros((10,10),Float) + 1.6 self.bounds4 = BoundingBox(points=((-0.7,-0.7),(0.7,0.7))) sv = SheetView(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')
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 = zeros((10, 10), Float) + RandomArray.random((10, 10)) self.bounds1 = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) sv = SheetView(self.matrix1, self.bounds1, metadata=metadata) self.sheet_view1 = NdMapping((None, sv), 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 = zeros((10, 10), Float) + 0.3 self.bounds2 = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) sv = SheetView(self.matrix2, self.bounds2, metadata=metadata) self.sheet_view2 = NdMapping((None, sv), src_name='TestInputParam', precedence=0.2, row_precedence=0.2, cyclic_range=None, timestamp=time) self.key2 = ('sv2', 0, 10) views[self.key2] = self.sheet_view2 ### SheetView3: ### Find a way to assign randomly the matrix. self.matrix3 = zeros((10, 10), Float) + RandomArray.random((10, 10)) self.bounds3 = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) sv = SheetView(self.matrix3, self.bounds3, metadata=metadata) self.sheet_view3 = NdMapping((None, sv), src_name='TestInputParam', precedence=0.3, row_precedence=0.3, cyclic_range=None, timestamp=time) self.key3 = ('sv3', 0, 'hello', (10, 0)) views[self.key3] = self.sheet_view3 ### SheetView4: for testing clipping + different bounding box ### Find a way to assign randomly the matrix. self.matrix4 = zeros((10, 10), Float) + 1.6 self.bounds4 = BoundingBox(points=((-0.7, -0.7), (0.7, 0.7))) sv = SheetView(self.matrix4, self.bounds4, metadata=metadata) self.sheet_view4 = NdMapping((None, sv), 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')