def mouseMoveEvent(self, event): """ captures a cursor movement while mouse is pressed on the canvas""" x = event.x() y = event.y() self.np = Point(x, y, -1) if distance(self.lp, self.np) > 5: self.path_points_1.addEllipse(QtCore.QRectF(x, y, 8, 8)) global stroke_id self.points.append(Point(x, y, stroke_id)) self.lp.x, self.lp.y = x, y self.update()
def draw_on_canvas(self, flag=True): """ draws Point_cloud on GUI Canvas :param flag: if is true we are drawing various fingers (different colours) """ # aux_points = [] to not overwriting original self.points array aux_points = amplify(self.points, 200) # kind of scale reversion aux_points = translate_to( aux_points, Point(self.origin.x, self.origin.y / 2, -1)) # translating amplilfied pc to center dic = { "f0": "path_points_0", "f1": "path_points_1", "f2": "path_points_2", "f3": "path_points_3", "f4": "path_points_4" } c = 0 if flag: path = dic.get(self.name) else: path = "path_points_1" # black color by default for p in aux_points: if c == num_points: # last point p.draw_on_canvas(10, path) else: p.draw_on_canvas(6, path) c += 1
def scale(points): """ this function returns the same point_cloud in different scales in order to comparison :param points: points :return: points """ min_x = gv.INF min_y = gv.INF max_x = -gv.INF max_y = -gv.INF for c in range(len(points)): min_x = min(min_x, points[c].x) min_y = min(min_y, points[c].y) max_x = max(max_x, points[c].x) max_y = max(max_y, points[c].y) scale = max(max_x - min_x, max_y - min_y) scale = 1 if scale == 0 else scale new_points = [] for c in range(len(points)): px = (points[c].x - min_x) / scale py = (points[c].y - min_y) / scale new_points.append(Point(px, py, points[c].id)) return new_points
class Tweet(APost): def __init__(self, tweet, point: Point): self._tweet = tweet self._point = point super().__init__() def __repr__(self): return "\n%s\n[%s]\n%s\n" % (self._tweet.text, " ".join( self.get_tags()), self._tweet.created_at) def get_tags(self) -> List[str]: return [ h.get('text', '') for h in self._tweet.entities.get('hashtags', []) ] def get_text(self) -> str: return self._tweet.text def get_creation_time(self) -> float: """ Time of models creation :return: timestamp """ return mktime(self._tweet.created_at.timetuple()) def get_point(self) -> Point: return self._point def get_photo(self): if 'retweeted_status' in self._tweet._json.keys(): return self._tweet.retweeted_status.quoted_status['entities'][ 'media'][0]['media_url'] else: return None def get_lang(self): return self._tweet.metadata.get('iso_language_code', '') def get_user_id(self): return self._tweet._json.get('user').get('id') def _get_post(self): return self._tweet def _extract_point(self): if self._tweet.coordinates: coord = self._tweet.coordinates.get('coordinates', None) self._point = Point(float(coord[1]), float(coord[0])) return self._point def for_df(self): return self._point.get_tuple(), \ self.get_lang(), \ self.get_text(), \ self.get_tags(), \ self.get_creation_time(), \ self.get_user_id(), \ self._get_post()
def get_centroid(points): """ 计算质心 :param points: :return Point: """ points = numpy.array(points) return Point(points[:, 0].mean(), points[:, 1].mean())
def data_handler(self, ctx, data): raw_point = parse_value(data) point = Point(raw_point.x, raw_point.y, raw_point.z, self.device.address) if BATCH_STORE: self.data.append(point) else: self.strage.store([point]) self.samples += 1
def get_centroid(moments): """ 计算质心 :param moments: :return Point: """ x = moments['m10'] / moments['m00'] y = moments['m01'] / moments['m00'] return Point(x, y)
def clear(self): """ clears the canvas, resetting all QPainterPaths""" aux = QPainterPath() self.path_points_0 = aux aux = QPainterPath() self.path_points_1 = aux aux = QPainterPath() self.path_points_2 = aux aux = QPainterPath() self.path_points_3 = aux aux = QPainterPath() self.path_points_4 = aux self.points = [] self.lp = Point(0, 0, -1) self.np = Point(0, 0, -1) self.update()
def __init__(self, name, points, where_to_translate=Point(gv.W / 4, gv.H / 4, -1)): self.origin = where_to_translate self.name = name self.points = resample(points, 32) # point cloud resizing self.points = scale(self.points) # point cloud scaling self.points = translate_to(self.points, self.origin) # point cloud centering
def amplify(points, mult): """ amplifies given collection of points keeping its distances between each other attending to mult argument :param points: points :param mult: amplifying size :return: points """ new_points = [] x = points[0].x y = points[0].y new_points.append(Point(x, y, points[0].id)) for c in range(1, len(points)): x += mult * (points[c].x - points[c - 1].x) y += mult * (points[c].y - points[c - 1].y) new_points.append(Point(x, y, points[c].id)) return new_points
def resample(points, resample_len): """resamples provided point_cloud in order to set homogeneous lengths for properly comparison resample_length indicates the length which to resample the pc. :param points: points point_cloud :param resample_len: usually 32 :return: points """ interval = path_length(points) / (resample_len - 1) d = 0.0 new_points = [points[0]] c = 1 for p in points: try: if points[c].id == points[c - 1].id: # we are int he same stroke dist = distance(points[c - 1], points[c]) if d + dist >= interval: px = points[c - 1].x + ((interval - d) / dist) * ( points[c].x - points[c - 1].x) py = points[c - 1].y + ((interval - d) / dist) * ( points[c].y - points[c - 1].y) p = Point(px, py, points[c].id) new_points.append(p) points.insert( c, p) # insert p in c position, reassigning all elements d = 0.0 else: d += dist c += 1 except: break if len(new_points) == resample_len - 1: new_points.append( Point(points[len(points) - 1].x, points[len(points) - 1].y, points[len(points) - 1].id)) return new_points
def mousePressEvent(self, event): """ captures a mouse click on the canvas""" x = event.x() y = event.y() self.parent._print("start point: (" + str(x) + "," + str(y) + ")") self.path_points_1.addEllipse(QtCore.QRectF(x, y, 16, 16)) global stroke_id stroke_id += 1 self.points.append(Point(x, y, stroke_id)) self.lp.x, self.lp.y = x, y
def __init__(self, parent, w, h): super(Widget_canvas, self).__init__(parent) self.setCursor(QCursor(QtCore.Qt.CrossCursor)) self.points = [] self.lp = Point(0, 0, -1) self.np = Point(0, 0, -1) self.path_points_0 = QPainterPath() self.path_points_1 = QPainterPath() self.path_points_2 = QPainterPath() self.path_points_3 = QPainterPath() self.path_points_4 = QPainterPath() self.canvas = None self.pen_color = Qt.white self.canvas_width = w self.canvas_height = h self.resize(self.canvas_width, self.canvas_height)
def affinity_point(point, matrix): if type(point) == Point: x = (matrix[0][0] * point.x + matrix[0][1] * point.y + matrix[0][2]) \ / (matrix[2][0] * point.x + matrix[2][1] * point.y + matrix[2][2]) y = (matrix[1][0] * point.x + matrix[1][1] * point.y + matrix[1][2]) \ / (matrix[2][0] * point.x + matrix[2][1] * point.y + matrix[2][2]) return Point(x, y) else: x = (matrix[0][0] * point[0] + matrix[0][1] * point[1] + matrix[0][2]) \ / (matrix[2][0] * point[0] + matrix[2][1] * point[1] + matrix[2][2]) y = (matrix[1][0] * point[0] + matrix[1][1] * point[1] + matrix[1][2]) \ / (matrix[2][0] * point[0] + matrix[2][1] * point[1] + matrix[2][2]) return numpy.array([[int(x), int(y)]])
def load_text_B(self): text = str(self.text_edit.toPlainText()) arr = text.split("\n") loaded_points = [] # containing new read points for c in range(2, len(arr) - 1): x, y = self.to_point(arr[c]) loaded_points.append(Point(x, y, -1)) pc = Point_cloud("loaded_points", loaded_points) self.widget_canvas.path = QPainterPath() # clear canvas self.update() pc.draw_on_canvas() # drawing loaded stroke global points points = loaded_points # allowing "F"
def get_centroid(points): """ this function calculates given points_cloud's centroid :param points: points :return: Point """ x = 0.0 y = 0.0 for c in range(0, len(points)): x += points[c].x y += points[c].y x /= len(points) y /= len(points) return Point(x, y, 0)
def translate_to(points, where): """ translates given points set (point_cloud) to provided centroid. It maps all pc to origin, in order to recognize pc that are similar but in different coordinates :param points: points :param where: Point where to translate points :return: translated points """ centroid = get_centroid(points) new_points = [] for c in range(0, len(points)): px = points[c].x + where.x - centroid.x py = points[c].y + where.y - centroid.y new_points.append(Point(px, py, points[c].id)) return new_points
def get_paces_near(self, point: Point) -> List[PostCluster]: lat_min, lat_max, lon_min, lon_max = self.mk_cell(point) res = [] places = self.get_places(lat_min, lat_max, lon_min, lon_max) for place in places: if place.get('properties').get('name'): p = self.get_place_info(place.get('properties').get('xid')) pc = PostCluster( Point( p.get('point', {}).get('lat'), p.get('point', {}).get('lon')), []) pc.set_category(p.get("kinds", "")) pc.name = p.get("name") pc.descr = [p.get('info', {}).get('descr', '')] pc.image_url = p.get('image', '') res.append(pc) return res
def mk_grid(center: Point, R, r=20): step = r * 1 / 2 # the number of kilometers in one radian # kms_per_radian = 6371.0088 # radian_per_km = 0.00015696101377226163 deg_per_km = 0.0089932036372453797 R_rad = deg_per_km * R r_rad = deg_per_km * r step_rad = deg_per_km * step lat_range = [ i for i in pl.frange(center.latitude - R_rad + r_rad, center.latitude + R_rad - r_rad, step_rad) ] lng_range = [ i for i in pl.frange(center.longitude - (deg_per_km * R), center.longitude + (deg_per_km * R), step_rad) ] grid = [] for x in lat_range: for y in lng_range: grid.append(Point(x, y)) return grid
self.provider = FlickrProvider() def get_posts(self, point: Point, radius=32, min_pos_date=0.0, max_pos_date=0.0, min_taken_date=0.0, max_taken_date=0.0): return self.provider.get_posts(point, radius, min_pos_date, max_pos_date, min_taken_date, max_taken_date) def to_csv(self, posts, date_from, date_to, name=''): df = pd.DataFrame(columns=[ 'coordinates', 'text', 'tags', 'creation_time', 'user_id', 'photo_url' 'post' ]) for i, post in enumerate(posts): df.loc[i] = post.for_df() df.to_csv(f"posts/flickr/{name}_{date_from}_{date_to}.csv", encoding='utf-8') m = FlickerMiner() posts = m.get_posts(Point(41.383333, 2.183333)) for post in posts[:5]: print(post.__repr__())
"bbox": { "lat_max": 59.941334, "lat_min": 59.938278, "lon_max": 30.336556, "lon_min": 30.328612 }, "osm": "relation/2614476", "otm": "https://opentripmap.com/en/card/R2614476", "kinds": "cultural,urban_environment,gardens_and_parks,interesting_places", "point": { "lon": 30.332874, "lat": 59.939827 }, "xid": "R2614476", "rate": "3h", "name": "Mikhailovsky garden", "wikipedia": "https://ru.wikipedia.org/wiki/%D0%9C%D0%B8%D1%85%D0%B0%D0%B9%D0%BB%D0%BE%D0%B2%D1%81%D0%BA%D0%B8%D0%B9%20%D1%81%D0%B0%D0%B4", "wikidata": "Q4297751" } """ url = self.base_url + \ f"/xid" \ f"/{xid}" \ f"?apikey={self.api_key}" res = requests.get(url).json() return res pm = PlacesMiner() places = pm.get_paces_near(Point(59.9390095, 29.5303098)) print(places)
def seed_db(): from datetime import date from models.User import User # Importing the User model from models.Profile import Profile # Importing the Profile model from models.League import League from models.Member import Member from models.Fine import Fine from models.Point import Point from models.Category import Category from models.Sprint import Sprint from main import bcrypt # Hashing module for the passwords from faker import Faker # Importing the faker module for fake data import random # Importing random from the python standard library import copy import time faker = Faker() users = [] leagues = [] categories = [] sprints = [] points = [] fines = [] for i in range(5): time.sleep(0.2) user = User() user.email = f"test{i+1}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() for i in range(5): # time.sleep(0.2) profile = Profile() profile.username = f"username{i}" profile.firstname = f"firstname{i}" profile.lastname = f"lastname{i}" profile.user_id = users[i].id db.session.add(profile) db.session.commit() for i in range(5): # time.sleep(0.2) new_league = League() new_league.title = f"League title {i}" new_league.description = f"A nice league to the power of {i}" new_league.owner = users[i].id leagues.append(new_league) db.session.add(new_league) db.session.commit() for i in range(5): # time.sleep(0.2) owner = Member() owner.user_id = leagues[i].owner owner.league_id = i + 1 owner.active = True db.session.add(owner) new_member = Member() new_member.active = True new_member.league_id = i + 1 new_member.user_id = random.choice(users).id while new_member.user_id == owner.user_id: new_member.user_id = random.choice(users).id db.session.add(new_member) db.session.commit() for i in range(5): new_sprint = Sprint() new_sprint.title = f"Sprint title #{i}" new_sprint.meeting_point = f"The Outback" new_sprint.creation_time = date.today() league = leagues[i] new_sprint.league = league sprints.append(new_sprint) db.session.commit() for i in range(5): # time.sleep(0.2) new_category = Category() new_category.title = f"category title {i}" new_category.description = f"category description {i}" if i % 2 == 0: private = True else: private = False new_category.private = private new_category.owner = random.choice(users).id new_category.leagues_categories.append(leagues[i]) categories.append(new_category) db.session.commit() for i in range(5): # time.sleep(0.2) new_fine = Fine() new_fine.title = f"Title {i}" new_fine.description = f"Description {i}" new_fine.amount = i if i % 2 == 0: style = "Award" else: style = "Fine" new_fine.style = style category = categories[i] new_fine.category = category fines.append(new_fine) db.session.commit() for i in range(4): # time.sleep(0.2) new_point = Point() new_point.creation_time = date.today() new_point.fine_id = random.choice(fines).id sprint = sprints[i] new_point.sprint = sprint new_point.giver_id = sprint.league.owner new_point.receiver_id = sprint.league.members[1].id db.session.commit() print("Tables seeded")
def init_templates(): """ initialize templates array for PCRecognizer class :return: templates array """ templates = [] # single stroke templates (all fingers doing the same if various fingers) (1 finger) templates.append(Template("T", [ # different PC for having different ways of drawing Template.name (T) for better recognition PointCloud("T1", [Point(30, 7, 1), Point(103, 7, 1), Point(66, 7, 2), Point(66, 87, 2)]) , PointCloud("T2", [Point(30, 7, 1), Point(123, 7, 1), Point(80, 17, 2), Point(30, 7, 2), Point(80, 17, 3), Point(80, 77, 3)]) , PointCloud("T3", [Point(30, 7, 1), Point(123, 7, 1), Point(80, 17, 2), Point(30, 7, 2), Point(80, 17, 3), Point(80, 50, 3)]) ], None) ) templates.append(Template("V", [ PointCloud("V1", [Point(30, 7, 1), Point(40, 37, 1), Point(40, 37, 2), Point(50, 7, 2)]) , PointCloud("V2", [Point(0, 7, 1), Point(25, 37, 1), Point(25, 37, 2), Point(50, 7, 2)]) , PointCloud("V3", [Point(30, 7, 1), Point(40, 25, 1), Point(40, 25, 2), Point(50, 7, 2)]) , PointCloud("V4", [Point(30, 16, 1), Point(33, 25, 1), Point(33, 25, 2), Point(38, 7, 2)]) , PointCloud("V5", [Point(30, 7, 1), Point(33, 25, 1), Point(33, 25, 2), Point(38, 16, 2)]) ], None) ) templates.append(Template("D", [ PointCloud("D1", [Point(30, 7, 1), Point(30, 67, 1), Point(30, 67, 2), Point(50, 53, 2), Point(50, 53, 3), Point(55, 37, 3), Point(55, 37, 4), Point(50, 21, 4), Point(50, 21, 5), Point(30, 7, 5)]) , PointCloud("D1", [Point(30, 7, 1), Point(30, 67, 1), Point(30, 67, 2), Point(60, 53, 2), Point(60, 53, 3), Point(65, 37, 3), Point(65, 37, 4), Point(60, 21, 4), Point(60, 21, 5), Point(30, 7, 5)]) , ], None) ) templates.append(Template("X", [ PointCloud("X1", [Point(30, 7, 1), Point(60, 47, 1), Point(60, 7, 2), Point(30, 47, 2)]) , PointCloud("X1_2", [Point(30, 7, 1), Point(60, 34, 1), Point(60, 7, 2), Point(30, 34, 2)]) , PointCloud("X2", [Point(30, 7, 1), Point(60, 47, 1), Point(60, 7, 2), Point(30, 47, 2), Point(30, 7, 3), Point(60, 7, 3)]) , PointCloud("X3", [Point(30, 7, 1), Point(60, 47, 1), Point(60, 7, 2), Point(30, 47, 2), Point(30, 47, 3), Point(60, 47, 3)]) , PointCloud("X4", [Point(30, 7, 1), Point(60, 47, 1), Point(60, 7, 2), Point(30, 47, 2), Point(30, 7, 3), Point(30, 47, 3)]) ], None) ) templates.append(Template("W", [ PointCloud("W1", [Point(30, 7, 1), Point(40, 37, 1), Point(40, 37, 2), Point(50, 20, 2), Point(50, 20, 3), Point(60, 37, 3), Point(60, 37, 4), Point(70, 7, 4)]) , PointCloud("W2", [Point(30, 7, 1), Point(50, 37, 1), Point(50, 37, 2), Point(70, 7, 2), Point(70, 7, 3), Point(90, 37, 3), Point(90, 37, 4), Point(110, 7, 4)]) ], None) ) templates.append(Template("L", [ PointCloud("L1", [Point(30, 27, 1), Point(30, 37, 1), Point(30, 37, 2), Point(40, 37, 2)]) , PointCloud("L2", [Point(30, 17, 1), Point(30, 37, 1), Point(30, 37, 2), Point(40, 37, 2)]) ], None) ) templates.append(Template("Z", [ PointCloud("Z1", [Point(30, 7, 1), Point(60, 7, 1), Point(60, 7, 2), Point(30, 27, 2), Point(30, 27, 3), Point(60, 27, 3)]) , PointCloud("Z2", [Point(30, 7, 1), Point(50, 12, 1), Point(50, 12, 2), Point(30, 35, 2), Point(30, 35, 3), Point(55, 30, 3)]) , PointCloud("Z3", [Point(30, 7, 1), Point(50, 12, 1), Point(50, 12, 2), Point(20, 37, 2), Point(20, 37, 3), Point(52, 33, 3)]) , PointCloud("Z4", [Point(30, 21, 1), Point(50, 8, 1), Point(50, 8, 2), Point(23, 30, 2), Point(23, 30, 3), Point(54, 27, 3)]) , PointCloud("Z5", [Point(40, 7, 1), Point(60, 7, 1), Point(60, 7, 2), Point(30, 25, 2), Point(30, 25, 3), Point(70, 27, 3)]) , PointCloud("Z6", [Point(20, 7, 1), Point(70, 7, 1), Point(70, 7, 2), Point(30, 28, 2), Point(30, 28, 3), Point(57, 27, 3)]) ], None) ) return templates
def _extract_point(self) -> Point: coord = self._post['venue']['coordinates'].split(' ') return Point(float(coord[0]), float(coord[1]))
def _extract_point(self) -> Point: return Point(float(self._post['latitude']), float(self._post['longitude']))
df.loc[i] = post.for_df() df.to_csv( f"posts/tweets/{self.get_dest(without_cords)}/{city}_{language}_{self.today()}.csv", encoding='utf-8') def today(self): return f"{datetime.datetime.today().time().hour}_" \ f"{datetime.datetime.today().time().minute}__" \ f"{datetime.datetime.today().date().day}_" \ f"{datetime.datetime.today().date().month}_" \ f"{datetime.datetime.today().date().year}" cities = { "kzn": Point(55.7714676, 49.0887294), "spb": Point(59.9330659, 30.3059148), "msk": Point(55.7564364, 37.5446647), "soc": Point(43.6025106, 39.7107868), "ros": Point(47.2441707, 39.595124), "kad": Point(54.7252967, 20.4338166), "vod": Point(48.6627428, 44.4551274), "sar": Point(51.5572534, 45.9373729), "sam": Point(53.2235426, 50.1559591), "ekb": Point(56.8501552, 60.5699289), "niz": Point(56.2953442, 43.936181) } pm = TwitterMiner() while True:
def _extract_point(self): if self._tweet.coordinates: coord = self._tweet.coordinates.get('coordinates', None) self._point = Point(float(coord[1]), float(coord[0])) return self._point