def distance(cls, latitude, longitude): earth_radius = 6371 pi_converter = func.pi() / 180 dlat = (latitude - cls.latitude) * pi_converter dlon = (longitude - cls.longitude) * pi_converter lat1 = (cls.latitude) * pi_converter lat2 = (latitude) * pi_converter haversine = func.sin(dlat/2) * func.sin(dlat/2) + func.sin(dlon/2) * func.sin(dlon/2) * func.cos(lat1) * func.cos(lat2) return earth_radius * 2 * func.atan2(func.sqrt(haversine), func.sqrt(1-haversine))
def haversine(cls, other): r = 6371 lat1, lon1 = cls.latitude_pos, cls.longitude_pos lat2, lon2 = other.latitude_pos, other.longitude_pos import math dlat = func.radians(lat2 - lat1) dlon = func.radians(lon2 - lon1) a = func.sin(dlat/2) * func.sin(dlat/2) + func.cos(func.radians(lat1)) \ * func.cos(math.radians(lat2)) * func.sin(dlon/2) * func.sin(dlon/2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) d = r * c return d
def distance(pt1, pt2): lat1, lon1 = pt1 lat2, lon2 = pt2 R = 3961 dlat = func.radians(lat2 - lat1) dlon = func.radians(lon2 - lon1) a = func.power(func.sin(dlat / 2), 2) + func.cos(func.radians(lat1)) * func.cos( func.radians(lat2) ) * func.power(func.sin(dlon / 2), 2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) d = R * c return d
def in_range(cls, latitude, longitude, radius): """Check if user in range (radius in meters).""" R = 6373.0 # approximate radius of earth in km from sqlalchemy import func lat1 = func.radians(cls.latitude) lon1 = func.radians(cls.longitude) lat2 = func.radians(latitude) lon2 = func.radians(longitude) dlon = lon2 - lon1 dlat = lat2 - lat1 a = func.pow(func.sin(dlat / 2), 2) + func.cos(lat1) * func.cos(lat2) \ * func.pow(func.sin(dlon / 2), 2) c = R * 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) print(c) return c
def get_distance(self, user_lat, user_lng): pi180 = pi / 180 lat1 = float(user_lat) * pi180 lng1 = float(user_lng) * pi180 lat2 = Location.lat * pi180 lng2 = Location.lng * pi180 r = 6371 dlat = lat2 - lat1 dlng = lng2 - lng1 a = func.sin(dlat / 2) * func.sin(dlat / 2) + func.cos( lat1) * func.cos(lat2) * func.sin(dlng / 2) * func.sin(dlng / 2) c = 2 * func.atan2(func.sqrt(a), func.sqrt(1 - a)) km = r * c return km
def standardize_scores(query, grouping_cols): """ Recalibrate scores to be calibrated against others' answers. Args: query (flask_sqlalchemy.BaseQuery): grouping_cols (list): List of columns to group by Returns: """ query = query.add_columns( func.sum(Alignment.raw_score).over( partition_by=[Dimension.name, Respondent.id]).label('raw_sum'), func.avg(Alignment.binary_score).over( partition_by=[Dimension.name, Option.question_id]).label('mean')) std = func.nullif(func.sqrt(column('mean') * (1 - column('mean'))), 0) query = query.from_self('dimension', 'raw_score', 'binary_score', 'question_id', 'option_id', ((column('binary_score') - column('mean')) / std).label('adjusted_score'), *grouping_cols) query = query.from_self(*grouping_cols) \ .add_columns(_sum_case_when('Chaotic vs. Lawful'), _sum_case_when('Evil vs. Good')) \ .group_by(*grouping_cols) return query
def dist_to(self, other): """ Compute the distance from this system to other. """ return sqlfunc.sqrt((other.x - self.x) * (other.x - self.x) + (other.y - self.y) * (other.y - self.y) + (other.z - self.z) * (other.z - self.z))
def best(cls): n = cls.upvotes + cls.downvotes z = 1.281551565545 p = cls.upvotes * 1.0 / n left = p + z * z / (2 * n) right = z * func.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) under = 1 + z * z / n return func.IF(n == 0, 0, 100 * (left - right) / under)
def calculate_score(ws, ls): # wins, losses ws += 1 # Add hidden win, so drawings with 0 losses get a score bigger than 0 n = cast(ws + ls, Float) # Integer division in Postgres returns an integer score = ((ws + z**2 / 2) / n - z * func.sqrt((ws * ls) / n + z**2 / 4) / n) / (1 + z**2 / n) # Normalize score to a range from 0 to 1 score = (score - score_min) / (score_max - score_min) return score
def get_using_self(lat, lng): lat = float(lat) lng = float(lng) distance_func = func.sqrt((111.12 * (Location.lat - lat)) * (111.12 * (Location.lat - lat)) + ( 111.12 * (Location.lng - lng) * func.cos(lat / 92.215)) * ( 111.12 * (Location.lng - lng) * func.cos(lat / 92.215))); query = db.session.query(Location, distance_func).filter(distance_func < 5).order_by(distance_func) result = query.all() mapped = [] for row in result: mapped.append({"name": row[0].__dict__["name"]}) return jsonify(mapped)
def hybridMag(cls): if index is not None: # It needs to be index + 1 because Postgresql arrays are 1-indexed. flux = getattr(cls, flux_parameter)[index + 1] else: flux = getattr(cls, flux_parameter) flux *= 1e-9 bb_band = bb[band] xx = flux / (2. * bb_band) asinh_mag = (-2.5 / func.log(10) * (func.log(xx + func.sqrt(func.pow(xx, 2) + 1)) + func.log(bb_band))) return cast(asinh_mag, Float)
def score(cls): ups = select([func.sum(AnswerVote.vote) ]).where(AnswerVote.answer_id == cls.id and AnswerVote.vote == 1).label('ups') downs = select([func.sum(AnswerVote.vote) ]).where(AnswerVote.answer_id == cls.id and AnswerVote.vote == -1).label('downs') n = ups - downs if n == 0: return 0 z = 1.0 phat = ups / n return (phat + z * z / (2 * n) - z * func.sqrt( (phat * (1 - phat) + z * z / (4 * n)) / n)) / (1 + z * z / n)
def get_labels(session, score, detection_filters, vehicle_filters, model, threshold): """Retrieves all possible detection-annotation pairings that satify the VOC criterion.""" overlap_score = overlap(Detection, Vehicle) # pylint: disable-msg=E1101 dist_x = (func.ST_X(func.ST_Transform(Detection.lla, 102718)) - func.ST_X(func.ST_Transform(Vehicle.lla, 102718))) \ * 0.3048 dist_y = (func.ST_Y(func.ST_Transform(Detection.lla, 102718)) - func.ST_Y(func.ST_Transform(Vehicle.lla, 102718))) \ * 0.3048 dist = func.sqrt(dist_x * dist_x + dist_y * dist_y) height_diff = func.abs( func.ST_Z(Detection.lla) - func.ST_Z(Vehicle.lla)) labels = session.query( overlap_score.label('overlap'), Vehicle.id.label('vid'), Detection.id.label('did'), dist.label('dist'), height_diff.label('height_diff'), score.label('score')) \ .select_from(Detection) \ .join(Photo) \ .join(Vehicle) \ .join(Model) \ .filter(Model.filename == model) \ .filter(Photo.test == True) \ .filter(overlap_score > 0.5) \ .filter(score > threshold) # pylint: enable-msg=E1101 for query_filter in detection_filters: labels = labels.filter(query_filter) for query_filter in vehicle_filters: labels = labels.filter(query_filter) labels = labels.order_by(desc(overlap_score)).all() return labels
def distance(cls, pos): # approximate radius of earth in km R = 6373.0 (pos_lat, pos_lng) = pos pos_lat = math.radians(pos_lat) pos_lng = math.radians(pos_lng) rad_latitude = func.radians(cls.latitude) rad_longitude = func.radians(cls.longitude) delta_lat = pos_lat - rad_latitude delta_lng = pos_lng - rad_longitude a = func.pow(func.sin(delta_lat / 2), 2) + func.cos(pos_lat) * \ func.cos(rad_latitude) * func.pow(func.sin(delta_lng / 2), 2) c = 2 * func.asin(func.sqrt(a)) return R * c
def calc_distance(p1, p2): return func.sqrt( func.pow(69.1 * (p1[0] - p2[0], 2) + func.pow(53.0 * (p1[1] - p2[1]), 2)))
def calc_distance(lat1, lon1, lat2, lon2): Earth_radius_km = 6371.009 km_per_deg_lat = 2 * 3.14159265 * Earth_radius_km / 360.0 km_per_deg_lon = km_per_deg_lat * func.cos(func.radians(lat1)) distance = func.sqrt(func.pow((km_per_deg_lat * (lat1 - lat2)), 2) + func.pow((km_per_deg_lon * (lon1 - lon2)), 2)) return distance
def distance(cls, other): return func.abs( func.sqrt( func.pow(other.x - cls.x, 2) + func.pow(other.y - cls.y, 2) + func.pow(other.z - cls.z, 2)))
def distance(self, lat, lon): return func.sqrt((self._latitude - lat) * (self._latitude - lat) + (self._longitude - lon) * (self._longitude - lon))