def __init__(self, **params): """ Initialize this object as an EventProcessor, then also as a SheetCoordinateSystem with equal xdensity and ydensity. views is a Layout, which stores associated measurements, i.e. representations of the sheet for use by analysis or plotting code. """ EventProcessor.__init__(self, **params) # Initialize this object as a SheetCoordinateSystem, with # the same density along y as along x. SheetCoordinateSystem.__init__(self, self.nominal_bounds, self.nominal_density) n_units = round((self.lbrt[2] - self.lbrt[0]) * self.xdensity, 0) if n_units < 1: raise ValueError( "Sheet bounds and density must be specified such that the "+ \ "sheet has at least one unit in each direction; " \ +self.name+ " does not.") # setup the activity matrix self.activity = zeros(self.shape, activity_type) # For non-plastic inputs self.__saved_activity = [] self._plasticity_setting_stack = [] self.views = Layout() self.views.Maps = Layout() self.views.Curves = Layout()
def __call__(self, **params): p = ParamOverrides(self, params) measured_sheets = [ s for s in topo.sim.objects(CFSheet).values() if hasattr(s, 'measure_maps') and s.measure_maps ] results = Layout() # Could easily be extended to measure CoG of all projections # and e.g. register them using different names (e.g. "Afferent # XCoG"), but then it's not clear how the PlotGroup would be # able to find them automatically (as it currently supports # only a fixed-named plot). requested_proj = p.proj_name for sheet in measured_sheets: for proj in sheet.in_connections: if (proj.name == requested_proj) or \ (requested_proj == '' and (proj.src != sheet)): cog_data = self._update_proj_cog(p, proj) for key, data in cog_data.items(): name = proj.name[0].upper() + proj.name[1:] results.set_path((key, name), data) if p.measurement_storage_hook: p.measurement_storage_hook(results) return results
def __call__(self, **params): p = ParamOverrides(self, params) measured_sheets = [s for s in topo.sim.objects(CFSheet).values() if hasattr(s,'measure_maps') and s.measure_maps] results = Layout() # Could easily be extended to measure CoG of all projections # and e.g. register them using different names (e.g. "Afferent # XCoG"), but then it's not clear how the PlotGroup would be # able to find them automatically (as it currently supports # only a fixed-named plot). requested_proj=p.proj_name for sheet in measured_sheets: for proj in sheet.in_connections: if (proj.name == requested_proj) or \ (requested_proj == '' and (proj.src != sheet)): cog_data = self._update_proj_cog(p, proj) for key, data in cog_data.items(): name = proj.name[0].upper() + proj.name[1:] results.set_path((key, name), data) if p.measurement_storage_hook: p.measurement_storage_hook(results) return results
def test_layout_update(self): tr1 = Layout() tr2 = Layout() tr1.Test1.Path1 = 42 tr2.Test2.Path2 = -42 tr1.update(tr2) self.assertEqual(tr1.Test1.Path1, 42) self.assertEqual(tr1.Test2.Path2, -42)
def test_layout_shallow_fixed_setter(self): tr = Layout() tr.fixed = True try: tr.Test = 42 raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error)
def test_layout_getter_fixed(self): tr = Layout() tr.fixed = True try: tr.Test.Path raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error)
def test_viewgroup_update(self): tr1 = Layout() tr2 = Layout() tr1.Test1.Path1 = 42 tr2.Test2.Path2 = -42 tr1.update(tr2) self.assertEqual(tr1.Test1.Path1, 42) self.assertEqual(tr1.Test2.Path2, -42)
def test_viewgroup_getter_fixed(self): tr = Layout() tr.fixed = True try: tr.Test.Path raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error)
def test_viewgroup_shallow_fixed_setter(self): tr = Layout() tr.fixed = True try: tr.Test = 42 raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error)
def _populate_grid(self, results): trees = [] for coord, viewgroup in results.items(): for path, stack in viewgroup.data.items(): grid_container = Layout() coord_map = stack.add_dimension(f.Y, 0, coord[1]) coord_map = coord_map.add_dimension(f.X, 0, coord[0]) grid_container.set_path(path, coord_map) trees.append(grid_container) return Layout.merge(trees)
def test_layout_toggle_fixed(self): tr = Layout() tr.fixed = True try: tr.Test = 42 raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error) tr.fixed = False tr.Test = 42
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 test_layout_overlay_overlay(self): t = (self.el1 + self.el2) * (self.el3 * self.el4) self.assertEqual( t, Layout([ self.el1 * self.el3 * self.el4, self.el2 * self.el3 * self.el4 ]))
def test_layout_overlay_overlay_reverse(self): t = (self.el3 * self.el4) * (self.el1 + self.el2) self.assertEqual( t, Layout([ self.el3 * self.el4 * self.el1, self.el3 * self.el4 * self.el2 ]))
def test_deep_map_apply_dmap_function(self): fn = lambda i: Curve(np.arange(i)) dmap1 = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))]) dmap2 = DynamicMap(fn, kdims=[Dimension('Test', range=(10, 20))]) mapped = (dmap1 + dmap2).map(lambda x: x[10], DynamicMap) self.assertEqual(mapped, Layout([('DynamicMap.I', fn(10)), ('DynamicMap.II', fn(10))]))
def __call__(self): """ Calls the collector specified by the user in the run_batch context. Invoked as an analysis function by RunBatchCommand. """ self.collector.interval_hook = topo.sim.run topo_time = topo.sim.time() filename = '%s%s_%s' % (self._info.batch_name, ('[%s]' % self._info.batch_tag if self._info.batch_tag else ''), topo_time) viewtree = Layout() viewtree = self.collector(viewtree, times=[topo_time]) spec_metadata = [(key, self._info.specs[key]) for key in self.metadata if '.' not in key] path_metadata = [(key, viewtree.items.get(tuple(key.split('.')), float('nan'))) for key in self.metadata if '.' in key] Pickler.save(viewtree, param.normalize_path(filename), key=dict(spec_metadata + path_metadata + [('time', topo_time)]))
def test_overlay_from_values_with_mixed_types(self): overlay1 = self.el1 + self.el4 + self.el7 overlay2 = self.el2 + self.el5 + self.el8 paths = Layout.from_values([overlay1, overlay2, self.el3]).keys() self.assertEqual(paths, [('Element', 'I'), ('ValA', 'I'), ('ValA', 'LabelA'), ('Element', 'II'), ('ValB', 'I'), ('ValA', 'LabelB'), ('Element', 'III')])
def test_layout_from_values_with_mixed_types(self): layout1 = self.el1 + self.el4 + self.el7 layout2 = self.el2 + self.el5 + self.el8 paths = Layout.from_values([layout1, layout2, self.el3]).keys() self.assertEqual(paths, [('Element', 'I'), ('ValA', 'I'), ('ValA', 'LabelA'), ('Element', 'II'), ('ValB', 'I'), ('ValA', 'LabelB'), ('Element', 'III')])
def test_layout_overlay_holomap_reverse(self): t = HoloMap({0: self.el3}) * (self.el1 + self.el2) self.assertEqual( t, Layout([ HoloMap({0: self.el3 * self.el1}), HoloMap({0: self.el3 * self.el2}) ]))
def test_layout_overlay_holomap(self): t = (self.el1 + self.el2) * HoloMap({0: self.el3}) self.assertEqual( t, Layout([ HoloMap({0: self.el1 * self.el3}), HoloMap({0: self.el2 * self.el3}) ]))
def test_pickle_with_data(self): tr = Layout() tr.Example1.Data = 42 tr.Example2.Data = 'some data' dumped = pickle.dumps(tr) tr2 = pickle.loads(dumped) self.assertEqual(tr.data, OrderedDict([(('Example1', 'Data'), 42), (('Example2', 'Data'), 'some data')])) self.assertEqual(tr.data, tr2.data)
def test_layout_title_fontsize(self): hmap1 = HoloMap({a: Image(np.random.rand(10, 10)) for a in range(3)}) hmap2 = HoloMap({a: Image(np.random.rand(10, 10)) for a in range(3)}) layout = Layout([hmap1, hmap2])(plot=dict(fontsize={'title': '12pt'})) plot = bokeh_renderer.get_plot(layout) title = plot.handles['title'] self.assertIsInstance(title, Div) text = "<span style='font-size: 12pt'><b>Default: 0</b></font>" self.assertEqual(title.text, text)
def test_pickler_save_load_single_layout(self): single_layout = Layout.from_values([self.image1]) Pickler.save(single_layout, 'test_pickler_save_load_single_layout', info={'info':'example'}, key={1:2}) entries = Unpickler.entries('test_pickler_save_load_single_layout.hvz') self.assertEqual(entries, ['Image.I(L)']) loaded = Unpickler.load('test_pickler_save_load_single_layout.hvz', entries=['Image.I(L)']) self.assertEqual(single_layout, loaded)
def test_pickler_save_load_single_layout(self): single_layout = Layout([self.image1]) Pickler.save(single_layout, 'test_pickler_save_load_single_layout', info={'info':'example'}, key={1:2}) entries = Unpickler.entries('test_pickler_save_load_single_layout.hvz') self.assertEqual(entries, ['Image.I(L)']) loaded = Unpickler.load('test_pickler_save_load_single_layout.hvz', entries=['Image.I(L)']) self.assertEqual(single_layout, loaded)
def _collate_results(self, responses, label): time = self.metadata.timestamp dims = [f.Time, f.Duration] response_label = label + ' Response' results = Layout() for label, response in responses.items(): name, duration = label path = (response_label.replace(' ', ''), name) label = ' '.join([name, response_label]) metadata = self.metadata['outputs'][name] if path not in results: vmap = HoloMap(key_dimensions=dims) vmap.metadata = AttrDict(**metadata) results.set_path(path, vmap) im = Image(response, metadata['bounds'], label=label, group='Activity') im.metadata=AttrDict(timestamp=time) results[path][(time, duration)] = im return results
def plot_function(data): nonlocal keys curves = [] keys = data.fields.data_vars if keys == "all" else keys for var in keys if not isinstance(keys, str) else [keys]: displayed_field = data.fields[var] if displayed_field.dims != ('x', ): continue curves.append(Curve((displayed_field.squeeze()))) return Layout(curves).cols(1)
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) 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) view = GridSpace(group='RFs', label=out_label) 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], input_metadata['bounds'], label=out_label, group='Receptive Field', value_dimensions=['Weight']) im.metadata = AttrDict(timestamp=timestamp) view[coord] = HoloMap((time_key, im), key_dimensions=dimensions, label=out_label, group='Receptive Field') view[coord].metadata = AttrDict(**input_metadata) 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, out_label), self._responses[out_label]) return results
def __init__(self, initialize_cfs=True, **params): """ Initialize the Projection with a set of cf_type objects (typically ConnectionFields), each located at the location in the source sheet corresponding to the unit in the target sheet. The cf_type objects are stored in the 'cfs' array. The nominal_bounds_template specified may be altered: the bounds must be fitted to the Sheet's matrix, and the weights matrix must have odd dimensions. These altered bounds are passed to the individual connection fields. A mask for the weights matrix is constructed. The shape is specified by cf_shape; the size defaults to the size of the nominal_bounds_template. """ super(CFProjection, self).__init__(**params) self.weights_generator.set_dynamic_time_fn(None, sublistattr='generators') # get the actual bounds_template by adjusting a copy of the # nominal_bounds_template to ensure an odd slice, and to be # cropped to sheet if necessary self._slice_template = Slice(copy(self.nominal_bounds_template), self.src, force_odd=True, min_matrix_radius=self.min_matrix_radius) self.bounds_template = self._slice_template.compute_bounds(self.src) self.mask_template = _create_mask(self.cf_shape, self.bounds_template, self.src, self.autosize_mask, self.mask_threshold) self.n_units = self._calc_n_units() if initialize_cfs: self._create_cfs() if self.apply_output_fns_init: self.apply_learn_output_fns(active_units_mask=False) ### JCALERT! We might want to change the default value of the ### input value to self.src.activity; but it fails, raising a ### type error. It probably has to be clarified why this is ### happening self.input_buffer = None self.activity = np.array(self.dest.activity) if 'cfs' not in self.dest.views: self.dest.views.CFs = Layout() self.dest.views.CFs[self.name] = self._cf_grid()
def test_viewgroup_toggle_fixed(self): tr = Layout() tr.fixed = True try: tr.Test = 42 raise AssertionError except AttributeError as e: self.assertEqual(str(e), self.fixed_error) tr.fixed = False tr.Test = 42
def _process(self, tree, key=None): preferences = tree[self.p.feature + 'Preference'] layout = Layout() for s, p in self.p.projections: if s not in preferences: continue featurepref = preferences[s] if isinstance(featurepref, HoloMap): featurepref = featurepref.last feature = featurepref.value_dimensions[0] feature_arr = featurepref.data.flat cfs = tree.CFs[p] deltas, weights = [], [] for k, cf in cfs.items(): preferred = featurepref[k] weight_arr = cf.situated.data.flat feature_slice = feature_arr[weight_arr > 0] weight_slice = weight_arr[weight_arr > 0] if feature.cyclic: feature_delta = circular_dist(preferred, feature_slice, feature.range[1]) else: feature_delta = np.abs(feature_slice - preferred) deltas.append(feature_delta) weights.append(weight_slice) deltas = np.concatenate(deltas) weights = np.concatenate(weights) bin_range = (0, feature.range[1] / 2.) if feature.cyclic else None bins, edges = np.histogram(deltas, range=bin_range, bins=self.p.num_bins, weights=weights, normed=self.p.normalized) # Construct Elements label = ' '.join([s, p]) group = '%s Weight Distribution' % self.p.feature histogram = Histogram( bins, edges, group=group, label=label, kdims=[' '.join([self.p.feature, 'Difference'])], vdims=['Weight']) layout.WeightDistribution['_'.join([s, p])] = histogram return layout
def test_layouttree_from_values2(self): t = Layout.from_values(self.el8) self.assertEqual(t.keys(), [('ValA', 'LabelB')])
def test_layouttree_from_values1(self): t = Layout.from_values(self.el1) self.assertEqual(t.keys(), [('Element', 'I')])
def callback(i): return Layout([Curve([], label=str(j)) for j in range(i, i + 2)])
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 test_viewgroup_getter(self): tr = Layout() self.assertEqual(isinstance(tr.Test.Path, Layout), True)
def test_overlay_from_values_with_layouts(self): layout1 = self.el1 + self.el4 layout2 = self.el2 + self.el5 paths = Layout.from_values([layout1, layout2]).keys() self.assertEqual(paths, [('Element', 'I'), ('ValA', 'I'), ('Element', 'II'), ('ValB', 'I')])
def test_layouttree_keys_2(self): t = Layout([self.el1, self.el2]) self.assertEqual(t.keys(), [('Element', 'I'), ('Element', 'II')])
def test_simple_pickle(self): tr = Layout() dumped = pickle.dumps(tr) tr2 = pickle.loads(dumped) self.assertEqual(tr.data, OrderedDict()) self.assertEqual(tr.data, tr2.data)
def test_contains_tuple(self): tr = Layout() tr.Test.Path = 42 self.assertEqual(('Test', 'Path') in tr, True)
def test_contains_child(self): tr = Layout() tr.Test.Path = 42 self.assertEqual('Path' in tr.Test, True)
def test_viewgroup_set_path(self): tr = Layout() tr.set_path(('Test', 'Path'), -42) self.assertEqual(tr.Test.Path, -42)
def test_layout_set_path(self): tr = Layout() tr.set_path(('Test', 'Path'), -42) self.assertEqual(tr.Test.Path, -42)
def test_layout_from_values_retains_custom_path_with_label(self): layout = Layout([('Custom', self.el6)]) paths = Layout.from_values([layout, self.el2]).keys() self.assertEqual(paths, [('Custom', 'LabelA'), ('Element', 'I')])
""" Test cases for the Comparisons class over the composite types: Layout (the + operator) Overlay (the * operator) HoloMaps are not tested in this file. """ from holoviews import Element, Layout, Overlay from holoviews.element.comparison import ComparisonTestCase class CompositeComparisonTestCase(ComparisonTestCase): def setUp(self): self.el1 = Element('data1') self.el2 = Element('data2') self.el3 = Element('data3') self.el4 = Element('data5', group='ValB') self.el5 = Element('data6', label='LabelA') #========================# # Tests for layout trees # #========================# def test_layouttree_comparison_equal(self): t1 = self.el1 + self.el2 t2 = self.el1 + self.el2 self.assertEqual(t1, t2)
def setUp(self): super(ViewRefTest, self).setUp() tree = Layout() tree.Example.Path1 = Image(np.random.rand(5, 5)) tree.Example.Path2 = Image(np.random.rand(5, 5)) self.tree = tree
def test_viewgroup_setter(self): tr = Layout() tr.Test.Path = 42 self.assertEqual(tr.Test.Path, 42)
def test_layouttree_values_2(self): t = Layout([self.el1, self.el2]) self.assertEqual(t.values(), [self.el1, self.el2])
def test_viewgroup_init(self): Layout()