Skip to content

Commit

Permalink
add demo notebook to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Feb 29, 2024
1 parent 33ed71f commit 6d318ba
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 52 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ documents efficiently.
## Installation
```bash
pip install bigraph-builder
```

## Tutorials

To get started with Bigraph-viz, explore our resources:
* [Bigraph Builder Tutorial](https://vivarium-collective.github.io/bigraph-builder/notebooks/basics.html).
119 changes: 67 additions & 52 deletions notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@
{
"data": {
"text/plain": [
"['toy',\n",
"['console-emitter',\n",
" 'ram-emitter',\n",
" 'console-emitter',\n",
" 'GillespieEvent',\n",
" 'GillespieInterval']"
" 'toy',\n",
" 'GillespieInterval',\n",
" 'GillespieEvent']"
]
},
"execution_count": 8,
Expand Down Expand Up @@ -319,7 +319,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x113debd60>"
"<graphviz.graphs.Digraph at 0x1115ecf70>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -382,7 +382,7 @@
" 'address': 'local:GillespieEvent',\n",
" 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n",
" 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieEvent object at 0x113ddf760>,\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieEvent object at 0x1115eca90>,\n",
" 'interval': 1.0,\n",
" 'outputs': {'mRNA': ['mRNA_store']}}})"
]
Expand Down Expand Up @@ -493,7 +493,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x113deb5e0>"
"<graphviz.graphs.Digraph at 0x1115ec940>"
]
},
"execution_count": 13,
Expand Down Expand Up @@ -668,7 +668,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x113ddfb80>"
"<graphviz.graphs.Digraph at 0x1115eb100>"
]
},
"execution_count": 15,
Expand Down Expand Up @@ -846,7 +846,7 @@
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x113dffd30>"
"<graphviz.graphs.Digraph at 0x1115eb1f0>"
]
},
"execution_count": 17,
Expand Down Expand Up @@ -880,15 +880,15 @@
" 'address': 'local:GillespieEvent',\n",
" 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n",
" 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieEvent object at 0x113ddf760>,\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieEvent object at 0x1115eca90>,\n",
" 'interval': 1.0,\n",
" 'outputs': {'mRNA': ['mRNA_store']}},\n",
" 'interval_process': { '_type': 'step',\n",
" 'address': 'local:GillespieInterval',\n",
" 'config': {'kdeg': 0.1, 'ktsc': 5.0},\n",
" 'inputs': { 'DNA': ['DNA_store'],\n",
" 'mRNA': ['mRNA_store']},\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieInterval object at 0x113dff070>,\n",
" 'instance': <process_bigraph.experiments.minimal_gillespie.GillespieInterval object at 0x1115eb790>,\n",
" 'outputs': {'interval': ['event_process', 'interval']}},\n",
" 'mRNA_store': {}})"
]
Expand All @@ -913,7 +913,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "212f4501-8050-4c39-88b8-986b3c38e716",
"metadata": {},
"outputs": [],
Expand All @@ -922,51 +922,23 @@
" 'DNA_store': {\n",
" 'A gene': 2.0,\n",
" 'B gene': 1.0},\n",
"}"
"}\n",
"b.update(initial_state)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 20,
"id": "e9a69f36-154c-416e-b00c-5df0726be49e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"produced interval: {'interval': 0.029337100553640385}\n",
"received interval: 0.029337100553640385\n",
"produced interval: {'interval': 0.0}\n",
"received interval: 0.0\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/eranagmon/code/process-bigraph/process_bigraph/experiments/minimal_gillespie.py:139: RuntimeWarning: invalid value encountered in scalar divide\n",
" if r_rxn < propensities[i] / prop_sum:\n"
]
},
{
"ename": "ValueError",
"evalue": "scale < 0",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[24], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# make composite from the current builder config, simulate\u001b[39;00m\n\u001b[1;32m 2\u001b[0m composite \u001b[38;5;241m=\u001b[39m b\u001b[38;5;241m.\u001b[39mgenerate()\n\u001b[0;32m----> 3\u001b[0m \u001b[43mcomposite\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/composite.py:1044\u001b[0m, in \u001b[0;36mComposite.run\u001b[0;34m(self, interval, force_complete)\u001b[0m\n\u001b[1;32m 1041\u001b[0m \u001b[38;5;66;03m# get all update paths, then trigger steps that\u001b[39;00m\n\u001b[1;32m 1042\u001b[0m \u001b[38;5;66;03m# depend on those paths\u001b[39;00m\n\u001b[1;32m 1043\u001b[0m update_paths \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mapply_updates(updates)\n\u001b[0;32m-> 1044\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrigger_steps\u001b[49m\u001b[43m(\u001b[49m\u001b[43mupdate_paths\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1046\u001b[0m \u001b[38;5;66;03m# # display and emit\u001b[39;00m\n\u001b[1;32m 1047\u001b[0m \u001b[38;5;66;03m# if self.progress_bar:\u001b[39;00m\n\u001b[1;32m 1048\u001b[0m \u001b[38;5;66;03m# print_progress_bar(self.global_time, end_time)\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1056\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1057\u001b[0m \u001b[38;5;66;03m# all processes have run past the interval\u001b[39;00m\n\u001b[1;32m 1058\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mglobal_time\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m end_time\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/composite.py:1136\u001b[0m, in \u001b[0;36mComposite.trigger_steps\u001b[0;34m(self, update_paths)\u001b[0m\n\u001b[1;32m 1133\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mreset_step_state(steps_to_run)\n\u001b[1;32m 1134\u001b[0m to_run \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcycle_step_state()\n\u001b[0;32m-> 1136\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_steps\u001b[49m\u001b[43m(\u001b[49m\u001b[43mto_run\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/composite.py:1097\u001b[0m, in \u001b[0;36mComposite.run_steps\u001b[0;34m(self, step_paths)\u001b[0m\n\u001b[1;32m 1087\u001b[0m step \u001b[38;5;241m=\u001b[39m get_path(\n\u001b[1;32m 1088\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate,\n\u001b[1;32m 1089\u001b[0m step_path)\n\u001b[1;32m 1091\u001b[0m state \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcore\u001b[38;5;241m.\u001b[39mview_edge(\n\u001b[1;32m 1092\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcomposition,\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate,\n\u001b[1;32m 1094\u001b[0m step_path,\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minputs\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m-> 1097\u001b[0m step_update \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess_update\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1098\u001b[0m \u001b[43m \u001b[49m\u001b[43mstep_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1099\u001b[0m \u001b[43m \u001b[49m\u001b[43mstep\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1100\u001b[0m \u001b[43m \u001b[49m\u001b[43mstate\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1101\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1102\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43moutputs\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1104\u001b[0m updates\u001b[38;5;241m.\u001b[39mappend(step_update)\n\u001b[1;32m 1106\u001b[0m update_paths \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mapply_updates(updates)\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/composite.py:885\u001b[0m, in \u001b[0;36mComposite.process_update\u001b[0;34m(self, path, process, states, interval, ports_key)\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprocess_update\u001b[39m(\n\u001b[1;32m 859\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 860\u001b[0m path,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 863\u001b[0m interval,\n\u001b[1;32m 864\u001b[0m ports_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124moutputs\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[1;32m 866\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Start generating a process's update.\u001b[39;00m\n\u001b[1;32m 867\u001b[0m \n\u001b[1;32m 868\u001b[0m \u001b[38;5;124;03m This function is similar to :py:meth:`_invoke_process` except in\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 883\u001b[0m \u001b[38;5;124;03m ``store``.\u001b[39;00m\n\u001b[1;32m 884\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 885\u001b[0m update \u001b[38;5;241m=\u001b[39m \u001b[43mprocess\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43minstance\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minterval\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 887\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdefer_project\u001b[39m(update, args):\n\u001b[1;32m 888\u001b[0m schema, state, path \u001b[38;5;241m=\u001b[39m args\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/composite.py:401\u001b[0m, in \u001b[0;36mStep.invoke\u001b[0;34m(self, state, _)\u001b[0m\n\u001b[1;32m 400\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minvoke\u001b[39m(\u001b[38;5;28mself\u001b[39m, state, _\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m--> 401\u001b[0m update \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mupdate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstate\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 402\u001b[0m sync \u001b[38;5;241m=\u001b[39m SyncUpdate(update)\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m sync\n",
"File \u001b[0;32m~/code/process-bigraph/process_bigraph/experiments/minimal_gillespie.py:79\u001b[0m, in \u001b[0;36mGillespieInterval.update\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 76\u001b[0m prop_sum \u001b[38;5;241m=\u001b[39m \u001b[38;5;28msum\u001b[39m(propensities)\n\u001b[1;32m 78\u001b[0m \u001b[38;5;66;03m# The wait time is distributed exponentially\u001b[39;00m\n\u001b[0;32m---> 79\u001b[0m interval \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrandom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexponential\u001b[49m\u001b[43m(\u001b[49m\u001b[43mscale\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprop_sum\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 81\u001b[0m output \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 82\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minterval\u001b[39m\u001b[38;5;124m'\u001b[39m: interval}\n\u001b[1;32m 84\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mproduced interval: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00moutput\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
"File \u001b[0;32mnumpy/random/mtrand.pyx:570\u001b[0m, in \u001b[0;36mnumpy.random.mtrand.RandomState.exponential\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32m_common.pyx:612\u001b[0m, in \u001b[0;36mnumpy.random._common.cont\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32m_common.pyx:422\u001b[0m, in \u001b[0;36mnumpy.random._common.check_constraint\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: scale < 0"
"produced interval: {'interval': 5.929431187150016}\n",
"received interval: 5.929431187150016\n",
"produced interval: {'interval': 6.845295472533056}\n"
]
}
],
Expand All @@ -978,17 +950,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"id": "578492a9-027d-4c5b-b90a-7d63e3f46bc0",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<process_bigraph.composite.Composite at 0x111605f10>"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"composite"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"id": "c975aab012b2706f",
"metadata": {
"ExecuteTime": {
Expand All @@ -999,15 +982,39 @@
"outputs_hidden": false
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'event_process': {'_type': 'process',\n",
" 'address': 'local:GillespieEvent',\n",
" 'config': {'kdeg': 1.0, 'ktsc': 5.0},\n",
" 'inputs': {'mRNA': ['mRNA_store'], 'DNA': ['DNA_store']},\n",
" 'outputs': {'mRNA': ['mRNA_store']},\n",
" 'interval': 1.0},\n",
" 'mRNA_store': {'A mRNA': '1.0', 'B mRNA': '1.0'},\n",
" 'DNA_store': {'A gene': '2.0', 'B gene': '1.0'},\n",
" 'interval_process': {'_type': 'step',\n",
" 'address': 'local:GillespieInterval',\n",
" 'config': {'ktsc': 5.0, 'kdeg': 0.1},\n",
" 'inputs': {'DNA': ['DNA_store'], 'mRNA': ['mRNA_store']},\n",
" 'outputs': {'interval': ['event_process', 'interval']}},\n",
" 'global_time': '0.0'}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"doc = b.document()\n",
"doc"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"id": "752e43636bf9fd17",
"metadata": {
"ExecuteTime": {
Expand All @@ -1018,7 +1025,15 @@
"outputs_hidden": false
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File 'toy_bigraph' successfully written in 'out' directory.\n"
]
}
],
"source": [
"b.write(filename='toy_bigraph')"
]
Expand Down

0 comments on commit 6d318ba

Please sign in to comment.