Skip to content

Instantly share code, notes, and snippets.

@snowman2
Last active October 3, 2019 00:57
Show Gist options
  • Save snowman2/c62d96079725a4d5a83f619a43704908 to your computer and use it in GitHub Desktop.
Save snowman2/c62d96079725a4d5a83f619a43704908 to your computer and use it in GitHub Desktop.
Compare pygeos to array_transform
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from functools import partial\n",
"\n",
"import numpy\n",
"from numpy import arange\n",
"from pyproj import Transformer\n",
"from shapely.geometry import Point, LineString\n",
"from shapely.vectorized import array_transform\n",
"from pygeos import apply, Geometry"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"linestring = LineString([Point(ii, ii) for ii in arange(0., 90., 0.001)])\n",
"glinestring = Geometry(str(linestring))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"transformer = Transformer.from_crs(4326, 26913)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def transformation(coords):\n",
" x, y = transformer.transform(*coords.T)\n",
" return numpy.array([x, y]).T\n",
"shape_apply = partial(apply, transformation=transformation)\n",
"array_shape_transformer = partial(array_transform, transformer.transform)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"66.9 ms ± 2.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit shape_apply(glinestring)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"67.7 ms ± 2.78 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit array_shape_transformer(linestring)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"ar_linestring = numpy.array([glinestring for _ in range(1000)], dtype=numpy.object)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1min 5s ± 2.3 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit shape_apply(ar_linestring)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def apply1k():\n",
" for _ in range(1000):\n",
" array_shape_transformer(linestring)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1min 4s ± 1.45 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit apply1k()"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment