Skip to content

Instantly share code, notes, and snippets.

View ekhoda's full-sized avatar

Ehsan Khodabandeh ekhoda

View GitHub Profile
@ekhoda
ekhoda / profile.py
Last active October 20, 2020 17:12
A profile decorator
import cProfile
import pstats
from functools import wraps
def profile(output_file=None, sort_by='cumulative', lines_to_print=None, strip_dirs=False):
"""A time profiler decorator.
Inspired by and modified the profile decorator of Giampaolo Rodola:
http://code.activestate.com/recipes/577817-profile-decorator/
lat_range, lon_range = get_lat_lon_range(df)
scope = get_scope(lat_range, lon_range)
layout = dict(title=title,
showlegend=False,
geo=dict(
scope=scope, # this is the only place we changed
showland=True,
landcolor='rgb(243, 243, 243)',
countrycolor='rgb(204, 204, 204)'))
def get_scope(lat_range, lon_range):
"""Assign the proper scope based on range of data's lat/lon."""
us_lat_rng = [24, 55]
us_lon_rng = [-127, -50]
na_lat_rng = [15, 85]
na_lon_rng = [-170, -50]
eu_lat_rng = [30, 80]
eu_lon_rng = [-20, 70]
sa_lat_rng = [-60, 12]
sa_lon_rng = [-81, -34]
def get_lat_lon_range(df):
"""Return the range of lat and lon in the data."""
return [df['lat'].min(), df['lat'].max()], [df['lon'].min(), df['lon'].max()]
# Specify the layout attributes
title = f'{scope.upper()} Network'
layout = dict(title=title,
showlegend=False,
geo=dict(
scope='usa',
showland=True,
landcolor='rgb(243, 243, 243)',
countrycolor='rgb(204, 204, 204)'))
# Add paths
for row in data.itertuples():
fig.add_trace(go.Scattergeo(
lon=[row.OriginLong, row.DestinationLong],
lat=[row.OriginLat, row.DestinationLat],
mode='lines',
line=dict(width=2, color='green'),
opacity=0.8,
showlegend=False,
))
fig = go.Figure()
# Add locations
fig.add_trace(go.Scattergeo(
lon=df['lon'],
lat=df['lat'],
text=df['text'],
marker=dict(size=df['size'],
symbol=df['shape'],
color=df['color'],
def create_location_dataframe(data):
"""Return a dataframe with the proper attributes needed for the plot."""
origin_df = data[['OriginCity', 'OriginState', 'OriginCountry',
'OriginLat', 'OriginLong']].drop_duplicates()
# If we have all the states, use them. Otherwise, use the countries
state_or_country = (origin_df['OriginState'] if not origin_df['OriginState'].isnull().values.any()
else origin_df['OriginCountry'])
origin_df['text'] = origin_df['OriginCity'] + '-' + state_or_country
origin_df['size'] = 10
origin_df['color'] = 'blue'