Example #1
0
File: nms.py Project: liuguoyou/RON
def nms(dets, thresh):
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / ((areas[i] + areas[order[1:]] - inter)+0.0000001)

        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]

    return keep
Example #2
0
	def ParseMDH (self, n = 0, dry = False):
		''' Parse one MDH entry '''

		self.bm = struct.unpack_from ('2I', self.buf, n +  20)
		if (self.bm[0] & SYNCDATA):
			return n + MDHSIZE
		
		if (self.bm[0] & NOISEADJSCAN):
			self.lc  = struct.unpack_from ('%dH' % 16, self.buf, n + 28) # loop counters
			self.ch  = struct.unpack_from ('H', self.buf, n + 124)
	
			if (dry):
				if (self.noisedims[0] == 0):
					self.noisedims[0] = self.lc[0]
					self.noisedims[1] = struct.unpack_from ('H', self.buf, n + 30)[0]
					self.noisencolb = self.noisedims[md.COL] * self.ndds
					self.noisedims[2:16] = np.array(self.lc[2:16])
				else:
					self.noisedims[2:16] = np.maximum (self.noisedims[2:16],self.lc[2:16])
			return n + MDHSIZE
				
		self.lc  = struct.unpack_from ('%dH' % 16, self.buf, n + 28) # loop counters
		self.ch  = struct.unpack_from ('H', self.buf, n + 124)

		if (dry):
			if (self.dims[0] == 0):
				self.dims[0] = self.lc[0]
				self.dims[1] = struct.unpack_from ('H', self.buf, n + 30)[0]
				self.ncolb = self.dims[md.COL] * self.ds
				self.dims[2:16] = np.array(self.lc[2:16])
			else:
				self.dims[2:16] = np.maximum (self.dims[2:16],self.lc[2:16])

		return n + MDHSIZE
Example #3
0
    def autoRange(self,zmin=None,zmax=None,coordList=None):
        """ determine X,Y,Z limits
        """

        if coordList==None:
            coordList = self.coordList

        # autorange in X,Y
        minX = 1.e50
        maxX = -1.e50
        minY = 1.e50
        maxY = -1.e50
        minZ = 1.e50
        maxZ = -1.e50
        for iCoord in coordList:

            #if self.interpPresent:
            #    X,Y = self.interpGrids[iCoord]
            #    Z = self.interpValues[iCoord]
            #else:
            X,Y,Z = self.getXYZpoints(coordList=coordList)

            minX = numpy.minimum(minX,X.min())
            maxX = numpy.maximum(maxX,X.max())
            minY = numpy.minimum(minY,Y.min())
            maxY = numpy.maximum(maxY,Y.max())
            minZ = numpy.minimum(minZ,Z.min())
            maxZ = numpy.maximum(maxZ,Z.max())

        if zmin!=None:
            minZ = zmin
        if zmax!=None:
            maxZ = zmax

        return minX,maxX,minY,maxY,minZ,maxZ
Example #4
0
    def update_swe(self):

        #--------------------------------------------------------
        # Note: The Meteorology component uses air temperature
        # to compute P_rain (precip that falls as liquid) and
        # P_snow (precip that falls as snow or ice) separately.
        # P_snow = (self.P * (self.T_air <= 0)) 
        #----------------------------------------------------------
        # Note: This method must be written to work regardless
        # of whether P_rain and T are scalars or grids. (3/14/07)
        #------------------------------------------------------------
        # If P or T_air is a grid, then h_swe and h_snow are grids.
        # This is set up in initialize_computed_vars().
        #------------------------------------------------------------
      
        #------------------------------------------------
        # Increase snow water equivalent due to snowfall
        #------------------------------------------------
        # Meteorology and Channel components may have
        # different time steps, but then self.P_snow
        # will be a time-interpolated value.
        #------------------------------------------------
        dh1_swe  = (self.P_snow * self.dt)
        self.h_swe  += dh1_swe

        #------------------------------------------------
        # Decrease snow water equivalent due to melting
        # Note that SM depends partly on h_snow.
        #------------------------------------------------
        dh2_swe    = self.SM * self.dt
        self.h_swe -= dh2_swe
        np.maximum(self.h_swe, np.float64(0), self.h_swe)  # (in place)
Example #5
0
def _joint_probabilities(distances, desired_perplexity, verbose):
    """Compute joint probabilities p_ij from distances.

    Parameters
    ----------
    distances : array, shape (n_samples * (n_samples-1) / 2,)
        Distances of samples are stored as condensed matrices, i.e.
        we omit the diagonal and duplicate entries and store everything
        in a one-dimensional array.

    desired_perplexity : float
        Desired perplexity of the joint probability distributions.

    verbose : int
        Verbosity level.

    Returns
    -------
    P : array, shape (n_samples * (n_samples-1) / 2,)
        Condensed joint probability matrix.
    """
    # Compute conditional probabilities such that they approximately match
    # the desired perplexity
    distances = distances.astype(np.float32, copy=False)
    conditional_P = _utils._binary_search_perplexity(
        distances, None, desired_perplexity, verbose)
    P = conditional_P + conditional_P.T
    sum_P = np.maximum(np.sum(P), MACHINE_EPSILON)
    P = np.maximum(squareform(P) / sum_P, MACHINE_EPSILON)
    return P
Example #6
0
def shifted_corr(reference, image, displacement):
    """Calculate the correlation between the reference and the image shifted
    by the given displacement.

    Parameters
    ----------
    reference : np.ndarray
    image : np.ndarray
    displacement : np.ndarray

    Returns
    -------
    correlation : float

    """

    ref_cuts = np.maximum(0, displacement)
    ref = reference[ref_cuts[0]:, ref_cuts[1]:, ref_cuts[2]:]
    im_cuts = np.maximum(0, -displacement)
    im = image[im_cuts[0]:, im_cuts[1]:, im_cuts[2]:]
    s = np.minimum(im.shape, ref.shape)
    ref = ref[:s[0], :s[1], :s[2]]
    im = im[:s[0], :s[1], :s[2]]
    ref -= nanmean(ref.reshape(-1, ref.shape[-1]), axis=0)
    ref = np.nan_to_num(ref)
    im -= nanmean(im.reshape(-1, im.shape[-1]), axis=0)
    im = np.nan_to_num(im)
    assert np.all(np.isfinite(ref)) and np.all(np.isfinite(im))
    corr = nanmean(
        [old_div(np.sum(i * r), np.sqrt(np.sum(i * i) * np.sum(r * r))) for
         i, r in zip(np.rollaxis(im, -1), np.rollaxis(ref, -1))])
    return corr
    def _init_energy(self, pc):
        if pc is self._pc:
            return
        self.set_transform(self._t, pc)
        self._pc = pc
        self._res[:] = self.data[:, self._t] - self.mu[:]
        self._V = np.maximum(self.offset + np.mean(self._res ** 2), SMALL)
        self._res0[:] = self.data[:, self._t] - self.mu0
        self._V0 = np.maximum(self.offset0 + np.mean(self._res0 ** 2), SMALL)

        if self.use_derivatives:
            # linearize the data wrt the transform parameters
            # use the auxiliary array to save the current resampled data
            self._aux[:] = self.data[:, self._t]
            basis = np.eye(6)
            for j in range(pc.size):
                self.set_transform(self._t, pc + self.stepsize * basis[j])
                self.A[:, j] = (self.data[:, self._t] - self._aux)\
                    / self.stepsize
            self.transforms[self._t].param = pc
            self.data[:, self._t] = self._aux[:]
            # pre-compute gradient and hessian of numerator and
            # denominator
            c = 2 / float(self.data.shape[0])
            self._dV = c * np.dot(self.A.T, self._res)
            self._dV0 = c * np.dot(self.A.T, self._res0)
            self._H = c * np.dot(self.A.T, self.A)
Example #8
0
 def _logpmf(self, x, mu, alpha, p):
     mu_p = mu ** (p - 1.)
     a1 = np.maximum(np.nextafter(0, 1), 1 + alpha * mu_p)
     a2 = np.maximum(np.nextafter(0, 1), mu + (a1 - 1.) * x)
     logpmf_ = np.log(mu) + (x - 1.) * np.log(a2)
     logpmf_ -=  x * np.log(a1) + gammaln(x + 1.) + a2 / a1
     return logpmf_
def nms2d(boxes, overlap=0.3):
    """Compute the nms given a set of scored boxes,
    as numpy array with 5 columns <x1> <y1> <x2> <y2> <score>
    return the indices of the tubelets to keep
    """

    if boxes.size == 0:
        return np.array([],dtype=np.int32)

    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]

    scores = boxes[:, 4]
    areas = (x2-x1+1) * (y2-y1+1)
    I = np.argsort(scores)
    indices = np.zeros(scores.shape, dtype=np.int32)

    counter = 0
    while I.size > 0:
        i = I[-1]
        indices[counter] = i
        counter += 1

        xx1 = np.maximum(x1[i],x1[I[:-1]])
        yy1 = np.maximum(y1[i],y1[I[:-1]])
        xx2 = np.minimum(x2[i],x2[I[:-1]])
        yy2 = np.minimum(y2[i],y2[I[:-1]])

        inter = np.maximum(0.0, xx2 - xx1 + 1) * np.maximum(0.0, yy2 - yy1 + 1)
        iou = inter / (areas[i] + areas[I[:-1]] - inter)
        I = I[np.where(iou <= overlap)[0]]

    return indices[:counter]
Example #10
0
    def pdf(self, t):
        """
        Implementing the code distributed with Logan et al. 2013 using
        the reparametrization given above.

        Also, the constant theta is added as usual.
        """
        t=np.maximum(t-self.theta, 1e-5) # absorbed into pdf
        sqrt_t=np.sqrt(t)

        # reparametrization
        a=self.A/2.0
        k=self.alpha-self.A/2.0
        l=self.gamma

        if self.A<1e-10: # this is the solution without starting-point variability
            r=self.alpha/(np.sqrt(2*np.pi*(t**3)))*np.exp(- ((self.alpha-self.gamma*t)**2)/(2*t))
        elif self.gamma<1e-10:
            r=np.exp( -.5*( np.log(2)+np.log(np.pi)+np.log(t))
                      + np.log( np.exp(-( (k-a)**2/(2*t)))-np.exp(-( (k+a)**2/(2*t) )) )
                      - np.log(2) - np.log(a) )
        else:
            r=np.exp( np.log( (np.exp(- (a-k+t*l)**2/(2*t) )-np.exp(- (a+k-t*l)**2/(2*t) ))/np.sqrt(2*np.pi*t)
                              + np.exp(np.log(.5)+np.log(l))*( 2*pnormP( (-k+a)/sqrt_t + sqrt_t*l)-1
                                                               + 2*pnormP( (k+a)/sqrt_t - sqrt_t*l)-1) )
                      - np.log(2) - np.log(a))

        return np.maximum(0.0, np.where( np.isnan(r), 0, r))
Example #11
0
    def cdf(self,t):
        t=np.maximum(t-self.theta, 1e-5) # absorbed into cdf

        sqrt_t=np.sqrt(t)

        # reparametrization
        a=self.A/2.0
        k=self.alpha-self.A/2.0
        l=self.gamma

        if self.A<1e-10: # this is the solution without starting-point variability
            r=pnormP( (self.gamma*t-self.alpha)/sqrt_t)+np.exp(2*self.alpha*self.gamma)*pnormP(-(self.gamma*t+self.alpha)/(sqrt_t))
        elif self.gamma<1e-10:
            r=(( -(k+a)*(2*pnormP( (k+a)/sqrt_t)-1)
                 -(k-a)*(2*pnormP(-(k-a)/sqrt_t)-1))/(2*a)) \
              + (1 + np.exp(-.5*(k-a)**2/t - .5*np.log(2) - .5*np.log(np.pi) + .5*np.log(t) - np.log(a))
                 -   np.exp(-.5*(k+a)**2/t - .5*np.log(2) - .5*np.log(np.pi) + .5*np.log(t) - np.log(a)))
        else:
            t1=np.exp( .5*np.log(t)-.5*np.log(2*np.pi) ) * (  np.exp( -((k-a-t*l)**2/t)/2.)
                                                            - np.exp( -((k+a-t*l)**2/t)/2.) ) # ok
            t2=a+(   np.exp(2*l*(k+a)+np.log(pnormP(-(k+a+t*l)/sqrt_t)))
                   - np.exp(2*l*(k-a)+np.log(pnormP(-(k-a+t*l)/sqrt_t))) )/(2*l) # ok
            t4= (.5*(t*l-a-k+.5/l)) * ( 2*pnormP((k+a)/sqrt_t-sqrt_t*l)-1) \
               + .5*(k-a-t*l-.5/l)*( 2*pnormP((k-a)/sqrt_t-sqrt_t*l)-1)
            r=(t4+t2+t1)/(2*a)


        return np.minimum( np.maximum( 0., np.where( np.isnan(r), 0, r) ), 1.)
Example #12
0
def nms(boxes, threshold, method):
    if boxes.size == 0:
        return np.empty((0, 3))
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    s = boxes[:, 4]
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    I = np.argsort(s)
    pick = np.zeros_like(s, dtype=np.int16)
    counter = 0
    while I.size > 0:
        i = I[-1]
        pick[counter] = i
        counter += 1
        idx = I[0:-1]
        xx1 = np.maximum(x1[i], x1[idx])
        yy1 = np.maximum(y1[i], y1[idx])
        xx2 = np.minimum(x2[i], x2[idx])
        yy2 = np.minimum(y2[i], y2[idx])
        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        if method is 'Min':
            o = inter / np.minimum(area[i], area[idx])
        else:
            o = inter / (area[i] + area[idx] - inter)
        I = I[np.where(o <= threshold)]
    pick = pick[0:counter]
    return pick
Example #13
0
    def test_elementwise_max_grad(self, n, m, d, gc, dc):
        go = np.random.rand(n, m, d).astype(np.float32)
        X = np.random.rand(n, m, d).astype(np.float32)
        Y = np.random.rand(n, m, d).astype(np.float32)
        Z = np.random.rand(n, m, d).astype(np.float32)
        mx = np.maximum(np.maximum(X, Y), Z)
        inputs = [mx, go, X, Y, Z]

        def max_grad_op(mx, go, X, Y, Z):
            def mx_grad(a):
                return go * (mx == a)

            return [mx_grad(a) for a in [X, Y, Z]]

        op = core.CreateOperator(
            "MaxGradient",
            ["mx", "go", "X", "Y", "Z"],
            ["gX", "gY", "gZ"]
        )

        self.assertReferenceChecks(
            device_option=gc,
            op=op,
            inputs=inputs,
            reference=max_grad_op,
        )
        self.assertDeviceChecks(dc, op, inputs, [0, 1, 2])
Example #14
0
def prox_l1(Y, alpha, n_orient):
    """proximity operator for l1 norm with multiple orientation support

    L2 over orientation and L1 over position (space + time)

    Example
    -------
    >>> Y = np.tile(np.array([1, 2, 3, 2, 0], dtype=np.float), (2, 1))
    >>> Y = np.r_[Y, np.zeros_like(Y)]
    >>> print Y
    [[ 1.  2.  3.  2.  0.]
     [ 1.  2.  3.  2.  0.]
     [ 0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.]]
    >>> Yp, active_set = prox_l1(Y, 2, 2)
    >>> print Yp
    [[ 0.          0.58578644  1.58578644  0.58578644  0.        ]
     [ 0.          0.58578644  1.58578644  0.58578644  0.        ]]
    >>> print active_set
    [ True  True False False]
    """
    n_positions = Y.shape[0] // n_orient
    norms = np.sqrt(np.sum((np.abs(Y) ** 2).T.reshape(-1, n_orient), axis=1))
    # Ensure shrink is >= 0 while avoiding any division by zero
    shrink = np.maximum(1.0 - alpha / np.maximum(norms, alpha), 0.0)
    shrink = shrink.reshape(-1, n_positions).T
    active_set = np.any(shrink > 0.0, axis=1)
    shrink = shrink[active_set]
    if n_orient > 1:
        active_set = np.tile(active_set[:, None], [1, n_orient]).ravel()
    Y = Y[active_set]
    if len(Y) > 0:
        for o in range(n_orient):
            Y[o::n_orient] *= shrink
    return Y, active_set
Example #15
0
def test_maximum_minimum_scalar():
    data1 = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp1 = np.random.rand(3,4)
    data_tmp1[:] = 2
 
    arr_data1 = mx.nd.array(data_tmp1)
    arr_grad1 = mx.nd.empty(shape)

    test =  mx.sym.maximum(data1,3) + mx.sym.maximum(9,data1) + mx.sym.minimum(5,data1) + mx.sym.minimum(data1,4)
    exe_test = test.bind(mx.cpu(), args=[arr_data1], args_grad=[arr_grad1])
    exe_test.forward()
    out = exe_test.outputs[0].asnumpy()
    npout =  np.maximum(data_tmp1,3) + np.maximum(9,data_tmp1) + np.minimum(5,data_tmp1) + np.minimum(data_tmp1,4)
    assert reldiff(out, npout) < 1e-6

    out_grad = mx.nd.empty(shape)
    out_grad[:] = 2
    exe_test.backward(out_grad)
    
    npout_grad = np.ones(shape)
    npout_grad[:] = 2
    mask1 = (data_tmp1 > 3).astype('float')
    mask2 = (9 > data_tmp1).astype('float')
    mask3 = (5 < data_tmp1).astype('float')
    mask4 = (data_tmp1 < 4).astype('float')
    npout_grad1 = npout_grad * mask1 + (npout_grad - npout_grad * mask2) + (npout_grad - npout_grad * mask3) + npout_grad * mask4
    
    assert reldiff(arr_grad1.asnumpy(), npout_grad1) < 1e-6
def rp_gumbel_original(p_zero, loc, scale, flvol, max_return_period=1e9):
    """
    Transforms a unique, or array of flood volumes into the belonging return
    periods, according to gumbel parameters (belonging to non-zero part of the
    distribution) and a zero probability
    Inputs:
        p_zero:        probability that flood volume is zero
        loc:           Gumbel location parameter (of non-zero part of distribution)
        scale:         Gumbel scale parameter (of non-zero part of distribution)
        flvol:         Flood volume that will be transformed to return period
        max_return_period: maximum return period considered. This maximum is needed to prevent that floating point
                        precision becomes a problem (default: 1e9)
    This function is copied from: https://repos.deltares.nl/repos/Hydrology/trunk/GLOFRIS/src/rp_bias_corr.py
    """
    
    np.seterr(divide='ignore')
    np.seterr(invalid='ignore')
    max_p = 1-1./max_return_period
    max_p_residual = np.minimum(np.maximum((max_p-np.float64(p_zero))/(1-np.float64(p_zero)), 0), 1)
    max_reduced_variate = -np.log(-np.log(np.float64(max_p_residual)))
    # compute the gumbel reduced variate belonging to the Gumbel distribution (excluding any zero-values)
    # make sure that the reduced variate does not exceed the one, resembling the 1,000,000 year return period
    reduced_variate = np.minimum((flvol-loc)/scale, max_reduced_variate)
    # reduced_variate = (flvol-loc)/scale
    # transform the reduced variate into a probability (residual after removing the zero volume probability)
    p_residual = np.minimum(np.maximum(np.exp(-np.exp(-np.float64(reduced_variate))), 0), 1)
    # tranform from non-zero only distribution to zero-included distribution
    p = np.minimum(np.maximum(p_residual*(1-p_zero) + p_zero, p_zero), max_p)  # Never larger than max_p
    # transform into a return period    
    return_period = 1./(1-p)
    test_p = p == 1    
    return return_period, test_p
Example #17
0
def psfplots():
	tpsf = wise.get_psf_model(1, pixpsf=True)
	
	psfp = tpsf.getPointSourcePatch(0, 0)
	psf = psfp.patch
	
	psf /= psf.sum()
	
	plt.clf()
	plt.imshow(np.log10(np.maximum(1e-5, psf)), interpolation='nearest', origin='lower')
	plt.colorbar()
	ps.savefig()
	
	h,w = psf.shape
	cx,cy = w/2, h/2
	
	X,Y = np.meshgrid(np.arange(w), np.arange(h))
	R = np.sqrt((X - cx)**2 + (Y - cy)**2)
	plt.clf()
	plt.semilogy(R.ravel(), psf.ravel(), 'b.')
	plt.xlabel('Radius (pixels)')
	plt.ylabel('PSF value')
	plt.ylim(1e-8, 1.)
	ps.savefig()
	
	plt.clf()
	plt.loglog(R.ravel(), psf.ravel(), 'b.')
	plt.xlabel('Radius (pixels)')
	plt.ylabel('PSF value')
	plt.ylim(1e-8, 1.)
	ps.savefig()
	
	print('PSF norm:', np.sqrt(np.sum(np.maximum(0, psf)**2)))
	print('PSF max:', psf.max())
Example #18
0
def _beta_divergence_dense(X, W, H, beta):
    """Compute the beta-divergence of X and W.H for dense array only.

    Used as a reference for testing nmf._beta_divergence.
    """
    if isinstance(X, numbers.Number):
        W = np.array([[W]])
        H = np.array([[H]])
        X = np.array([[X]])

    WH = np.dot(W, H)

    if beta == 2:
        return squared_norm(X - WH) / 2

    WH_Xnonzero = WH[X != 0]
    X_nonzero = X[X != 0]
    np.maximum(WH_Xnonzero, 1e-9, out=WH_Xnonzero)

    if beta == 1:
        res = np.sum(X_nonzero * np.log(X_nonzero / WH_Xnonzero))
        res += WH.sum() - X.sum()

    elif beta == 0:
        div = X_nonzero / WH_Xnonzero
        res = np.sum(div) - X.size - np.sum(np.log(div))
    else:
        res = (X_nonzero ** beta).sum()
        res += (beta - 1) * (WH ** beta).sum()
        res -= beta * (X_nonzero * (WH_Xnonzero ** (beta - 1))).sum()
        res /= beta * (beta - 1)

    return res
Example #19
0
def fuzzy_c_means(points, num_centers, m=2., tol=1e-4, max_iter=100,
                  verbose=False):
  '''Uses Fuzzy C-Means to downsample `points`.
  m : aggregation parameter >1, larger implies smoother clusters
  Returns indices of downsampled points.
  '''
  num_points = points.shape[0]
  if num_centers >= num_points:
    return np.arange(num_points)
  # randomly initialize cluster assignments matrix
  assn = np.random.random((points.shape[0], num_centers))
  # iterate assignments until they converge
  for i in range(max_iter):
    # compute centers
    w = assn ** m
    w /= w.sum(axis=0)
    centers = w.T.dot(points)
    # calculate new assignments
    d = pairwise_distances(points, centers)
    d **= 2. / (m - 1)
    np.maximum(d, 1e-10, out=d)
    new_assn = 1. / np.einsum('ik,ij->ik', d, 1./d)
    # check for convergence
    change = np.linalg.norm(new_assn - assn)
    if verbose:
      print('At iteration %d: change = %g' % (i+1, change))
    if change < tol:
      break
    assn = new_assn
  else:
    warnings.warn("fuzzy_c_means didn't converge in %d iterations" % max_iter)
  # find points closest to the selected cluster centers
  return d.argmin(axis=0)
Example #20
0
def clip_lower(arr,lower_bound):
    """
    In-place, one-sided version of numpy.clip().

    i.e. numpy.clip(arr,a_min=lower_bound,out=arr) if it existed.
    """
    maximum(arr,lower_bound,arr)
Example #21
0
def compute_overlap(a, b):
    """
    Parameters
    ----------
    a: (N, 4) ndarray of float
    b: (K, 4) ndarray of float
    Returns
    -------
    overlaps: (N, K) ndarray of overlap between boxes and query_boxes
    """
    area = (b[:, 2] - b[:, 0] + 1) * (b[:, 3] - b[:, 1] + 1)

    iw = np.minimum(np.expand_dims(a[:, 2], axis=1), b[:, 2]) - np.maximum(np.expand_dims(a[:, 0], 1), b[:, 0]) + 1
    ih = np.minimum(np.expand_dims(a[:, 3], axis=1), b[:, 3]) - np.maximum(np.expand_dims(a[:, 1], 1), b[:, 1]) + 1

    iw = np.maximum(iw, 0)
    ih = np.maximum(ih, 0)

    ua = np.expand_dims((a[:, 2] - a[:, 0] + 1) * (a[:, 3] - a[:, 1] + 1), axis=1) + area - iw * ih

    ua = np.maximum(ua, np.finfo(float).eps)

    intersection = iw * ih

    return intersection / ua
def MakeQCFlag(ds,SeriesList):
    flag = []
    if len(SeriesList)<=0:
        #log.info('  MakeQCFlag: no series list specified')
        pass
    if len(SeriesList)==1:
        if SeriesList[0] in ds.series.keys():
            flag = ds.series[SeriesList[0]]['Flag'].copy()
        else:
            log.error('  MakeQCFlag: series '+str(SeriesList[0])+' not in ds.series')
    if len(SeriesList)>1:
        for ThisOne in SeriesList:
            if ThisOne in ds.series.keys():
                if len(flag)==0:
                    #flag = numpy.ones(numpy.size(ds.series[ThisOne]['Flag']))
                    flag = ds.series[ThisOne]['Flag'].copy()
                else:
                    tmp_flag = ds.series[ThisOne]['Flag'].copy()      # get a temporary copy of the flag
                    goodindex = numpy.where((numpy.mod(tmp_flag,10)==0) & (numpy.mod(flag,10)==0))    # find the elements with flag = 0, 10, 20 etc
                    badindex1 = numpy.where((numpy.mod(tmp_flag,10)==0) & (numpy.mod(flag,10)!=0))
                    badindex2 = numpy.where((numpy.mod(tmp_flag,10)!=0) & (numpy.mod(flag,10)==0))
                    badindex3 = numpy.where((numpy.mod(tmp_flag,10)!=0) & (numpy.mod(flag,10)!=0))
                    tmp_flag1 = numpy.zeros(len(tmp_flag),dtype=numpy.int32) + tmp_flag
                    flag2 = numpy.zeros(len(flag),dtype=numpy.int32) + flag
                    tmp_flag[badindex1] = 0                               # set them all to 0
                    flag2[badindex2] = 0                               # set them all to 0
                    flag[badindex1] = numpy.maximum(tmp_flag[badindex1],flag[badindex1])               # now take the maximum
                    flag[badindex2] = numpy.maximum(tmp_flag1[badindex2],flag2[badindex2])
                    flag[badindex3] = numpy.maximum(tmp_flag1[badindex3],flag[badindex3])
                    flag[goodindex] = numpy.maximum(tmp_flag1[goodindex],flag[goodindex])
            else:
                log.error('  MakeQCFlag: series '+ThisOne+' not in ds.series')
    return flag
Example #23
0
File: glm.py Project: Naereen/nipy
    def stat(self, baseline=0.0):
        """
        Return the decision statistic associated with the test of the
        null hypothesis: (H0) 'contrast equals baseline'
        """
        self._baseline = baseline

        # Case: one-dimensional contrast ==> t or t**2
        if self.dim == 1:
            # avoids division by zero
            t = (self.effect - baseline) / np.sqrt(
                np.maximum(self.variance, self._tiny))
            if self.type == 'F':
                t = t ** 2
        # Case: F contrast
        elif self.type == 'F':
            # F = |t|^2/q ,  |t|^2 = e^t v-1 e
            t = mahalanobis(self.effect - baseline, np.maximum(
                    self.variance, self._tiny)) / self.dim
        # Case: tmin (conjunctions)
        elif self.type == 'tmin':
            vdiag = self.variance.reshape([self.dim ** 2] + list(
                    self.variance.shape[2:]))[:: self.dim + 1]
            t = (self.effect - baseline) / np.sqrt(
                np.maximum(vdiag, self._tiny))
            t = t.min(0)

        # Unknwon stat
        else:
            raise ValueError('Unknown statistic type')
        self._stat = t
        return t
Example #24
0
 def update_core_cpu(self, param):
     grad = param.grad
     if grad is None:
         return
     hp = self.hyperparam
     eps = grad.dtype.type(hp.eps)
     if hp.eps != 0 and eps == 0:
         raise ValueError(
             'eps of Adam optimizer is too small for {} ({})'.format(
                 grad.dtype.name, hp.eps))
     m, v = self.state['m'], self.state['v']
     if (isinstance(m, intel64.mdarray)
             and isinstance(v, intel64.mdarray)):
         m.inplace_axpby(1.0, 1.0 - hp.beta1, grad - m)
         v.inplace_axpby(1.0, 1.0 - hp.beta2, grad*grad - v)
         if hp.amsgrad:
             vhat = self.state['vhat']
             numpy.maximum(vhat, v, out=vhat)
         else:
             vhat = v
         param.data.inplace_axpby(
             1.0 - hp.weight_decay_rate, -hp.eta,
             self.alpha_t * m / (numpy.sqrt(vhat) + hp.eps))
     else:
         m += (1 - hp.beta1) * (grad - m)
         v += (1 - hp.beta2) * (grad * grad - v)
         if hp.amsgrad:
             vhat = self.state['vhat']
             numpy.maximum(vhat, v, out=vhat)
         else:
             vhat = v
         param.data -= hp.eta * (
             self.alpha_t * m / (numpy.sqrt(vhat) + hp.eps) +
             hp.weight_decay_rate * param.data)
    def SetZoomAxes(self):

        x = self.zoompoint[0]
        y = self.zoompoint[1]
        
        W = params.params.movie.get_width()
        H = params.params.movie.get_height()
        h = H/self.zoomfactor
        w = W/self.zoomfactor
        x1 = x-w/2
        x2 = x+w/2
        y1 = y-h/2
        y2 = y+h/2

        if x1 < 0:
            x2 -= x1
            x1 = 0
        elif x2 > W-1:
            x1 -= (x2 - W + 1)
            x2 = W-1
        if y1 < 0:
            y2 -= y1
            y1 = 0
        elif y2 > H-1:
            y1 -= (y2 - H + 1)
            y2 = H-1
        x1 = num.maximum(int(x1),0)
        x2 = num.minimum(int(x2),W-1)
        y1 = num.maximum(int(y1),0)
        y2 = num.minimum(int(y2),H-1)

        self.zoomaxes = [x1,x2,y1,y2]
        self.ShowImage()
Example #26
0
def ensure_within_bounds(box, size):
    box['xmin'] = np.minimum(np.maximum(box['xmin'], 0), size['width']-1)
    box['xmax'] = np.minimum(np.maximum(box['xmax'], 0), size['width']-1)
    box['ymin'] = np.minimum(np.maximum(box['ymin'], 0), size['height']-1)
    box['ymax'] = np.minimum(np.maximum(box['ymax'], 0), size['height']-1)

    return box
    def process_chunk(self, t0, t1, intensity, weights, pp_intensity, pp_weights):
        # Loop over intensity/weights in chunks of size v1_chunk
        for ichunk in xrange(0, self.nt_chunk, self.v1_chunk):
            for frequency in xrange(self.nfreq):
                # Calculate the v1 for each frequency
                self.v1_tmp[frequency] =  self._v1(intensity[frequency, ichunk:ichunk+self.v1_chunk], weights[frequency, ichunk:ichunk+self.v1_chunk])

            # Once v1s have been calculated for each frequency, update the weights and running variance
            non_zero_v1 = self.v1_tmp != 0
            zero_v1 = np.logical_not(non_zero_v1)

            # For nonzero (successful) v1s, increase the weights (if possible) and update the running variance
            self.running_weights[non_zero_v1] = np.minimum(2.0, self.running_weights[non_zero_v1] + self.w_clamp)
            self.v1_tmp[non_zero_v1] = np.minimum((1-self.var_weight) * self.running_var[non_zero_v1] + self.var_weight * self.v1_tmp[non_zero_v1], 
                                                  self.running_var[non_zero_v1] + self.var_clamp_add + self.running_var[non_zero_v1] * self.var_clamp_mult)
            self.v1_tmp[non_zero_v1] = np.maximum(self.v1_tmp[non_zero_v1], self.running_var[non_zero_v1] - self.var_clamp_add - self.running_var[non_zero_v1] * self.var_clamp_mult)
            self.running_var[non_zero_v1] = self.v1_tmp[non_zero_v1]

            # For unsuccessful v1s, decrease the weights (if possible) and do not modify the running variance 
            self.running_weights[zero_v1] = np.maximum(0, self.running_weights[zero_v1] - self.w_clamp)
            
            # Mask fill!
            intensity_valid = (weights[:, ichunk:ichunk+self.v1_chunk] > self.w_cutoff)
            rand_intensity = np.random.standard_normal(size=intensity[:, ichunk:ichunk+self.v1_chunk].shape)
            for (ifreq,v) in enumerate(self.running_var):
                if v > 0.0:
                    rand_intensity[ifreq, :] *= v**0.5
            intensity[:, ichunk:ichunk+self.v1_chunk] = np.where(intensity_valid, intensity[:, ichunk:ichunk+self.v1_chunk], rand_intensity)
            weights[:, ichunk:ichunk+self.v1_chunk] = np.repeat(self.running_weights, self.v1_chunk).reshape(self.nfreq, self.v1_chunk)
Example #28
0
def make_strictly_feasible(x, lb, ub, rstep=1e-10):
    """Shift a point to the interior of a feasible region.
    
    Each element of the returned vector is at least at a relative distance
    `rstep` from the closest bound. If ``rstep=0`` then `np.nextafter` is used.
    """
    x_new = x.copy()

    active = find_active_constraints(x, lb, ub, rstep)
    lower_mask = np.equal(active, -1)
    upper_mask = np.equal(active, 1)

    if rstep == 0:
        x_new[lower_mask] = np.nextafter(lb[lower_mask], ub[lower_mask])
        x_new[upper_mask] = np.nextafter(ub[upper_mask], lb[upper_mask])
    else:
        x_new[lower_mask] = (lb[lower_mask] +
                             rstep * np.maximum(1, np.abs(lb[lower_mask])))
        x_new[upper_mask] = (ub[upper_mask] -
                             rstep * np.maximum(1, np.abs(ub[upper_mask])))

    tight_bounds = (x_new < lb) | (x_new > ub)
    x_new[tight_bounds] = 0.5 * (lb[tight_bounds] + ub[tight_bounds])

    return x_new
Example #29
0
    def scale(self, factor_x, factor_y=None):
        """
        Expand or contract the bounding box or contour around its center by a given factor

        :param factor_x: The multiplicative scale parameter in the x direction
        :type factor_x: float
        :param factor_y: The multiplicative scale parameter in the y direction
        :type factor_y: float

        .. note::
            if factor_y parameter is omitted, then the factor_x is used in both directions

        .. note::
            The scaling is done with respect to the contour's centroid as computed by the get_centroid
            methods.

        :Example:
        ::

            shape = (100, 100, 3)
            image = np.zeros(shape, dtype=np.uint8)
            d = bounding_region(shape, contour=np.array([[[10, 20]],
                                                         [[25, 15]],
                                                         [[80, 65]],
                                                         [[60, 70]],
                                                         [[20, 75]],
                                                         [[5, 50]]]))
            d.draw_contour(image, color=(0, 255, 0))
            # Scale to half the size
            d.scale(0.5)
            d.draw_contour(image, color=(255, 255, 0))
            d.draw_box(image)
            cv2.imshow("Two contours", image)
            cv2.waitKey(0)

        """
        if self._empty:
            return
        if factor_y is None:
            factor_y = factor_x
        if self.image_shape is None:
            raise Exception("Image shape is nescessary to compute the relative coordinates")
        if self.box_is_primary:
            shift_x = self.box[2] * (1.-factor_x) * 0.5
            shift_y = self.box[3] * (1.-factor_y) * 0.5
            self.box = np.array([np.maximum(self.box[0]+shift_x, 0),
                                 np.maximum(self.box[1]+shift_y, 0),
                                 np.minimum(self.box[2]*factor_x, self.image_shape[1]),
                                 np.minimum(self.box[3]*factor_y, self.image_shape[0])]).astype(np.int32)
            self._contour_from_box()
            self._update_internals()
        else:
            (cx, cy) = self.get_centroid_pixels()
            new_contour = np.zeros_like(self.contour, dtype=np.int32)
            for i in xrange(self.contour.shape[0]):
                new_contour[i][0][0] = np.clip(int(cx + (self.contour[i][0][0]-cx)*factor_x), a_min=0, a_max=self.image_shape[1])
                new_contour[i][0][1] = np.clip(int(cy + (self.contour[i][0][1]-cy)*factor_y), a_min=0, a_max=self.image_shape[0])
            self.contour = new_contour
            self._box_from_contour()
            self._update_internals()
Example #30
0
def conv_backward_naive(dout, cache):
  """
  A naive implementation of the backward pass for a convolutional layer.

  Inputs:
  - dout: Upstream derivatives.
  - cache: A tuple of (x, w, b, conv_param) as in conv_forward_naive

  Returns a tuple of:
  - dx: Gradient with respect to x
  - dw: Gradient with respect to w
  - db: Gradient with respect to b
  """
  x, w, b, conv_param = cache
  p = conv_param['pad']
  s = conv_param['stride']
  
  dx = np.zeros_like(x)
  N, C, H, W = x.shape
  F, C, HH, WW = w.shape
  lmin = 0
  lmax = int((H + 2*p - HH)/s)
  kmin = 0
  kmax = int((W + 2*p - WW)/s)
  for n in range(N):
      for c in range(C):
          for h in range(H):
              for g in range(W):
                  delta = 0.0
                  l0 = int(np.maximum(lmin, np.ceil((h+p-HH+1)/s)))
                  l1 = int(np.minimum(lmax, np.floor((h+p)/s)))
                  k0 = int(np.maximum(kmin,np.ceil((g+p-WW+1)/s)))
                  k1 = int(np.minimum(kmax, np.floor((g+p)/s)))
                  for l in range(l0,l1+1):
                      for k in range(k0, k1+1):
                          for f in range(F):
                              delta += w[f,c,h + p - l * s, g + p - k*s] * dout[n,f,l,k]
          
                  dx[n,c,h,g] = delta  
  
  dw = np.zeros_like(w)  
  for f in range(F):
      for c in range(C):
          for i in range(HH):
              for j in range(WW):
                  delta = 0.0
                  l0 = int(np.maximum(lmin, np.ceil((p-i)/s)))
                  l1 = int(np.minimum(lmax, np.floor((p-i+H-1)/s)))
                  k0 = int(np.maximum(kmin, np.ceil((p-j)/s)))
                  k1 = int(np.minimum(kmax, np.floor((p-j+W-1)/s)))
                  for l in range(l0, l1 + 1):
                      for k in range(k0, k1 + 1):
                          for n in range(N):
                              delta += x[n,c,-p+l*s+i, -p+k*s+j] * dout[n,f,l,k]
      
                  dw[f,c,i,j] = delta

  db = np.sum(dout, axis=(0,2,3))
 ##########
  return dx, dw, db