Skip to content

Commit

Permalink
Merge 1ade064 into adbda77
Browse files Browse the repository at this point in the history
  • Loading branch information
ajduberstein committed Sep 19, 2019
2 parents adbda77 + 1ade064 commit 2a9b315
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 546 deletions.
14 changes: 11 additions & 3 deletions bindings/python/pydeck/examples/01 - Introduction.ipynb
Expand Up @@ -160,7 +160,15 @@
"outputs": [],
"source": [
"import time\n",
"\n",
"r.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(0, 10000, 1000):\n",
" layer.elevation_range = [0, i]\n",
" r.update()\n",
Expand Down Expand Up @@ -190,9 +198,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/pydeck/examples/02 - Scatterplots.ipynb
Expand Up @@ -64,7 +64,7 @@
"metadata": {},
"outputs": [],
"source": [
"from literal_eval import ast\n",
"from ast import literal_eval\n",
"# We have to re-code position to be one field in a list, so we'll do that here:\n",
"# The CSV encodes the [R, G, B, A] color values listed in it as a string\n",
"df['color'] = df.apply(lambda x: literal_eval(x['color']), axis=1)"
Expand Down
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Selecting data for use in Python: Uber pickup data\n",
"# Selecting data for use in Python: Singapore taxi location data\n",
"\n",
"*(About 5-10 minutes to read)*\n",
"\n",
Expand All @@ -23,72 +23,142 @@
"source": [
"## Getting the data\n",
"\n",
"Here we'll use a set of Uber pickup locations made available by a Freedom of Information Act request in 2014."
"Here we'll use the live taxi location API provided by the government of Singapore."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"from pydeck import (\n",
" data_utils,\n",
" Deck,\n",
" Layer\n",
")\n",
"\n",
"\n",
"DATA_URL = 'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/screen-grid/uber-pickup-locations.json'\n",
"import pydeck as pdk\n",
"\n",
"DATA_URL = 'https://api.data.gov.sg/v1/transport/taxi-availability'\n",
"COLOR_RANGE = [\n",
" [255, 255, 178, 25],\n",
" [254, 217, 118, 85],\n",
" [254, 178, 76, 127],\n",
" [253, 141, 60, 170],\n",
" [240, 59, 32, 212],\n",
" [189, 0, 38, 255]\n",
"]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import requests\n",
"\n",
"viewport = View(longitude=-73.998, latitude=40.729, zoom=10)\n",
"json = requests.get(DATA_URL).json()\n",
"df = pd.DataFrame(json[\"features\"][0][\"geometry\"][\"coordinates\"])\n",
"df.columns = ['lng', 'lat']\n",
"\n",
"viewport = pdk.data_utils.compute_viewport(df[['lng', 'lat']])\n",
"layer = pdk.Layer(\n",
" 'ScreenGridLayer',\n",
" UK_ACCIDENTS_DATA,\n",
" df,\n",
" cell_size_pixels=20,\n",
" color_range=COLOR_RANGE,\n",
" get_position='[row[0], row[1]]',\n",
" get_weight='row[2]',\n",
" get_position='[lng, lat]',\n",
" pickable=True,\n",
" auto_highlight=True)\n"
" auto_highlight=True)\n",
"r = pdk.Deck(layers=[layer], initial_view_state=viewport)"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3df28f07550a420a9cc4394b2481bc88",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"DeckGLWidget(json_input='{\"initialViewState\": {\"bearing\": 0, \"latitude\": 1.3466294511520216, \"longitude\": 103.…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plotting the data\n",
"\n",
"As we've done in previous examples, we'll call `r.show()` to view the data:"
"r.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"r = pdk.Deck(layers=[layer], viewport=viewport)\n",
"r.show()"
"# Two way communication\n",
"\n",
"Click the above visualization and then execute the cell below to pass data from the application to Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 71,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>cellCount</th>\n",
" <th>cellWeight</th>\n",
" <th>maxCellWieght</th>\n",
" <th>totalCount</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>161</td>\n",
" <td>161</td>\n",
" <td>203</td>\n",
" <td>2662</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" cellCount cellWeight maxCellWieght totalCount\n",
"0 161 161 203 2662"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(s.selected_data).mean()"
"pd.DataFrame([r.deck_widget.selected_data])"
]
}
],
Expand Down
167 changes: 167 additions & 0 deletions bindings/python/pydeck/examples/04 - Plotting massive data sets.ipynb
@@ -0,0 +1,167 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting massive data sets\n",
"\n",
"**This notebook is experimental in pydeck's beta version. It may not work on all devices.**\n",
"\n",
"This notebook plots 1.6 million points of LIDAR points around the Carnegie Mellon University campus. ([Source](https://github.com/ajduberstein/oakland_point_cloud)) The data points are labeled. With pydeck, we can render these points and interact with them. We'll downsample this data to 800,000 points–depending on your device, you may be able to run the full data set."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cleaning the data\n",
"\n",
"First we need to import the data. We should expect about 1.6M points."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"URL = 'https://raw.githubusercontent.com/ajduberstein/oakland_point_cloud/master/%s'\n",
"DATA_URL_1 = URL % 'lidar_chunks_1.csv'\n",
"DATA_URL_2 = URL % 'lidar_chunks_2.csv'\n",
"LOOKUP_URL = URL % 'ground_truth_label.csv'\n",
"lidar = pd.concat([pd.read_csv(DATA_URL_1), pd.read_csv(DATA_URL_2)])\n",
"lookup = pd.read_csv(LOOKUP_URL)\n",
"lidar = lidar.merge(lookup)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('Number of points:', lidar.count()[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It does not appear to be in a standard coordinate format, so we'll scale it to make it easy to plot on a map. We'll also color objects by label type. The `data_utils.assign_random_colors` assigns a random RGB value to a vector of data labels."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pydeck import data_utils\n",
"\n",
"color_lookup = data_utils.assign_random_colors(lidar['label_name'])\n",
"lidar['rgb'] = lidar.apply(lambda row: color_lookup.get(row['label_name']), axis=1)\n",
"# Scaling the points using min-max scaling\n",
"lidar[['x', 'y', 'z']] -= lidar[['x', 'y', 'z']].max()\n",
"lidar[['x', 'y', 'z']] /= lidar[['x', 'y', 'z']].min()\n",
"lidar[['x', 'y']] /= 1000\n",
"lidar[['z']] *= 10\n",
"lidar.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting the data\n",
"\n",
"We'll define a single `PointCloudLayer` and plot it.\n",
"\n",
"pydeck by default expects the input of `get_position` to be a string name indicating a single position value. For convenience, you can pass in a string indicating the X/Y/Z coordinate, here `get_position='[x, y, z]'`.\n",
"\n",
"We'll zoom to the approximate center of the data by taking a mean of a few hundred points in pandas.\n",
"\n",
"This example may take 10-15 seconds to render."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pydeck as pdk\n",
"\n",
"point_cloud = pdk.Layer(\n",
" 'PointCloudLayer',\n",
" lidar[['x', 'y', 'z', 'label_name', 'rgb']].sample(800000),\n",
" # You can specify the XYZ coordinate in a list as a string\n",
" get_position='[x, y, -1*z]',\n",
" get_normal=[0, 0, 1],\n",
" get_color='rgb',\n",
" pickable=True,\n",
" auto_highlight=True,\n",
" radius_pixels=3)\n",
"\n",
"\n",
"view_state = pdk.data_utils.compute_viewport(lidar[['x', 'y']])\n",
"view_state.max_zoom = 23\n",
"view_state.zoom = 22\n",
"view_state.pitch = 60\n",
"\n",
"\n",
"r = pdk.Deck(\n",
" point_cloud,\n",
" initial_view_state=view_state,\n",
" map_style='',\n",
" width=700)\n",
"r.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%javascript\n",
"// Use Javascript to change the cell background to black\n",
"document.getElementById(\"deckgl-overlay\").style.backgroundColor = \"black\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Citations:\n",
"\n",
"Contextual Classification with Functional Max-Margin Markov Networks. \n",
"Daniel Munoz, J. Andrew (Drew) Bagnell, Nicolas Vandapel, and Martial Hebert. \n",
"IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR), June, 2009."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 2a9b315

Please sign in to comment.