Skip to content

Instantly share code, notes, and snippets.

@jreadey
Created May 6, 2024 23:10
Show Gist options
  • Save jreadey/884b59a30090b0f8122c3ab51291aa6c to your computer and use it in GitHub Desktop.
Save jreadey/884b59a30090b0f8122c3ab51291aa6c to your computer and use it in GitHub Desktop.
Attempt to write to vlen dataset
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dtvlen = h5py.special_dtype(vlen=np.float64)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"f = h5py.File(\"vlen_test.h5\", \"w\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"e0 = np.array([1.1, 2.2, 3.3], dtype=np.float64)\n",
"e1 = np.array([1.9, 2.8, 3.7], dtype=np.float64) \n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([e0, e1], dtype=dtvlen)\n",
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2,)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.empty((2,), dtype=dtvlen)\n",
"arr[...] = [e0, e1]\n",
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"dset = f.create_dataset(\"vlen_dset\", (2,), dtype=dtvlen)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "Can't broadcast (2, 3) -> (2,)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m dset[\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m] \u001b[38;5;241m=\u001b[39m arr\n",
"File \u001b[0;32mh5py/_objects.pyx:54\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mh5py/_objects.pyx:55\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32m~/anaconda3/envs/py39/lib/python3.9/site-packages/h5py/_hl/dataset.py:1007\u001b[0m, in \u001b[0;36mDataset.__setitem__\u001b[0;34m(self, args, val)\u001b[0m\n\u001b[1;32m 1004\u001b[0m mshape \u001b[38;5;241m=\u001b[39m val\u001b[38;5;241m.\u001b[39mshape\n\u001b[1;32m 1006\u001b[0m \u001b[38;5;66;03m# Perform the write, with broadcasting\u001b[39;00m\n\u001b[0;32m-> 1007\u001b[0m mspace \u001b[38;5;241m=\u001b[39m h5s\u001b[38;5;241m.\u001b[39mcreate_simple(\u001b[43mselection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexpand_shape\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmshape\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 1008\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m fspace \u001b[38;5;129;01min\u001b[39;00m selection\u001b[38;5;241m.\u001b[39mbroadcast(mshape):\n\u001b[1;32m 1009\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mid\u001b[38;5;241m.\u001b[39mwrite(mspace, fspace, val, mtype, dxpl\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_dxpl)\n",
"File \u001b[0;32m~/anaconda3/envs/py39/lib/python3.9/site-packages/h5py/_hl/selections.py:264\u001b[0m, in \u001b[0;36mSimpleSelection.expand_shape\u001b[0;34m(self, source_shape)\u001b[0m\n\u001b[1;32m 262\u001b[0m eshape\u001b[38;5;241m.\u001b[39mappend(t)\n\u001b[1;32m 263\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 264\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt broadcast \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m -> \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (source_shape, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39marray_shape)) \u001b[38;5;66;03m# array shape\u001b[39;00m\n\u001b[1;32m 266\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28many\u001b[39m([n \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m remaining_src_dims]):\n\u001b[1;32m 267\u001b[0m \u001b[38;5;66;03m# All dimensions from target_shape should either have been popped\u001b[39;00m\n\u001b[1;32m 268\u001b[0m \u001b[38;5;66;03m# to match the selection shape, or be 1.\u001b[39;00m\n\u001b[1;32m 269\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt broadcast \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m -> \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (source_shape, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39marray_shape)) \u001b[38;5;66;03m# array shape\u001b[39;00m\n",
"\u001b[0;31mTypeError\u001b[0m: Can't broadcast (2, 3) -> (2,)"
]
}
],
"source": [
"dset[...] = arr"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2,)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dset.shape"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py39",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment