def get_distance(locA, locB): # use haversine forumla print "ayyo" earth_rad = 6371.0 dlat = deg2rad(locB[0] - locA[0]) dlon = deg2rad(locB[1] - locA[1]) a = sin(dlat / 2) * sin(dlat / 2) + \ cos(deg2rad(locA[0])) * cos(deg2rad(locB[0])) * \ sin(dlon / 2) * sin(dlon / 2) c = 2 * arctan2(sqrt(a), sqrt(1 - a)) return earth_rad * c
def angle(z, deg=0): """Return the angle of the complex argument z. """ if deg: fact = 180/pi else: fact = 1.0 z = asarray(z) if (issubclass(z.dtype.type, _nx.complexfloating)): zimag = z.imag zreal = z.real else: zimag = 0 zreal = z return arctan2(zimag, zreal) * fact
def update_metric(self, pos_mat, ang_vec): #This method allows us to update the flocking model with the Metric model #Each new velocity is constructed by averaging over all of the velocities within #the radius selected, self.r. #Inputs -- x-coordinates, y-coordinates, trajectories for time = t #Outputs -- x-coordinates, y-coordinates, trajectories for time = t + (delta t) avg_sin = 0 * ang_vec avg_cos = 0 * ang_vec for j in range(0, self.N): #find distances for all particles dist_vec = self.calc_dist(pos_mat, j) #find indicies that are within the radius ngbs_idx_vec = np.where(dist_vec <= self.r)[0] if len(ngbs_idx_vec) == 0: avg_sin[j] = 0 avg_cos[j] = 0 else: #find average velocity of those inside the radius sint = np.average(sin(ang_vec[ngbs_idx_vec])) cost = np.average(cos(ang_vec[ngbs_idx_vec])) avg_sin[j] = sint avg_cos[j] = cost #construct the noise noise = self.theta_noise() #update velocities and positions cosi = (self.ep) * avg_cos + (1 - self.ep) * np.cos(ang_vec) sini = (self.ep) * avg_sin + (1 - self.ep) * np.sin(ang_vec) new_ang_vec = arctan2(sini, cosi) + noise mask = self.status_vec == 1 new_ang_vec[mask] += np.pi new_mod_ang_vec = np.mod(new_ang_vec, 2 * np.pi) pos_mat[:, 0] = pos_mat[:, 0] + self.dt * self.v * cos(new_mod_ang_vec) pos_mat[:, 1] = pos_mat[:, 1] + self.dt * self.v * sin(new_mod_ang_vec) #Make sure that the positions are not outside the boundary. #If so, correct for periodicity pos_mat = self.check_boundary(pos_mat) #Outputs returned return (pos_mat, new_ang_vec)
def sunposIntermediate(iYear, iMonth, iDay, dHours, dMinutes, dSeconds): # Calculate difference in days between the current Julian Day # and JD 2451545.0, which is noon 1 January 2000 Universal Time # Calculate time of the day in UT decimal hours dDecimalHours = dHours + (dMinutes + dSeconds / 60.0) / 60.0 # Calculate current Julian Day liAux1 = (iMonth - 14) / 12 liAux2 = (1461 * (iYear + 4800 + liAux1)) / 4 + ( 367 * (iMonth - 2 - 12 * liAux1)) / 12 - (3 * ( (iYear + 4900 + liAux1) / 100)) / 4 + iDay - 32075 dJulianDate = liAux2 - 0.5 + dDecimalHours / 24.0 # Calculate difference between current Julian Day and JD 2451545.0 dElapsedJulianDays = dJulianDate - 2451545.0 # Calculate ecliptic coordinates (ecliptic longitude and obliquity of the # ecliptic in radians but without limiting the angle to be less than 2*Pi # (i.e., the result may be greater than 2*Pi) dOmega = 2.1429 - 0.0010394594 * dElapsedJulianDays dMeanLongitude = 4.8950630 + 0.017202791698 * dElapsedJulianDays # Radians dMeanAnomaly = 6.2400600 + 0.0172019699 * dElapsedJulianDays dEclipticLongitude = dMeanLongitude + 0.03341607 * sin( dMeanAnomaly) + 0.00034894 * sin( 2 * dMeanAnomaly) - 0.0001134 - 0.0000203 * sin(dOmega) dEclipticObliquity = 0.4090928 - 6.2140e-9 * dElapsedJulianDays + 0.0000396 * cos( dOmega) # Calculate celestial coordinates ( right ascension and declination ) in radians # but without limiting the angle to be less than 2*Pi (i.e., the result may be # greater than 2*Pi) dSin_EclipticLongitude = sin(dEclipticLongitude) dY = cos(dEclipticObliquity) * dSin_EclipticLongitude dX = cos(dEclipticLongitude) dRightAscension = arctan2(dY, dX) if dRightAscension < 0.0: dRightAscension = dRightAscension + 2 * pi dDeclination = arcsin(sin(dEclipticObliquity) * dSin_EclipticLongitude) dGreenwichMeanSiderealTime = 6.6974243242 + 0.0657098283 * dElapsedJulianDays + dDecimalHours return (dRightAscension, dDeclination, dGreenwichMeanSiderealTime)
def assert_arctan2_isnan(x, y): assert_(np.isnan(ncu.arctan2(x, y)), "arctan(%s, %s) is %s, not nan" % (x, y, ncu.arctan2(x, y)))
def assert_arctan2_isnzero(x, y): assert_( (ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y))), "arctan(%s, %s) is %s, not -0" % (x, y, ncu.arctan2(x, y)), )
def test_inf_any(self): # atan2(+-infinity, x) returns +-pi/2 for finite x. assert_almost_equal(ncu.arctan2(np.inf, 1), 0.5 * np.pi) assert_almost_equal(ncu.arctan2(-np.inf, 1), -0.5 * np.pi)
def test_inf_pinf(self): # atan2(+-infinity, +infinity) returns +-pi/4. assert_almost_equal(ncu.arctan2(np.inf, np.inf), 0.25 * np.pi) assert_almost_equal(ncu.arctan2(-np.inf, np.inf), -0.25 * np.pi)
def test_zero_negative(self): # atan2(+-0, x) returns +-pi for x < 0. assert_almost_equal(ncu.arctan2(np.PZERO, -1), np.pi) assert_almost_equal(ncu.arctan2(np.NZERO, -1), -np.pi)
def test_negative_zero(self): # atan2(y, +-0) returns -pi/2 for y < 0. assert_almost_equal(ncu.arctan2(-1, np.PZERO), -0.5 * np.pi) assert_almost_equal(ncu.arctan2(-1, np.NZERO), -0.5 * np.pi)
def test_any_ninf(self): # atan2(+-y, -infinity) returns +-pi for finite y > 0. assert_almost_equal(ncu.arctan2(1, np.NINF), np.pi) assert_almost_equal(ncu.arctan2(-1, np.NINF), -np.pi)
def hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(3, 3), visualise=False, normalise=False): """Extract Histogram of Oriented Gradients (HOG) for a given image. Compute a Histogram of Oriented Gradients (HOG) by 1. (optional) global image normalisation 2. computing the gradient image in x and y 3. computing gradient histograms 4. normalising across blocks 5. flattening into a feature vector Parameters ---------- image : (M, N) ndarray Input image (greyscale). orientations : int Number of orientation bins. pixels_per_cell : 2 tuple (int, int) Size (in pixels) of a cell. cells_per_block : 2 tuple (int,int) Number of cells in each block. visualise : bool, optional Also return an image of the HOG. normalise : bool, optional Apply power law compression to normalise the image before processing. Returns ------- newarr : ndarray HOG for the image as a 1D (flattened) array. hog_image : ndarray (if visualise=True) A visualisation of the HOG image. References ---------- * http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients * Dalal, N and Triggs, B, Histograms of Oriented Gradients for Human Detection, IEEE Computer Society Conference on Computer Vision and Pattern Recognition 2005 San Diego, CA, USA """ image = np.atleast_2d(image) """ The first stage applies an optional global image normalisation equalisation that is designed to reduce the influence of illumination effects. In practice we use gamma (power law) compression, either computing the square root or the log of each colour channel. Image texture strength is typically proportional to the local surface illumination so this compression helps to reduce the effects of local shadowing and illumination variations. """ assert_nD(image, 2) if normalise: image = sqrt(image) """ The second stage computes first order image gradients. These capture contour, silhouette and some texture information, while providing further resistance to illumination variations. The locally dominant colour channel is used, which provides colour invariance to a large extent. Variant methods may also include second order image derivatives, which act as primitive bar detectors - a useful feature for capturing, e.g. bar like structures in bicycles and limbs in humans. """ if image.dtype.kind == 'u': # convert uint image to float # to avoid problems with subtracting unsigned numbers in np.diff() image = image.astype('float') gx = np.empty(image.shape, dtype=np.double) gx[:, 0] = 0 gx[:, -1] = 0 gx[:, 1:-1] = image[:, 2:] - image[:, :-2] gy = np.empty(image.shape, dtype=np.double) gy[0, :] = 0 gy[-1, :] = 0 gy[1:-1, :] = image[2:, :] - image[:-2, :] """ The third stage aims to produce an encoding that is sensitive to local image content while remaining resistant to small changes in pose or appearance. The adopted method pools gradient orientation information locally in the same way as the SIFT [Lowe 2004] feature. The image window is divided into small spatial regions, called "cells". For each cell we accumulate a local 1-D histogram of gradient or edge orientations over all the pixels in the cell. This combined cell-level 1-D histogram forms the basic "orientation histogram" representation. Each orientation histogram divides the gradient angle range into a fixed number of predetermined bins. The gradient magnitudes of the pixels in the cell are used to vote into the orientation histogram. """ magnitude = sqrt(gx**2 + gy**2) orientation = arctan2(gy, gx) * (180 / pi) % 180 sy, sx = image.shape cx, cy = pixels_per_cell bx, by = cells_per_block n_cellsx = int(np.floor(sx // cx)) # number of cells in x n_cellsy = int(np.floor(sy // cy)) # number of cells in y # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) subsample = np.index_exp[cy // 2:cy * n_cellsy:cy, cx // 2:cx * n_cellsx:cx] for i in range(orientations): # create new integral image for this orientation # isolate orientations in this range temp_ori = np.where(orientation < 180.0 / orientations * (i + 1), orientation, -1) temp_ori = np.where(orientation >= 180.0 / orientations * i, temp_ori, -1) # select magnitudes for those orientations cond2 = temp_ori > -1 temp_mag = np.where(cond2, magnitude, 0) temp_filt = uniform_filter(temp_mag, size=(cy, cx)) orientation_histogram[:, :, i] = temp_filt[subsample] # now for each cell, compute the histogram hog_image = None if visualise: from .. import draw radius = min(cx, cy) // 2 - 1 hog_image = np.zeros((sy, sx), dtype=float) for x in range(n_cellsx): for y in range(n_cellsy): for o in range(orientations): centre = tuple([y * cy + cy // 2, x * cx + cx // 2]) dx = radius * cos(float(o) / orientations * np.pi) dy = radius * sin(float(o) / orientations * np.pi) rr, cc = draw.line(int(centre[0] - dx), int(centre[1] + dy), int(centre[0] + dx), int(centre[1] - dy)) hog_image[rr, cc] += orientation_histogram[y, x, o] """ The fourth stage computes normalisation, which takes local groups of cells and contrast normalises their overall responses before passing to next stage. Normalisation introduces better invariance to illumination, shadowing, and edge contrast. It is performed by accumulating a measure of local histogram "energy" over local groups of cells that we call "blocks". The result is used to normalise each cell in the block. Typically each individual cell is shared between several blocks, but its normalisations are block dependent and thus different. The cell thus appears several times in the final output vector with different normalisations. This may seem redundant but it improves the performance. We refer to the normalised block descriptors as Histogram of Oriented Gradient (HOG) descriptors. """ n_blocksx = (n_cellsx - bx) + 1 n_blocksy = (n_cellsy - by) + 1 normalised_blocks = np.zeros((n_blocksy, n_blocksx, by, bx, orientations)) for x in range(n_blocksx): for y in range(n_blocksy): block = orientation_histogram[y:y + by, x:x + bx, :] eps = 1e-5 normalised_blocks[y, x, :] = block / sqrt(block.sum()**2 + eps) """ The final step collects the HOG descriptors from all blocks of a dense overlapping grid of blocks covering the detection window into a combined feature vector for use in the window classifier. """ if visualise: return normalised_blocks, hog_image else: return normalised_blocks.squeeze()
def test_zero_nzero(self): # atan2(+-0, -0) returns +-pi. assert_almost_equal(ncu.arctan2(np.PZERO, np.NZERO), np.pi) assert_almost_equal(ncu.arctan2(np.NZERO, np.NZERO), -np.pi)
def assert_arctan2_isnzero(x, y): assert ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y))
def assert_arctan2_isninf(x, y): assert np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) < 0
def assert_arctan2_isnan(x, y): assert np.isnan(ncu.arctan2(x, y))
def test_arctan2_special_values(): def assert_arctan2_isnan(x, y): assert np.isnan(ncu.arctan2(x, y)) def assert_arctan2_ispinf(x, y): assert np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) > 0 def assert_arctan2_isninf(x, y): assert np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) < 0 def assert_arctan2_ispzero(x, y): assert ncu.arctan2(x, y) == 0 and not np.signbit(ncu.arctan2(x, y)) def assert_arctan2_isnzero(x, y): assert ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y)) # atan2(1, 1) returns pi/4. yield assert_almost_equal, ncu.arctan2(1, 1), 0.25 * np.pi yield assert_almost_equal, ncu.arctan2(-1, 1), -0.25 * np.pi yield assert_almost_equal, ncu.arctan2(1, -1), 0.75 * np.pi # atan2(+-0, -0) returns +-pi. yield assert_almost_equal, ncu.arctan2(np.PZERO, np.NZERO), np.pi yield assert_almost_equal, ncu.arctan2(np.NZERO, np.NZERO), -np.pi # atan2(+-0, +0) returns +-0. yield assert_arctan2_ispzero, np.PZERO, np.PZERO yield assert_arctan2_isnzero, np.NZERO, np.PZERO # atan2(+-0, x) returns +-pi for x < 0. yield assert_almost_equal, ncu.arctan2(np.PZERO, -1), np.pi yield assert_almost_equal, ncu.arctan2(np.NZERO, -1), -np.pi # atan2(+-0, x) returns +-0 for x > 0. yield assert_arctan2_ispzero, np.PZERO, 1 yield assert_arctan2_isnzero, np.NZERO, 1 # atan2(y, +-0) returns +pi/2 for y > 0. yield assert_almost_equal, ncu.arctan2(1, np.PZERO), 0.5 * np.pi yield assert_almost_equal, ncu.arctan2(1, np.NZERO), 0.5 * np.pi # atan2(y, +-0) returns -pi/2 for y < 0. yield assert_almost_equal, ncu.arctan2(-1, np.PZERO), -0.5 * np.pi yield assert_almost_equal, ncu.arctan2(-1, np.NZERO), -0.5 * np.pi # atan2(+-y, -infinity) returns +-pi for finite y > 0. yield assert_almost_equal, ncu.arctan2(1, np.NINF), np.pi yield assert_almost_equal, ncu.arctan2(-1, np.NINF), -np.pi # atan2(+-y, +infinity) returns +-0 for finite y > 0. yield assert_arctan2_ispzero, 1, np.inf yield assert_arctan2_isnzero, -1, np.inf # atan2(+-infinity, x) returns +-pi/2 for finite x. yield assert_almost_equal, ncu.arctan2(np.inf, 1), 0.5 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, 1), -0.5 * np.pi # atan2(+-infinity, -infinity) returns +-3*pi/4. yield assert_almost_equal, ncu.arctan2(np.inf, -np.inf), 0.75 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, -np.inf), -0.75 * np.pi # atan2(+-infinity, +infinity) returns +-pi/4. yield assert_almost_equal, ncu.arctan2(np.inf, np.inf), 0.25 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, np.inf), -0.25 * np.pi # atan2(nan, x) returns nan for any x, including inf yield assert_arctan2_isnan, np.nan, np.inf yield assert_arctan2_isnan, np.inf, np.nan yield assert_arctan2_isnan, np.nan, np.nan
def test_positive_zero(self): # atan2(y, +-0) returns +pi/2 for y > 0. assert_almost_equal(ncu.arctan2(1, np.PZERO), 0.5 * np.pi) assert_almost_equal(ncu.arctan2(1, np.NZERO), 0.5 * np.pi)
def getAngleToNegativeXAxis(self): rad = arctan2( self[1], self[0]); deg = (rad/pi)*180.0 + 180.0; return deg
def test_inf_ninf(self): # atan2(+-infinity, -infinity) returns +-3*pi/4. assert_almost_equal(ncu.arctan2(np.inf, -np.inf), 0.75 * np.pi) assert_almost_equal(ncu.arctan2(-np.inf, -np.inf), -0.75 * np.pi)
def test_arctan2_special_values(): def assert_arctan2_isnan(x, y): assert np.isnan(ncu.arctan2(x, y)) def assert_arctan2_ispinf(x, y): assert np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) > 0 def assert_arctan2_isninf(x, y): assert np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) < 0 def assert_arctan2_ispzero(x, y): assert ncu.arctan2(x, y) == 0 and not np.signbit(ncu.arctan2(x, y)) def assert_arctan2_isnzero(x, y): assert ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y)) # atan2(1, 1) returns pi/4. yield assert_almost_equal, ncu.arctan2(1, 1), 0.25 * np.pi yield assert_almost_equal, ncu.arctan2(-1, 1), -0.25 * np.pi yield assert_almost_equal, ncu.arctan2(1, -1), 0.75 * np.pi # atan2(+-0, -0) returns +-pi. yield assert_almost_equal, ncu.arctan2(np.PZERO, np.NZERO), np.pi yield assert_almost_equal, ncu.arctan2(np.NZERO, np.NZERO), -np.pi # atan2(+-0, +0) returns +-0. yield assert_arctan2_ispzero, np.PZERO, np.PZERO yield assert_arctan2_isnzero, np.NZERO, np.PZERO # atan2(+-0, x) returns +-pi for x < 0. yield assert_almost_equal, ncu.arctan2(np.PZERO, -1), np.pi yield assert_almost_equal, ncu.arctan2(np.NZERO, -1), -np.pi # atan2(+-0, x) returns +-0 for x > 0. yield assert_arctan2_ispzero, np.PZERO, 1 yield assert_arctan2_isnzero, np.NZERO, 1 # atan2(y, +-0) returns +pi/2 for y > 0. yield assert_almost_equal, ncu.arctan2(1, np.PZERO), 0.5 * np.pi yield assert_almost_equal, ncu.arctan2(1, np.NZERO), 0.5 * np.pi # atan2(y, +-0) returns -pi/2 for y < 0. yield assert_almost_equal, ncu.arctan2(-1, np.PZERO), -0.5 * np.pi yield assert_almost_equal, ncu.arctan2(-1, np.NZERO), -0.5 * np.pi # atan2(+-y, -infinity) returns +-pi for finite y > 0. yield assert_almost_equal, ncu.arctan2(1, np.NINF), np.pi yield assert_almost_equal, ncu.arctan2(-1, np.NINF), -np.pi # atan2(+-y, +infinity) returns +-0 for finite y > 0. yield assert_arctan2_ispzero, 1, np.inf yield assert_arctan2_isnzero, -1, np.inf # atan2(+-infinity, x) returns +-pi/2 for finite x. yield assert_almost_equal, ncu.arctan2( np.inf, 1), 0.5 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, 1), -0.5 * np.pi # atan2(+-infinity, -infinity) returns +-3*pi/4. yield assert_almost_equal, ncu.arctan2( np.inf, -np.inf), 0.75 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, -np.inf), -0.75 * np.pi # atan2(+-infinity, +infinity) returns +-pi/4. yield assert_almost_equal, ncu.arctan2( np.inf, np.inf), 0.25 * np.pi yield assert_almost_equal, ncu.arctan2(-np.inf, np.inf), -0.25 * np.pi # atan2(nan, x) returns nan for any x, including inf yield assert_arctan2_isnan, np.nan, np.inf yield assert_arctan2_isnan, np.inf, np.nan yield assert_arctan2_isnan, np.nan, np.nan
def assert_arctan2_ispinf(x, y): assert (np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) > 0 ), "arctan(%s, %s) is %s, not +inf" % (x, y, ncu.arctan2(x, y))
def assert_arctan2_ispinf(x, y): assert_((np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) > 0), "arctan(%s, %s) is %s, not +inf" % (x, y, ncu.arctan2(x, y)))
def assert_arctan2_ispzero(x, y): assert (ncu.arctan2(x, y) == 0 and not np.signbit(ncu.arctan2(x, y)) ), "arctan(%s, %s) is %s, not +0" % (x, y, ncu.arctan2(x, y))
def assert_arctan2_isninf(x, y): assert_( (np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) < 0), "arctan(%s, %s) is %s, not -inf" % (x, y, ncu.arctan2(x, y)), )
def assert_arctan2_ispzero(x, y): assert (ncu.arctan2(x, y) == 0 and not np.signbit(ncu.arctan2(x, y))), "arctan(%s, %s) is %s, not +0" % (x, y, ncu.arctan2(x, y))
def test_one_one(self): # atan2(1, 1) returns pi/4. assert_almost_equal(ncu.arctan2(1, 1), 0.25 * np.pi) assert_almost_equal(ncu.arctan2(-1, 1), -0.25 * np.pi) assert_almost_equal(ncu.arctan2(1, -1), 0.75 * np.pi)
def assert_arctan2_isninf(x, y): assert_((np.isinf(ncu.arctan2(x, y)) and ncu.arctan2(x, y) < 0), "arctan(%s, %s) is %s, not -inf" % (x, y, ncu.arctan2(x, y)))
def assert_arctan2_isnzero(x, y): assert_((ncu.arctan2(x, y) == 0 and np.signbit(ncu.arctan2(x, y))), "arctan(%s, %s) is %s, not -0" % (x, y, ncu.arctan2(x, y)))
def anglePoints(x, y): # Angle between two points. ang1 = arctan2(x[0], x[1]) ang2 = arctan2(y[0], y[1]) arctan2 return rad2deg((ang1 - ang2) % (2 * pi))