Created
January 25, 2018 23:23
-
-
Save asford/ab9400909c346cf73141feebdad40abd to your computer and use it in GitHub Desktop.
packed dtype transformations demo
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": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import numpy", | |
"execution_count": 54, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "full_type = numpy.dtype([\n (\"id\", \"u8\"),\n (\"ht\", \"f8\", (4, 4)),\n])\nhashed_type = numpy.dtype([(\"id\", \"u8\"), (\"hashed_ht\", \"u8\")])", | |
"execution_count": 55, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def hash_ht(ht_array):\n return (ht_array.sum(axis=-1).sum(axis=-1) * 1000).astype(\"u8\")", | |
"execution_count": 56, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def hash_full(full_array):\n result = numpy.empty_like(full_array, dtype=hashed_type)\n result[\"id\"] = full_array[\"id\"]\n result[\"hashed_ht\"] = hash_ht(full_array[\"ht\"])\n return result", | |
"execution_count": 57, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "src_data = numpy.empty(10, full_type)\nsrc_data[\"id\"] = numpy.arange(10)\nsrc_data[\"ht\"] = numpy.random.random((10, 4, 4))", | |
"execution_count": 58, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "print(hash_full(src_data))", | |
"execution_count": 59, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "[(0, 8039) (1, 10326) (2, 7956) (3, 7414) (4, 6769) (5, 8062)\n (6, 9912) (7, 7881) (8, 7764) (9, 9540)]\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "conda-env-dev-py", | |
"display_name": "Python [conda env:dev]", | |
"language": "python" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": "block", | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"version": 3, | |
"name": "ipython" | |
}, | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"version": "3.5.4", | |
"pygments_lexer": "ipython3", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "packed dtype transformations demo", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment