Example #1
0
    def indices_to_coords(self,indices):
        # Converts indices to coordinates
        assert is_vect(indices)
        assert is_int(indices)
        
        (N,) = indices.shape
        D = len(self.coef)

        # Does the hard work
        raw_coords = np.empty((N,D))
        res = indices
        for d in xrange(D):
            (coord,res) = divmod(res,self.coef[d])
            raw_coords[:,d] = coord

        # OOB indices mapped to NAN
        oob_mask = self.are_indices_oob(indices)
        raw_coords[oob_mask,:] = np.nan

        oob_indices = self.indices_to_oob_indices(indices,oob_mask)

        oob = OutOfBounds()
        oob.build_from_oob_indices(oob_indices,D)

        coords = Coordinates(raw_coords,oob)
        assert coords.check()
        
        return coords
Example #2
0
    def points_to_cell_coords(self,points):
        """
        Figure out where points are. Returns the cell coordinate.
        """
        assert is_mat(points) 
        (N,D) = points.shape
        assert D == self.dim
        
        # Get the OOB info
        oob = OutOfBounds()
        oob.build_from_points(self,points)
        assert oob.check()
        
        raw_coords = np.empty((N,D))
        for d in xrange(D):
            (low,high,num_cells) = self.grid_desc[d]
            # Transform: [low,high) |-> [0,n)
            transform = num_cells * (points[:,d] - low) / (high - low)
            transform += self.fuzz
            raw_coords[:,d] = np.floor(transform).astype(np.integer)
            # Add a little fuzz to make sure stuff on the boundary is
            # mapped correctly

            # Fuzz top boundary to get [low,high]
            fuzz_mask = np.logical_and(high <= points[:,d],
                                     points[:,d] < high + 2*self.fuzz)
            raw_coords[fuzz_mask,d] = num_cells - 1
            # Counts things just a littttle bit greater than last cell
            # boundary as part of the last cell
        
        raw_coords[oob.mask,:] = np.nan
        assert is_int(raw_coords)
        coords = Coordinates(raw_coords,oob)
        assert coords.check()

        return coords