Skip to content

Instantly share code, notes, and snippets.

@zhicongchen
Last active July 13, 2017 08:36
Show Gist options
  • Save zhicongchen/60a76f023a4c0bbbba50f6a94690b21f to your computer and use it in GitHub Desktop.
Save zhicongchen/60a76f023a4c0bbbba50f6a94690b21f to your computer and use it in GitHub Desktop.
# radius of gyration
from collections import Counter
import math
def radius_of_gyration(positions):
"""
position : tuple
A tuple (lat, lon) with the latitude and longitude of the antenna,
encoded as floating point numbers.
Returns the radius of gyration, the *equivalent distance* of the mass from
the center of gravity, for all visited places. [GON2008]_
References
----------
.. [GON2008] Gonzalez, M. C., Hidalgo, C. A., & Barabasi, A. L. (2008).
Understanding individual human mobility patterns. Nature, 453(7196),
779-782.
"""
d = Counter(positions)
sum_weights = sum(d.values())
positions = list(d.keys()) # Unique positions
if len(positions) == 0:
return None
barycenter = [0, 0]
for pos, t in d.items():
barycenter[0] += pos[0] * t
barycenter[1] += pos[1] * t
barycenter[0] /= sum_weights
barycenter[1] /= sum_weights
r = 0.
for pos, t in d.items():
r += float(t) / sum_weights * \
great_circle_distance(barycenter, pos) ** 2
return math.sqrt(r)
def great_circle_distance(pt1, pt2):
"""
Return the great-circle distance in kilometers between two points,
defined by a tuple (lat, lon).
Examples
--------
>>> brussels = (50.8503, 4.3517)
>>> paris = (48.8566, 2.3522)
>>> great_circle_distance(brussels, paris)
263.9754164080347
"""
r = 6371.
delta_latitude = math.radians(pt1[0] - pt2[0])
delta_longitude = math.radians(pt1[1] - pt2[1])
latitude1 = math.radians(pt1[0])
latitude2 = math.radians(pt2[0])
a = math.sin(delta_latitude / 2) ** 2 + math.cos(latitude1) * math.cos(latitude2) * math.sin(delta_longitude / 2) ** 2
return r * 2. * math.asin(math.sqrt(a))
# geopy
from geopy.geocoders import Nominatim
geolocator = Nominatim()
for i in np.array(user_trajectory[['116.434611', '39.803722']].drop_duplicates()):
print i[0], i[1]
location = geolocator.reverse(str(i[1]) + ', ' + str(i[0]))
print(location.address)
# urlparse
import urlparse
for url in del_cut['acce_url'].dropna()[:20]:
# url = del_cut['acce_url'][0]
result = urlparse.urlsplit(url)
print result.netloc
# clean url
def urlclean(url):
try:
url = urlparse.urlsplit(url).hostname
if url.replace('.','').isdigit():
return 'none'
else:
if len(url.split('.')) >=2 :
if url[-6:]=='com': return '.'.join(url.split('.')[-3:])
return '.'.join(url.split('.')[-2:])
except:
return 'error'
for url in data['acce_url'].dropna()[:20]:
print urlparse.urlsplit(url).hostname
print urlclean(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment