Created
September 28, 2020 00:37
-
-
Save ajfriend/1afc9d063405e3d2482044ea26caf58a to your computer and use it in GitHub Desktop.
H3 Cell iterator ideas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We could probably do something similar in C if we defined an iterator struct with some helper functions like option 2 here:\n", | |
"\n", | |
"https://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Iterators.html\n", | |
"\n", | |
"This iterator pattern would be very flexible, allowing us to do the \"exhaustive\" testing in C without code duplication or function pointers.\n", | |
"\n", | |
"Would this help speed up or reduce memory usage of:\n", | |
"\n", | |
"- testing code (yes!)\n", | |
"- `uncompact` (probably?)\n", | |
"- `polyfill` (maybe?)\n", | |
"\n", | |
"\n", | |
"## Questions\n", | |
"\n", | |
"- does it make more sense to have depth parameterized in absolute terms (resolution), or in relative terms (\"how many resolution hops down\")?\n", | |
"- i think we had a similar discussion at some point on if `h3.h3_to_children` should be in relative or absolute terms" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import h3\n", | |
"\n", | |
"# actually, this function is almost an exact duplicate of `h3_to_children`! (just the iterator/relative version)\n", | |
"def iter_children(h, depth=0):\n", | |
" assert depth >= 0\n", | |
" \n", | |
" if depth == 0:\n", | |
" yield h\n", | |
" else:\n", | |
" for c in h3.h3_to_children(h):\n", | |
" yield from iter_children(c, depth-1)\n", | |
"\n", | |
" \n", | |
"def iter_all_cells_at_res(res):\n", | |
" for c in h3.get_res0_indexes():\n", | |
" yield from iter_children(c, res)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"h0 = h3.geo_to_h3(0,0,0)\n", | |
"depth = 6" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Things seem to run a little bit slower when we recursively iterate through children.\n", | |
"- I wonder if the recursive/iterative pattern wins out when it is all done in C?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"489 ms ± 11.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"sum(\n", | |
" h3.cell_area(h)\n", | |
" for h in h3.uncompact({h0}, depth)\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"641 ms ± 16.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"sum(\n", | |
" h3.cell_area(h)\n", | |
" for h in iter_children(h0, depth)\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"842" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(list(iter_all_cells_at_res(1)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment