Replies: 2 comments 2 replies
-
Can you show an example? What is done in notebook1 and in notebook2? Why is it not sufficient to load the registry from the same definition file? The text that you refer to is explaining how to share a unit registry across different parts of an application (not between different applications). |
Beta Was this translation helpful? Give feedback.
-
So I have two jupyter notebooks, A and B:
In B I've just loaded the variable, not even imported the pint module. I don't know what to do with it because of course when I initialize a new registry in notebook B, the Can't ``AttributeError: Can't pickle local object 'Relation.transformation..'` I would like to find a way to easily save data from one notebook and the load into another. These notebooks are meant for producing a report, so the value I want to store from notebook A derive from computation and other parameters not needed in notebook B. Keeping the file segregated help me to better authoring those document. I attached the notebook code notebook A{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"3 kilonewton"
],
"text/latex": [
"$3\\ \\mathrm{kilonewton}$"
],
"text/plain": [
"3 <Unit('kilonewton')>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pint\n",
"\n",
"# initialize a unit registry\n",
"u = pint.UnitRegistry()\n",
"\n",
"# create a quantity\n",
"a = 3*u.kN; a"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stored 'a' (Quantity)\n"
]
}
],
"source": [
"%store a"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "Can't pickle local object 'Relation.transformation.<locals>.<lambda>'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [7], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# try to store also the unit registry will fail\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_line_magic\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mstore\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mu\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2364\u001b[0m, in \u001b[0;36mInteractiveShell.run_line_magic\u001b[0;34m(self, magic_name, line, _stack_depth)\u001b[0m\n\u001b[1;32m 2362\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocal_ns\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_local_scope(stack_depth)\n\u001b[1;32m 2363\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbuiltin_trap:\n\u001b[0;32m-> 2364\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2365\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/IPython/extensions/storemagic.py:229\u001b[0m, in \u001b[0;36mStoreMagics.store\u001b[0;34m(self, parameter_s)\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m 228\u001b[0m \u001b[38;5;66;03m#pickled = pickle.dumps(obj)\u001b[39;00m\n\u001b[0;32m--> 229\u001b[0m \u001b[43mdb\u001b[49m\u001b[43m[\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mautorestore/\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43marg\u001b[49m\u001b[43m \u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m obj\n\u001b[1;32m 230\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mStored \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m (\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (arg, obj\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m))\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/pickleshare.py:118\u001b[0m, in \u001b[0;36mPickleShareDB.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[38;5;66;03m# We specify protocol 2, so that we can mostly go between Python 2\u001b[39;00m\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# and Python 3. We can upgrade to protocol 3 when Python 2 is obsolete.\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m fil\u001b[38;5;241m.\u001b[39mopen(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwb\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[0;32m--> 118\u001b[0m \u001b[43mpickle\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdump\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprotocol\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 119\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 120\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache[fil] \u001b[38;5;241m=\u001b[39m (value, fil\u001b[38;5;241m.\u001b[39mstat()\u001b[38;5;241m.\u001b[39mst_mtime)\n",
"\u001b[0;31mAttributeError\u001b[0m: Can't pickle local object 'Relation.transformation.<locals>.<lambda>'"
]
}
],
"source": [
"# try to store also the unit registry will fail with \"AttributeError: Can't pickle local object 'Relation.transformation.<locals>.<lambda>'\"\n",
"\n",
"%store u"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
notebook B{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"3 kilonewton"
],
"text/latex": [
"$3\\ \\mathrm{kilonewton}$"
],
"text/plain": [
"3 <Unit('kilonewton')>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#retrieve the stored quantity\n",
"%store -r a\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"pint.util.Quantity"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|
Beta Was this translation helpful? Give feedback.
-
Hello,
I want to share data between jupyter notebooks, probably using the magic
%store
.I can share a
Quantity
from the first notebook to the second, but thatQuantity
is now an "orphan" ofUnitRegistry
in the seconds notebook. I didn't understand how to use theset_application_registry
indicated at this link.Can please the process and usage be expressed more in detail? Or can the proper manner to share unit registry between notebooks being suggested?
thank you in advance
Beta Was this translation helpful? Give feedback.
All reactions