Skip to content

Instantly share code, notes, and snippets.

@darribas
Created May 2, 2023 19:13
Show Gist options
  • Save darribas/f6067f58f3f54fe227482529ab3ac6d3 to your computer and use it in GitHub Desktop.
Save darribas/f6067f58f3f54fe227482529ab3ac6d3 to your computer and use it in GitHub Desktop.
Geospatial workbench
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "aacf3781",
"metadata": {},
"source": [
"# Workbench"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bb70572a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.0\r\n"
]
}
],
"source": [
"! echo $GDS_ENV_VERSION"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "766a6172",
"metadata": {},
"outputs": [],
"source": [
"from libpysal import weights\n",
"import pandas\n",
"import geopandas\n",
"import numpy as np\n",
"import polars as pl"
]
},
{
"cell_type": "markdown",
"id": "e786da57",
"metadata": {},
"source": [
"Utilities:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "d05eab7d",
"metadata": {},
"outputs": [],
"source": [
"def lag_spatial_adj(w_adj, y):\n",
" return (\n",
" w_synth_adj\n",
" .join(y_synth, on='focal')\n",
" .eval('product = weight * y_synth')\n",
" .groupby('focal')\n",
" ['product']\n",
" .mean()\n",
" )\n",
"\n",
"def lag_spatial_adj_polars(w_adj_pl, y_pl):\n",
" return (\n",
" w_adj_pl\n",
" .join()\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "0fc56966",
"metadata": {},
"source": [
"## Data setup"
]
},
{
"cell_type": "markdown",
"id": "71c2aac9",
"metadata": {},
"source": [
"- Synthetic data"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "2ddaf1fe",
"metadata": {},
"outputs": [],
"source": [
"w_synth = weights.lat2W(1000, 1000, 'queen')\n",
"w_synth.transform = 'R'\n",
"w_synth_adj = w_synth.to_adjlist()\n",
"w_synth_adj_pl = pl.DataFrame(w_synth_adj)\n",
"y_synth = pandas.Series(np.random.random(1000000), name='y_synth')\n",
"y_synth_pl = pl.Series(y_synth)"
]
},
{
"cell_type": "markdown",
"id": "58762ae2",
"metadata": {},
"source": [
"- Spatial Signatures"
]
},
{
"cell_type": "markdown",
"id": "c772aa45",
"metadata": {},
"source": [
"## Spatial ops"
]
},
{
"cell_type": "markdown",
"id": "ba7d354a",
"metadata": {},
"source": [
"### Spatial Lag"
]
},
{
"cell_type": "markdown",
"id": "b5db2788",
"metadata": {},
"source": [
"- PySAL"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "cacf2df9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.02 ms ± 39.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"_ = weights.lag_spatial(w_synth, y)\n",
"%timeit pysal_lag = weights.lag_spatial(w_synth, y)"
]
},
{
"cell_type": "markdown",
"id": "6739772f",
"metadata": {},
"source": [
"- Adjacency `pandas`"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "682417de",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"299 ms ± 7.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit pandas_lag = lag_spatial_adj(w_synth_adj, y_synth)"
]
},
{
"cell_type": "markdown",
"id": "59d7f9ba",
"metadata": {},
"source": [
"- Adjacency `polars`"
]
},
{
"cell_type": "markdown",
"id": "c1e51920",
"metadata": {},
"source": [
"### LISA"
]
},
{
"cell_type": "markdown",
"id": "eaec1895",
"metadata": {},
"source": [
"- Single core"
]
},
{
"cell_type": "markdown",
"id": "4239a179",
"metadata": {},
"source": [
"- Parallel"
]
},
{
"cell_type": "markdown",
"id": "fe04cafd",
"metadata": {},
"source": [
"### `geopandas.overlay`"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment