Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Created February 15, 2023 22:20
Show Gist options
  • Save bogovicj/49c112432d98ca0a7e1a04df6685c889 to your computer and use it in GitHub Desktop.
Save bogovicj/49c112432d98ca0a7e1a04df6685c889 to your computer and use it in GitHub Desktop.
Playing with zarr object stores
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"id": "b771bbb4",
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"import numcodecs\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "a41239a8",
"metadata": {},
"outputs": [],
"source": [
"class Thing:\n",
" i=0;\n",
" d=0.1;\n",
" s=\"a\";\n",
" t=None;\n",
" \n",
" def __init__(self, i, d, s, t):\n",
" self.i = i\n",
" self.d = d\n",
" self.s = s\n",
" self.t = t\n",
" \n",
" def __str__(self):\n",
" return f\"{self.i} {self.d} {self.s} ({self.t})\"\n",
" \n",
" def toJSON(self):\n",
" return json.dumps(self, default=lambda o: o.__dict__, \n",
" sort_keys=True, indent=4)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "56105666",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 0.4 d (2 0.3 c (1 0.2 b (0 0.1 a (None))))\n",
"{\n",
" \"d\": 0.4,\n",
" \"i\": 3,\n",
" \"s\": \"d\",\n",
" \"t\": {\n",
" \"d\": 0.3,\n",
" \"i\": 2,\n",
" \"s\": \"c\",\n",
" \"t\": {\n",
" \"d\": 0.2,\n",
" \"i\": 1,\n",
" \"s\": \"b\",\n",
" \"t\": {\n",
" \"d\": 0.1,\n",
" \"i\": 0,\n",
" \"s\": \"a\",\n",
" \"t\": null\n",
" }\n",
" }\n",
" }\n",
"}\n"
]
}
],
"source": [
"a=Thing(0,0.1,\"a\",None)\n",
"b=Thing(1,0.2,\"b\",a)\n",
"c=Thing(2,0.3,\"c\",b)\n",
"d=Thing(3,0.4,\"d\",c)\n"
]
},
{
"cell_type": "markdown",
"id": "5c065aa0",
"metadata": {},
"source": [
"Zarr complains that Thing objects above are not JSON serializable, so let's try dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "62fb567f",
"metadata": {},
"outputs": [],
"source": [
"def thing( i, d, s, t ):\n",
" return { \"i\":i, \"d\": d, \"s\":s, \"t\":t }\n",
"\n",
"a = thing( 0, 0.1, \"a\", None )\n",
"b = thing( 1, 0.2, \"b\", a )\n",
"c = thing( 2, 0.3, \"c\", b )\n",
"d = thing( 3, 0.4, \"d\", c )"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "8284d5a5",
"metadata": {},
"outputs": [],
"source": [
"store = zarr.DirectoryStore('thingObj.zarr')\n",
"z = zarr.empty(4, dtype=object, store=store, object_codec=numcodecs.JSON())\n",
"z[0] = a\n",
"z[1] = b\n",
"z[2] = c\n",
"z[3] = d"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "1ab79b78",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'d': 0.4,\n",
" 'i': 3,\n",
" 's': 'd',\n",
" 't': {'d': 0.3,\n",
" 'i': 2,\n",
" 's': 'c',\n",
" 't': {'d': 0.2,\n",
" 'i': 1,\n",
" 's': 'b',\n",
" 't': {'d': 0.1, 'i': 0, 's': 'a', 't': None}}}}"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zr = zarr.open('thingObj.zarr', mode='r')\n",
"zr[3]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment