Skip to content

Instantly share code, notes, and snippets.

View jens-andersson-2-wcar's full-sized avatar

Jens Andersson jens-andersson-2-wcar

View GitHub Profile
@jens-andersson-2-wcar
jens-andersson-2-wcar / vector-field-folium-uber-h3-with-preview.ipynb
Created August 2, 2024 13:40
Vector fields using Folium maps and Uber H3 gridding; Citi Bike dataset example
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jens-andersson-2-wcar
jens-andersson-2-wcar / aggregating-by-hex-cell.py
Created August 2, 2024 12:12
Aggregating data by H3 cell; be careful with angles!
positions_df_byhex = df_subset.groupby("hex_id", as_index=False).agg({
"longitude": "mean",
"latitude": "mean",
"vector_component_x": "mean",
"vector_component_y": "mean",
"bearing": "count", # "mean" would be wrong here; we need circmean
"speed_kmh": "mean"
})
@jens-andersson-2-wcar
jens-andersson-2-wcar / geojson-arrow-for-folium-map.py
Created August 2, 2024 11:56
Manually drawing an arrow in code for a Folium map
arrow_tip_x = size_of_arrow_x * math.cos(bearing_rad) + center_lat
arrow_tip_y = size_of_arrow_y * math.sin(bearing_rad) + center_long
arrow_back_left_x = size_of_arrow_x * math.cos(bearing_rad + 2*math.pi * 135.0 / 360.0 ) + center_lat
arrow_back_left_y = size_of_arrow_y * math.sin(bearing_rad + 2*math.pi * 135.0 / 360.0 ) + center_long
arrow_back_x = size_of_arrow_x * 0.2 * math.cos(bearing_rad + 2*math.pi * 180.0 / 360.0 ) + center_lat
arrow_back_y = size_of_arrow_y * 0.2 * math.sin(bearing_rad + 2*math.pi * 180.0 / 360.0 ) + center_long
arrow_back_right_x = size_of_arrow_x * math.cos(bearing_rad + 2*math.pi * 225.0 / 360.0 ) + center_lat
@jens-andersson-2-wcar
jens-andersson-2-wcar / circular-mean.py
Created August 2, 2024 11:51
Average bearing: one wrong way and two correct ones
import numpy as np
import scipy.stats
# compass bearings in degrees:
data = [2, 356]
# average bearing:
print(np.mean(data)) # == 179.0, aka "wrong"
print(scipy.stats.circmean(data, high=360, low=0)) # == 359.0, aka "correct"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jens-andersson-2-wcar
jens-andersson-2-wcar / h3-helpers-for-folium-maps.py
Last active November 15, 2023 14:04
Jupyter notebook snippet for plotting H3 hexagons on Folium maps
!conda install -y folium geojson
import folium
from geojson import Feature, Point, FeatureCollection
import json
def hexagons_dataframe_to_geojson(df_hex, file_output = None, column_name = "value"):
"""
Produce the GeoJSON for a dataframe, constructing the geometry from the "hex_id" column
and with a property matching the one in column_name
dataset2001byhexid = dataset2001.groupby("hex_id", as_index=False).agg({"pop_2001": "sum"})
!conda install -y h3-py
import h3
hex_ids = dataset2001.apply(lambda row: h3.geo_to_h3(row.lat, row.long, resolution), axis = 1)
dataset2001 = dataset2001.assign(hex_id=hex_ids.values)
@jens-andersson-2-wcar
jens-andersson-2-wcar / sagemaker-autopilot-batch-predictions-ipynb.py
Last active January 8, 2021 08:47
SageMaker Autopilot models used for batch prediction (preprocess + predict)
from sagemaker.pipeline import PipelineModel
from sagemaker.model import Model
from sagemaker.transformer import Transformer
import sagemaker
# The location of the input dataset, WITHOUT the column to predict (and WITHOUT headers)
batch_input = 's3://YOURBUCKET/YOURFOLDER/predictmeplease.csv'
# The location to store the results of the batch transform job (will be stored under this folder, with a single value per line)