def test_ulc_one_point_cluster(self): """ Tests update_location_centroid, using a cluster with one point """ max_distance = 20 min_samples = 2 point = Point(30.0, -9.0, None) # points to far apart, should compute centroid from the two cluster = [Point(0.0, 0.0, None)] centroid, new_cluster = update_location_centroid(point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(15.0, -4.5, None)) self.assert_points(new_cluster, [Point(0.0, 0.0, None), point]) # points close, both contribute cluster = [Point(30.0001, -8.9999, None)] centroid, new_cluster = update_location_centroid(point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(30.00005, -8.99995, None)) self.assert_points(new_cluster, [Point(30.0001, -8.9999, None), point])
def test_ulc_empty_cluster(self): """ Tests update_location_centroid, with empty cluster """ max_distance = 20 min_samples = 2 point = Point(30.0, -9.0, None) centroid, new_cluster = update_location_centroid(point, [], max_distance, min_samples) self.assert_point(centroid, point) self.assert_points(new_cluster, [point])
def test_ulc_one_point_cluster(self): """ Tests update_location_centroid, using a cluster with one point """ max_distance = 20 min_samples = 2 point = Point(30.0, -9.0, None) # points to far apart, should compute centroid from the two cluster = [Point(0.0, 0.0, None)] centroid, new_cluster = update_location_centroid( point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(15.0, -4.5, None)) self.assert_points(new_cluster, [Point(0.0, 0.0, None), point]) # points close, both contribute cluster = [Point(30.0001, -8.9999, None)] centroid, new_cluster = update_location_centroid( point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(30.00005, -8.99995, None)) self.assert_points(new_cluster, [Point(30.0001, -8.9999, None), point])
def test_ulc_empty_cluster(self): """ Tests update_location_centroid, with empty cluster """ max_distance = 20 min_samples = 2 point = Point(30.0, -9.0, None) centroid, new_cluster = update_location_centroid( point, [], max_distance, min_samples) self.assert_point(centroid, point) self.assert_points(new_cluster, [point])
def test_ulc_points_cluster(self): """ Tests update_location_centroid, using a cluster with more than one point """ max_distance = 20 min_samples = 2 point = Point(8.031852, 2.487095, None) p_1 = Point(8.03364868140281, 2.489711813527904, None) p_2 = Point(8.03364868140281, 2.489711813527904, None) cluster = [p_1, p_2] centroid, new_cluster = update_location_centroid(point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(8.03364868140281, 2.489711813527904, None)) self.assert_points(new_cluster, [p_1, p_2, point])
def test_ulc(self): """ Tests update_location_centroid for centroid computation """ max_distance = 20 min_samples = 2 p_1 = Point(8.0315188471279, 2.48728733258197, None) p_2 = Point(8.0335721637393, 2.4895146785343, None) p_3 = Point(8.0335721637393, 2.4895146785343, None) cluster = [p_1, p_2, p_3] point = Point(8.0318516937495, 2.48709511068233, None) centroid, new_cluster = update_location_centroid(point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(8.0335721637393, 2.4895146785343, None)) self.assert_points(new_cluster, [p_1, p_2, p_3, point])
def test_ulc_points_cluster(self): """ Tests update_location_centroid, using a cluster with more than one point """ max_distance = 20 min_samples = 2 point = Point(8.031852, 2.487095, None) p_1 = Point(8.03364868140281, 2.489711813527904, None) p_2 = Point(8.03364868140281, 2.489711813527904, None) cluster = [p_1, p_2] centroid, new_cluster = update_location_centroid( point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(8.03364868140281, 2.489711813527904, None)) self.assert_points(new_cluster, [p_1, p_2, point])
def insert_location(cur, label, point, max_distance, min_samples): """ Inserts a location into the database Args: cur (:obj:`psycopg2.cursor`) label (str): Location's name point (:obj:`Point`): Position marked with current label max_distance (float): Max location distance. See `tracktotrip.location.update_location_centroid` min_samples (float): Minimum samples requires for location. See `tracktotrip.location.update_location_centroid` """ label = unicode(label, 'utf-8') print 'Inserting location %s, %f, %f' % (label, point.lat, point.lon) cur.execute(""" SELECT location_id, label, centroid, point_cluster FROM locations WHERE label=%s ORDER BY ST_Distance(centroid, %s) """, (label, point)) if cur.rowcount > 0: # Updates current location set of points and centroid location_id, _, centroid, point_cluster = cur.fetchone() centroid = to_point(centroid) point_cluster = to_segment(point_cluster).points # print 'Previous point %s, cluster %s' % (point, [p.to_json() for p in point_cluster]) centroid, point_cluster = update_location_centroid( point, point_cluster, max_distance, min_samples ) # print 'Then point %s, cluster %s' % (point, [p.to_json() for p in point_cluster]) # print 'centroid: %s' % centroid.to_json() cur.execute(""" UPDATE locations SET centroid=%s, point_cluster=%s WHERE location_id=%s """, (centroid, Segment(point_cluster), location_id)) else: # print 'New location' # Creates new location cur.execute(""" INSERT INTO locations (label, centroid, point_cluster) VALUES (%s, %s, %s) """, (label, point, Segment([point])))
def test_ulc(self): """ Tests update_location_centroid for centroid computation """ max_distance = 20 min_samples = 2 p_1 = Point(8.0315188471279, 2.48728733258197, None) p_2 = Point(8.0335721637393, 2.4895146785343, None) p_3 = Point(8.0335721637393, 2.4895146785343, None) cluster = [p_1, p_2, p_3] point = Point(8.0318516937495, 2.48709511068233, None) centroid, new_cluster = update_location_centroid( point, cluster, max_distance, min_samples) self.assert_point(centroid, Point(8.0335721637393, 2.4895146785343, None)) self.assert_points(new_cluster, [p_1, p_2, p_3, point])