コード例 #1
0
    def decode_4hot_mtx_nd(self, pts_nd, axis=1, returnEncode=False):
        ''' Decode by finding MODE
		# 	(0) converting classification of points in hull into all points in grid
		# 	(1) returning position of hottest 2x2 region '''

        # t = rz.Timer()
        pts_full_flt = self.enc_full_grid_mtx_nd(pts_nd,
                                                 axis=axis,
                                                 returnFlat=True)
        # print ' %s'%t.tocStr()

        pts_full_4hot_flt = nd_argmax_4hot_conv22(pts_full_flt,
                                                  self.grid.A,
                                                  self.grid.B,
                                                  axis=1)
        # print ' %s'%t.tocStr()

        pts_dec_full_4hot_flt = self.grid.decode_points(pts_full_4hot_flt)
        # print ' %s'%t.tocStr()
        pts_dec_full_4hot = rz.unflatten_2d_array(pts_dec_full_4hot_flt,
                                                  pts_nd,
                                                  axis=axis)
        # print ' %s'%t.tocStr()
        if (returnEncode):
            pts_4hot_flt = pts_full_4hot_flt[:, self.grid_mask]
            # print ' %s'%t.tocStr()
            pts_full_4hot = rz.unflatten_2d_array(pts_4hot_flt,
                                                  pts_nd,
                                                  axis=axis)
            # print ' %s'%t.tocStr()
            return (pts_dec_full_4hot, pts_full_4hot)
        else:
            return pts_dec_full_4hot
コード例 #2
0
    def enc_full_grid_mtx_nd(self,
                             pts_enc,
                             axis=1,
                             returnFlat=False,
                             returnGrid=False):
        ''' Expand a distribution into full distribution over non-legit ab values
		INPUTS
		 	pts_enc 		NxCxXxY		encoded nd mtx (typically)
		 	axis 			integer
		 	returnFlat 		bool
		 	returnGrid 		bool 		expand dimension to AxB
		OUTPUTS
		 	pts_full 		if returnGrid 		NXYxAxB or NxAxBxXxY
		 	 				if not returnGrid 	NXYxC_full or NxC_fullxXxY '''
        pts_flt = rz.flatten_nd_array(pts_enc, axis=axis)
        P = pts_flt.shape[0]

        pts_full_flt = np.zeros((P, self.grid.AB), dtype='float32')
        pts_full_flt[:, self.grid_mask] = pts_flt
        if (returnFlat):
            ret_mtx = pts_full_flt
            ret_axis = 1
        else:
            ret_mtx = rz.unflatten_2d_array(pts_full_flt, pts_enc, axis=axis)
            ret_axis = axis

        if (returnGrid):
            return rz.reshape_single_axis(ret_mtx,
                                          self.grid.A,
                                          self.grid.B,
                                          axis=ret_axis)
        else:
            return ret_mtx
コード例 #3
0
def entropy_mtx_nd(distr_nd, axis=1, eps=1e-10):
    distr_flt = rz.flatten_nd_array(distr_nd, axis=axis)
    entropy_flt = -np.sum(distr_flt * np.log10(distr_flt + eps),
                          axis=1)[:, rz.na()]
    entropy_nd = rz.unflatten_2d_array(entropy_flt,
                                       distr_nd,
                                       axis=axis,
                                       squeeze=True)
    return entropy_nd
コード例 #4
0
def nd_argmax_4hot_conv22(pts_enc_nd, A, B, axis=1):
    # Compute highest index along axis, return 4-hot encoding along that axis
    # run a 2x2 convolution to figure out how which 4 indices to keep
    # (1) Reshape axis to be 2d
    # (2) Find maximum location
    # (3) Map to the 4 indices in that location
    # INPUTS
    # 	pts_enc_nd 		NxCxXxY (typically), or any nd array
    # 	A 				integer 	AxB=C
    # 	B 				integer
    # 	axis 			integer 	which contains channel C dimension
    # OUTPUTS
    # 	pts_enc_1hot_4d 	NxCxXxY (typically) with 1 non-zero values along each channel

    # flatten into NXYxC
    # reshape into NXYxAxB

    # t = rz.Timer()
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    NXY = pts_enc_flt.shape[0]
    pts_enc_flt = pts_enc_flt.reshape((NXY, A, B))
    # print '  %s'%t.tocStr()

    # run 2x2 convolution
    pts_enc_flt_conv22 = pts_enc_flt[:, :-1, :
                                     -1] + pts_enc_flt[:, 1:, :
                                                       -1] + pts_enc_flt[:, :-1,
                                                                         1:] + pts_enc_flt[:,
                                                                                           1:,
                                                                                           1:]
    pts_enc_flt_conv22 = pts_enc_flt_conv22.reshape((NXY, (A - 1) * (B - 1)))
    high_ind = np.argmax(pts_enc_flt_conv22, axis=1)
    high_sub = rz.ind2sub2(high_ind, (A - 1, B - 1))
    # print '  %s'%t.tocStr()

    # find top location
    ogrid = np.ogrid[:NXY, :A, :B]

    mask = np.zeros((NXY, A, B), dtype='bool')
    mask[ogrid[0][:, 0, 0], high_sub[:, 0], high_sub[:, 1]] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0] + 1, high_sub[:, 1]] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0], high_sub[:, 1] + 1] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0] + 1, high_sub[:, 1] + 1] = True
    # print '  %s'%t.tocStr()

    # mask & renormalize
    pts_enc_flt = (pts_enc_flt * mask).reshape((NXY, A * B))
    pts_enc_flt = pts_enc_flt / np.sum(pts_enc_flt, axis=1)[:, rz.na()]
    pts_enc_flt = rz.unflatten_2d_array(pts_enc_flt, pts_enc_nd, axis=axis)
    # print '  %s'%t.tocStr()

    return pts_enc_flt
コード例 #5
0
    def decode_med_mtx_nd(self, pts_nd, axis=1, returnEncode=False):
        ''' Encode by MEDIAN '''

        pts_full_flt = self.enc_full_grid_mtx_nd(pts_nd,
                                                 axis=axis,
                                                 returnFlat=True)
        med_enc_full_flt = nd_argmax_med_grid(pts_full_flt,
                                              self.grid.A,
                                              self.grid.B,
                                              axis=1)

        med_enc_flt = med_enc_full_flt[:, self.grid_mask]
        med_enc_nd = rz.unflatten_2d_array(med_enc_flt, pts_nd, axis=axis)

        # print med_enc_flt.shape
        # print self.grid.ab_grid_flat.shape
        pts_dec_flt = self.decode_nn_mtx_nd(med_enc_flt, axis=1)
        pts_dec_nd = rz.unflatten_2d_array(pts_dec_flt, pts_nd, axis=axis)

        if (returnEncode):
            return (pts_dec_nd, med_enc_nd)
        else:
            return pts_dec_nd
コード例 #6
0
    def encode_4hot_mtx_nd(self, pts_nd, axis=1):
        ''' Encode by
		# 	(1) finding 4 corners around grid
		# 	(2) weighting as a linear interpolation
		# 	(3) masking out points which are in hull
		# 	(4) re-normalizing (in case some points are on border) '''

        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        pts_enc_full_flt = self.grid.encode_points_mtx_nd(pts_nd,
                                                          axis=axis,
                                                          returnFlat=True)
        pts_enc_flt = pts_enc_full_flt[:, self.grid_mask]
        pts_enc_flt = pts_enc_flt / np.sum(pts_enc_flt, axis=1)[:, rz.na()]
        return rz.unflatten_2d_array(pts_enc_flt, pts_nd, axis=axis)
コード例 #7
0
    def encode_points_mtx_nd(self, pts_nd, axis=1, returnSparse=False):
        t = rz.Timer()
        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        P = pts_flt.shape[0]

        (dists, inds) = self.nbrs.kneighbors(pts_flt)

        pts_enc_flt = np.zeros((P, self.K))
        wts = np.exp(-dists**2 / (2 * self.sigma**2))
        wts = wts / np.sum(wts, axis=1)[:, rz.na()]

        pts_enc_flt[np.arange(0, P, dtype='int')[:, rz.na()], inds] = wts
        pts_enc_nd = rz.unflatten_2d_array(pts_enc_flt, pts_nd, axis=axis)

        return pts_enc_nd
コード例 #8
0
def nd_argmax_1hot(pts_enc_nd, axis=1):
    # Run 2x2 convolution, pick highest index, return
    # INPUTS
    # 	pts_enc_nd 	nd array
    # 	axis 			integer to perform argmax over
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    N = pts_enc_flt.shape[0]

    pts_enc_1hot_flt = np.zeros_like(pts_enc_flt)
    max_inds = np.argmax(pts_enc_flt, axis=1)
    pts_enc_1hot_flt[np.arange(0, N), max_inds] = True

    pts_enc_1hot_nd = rz.unflatten_2d_array(pts_enc_1hot_flt,
                                            pts_enc_nd,
                                            axis=axis)

    return pts_enc_1hot_nd
コード例 #9
0
def nd_argmax_1hot(pts_enc_nd, axis=1):
    # Compute highest index along 'axis', return 1 hot encoding of that axis
    # INPUTS
    # 	pts_enc_nd 	nd array
    # 	axis 			integer to perform argmax over
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    N = pts_enc_flt.shape[0]

    pts_enc_1hot_flt = np.zeros_like(pts_enc_flt)
    max_inds = np.argmax(pts_enc_flt, axis=1)
    pts_enc_1hot_flt[np.arange(0, N), max_inds] = True

    pts_enc_1hot_nd = rz.unflatten_2d_array(pts_enc_1hot_flt,
                                            pts_enc_nd,
                                            axis=axis)

    return pts_enc_1hot_nd
コード例 #10
0
def expand_axis_mask(pts, grid_mask, axis=1, returnFlat=False):
    ''' Expand a single axis by a mask.
	INPUTS
		pts 		N0xN1x...xNn 	n-dimensional matrix
	 	grid_mask 	M bool vector
	 	axis 		integer 		axis to expand
	 	returnFlat 	boolean 		if True, return a PxM vector
	 								if False, return a N0xN1x...xNn, but with Naxis replaced with M '''
    AB = grid_mask.size
    pts_flt = rz.flatten_nd_array(pts, axis=axis)
    P = pts_flt.shape[0]
    pts_full_flt = np.zeros((P, AB), dtype='float32')
    pts_full_flt[:, grid_mask] = pts_flt
    if (returnFlat):
        return pts_full_flt
    else:
        return rz.unflatten_2d_array(pts_full_flt, pts, axis=axis)
コード例 #11
0
    def decode_points_mtx_nd(self, pts_enc_nd, axis=1):
        # INPUTS
        # 	pts_enc_nd 		encoded nd points in ab space
        # 	axis 			axis containing ab

        pts_nd_polar_sqrt = self.grid.decode_points_mtx_nd(pts_enc_nd,
                                                           axis=axis)
        pts_flt_polar_sqrt = rz.flatten_nd_array(pts_nd_polar_sqrt, axis=axis)

        pts_flt_polar = pts_flt_polar_sqrt.copy()
        pts_flt_polar[:, 0] = pts_flt_polar[:, 0]**2
        pts_flt_polar[:, 1] = pts_flt_polar[:, 1] * np.pi / 180

        pts_flt = np.zeros_like(pts_flt_polar)
        pts_flt[:, 0] = pts_flt_polar[:, 0] * np.sin(pts_flt_polar[:, 1])
        pts_flt[:, 1] = pts_flt_polar[:, 0] * np.cos(pts_flt_polar[:, 1])

        pts_nd = rz.unflatten_2d_array(pts_flt, pts_enc_nd, axis=axis)
        return pts_nd
コード例 #12
0
def nd_argmax_med_grid(pts_enc_nd, A, B, axis=1):
    # Compute index for median, assuming distribution is over a grid
    # (1) Reshape axis to be 2d
    # (2) Run cum-sum in those 2 dimensions
    # (3) Find median point in those 2 dimensions
    # INPUTS
    # 	pts_enc_nd 		NxCxXxY (typically), or any nd array
    # 	A 				integer 	AxB=C
    # 	B 				integer
    # 	axis 			integer 	which contains channel C dimension
    # OUTPUTS
    # 	med_enc_nd 		NxCxXxY

    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    NXY = pts_enc_flt.shape[0]
    pts_enc_flt = pts_enc_flt.reshape((NXY, A, B))

    # marginalize out in either dimension
    pts_enc_full_flt_marg0 = np.sum(pts_enc_flt, axis=2)
    pts_enc_full_flt_marg1 = np.sum(pts_enc_flt, axis=1)

    med_inds0 = np.argmin(
        np.abs(np.cumsum(pts_enc_full_flt_marg0, axis=1) - .5), axis=1)
    med_inds1 = np.argmin(
        np.abs(np.cumsum(pts_enc_full_flt_marg1, axis=1) - .5), axis=1)

    # plt.hist(np.min(np.abs(np.cumsum(pts_enc_full_flt_marg0,axis=1)-.5),axis=1))
    # plt.show()

    med_subs = np.concatenate((med_inds0[:, rz.na()], med_inds1[:, rz.na()]),
                              axis=1)
    med_inds = rz.sub2ind2(med_subs, (A, B))

    med_enc_flt = np.zeros((NXY, A * B), dtype='float32')
    med_enc_flt[np.arange(0, NXY), med_inds] = 1

    med_enc_nd = rz.unflatten_2d_array(med_enc_flt, pts_enc_nd, axis=axis)

    return med_enc_nd
コード例 #13
0
    def encode_points_mtx_nd(self, pts_nd, axis=1):
        # INPUTS
        # 	pts_nd 	nd points in ab space
        # 	axis 	axis containing ab

        # self.grid()
        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        # print pts_flt.shape

        pts_flt_polar_sqrt = np.zeros_like(pts_flt)
        pts_flt_polar_sqrt[:, 0] = np.sqrt(np.sqrt(np.sum(pts_flt**2, axis=1)))
        pts_flt_polar_sqrt[:, 1] = np.arctan2(pts_flt[:, 0],
                                              pts_flt[:, 1]) * 180 / np.pi
        # print pts_flt_polar_sqrt.shape
        # print pts_flt_polar_sqrt

        pts_flt_enc = self.grid.encode_points(pts_flt_polar_sqrt,
                                              returnMatrix=True)
        # print pts_flt_enc.shape

        pts_enc_nd = rz.unflatten_2d_array(pts_flt_enc, pts_nd, axis=axis)

        return pts_enc_nd
コード例 #14
0
 def decode_points_mtx_nd(self, pts_enc_nd, axis=1):
     pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
     pts_dec_flt = np.dot(pts_enc_flt, self.cc)
     pts_dec_nd = rz.unflatten_2d_array(pts_dec_flt, pts_enc_nd, axis=axis)
     return pts_dec_nd