Skip to content

Commit

Permalink
add support for multiple coordinate selection
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Aug 13, 2024
1 parent 41be479 commit b8b3b96
Show file tree
Hide file tree
Showing 5 changed files with 544 additions and 50 deletions.
318 changes: 318 additions & 0 deletions examples/notebooks/fancy_selection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"USE_H5PY=False\n",
"if USE_H5PY:\n",
" import h5py\n",
" filepath = \"./fancy_select.h5\"\n",
"else:\n",
" import h5pyd as h5py\n",
" filepath = \"/home/test_user1/test/fancy_select.h5\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]],\n",
"\n",
" [[12, 13, 14, 15],\n",
" [16, 17, 18, 19],\n",
" [20, 21, 22, 23]]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a numpy array\n",
"arr = np.arange(2*3*4).reshape(2,3,4)\n",
"arr"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 1, 3],\n",
" [ 4, 5, 7],\n",
" [ 8, 9, 11]],\n",
"\n",
" [[12, 13, 15],\n",
" [16, 17, 19],\n",
" [20, 21, 23]]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fancy selection with one coordinate\n",
"arr[:, :, [0,1,3]]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 6, 11],\n",
" [13, 18, 23]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# numpy allows multiple coordinate selections also\n",
"arr[:,[0,1,2],[1,2,3]]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# create a new domain/file\n",
"f = h5py.File(filepath, \"w\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<HDF5 dataset \"dset\": shape (5, 1000, 1000), type \"<i4\">"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a dataset\n",
"dset = f.create_dataset(\"dset\", (5, 1000, 1000), dtype=\"i4\")\n",
"dset"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 500, 500)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# HSDS will auto-chunk the dataset\n",
"dset.chunks"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'d-613c271a-724c7351-aa2e-3f314c-8deb03'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dset.id.id"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.53 s, sys: 2.28 ms, total: 1.53 s\n",
"Wall time: 5.23 s\n"
]
}
],
"source": [
"%%time\n",
"# write some values to the dataset\n",
"for i in range(1000):\n",
" dset[:, i, i] = [i, i*10, i*100, i*1000, i*10000] # .."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 444, 4440, 44400, 444000, 4440000], dtype=int32)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# normal hyperslab selection\n",
"dset[:, 444, 444]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 444, 0],\n",
" [ 0, 4440, 0],\n",
" [ 0, 44400, 0],\n",
" [ 0, 444000, 0],\n",
" [ 0, 4440000, 0]], dtype=int32)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fancyselection with single coordinate (works with h5py and h5pyd)\n",
"dset[:, 444, [333,444,555]]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 0, 444],\n",
" [ 0, 0, 4440],\n",
" [ 0, 0, 44400],\n",
" [ 0, 0, 444000],\n",
" [ 0, 0, 4440000]], dtype=int32)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# coordinates need to be increasing with h5py (though not with h5pyd)\n",
"dset[:, 444, [333, 555, 444]]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 10, 100],\n",
" [ 10, 100, 1000],\n",
" [ 100, 1000, 10000],\n",
" [ 1000, 10000, 100000],\n",
" [ 10000, 100000, 1000000]], dtype=int32)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fancyselection with multiple coordinates (only works with h5pyd)\n",
"dset[:, [1,10,100],[1,10,100]]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# close file\n",
"f.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "h5",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit b8b3b96

Please sign in to comment.