def _re_bound(self,plot_bounding_box,mat,box,density): # CEBHACKALERT: for Julien... # If plot_bounding_box is that of a Sheet, it will already have been # setup so that the density in the x direction and the density in the # y direction are equal. # If plot_bounding_box comes from elsewhere (i.e. you create it from # arbitrary bounds), it might need to be adjusted to ensure the density # in both directions is the same (see Sheet.__init__()). I don't know where # you want to do that; presumably the code should be common to Sheet and # where it's used in the plotting? # # It's possible we can move some of the functionality # into SheetCoordinateSystem. if plot_bounding_box.containsbb_exclusive(box): ct = SheetCoordinateSystem(plot_bounding_box,density,density) new_mat = np.zeros(ct.shape,dtype=np.float) r1,r2,c1,c2 = Slice(box,ct) new_mat[r1:r2,c1:c2] = mat else: scs = SheetCoordinateSystem(box,density,density) s=Slice(plot_bounding_box,scs) s.crop_to_sheet(scs) new_mat = s.submatrix(mat) return new_mat
def __init__(self,cf,input_sheet,x=0.0,y=0.0,template=BoundingBox(radius=0.1), mask=patterngenerator.Constant(), min_matrix_radius=1): """ From an existing copy of ConnectionField (CF) that acts as a template, create a new CF that shares weights with the template CF. Copies all the properties of CF to stay identical except the weights variable that actually contains the data. The only difference from a normal CF is that the weights of the CF are implemented as a numpy view into the single master copy of the weights stored in the CF template. """ # CEBALERT: There's no call to super's __init__; see JAHACKALERT # below. template = copy(template) if not isinstance(template,Slice): template = Slice(template,input_sheet,force_odd=True, min_matrix_radius=min_matrix_radius) # Note: if passed in, mask is shared between CFs (but not if created here) if not hasattr(mask,'view'): mask = _create_mask(patterngenerator.Constant(), template.compute_bounds(input_sheet), input_sheet,True,0.5) self._has_norm_total=False self.mask=mask weights_slice = self._create_input_sheet_slice(input_sheet,x,y,template,min_matrix_radius=min_matrix_radius) self.weights = weights_slice.submatrix(cf.weights)
def __init__(self, template, input_sheet, projection, **params): """ Initializes the CF object and stores meta information about the CF's shape and position in the SparseCFProjection to allow for easier initialization. """ super(SparseConnectionField, self).__init__(**params) self.input_sheet = input_sheet self.projection = projection self.matrix_idx = self.projection.dest.sheet2matrixidx(self.x, self.y) self.oned_idx = self.matrix_idx[0] * self.projection.dest.shape[ 1] + self.matrix_idx[1] template = copy(template) if not isinstance(template, Slice): template = Slice(template, self.input_sheet, force_odd=True, min_matrix_radius=self.min_matrix_radius) self.weights_slice = self._create_input_sheet_slice(template) self.src_slice = tuple(self.input_sheet_slice.tolist())
def test_bounds2slice(self): # test that if you ask to slice the matrix with the sheet's BoundingBox, you # get back the whole matrix sheet_bb = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) ct = SheetCoordinateSystem(sheet_bb, 10) slice_ = Slice(sheet_bb, ct) true_slice = (0, 10, 0, 10 ) # inclusive left boundary, exclusive right boundary self.assertEqual(tuple(slice_.tolist()), true_slice) # for the following tests, the values have been all computed by hand and then # tested (by JC). The boundingbox and density tested have been chosen randomly, # then drawn to get the slice from it. # Test with 20 density. ct = SheetCoordinateSystem(sheet_bb, 20, 20) bb = BoundingBox(points=((-0.05, -0.20), (0.20, 0.05))) slice_ = Slice(bb, ct) true_slice = (9, 14, 9, 14) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((-0.40, 0), (-0.30, 0.30))) slice_ = Slice(bb, ct) true_slice = (4, 10, 2, 4) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((0.15, 0.10), (0.30, 0.30))) slice_ = Slice(bb, ct) true_slice = (4, 8, 13, 16) self.assertEqual(tuple(slice_.tolist()), true_slice) bb = BoundingBox(points=((-0.05, -0.45), (0.10, -0.25))) slice_ = Slice(bb, ct) true_slice = (15, 19, 9, 12) self.assertEqual(tuple(slice_.tolist()), true_slice) # test with 7 density sheet. bb = BoundingBox(points=((-0.5 + 2.0 / 7.0, 0.5 - 2.0 / 7.0), (-0.5 + 4.0 / 7.0, 0.5))) ct = SheetCoordinateSystem(sheet_bb, 7) slice_ = Slice(bb, ct) true_slice = (0, 2, 2, 4) self.assertEqual(tuple(slice_.tolist()), true_slice) #(4x4 matrix) ct = SheetCoordinateSystem(BoundingBox(radius=0.2), xdensity=10, ydensity=10) test_bounds = BoundingBox(radius=0.1) slice_ = Slice(test_bounds, ct) r1, r2, c1, c2 = slice_ self.assertEqual((r1, r2, c1, c2), (1, 3, 1, 3))
def __init__(self,initialize_cfs=True,**params): """ Initialize the Projection with a set of cf_type objects (typically SparseConnectionFields), 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() self.activity = np.array(self.dest.activity) self.norm_total = np.array(self.dest.activity,dtype=np.float64) self.has_norm_total = False if initialize_cfs: self._create_cfs() if self.apply_output_fns_init: self.apply_learn_output_fns() self.input_buffer = None
def test_connection_field_like(self): # test a ConnectionField-like example sheet = Sheet(nominal_density=10,nominal_bounds=BoundingBox(radius=0.5)) cf_bounds = BoundingBox(points=((0.3,0.3),(0.6,0.6))) slice_ = Slice(cf_bounds,sheet) slice_.crop_to_sheet(sheet) # check it's been cropped to fit onto sheet... self.assertEqual(slice_.tolist(),[0,2,8,10]) # now check that it gives the correct bounds... cropped_bounds = slice_.compute_bounds(sheet) true_cropped_bounds = BoundingBox(points=((0.3,0.3),(0.5,0.5))) for a,b in zip(cropped_bounds.lbrt(),true_cropped_bounds.lbrt()): self.assertAlmostEqual(a,b)
def test_slice2bounds_bounds2slice(self): bb = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) ct = SheetCoordinateSystem(bb, 10) slice_ = (0, 3, 7, 8) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (4, 7, 8, 10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (2, 3, 4, 8) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (0, 3, 9, 10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) bb = BoundingBox(points=((-0.75, -0.5), (0.75, 0.5))) ct = SheetCoordinateSystem(bb, 20, 20) slice_ = (9, 14, 27, 29) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (0, 6, 0, 7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (6, 10, 11, 29) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) bb = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) ct = SheetCoordinateSystem(bb, 7) slice_ = (4, 7, 2, 3) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b) slice_ = (0, 7, 0, 7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) test_slice = Slice(bounds, ct) for a, b in zip(slice_, test_slice): self.assertEqual(a, b)