Skip to content

Instantly share code, notes, and snippets.

@benbovy
Created June 10, 2020 08:26
Show Gist options
  • Save benbovy/bd88f519788aa395237b2f5692912cde to your computer and use it in GitHub Desktop.
Save benbovy/bd88f519788aa395237b2f5692912cde to your computer and use it in GitHub Desktop.
xarray-simlab process class factory
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import attr\n",
"import xsimlab as xs\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def create_process(dim):\n",
" \n",
" @xs.process\n",
" class A:\n",
" x = xs.index(dims=dim)\n",
" \n",
" def initialize(self):\n",
" self.x = [0, 1, 2, 3]\n",
" \n",
" return A"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"B = create_process('y')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.create_process.<locals>.A"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"C = create_process('z')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.create_process.<locals>.A"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<A (xsimlab process)>\n",
"Variables:\n",
" x [out] ('y',) \n",
"Simulation stages:\n",
" initialize\n"
]
}
],
"source": [
"xs.process_info(B)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<A (xsimlab process)>\n",
"Variables:\n",
" x [out] ('z',) \n",
"Simulation stages:\n",
" initialize\n"
]
}
],
"source": [
"xs.process_info(C)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"# do not decorate A with attr.s or xs.process!!\n",
"\n",
"class A:\n",
" x = xs.index(dims='x')\n",
"\n",
" def initialize(self):\n",
" self.x = [0, 1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
"def make_process(cls_name, dim):\n",
" new_cls = type(cls_name, A.__bases__, dict(A.__dict__))\n",
" \n",
" new_cls.x.metadata['dims'] = dim\n",
" \n",
" return xs.process(new_cls)\n"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"B = make_process('B', 'y')"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.B"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<B (xsimlab process)>\n",
"Variables:\n",
" x [out] y \n",
"Simulation stages:\n",
" initialize\n"
]
}
],
"source": [
"xs.process_info(B)"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"C = make_process('C', 'z')"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.C"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<C (xsimlab process)>\n",
"Variables:\n",
" x [out] z \n",
"Simulation stages:\n",
" initialize\n"
]
}
],
"source": [
"xs.process_info(C)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:xsimlab_dev]",
"language": "python",
"name": "conda-env-xsimlab_dev-py"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment