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 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 __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 _collate_results(self, p): results = Layout() timestamp = self.metadata.timestamp axis_name = p.x_axis.capitalize() axis_feature = [f for f in self.features if f.name.lower() == p.x_axis][0] if axis_feature.cyclic: axis_feature.values.append(axis_feature.range[1]) curve_label = ''.join([p.measurement_prefix, axis_name, 'Tuning']) dimensions = [features.Time, features.Duration] + [f for f in self.outer] + [axis_feature] pattern_dimensions = self.outer + self.inner pattern_dim_label = '_'.join(f.name.capitalize() for f in pattern_dimensions) for label in self.measurement_product: # Deconstruct label into source name and feature_values name = label[0] f_vals = label[1:] # Get data and metadata from the DistributionMatrix objects dist_matrix = self._featureresponses[name][f_vals][p.x_axis] curve_responses = dist_matrix.distribution_matrix output_metadata = self.metadata.outputs[name] rows, cols = output_metadata['shape'] # Create top level NdMapping indexing over time, duration, the outer # feature dimensions and the x_axis dimension if (curve_label, name) not in results: vmap = HoloMap(kdims=dimensions, group=curve_label, label=name) vmap.metadata = AttrDict(**output_metadata) results.set_path((curve_label, name), vmap) metadata = AttrDict(timestamp=timestamp, **output_metadata) # Populate the ViewMap with measurements for each x value for x in curve_responses[0, 0]._data.iterkeys(): y_axis_values = np.zeros(output_metadata['shape'], activity_dtype) for i in range(rows): for j in range(cols): y_axis_values[i, j] = curve_responses[i, j].get_value(x) key = (timestamp,)+f_vals+(x,) im = Image(y_axis_values, bounds=output_metadata['bounds'], label=name, group=' '.join([curve_label, 'Response']), vdims=['Response']) im.metadata = metadata.copy() results[(curve_label, name)][key] = im if axis_feature.cyclic and x == axis_feature.range[0]: symmetric_key = (timestamp,)+f_vals+(axis_feature.range[1],) results[(curve_label, name)][symmetric_key] = im if p.store_responses: info = (p.pattern_generator.__class__.__name__, pattern_dim_label, 'Response') results.set_path(('%s_%s_%s' % info, name), self._responses[name]) return results
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 test_sheetview_release(self): s = Sheet() s.activity = np.array([[1, 2], [3, 4]]) # Call s.sheet_view(..) with a parameter im2 = Image(s.activity, bounds=s.bounds) im2.metadata = dict(src_name=s.name) self.assertEqual(len(s.views.Maps.keys()), 0) s.views.Maps["Activity"] = im2 self.assertEqual(len(s.views.Maps.keys()), 1) s.release_sheet_view("Activity") self.assertEqual(len([v for v in s.views.Maps.values() if v is not None]), 0)
def test_sheetview_release(self): s = Sheet() s.activity = np.array([[1, 2], [3, 4]]) # Call s.sheet_view(..) with a parameter im2 = Image(s.activity, bounds=s.bounds) im2.metadata = dict(src_name=s.name) self.assertEqual(len(s.views.Maps.keys()), 0) s.views.Maps['Activity'] = im2 self.assertEqual(len(s.views.Maps.keys()), 1) s.release_sheet_view('Activity') self.assertEqual( len([v for v in s.views.Maps.values() if v is not None]), 0)
def _collate_results(self, p): """ Collate responses into the results dictionary containing a ProjectionGrid for each measurement source. """ results = Layout() timestamp = self.metadata.timestamp dimensions = [features.Time, features.Duration] pattern_dimensions = self.outer + self.inner pattern_dim_label = '_'.join(f.name.capitalize() for f in pattern_dimensions) grids, responses = {}, {} for labels in self.measurement_product: in_label, out_label, duration = labels input_metadata = self.metadata.inputs[in_label] output_metadata = self.metadata.outputs[out_label] rows, cols, scs = self._compute_roi(p, output_metadata) time_key = (timestamp, duration) grid_key = (in_label, out_label) if grid_key not in grids: if p.store_responses: responses[in_label] = self._responses[in_label] responses[out_label] = self._responses[out_label] grids[grid_key] = GridSpace(group='RFs', label=out_label) view = grids[grid_key] rc_response = self._featureresponses[in_label][out_label][duration] for i, ii in enumerate(rows): for j, jj in enumerate(cols): coord = scs.matrixidx2sheet(ii, jj) im = Image(rc_response[i, j], bounds=input_metadata['bounds'], label=out_label, group='Receptive Field', vdims=['Weight']) im.metadata = AttrDict(timestamp=timestamp) if coord in view: view[coord][time_key] = im else: view[coord] = HoloMap((time_key, im), kdims=dimensions, label=out_label, group='Receptive Field') view[coord].metadata = AttrDict(**input_metadata) for (in_label, out_label), view in grids.items(): results.set_path(('%s_Reverse_Correlation' % in_label, out_label), view) if p.store_responses: info = (p.pattern_generator.__class__.__name__, pattern_dim_label, 'Response') results.set_path(('%s_%s_%s' % info, in_label), responses[in_label]) results.set_path(('%s_%s_%s' % info, out_label), responses[out_label]) return results
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 __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 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 = Image(activity_copy, bounds=sheet.bounds) new_view.metadata=metadata sheet.views.Maps['%sActivity'%c]=new_view
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 = Image(activity_copy, bounds=sheet.bounds) new_view.metadata = metadata sheet.views.Maps['%sActivity' % c] = new_view
def _collate_results(self, p): results = Layout() timestamp = self.metadata.timestamp # Generate dimension info dictionary from features dimensions = [features.Time, features.Duration] + self.outer pattern_dimensions = self.outer + self.inner pattern_dim_label = '_'.join(f.name.capitalize() for f in pattern_dimensions) for label in self.measurement_product: name = label[0] # Measurement source f_vals = label[1:] # Duration and outer feature values #Metadata inner_features = dict([(f.name, f) for f in self.inner]) output_metadata = dict(self.metadata.outputs[name], inner_features=inner_features) # Iterate over inner features fr = self._featureresponses[name][f_vals] for fname, fdist in fr.items(): feature = fname.capitalize() base_name = self.measurement_prefix + feature # Get information from the feature fp = [f for f in self.features if f.name.lower() == fname][0] pref_fn = fp.preference_fn if has_preference_fn(fp)\ else self.preference_fn if p.selectivity_multiplier is not None: pref_fn.selectivity_scale = (pref_fn.selectivity_scale[0], p.selectivity_multiplier) # Get maps and iterate over them response = fdist.apply_DSF(pref_fn) for k, maps in response.items(): for map_name, map_view in maps.items(): # Set labels and metadata map_index = base_name + k + map_name.capitalize() map_label = ' '.join([base_name, map_name.capitalize()]) cyclic = (map_name != 'selectivity' and fp.cyclic) fprange = fp.range if cyclic else (None, None) value_dimension = Dimension(map_label, cyclic=cyclic, range=fprange) self._set_style(fp, map_name) # Create views and stacks im = Image(map_view, bounds=output_metadata['bounds'], label=name, group=map_label, vdims=[value_dimension]) im.metadata=AttrDict(timestamp=timestamp) key = (timestamp,)+f_vals if (map_label.replace(' ', ''), name) not in results: vmap = HoloMap((key, im), kdims=dimensions, label=name, group=map_label) vmap.metadata = AttrDict(**output_metadata) results.set_path((map_index, name), vmap) else: results.path_items[(map_index, name)][key] = im if p.store_responses: info = (p.pattern_generator.__class__.__name__, pattern_dim_label, 'Response') results.set_path(('%s_%s_%s' % info, name), self._responses[name]) return results
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))) im = Image(self.matrix1, self.bounds1) im.metadata=metadata self.sheet_view1 = NdMapping((None, im)) self.sheet_view1.metadata = AttrDict(src_name='TestInputParam', precedence=0.1, row_precedence=0.1, cyclic_range=None, timestamp=time) self.key1 = 'IM1' 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))) im = Image(self.matrix2, self.bounds2) im.metadata=metadata self.sheet_view2 = NdMapping((None, im)) self.sheet_view2.metadata = AttrDict(src_name='TestInputParam', precedence=0.2, row_precedence=0.2, cyclic_range=None, timestamp=time) self.key2 = 'IM2' 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))) im = Image(self.matrix3, self.bounds3) im.metadata=metadata self.sheet_view3 = NdMapping((None, im)) self.sheet_view3.metadata = AttrDict(src_name='TestInputParam', precedence=0.3, row_precedence=0.3, cyclic_range=None, timestamp=time) self.key3 = 'IM3' 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))) im = Image(self.matrix4, self.bounds4) im.metadata=metadata self.sheet_view4 = NdMapping((None, im)) self.sheet_view4.metadata = AttrDict(src_name='TestInputParam', precedence=0.4, row_precedence=0.4, cyclic_range=None, timestamp=time) self.key4 = 'IM4' 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 = np.zeros((10, 10), dtype=np.float) + np.random.random( (10, 10)) self.bounds1 = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) im = Image(self.matrix1, self.bounds1) im.metadata = metadata self.sheet_view1 = NdMapping((None, im)) self.sheet_view1.metadata = AttrDict(src_name='TestInputParam', precedence=0.1, row_precedence=0.1, cyclic_range=None, timestamp=time) self.key1 = 'IM1' 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))) im = Image(self.matrix2, self.bounds2) im.metadata = metadata self.sheet_view2 = NdMapping((None, im)) self.sheet_view2.metadata = AttrDict(src_name='TestInputParam', precedence=0.2, row_precedence=0.2, cyclic_range=None, timestamp=time) self.key2 = 'IM2' 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))) im = Image(self.matrix3, self.bounds3) im.metadata = metadata self.sheet_view3 = NdMapping((None, im)) self.sheet_view3.metadata = AttrDict(src_name='TestInputParam', precedence=0.3, row_precedence=0.3, cyclic_range=None, timestamp=time) self.key3 = 'IM3' 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))) im = Image(self.matrix4, self.bounds4) im.metadata = metadata self.sheet_view4 = NdMapping((None, im)) self.sheet_view4.metadata = AttrDict(src_name='TestInputParam', precedence=0.4, row_precedence=0.4, cyclic_range=None, timestamp=time) self.key4 = 'IM4' 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')