%pylab inline rcParams['figure.figsize'] = (16,9) from twython import Twython from credentials import CONSUMER_KEY, CONSUMER_SECRET twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET) auth = twitter.get_authentication_tokens() OAUTH_TOKEN = auth['oauth_token'] OAUTH_TOKEN_SECRET = auth['oauth_token_secret'] from IPython.core.display import HTML HTML('Please authenticate.'.format(auth['auth_url'])) # Set this to the PIN from the auth URL PIN = '1734078' twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) final_step = twitter.get_authorized_tokens(PIN) OAUTH_TOKEN = final_step['oauth_token'] OAUTH_TOKEN_SECRET = final_step['oauth_token_secret'] twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) import json print(json.dumps(twitter.get_user_timeline(screen_name='stephenfry')[0], indent=2)) from twython import TwythonStreamer class SourceStreamer(TwythonStreamer): def __init__(self, *args): super(SourceStreamer, self).__init__(*args) self.coords = { 'android': [], 'iOS': [], } def on_success(self, data): """If tweet has geo-information, add it to the list.""" try: source = data['source'] if 'Android' in source: self.coords['android'].append(data['geo']['coordinates']) elif 'iOS' in source or 'iPhone' in source or 'iPad' in source: self.coords['iOS'].append(data['geo']['coordinates']) except (AttributeError, KeyError, TypeError): pass def on_error(self, status_code, data): """On error, print the error code and disconnect ourselves.""" print('Error: {0}'.format(status_code)) self.disconnect() from threading import Thread class StreamThread(Thread): def run(self): self.stream = SourceStreamer( CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET ) self.stream.statuses.filter(locations='-180,-90,180,90') t = StreamThread() t.daemon = True t.start() t.stream.disconnect() len(t.stream.coords['android']) from osgeo import gdal, osr map_ds = gdal.Open('world-map-gall.tiff') map_image = np.transpose(map_ds.ReadAsArray(), (1,2,0)) ox, xs, _, oy, _, ys = map_ds.GetGeoTransform() map_extent = ( ox, ox + xs * map_ds.RasterXSize, oy + ys * map_ds.RasterYSize, oy ) map_proj = osr.SpatialReference() map_proj.ImportFromProj4('+proj=cea +lon_0=0 +lat_ts=45 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs') wgs84_proj = osr.SpatialReference() wgs84_proj.ImportFromEPSG(4326) wgs84_to_map_trans = osr.CoordinateTransformation(wgs84_proj, map_proj) def coord_to_pixel(coords): """Transform a sequence of co-ordinates in latitude, longitude pairs into map pixel co-ordinates.""" # Transfrom from lat, lngs -> lng, lats latlngs = np.array(coords) lnglats = latlngs[:,(1,0)] # Transform from lng, lats -> map projection map_coords = np.array(wgs84_to_map_trans.TransformPoints(lnglats)) # Transform from map projection -> pixel coord map_coords[:,0] = (map_coords[:,0] - ox) / xs map_coords[:,1] = (map_coords[:,1] - oy) / ys return map_coords[:,:2] from scipy.stats import norm kern_row = norm.pdf(np.linspace(-4, 4, 15)) kern_row /= np.sum(kern_row) plot(kern_row) kernel = np.outer(kern_row, kern_row) imshow(kernel, interpolation='nearest') def coord_histogram(coords, down_scale = 2): pixel_locs = coord_to_pixel(coords) histogram, _, _ = np.histogram2d(pixel_locs[:,1], pixel_locs[:,0], bins=[int(map_image.shape[0]/down_scale), int(map_image.shape[1]/down_scale)], range=[[0, map_image.shape[0]], [0, map_image.shape[1]]]) return histogram android = coord_histogram(t.stream.coords['android']) ios = coord_histogram(t.stream.coords['iOS']) imshow(np.sqrt(ios), cmap=cm.spectral) tight_layout() axis('off') from scipy.signal import convolve2d imshow(np.sqrt(convolve2d(android, kernel, mode='same')), cmap=cm.spectral) axis('off') figure(figsize=(12,9)) imshow(map_image, extent=map_extent) imshow(np.sqrt(convolve2d(android, kernel, mode='same')), extent=map_extent, alpha=0.5, cmap=cm.spectral) axis('off') title('Android usage as measured by Twitter ({0:,} tweets)'.format(int(android.sum()))) tight_layout() savefig('android-usage.png') figure(figsize=(12,9)) imshow(map_image, extent=map_extent) imshow(np.sqrt(convolve2d(ios, kernel, mode='same')), extent=map_extent, alpha=0.5, cmap=cm.spectral) axis('off') title('iOS usage as measured by Twitter ({0:,} tweets)'.format(int(ios.sum()))) tight_layout() savefig('ios-usage.png') figure(figsize=(12,6)) # Compute a preference matrix varying from -1 == iOS to +1 == Android A = convolve2d(android/android.sum() - ios/ios.sum(), kernel, mode='same') A /= max(A.max(), -A.min()) # Squash the *magnitude* of A by taking a square root A = np.sign(A) * np.sqrt(np.abs(A)) # Show the base map gca().set_axis_bgcolor('black') imshow(map_image, extent=map_extent, alpha=0.5) # Compute the coloured preference image and set alpha value based on preference pref_cmap = cm.RdYlBu pref_im = pref_cmap(0.5 * (A + 1)) pref_im[:,:,3] = np.minimum(1, np.abs(A) * 10) # Draw preference image imshow(pref_im, extent=map_extent, cmap=pref_cmap, clim=(-1,1)) # Draw a colour bar cb = colorbar() cb.set_ticks((-1, -0.5, 0, 0.5, 1)) cb.set_ticklabels(('Strong iOS', 'Weak iOS', 'No preference', 'Weak Android', 'Strong Android')) # Set up plot axes, title and layout gca().get_xaxis().set_visible(False) gca().get_yaxis().set_visible(False) title('Geographic preferences for Android vs. iOS') tight_layout() savefig('preferences.png')