{
"cells": [
{
"cell_type": "markdown",
"id": "098d426e-2d4a-4746-afcd-f88a05d66d9b",
"metadata": {},
"source": [
"# Process Bigraph Diagrams for Tumor-Tcell ABM"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "7538b3a8-e11c-4771-87ab-d83b077b70ed",
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"from bigraph_viz import plot_bigraph, plot_multitimestep, convert_vivarium_composite\n",
"from bigraph_viz.dict_utils import replace_regex_recursive\n",
"from tumor_tcell.experiments.main import large_experiment\n",
"from vivarium.core.engine import pf\n",
"from vivarium.core.composer import Composite\n",
"\n",
"plot_settings = {}\n",
"save_images = False\n",
"if save_images:\n",
" plot_settings.update({'out_dir': 'out','dpi': '250'})\n",
" \n",
"plot_settings2={}\n",
"if save_images:\n",
" plot_settings2.update({\n",
" 'out_dir': 'out',\n",
" 'dpi': '250'\n",
" })\n",
"\n",
"def get_in(dict, path):\n",
" if path:\n",
" dict = get_in(dict[path[0]], path[1:]) \n",
" return dict"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "e87c3aa6-acb0-452c-8256-268eb583400a",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"typed_store_spec = {\n",
" 'store1': {\n",
" '_value': 'int, float, array, ...'}}\n",
"\n",
"# plot\n",
"plot_bigraph(replace_regex_recursive(typed_store_spec), **plot_settings, show_values=True, show_types=True, filename='store')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "6712dec8-7a5f-4c4d-b9db-763bf401e57c",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"process_spec = {\n",
" '_type': 'python_process',\n",
" 'process1': {\n",
" '_type': 'edge',\n",
" '_ports': {\n",
" 'port1': 'Any',\n",
" 'port2': 'Any'}}}\n",
"\n",
"# plot\n",
"plot_bigraph(process_spec, **plot_settings, rankdir='RL', filename='process')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "731e6c49-cd43-4246-a4e5-f8267fbfefdf",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_spec = {\n",
" 'cell process': {\n",
" 'wires': {\n",
" 'port1': [],}}}\n",
"\n",
"abm_spec = {\n",
" 'environment': {\n",
" 'agents': {\n",
" 'cell 1': agent_spec,\n",
" 'cell 2': agent_spec}},\n",
" 'environment process': {\n",
" 'wires': {\n",
" 'port1': 'environment'}}}\n",
"\n",
"# plot\n",
"node_groups = [\n",
" [('environment',), ('environment process',)],\n",
" [('environment', 'agents', 'cell 1'), ('environment', 'agents', 'cell 1', 'cell process',)],\n",
" [('environment', 'agents', 'cell 2'), ('environment', 'agents', 'cell 2', 'cell process',)],\n",
"]\n",
"plot_bigraph(abm_spec, **plot_settings, node_groups=node_groups, port_labels=False, remove_process_place_edges=True, filename='abm_composite')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "b2f091b4-82df-4dc5-936c-bfddbf807a50",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"multitimestep_spec = {\n",
" 'temporal process2': {\n",
" '_ports': {'port1': 'Any'},\n",
" 'wires': {'port1': 'state'},\n",
" '_sync_step': 1.0,\n",
" },\n",
" 'temporal process1': {\n",
" '_ports': {'port1': 'Any'},\n",
" 'wires': {'port1': 'state'},\n",
" '_sync_step': 0.5,\n",
" },\n",
"}\n",
"multitimestep_spec = replace_regex_recursive(multitimestep_spec)\n",
"plot_multitimestep(multitimestep_spec, total_time=2.0, **plot_settings2, filename='multitimestep')"
]
},
{
"cell_type": "markdown",
"id": "4ee8b282-c54d-46f2-a60b-d366343fe73a",
"metadata": {},
"source": [
"## Full ABM"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "8166389b-a006-490a-8e84-9e67e49591ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing experiment tumor_tcell_20231029.114658\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"0it [00:00, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Completed in 0.00 seconds\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"# get the experiment\n",
"data, experiment = large_experiment(\n",
" n_tcells=1,\n",
" n_tumors=1,\n",
" n_dendritic=1,\n",
" n_tcells_lymph_node=0,\n",
" tumors_state_PDL1n=0.5,\n",
" tcells_total_PD1n=8,\n",
" dendritic_state_active=0.5,\n",
" lymph_nodes=True,\n",
" total_time=0, \n",
" field_molecules=['IFNg', 'tumor_debris'],\n",
" return_experiment=True,\n",
" )\n",
"\n",
"# make a composite from the experiment\n",
"composite = Composite(\n",
" state=experiment.state.get_value(), \n",
" processes=experiment.state.get_processes(), \n",
" steps=experiment.state.get_steps(), \n",
" topology=experiment.state.get_topology(), \n",
" flow=experiment.state.get_flow(),\n",
")\n",
"del composite['_schema']\n",
"del composite['state']\n",
"\n",
"# convert to a bigraph spec for visualization\n",
"abm_experiment_spec = convert_vivarium_composite(composite)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "589648aa-b5b8-497c-b937-366c87dae3de",
"metadata": {},
"outputs": [],
"source": [
"# tidy up\n",
"lymph_node_transfer = {'lymph_node_transfer': {'wires': experiment.state.get_value()['lymph_node_transfer'][1]}}\n",
"abm_experiment_spec.update(lymph_node_transfer)\n",
"del abm_experiment_spec['tumor_environment']['death_log']\n",
"del abm_experiment_spec['tumor_environment']['log']\n",
"del abm_experiment_spec['tumor_environment']['clock']\n",
"del abm_experiment_spec['tumor_environment']['global_time']\n",
"\n",
"node_groups = [\n",
" [\n",
" ('tumor_environment',), \n",
" ('in_transit',), ('lymph_node',), \n",
" ('lymph_node_transfer',)],\n",
" [\n",
" ('tumor_environment', 'dimensions',), \n",
" ('tumor_environment', 'fields',), \n",
" ('tumor_environment', 'agents',), \n",
" # ('tumor_environment', 'diffusion_field',), \n",
" # ('tumor_environment', 'neighbors_multibody',), \n",
" ],\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "10df1f44-fd56-46b9-8806-a959c071978f",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c1 = 'orange'\n",
"c2 = 'cyan'\n",
"c3 = 'orchid'\n",
"c4 = 'limegreen'\n",
"node_colors0 = {\n",
" ('tumor_environment',): c1,\n",
" ('in_transit',): c1,\n",
" ('lymph_node',): c1,\n",
"}\n",
"node_colors1 = {\n",
" ('tumor_environment', 'agents', 'tcell_0'): c3,\n",
" ('tumor_environment', 'agents', 'dendritic_0'): c3,\n",
" ('tumor_environment', 'agents', 'tumor_0'): c3,\n",
"}\n",
"node_colors2 = {\n",
" ('tumor_environment', 'agents',): c2,\n",
" ('tumor_environment', 'fields'): c2,\n",
" ('tumor_environment', 'dimensions'): c2,\n",
"}\n",
"\n",
"plot_bigraph(abm_experiment_spec, \n",
" **plot_settings,\n",
" node_groups=node_groups,\n",
" # node_fill_colors={**node_colors0, **node_colors1, **node_colors2},\n",
" remove_process_place_edges=True,\n",
" filename='abm_experiment_full',\n",
" # **{'out_dir': 'out','dpi': '250'}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "122b14d5-bed9-4502-94bd-1ad27ac0e16b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abm_experiment_spec2 = copy.deepcopy(abm_experiment_spec)\n",
"\n",
"# remove some cells for simplicity\n",
"abm_experiment_spec2['tumor_environment']['agents']['dendritic_0'] = {}\n",
"abm_experiment_spec2['tumor_environment']['agents']['tumor_0'] = {}\n",
"internal_spec = {'cell_state': {}}\n",
"boundary_spec = {'diameter': {}, 'exchange': {'IFNg': {}}}\n",
"neighbors_spec = {\n",
" 'present': {'PD1': {}, 'TCR': {}}, \n",
" 'accept': {'PDL1': {}, 'MHCI': {}}}\n",
"abm_experiment_spec2['tumor_environment']['agents']['tcell_0']['internal'] = internal_spec\n",
"abm_experiment_spec2['tumor_environment']['agents']['tcell_0']['boundary'].update(boundary_spec)\n",
"abm_experiment_spec2['tumor_environment']['agents']['tcell_0']['neighbors'].update(neighbors_spec)\n",
"\n",
"# plot full bigraph\n",
"node_colors3 = {\n",
" ('tumor_environment', 'agents', 'tcell_0', 'boundary'): c4,\n",
" ('tumor_environment', 'agents', 'tcell_0', 'internal'): c4,\n",
" ('tumor_environment', 'agents', 'tcell_0', 'neighbors'): c4,\n",
"}\n",
"node_border_colors = {\n",
" ('tumor_environment', 'agents', 'tcell_0', 't_cell'): 'red',\n",
" ('tumor_environment', 'agents', 'tcell_0', 'boundary', 'location'): 'gold',\n",
" ('tumor_environment', 'agents', 'tcell_0', 'boundary', 'diameter'): 'gold',\n",
" ('tumor_environment', 'agents', 'tcell_0', 'boundary', 'exchange', 'IFNg'): 'gold',\n",
" ('tumor_environment', 'agents', 'tcell_0', 'internal', 'cell_state'): 'gold',\n",
" ('tumor_environment', 'agents', 'tcell_0', 'neighbors', 'present', 'PD1'): 'gold',\n",
"}\n",
"plot_bigraph(abm_experiment_spec2, \n",
" **plot_settings,\n",
" node_groups=node_groups,\n",
" node_fill_colors={**node_colors0, **node_colors1, **node_colors2, **node_colors3},\n",
" node_border_colors=node_border_colors,\n",
" filename='abm_experiment_tailored',\n",
" remove_process_place_edges=True,\n",
" # **{'out_dir': 'out','dpi': '250'}\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "4c0863ec-d4d0-48a1-8af4-39899bbc314d",
"metadata": {},
"source": [
"## Environment composite"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "b3ac3d11-c31c-4ad7-9353-bb7092cfdc3c",
"metadata": {},
"outputs": [],
"source": [
"tumor_environment_spec = copy.deepcopy(abm_experiment_spec['tumor_environment'])\n",
"tumor_agents_spec = copy.deepcopy(abm_experiment_spec['tumor_environment']['agents'])\n",
"tumor_environment_spec['agents'] = {}"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "b2bc6fd1-e992-409b-8a9d-ccb98254f296",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot_bigraph(lymph_node_transfer, \n",
" node_fill_colors=node_colors0,\n",
" **plot_settings,\n",
" filename='lymph_node_transfer',\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "f2909500-ef91-459f-9d29-56ceb3a44223",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nodec={\n",
" ('agents',): c2,\n",
" ('fields',): c2,\n",
" ('dimensions',): c2,\n",
"}\n",
"\n",
"plot_bigraph(tumor_environment_spec, \n",
" node_fill_colors=nodec,\n",
" **plot_settings,\n",
" filename='tumor_environment',\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "08950018-fe70-4f90-bd63-1aa3f4ebabad",
"metadata": {},
"source": [
"## Cell Agent composites"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "a0c87e6c-ba24-410a-9f9b-85007de6627d",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tcell0 = copy.deepcopy(tumor_agents_spec['tcell_0'])\n",
"tcell0['local_field']['_ports'] = {\n",
" 'fields': '', 'dimensions': '', 'location': '', 'exchanges': ''}\n",
"del tcell0['local_field']['wires']['fields']\n",
"del tcell0['local_field']['wires']['dimensions']\n",
"\n",
"\n",
"boundary_spec = {'mass': {}, 'diameter': {}, 'velocity': {}, \n",
" 'exchange': {\n",
" 'IFNg': {}}, \n",
" 'external': {\n",
" 'IFNg': {}, 'tumor_debris': {}}}\n",
"internal_spec = {'cell_state': {}}\n",
"neighbors_spec = {\n",
" 'present': {'PD1': {}, 'TCR': {}}, \n",
" 'accept': {'PDL1': {}, 'MHCI': {}}}\n",
"\n",
"tcell0['boundary'].update(boundary_spec)\n",
"tcell0['internal'].update(internal_spec)\n",
"tcell0['neighbors'] = neighbors_spec\n",
"\n",
"# remove division and death connection to agents\n",
"tcell0['division']['_ports'] = {'global': '', 'agents': ''}\n",
"tcell0['death']['_ports'] = {'trigger': '', 'agents': ''}\n",
"del tcell0['division']['wires']['agents']\n",
"del tcell0['death']['wires']['agents']\n",
"\n",
"nodec1 = {\n",
" # ('agents',): c2,\n",
" ('t_cell',): c3,\n",
" ('t_cell', 'boundary',): c4,\n",
" ('t_cell', 'internal',): c4,\n",
" ('t_cell', 'neighbors',): c4,\n",
"}\n",
"node_border_colors = {\n",
" ('t_cell', 't_cell'): 'red',\n",
"}\n",
"\n",
"plot_bigraph({'t_cell': tcell0}, \n",
" node_fill_colors={**nodec1},\n",
" node_border_colors=node_border_colors,\n",
" **plot_settings,\n",
" filename='tcell_composite',\n",
" remove_process_place_edges=True,\n",
" # **{'out_dir': 'out','dpi': '250'}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "b759be45-60a0-47b6-8622-571e0af6f18f",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"node_groups = [[('agents',)]]\n",
"tcell0 = copy.deepcopy(tumor_agents_spec['tcell_0'])\n",
"tcell0['local_field']['_ports'] = {'fields': '', 'dimensions': '', 'location': '', 'exchanges': ''}\n",
"del tcell0['local_field']['wires']['fields']\n",
"del tcell0['local_field']['wires']['dimensions']\n",
"\n",
"internal_spec = {'cell_state': {}}\n",
"boundary_spec = {'mass': {}, 'diameter': {}, 'velocity': {}}\n",
"tcell0['boundary'].update(boundary_spec)\n",
"tcell0['internal'].update(internal_spec)\n",
"\n",
"nodec1 = {\n",
" ('agents',): c2,\n",
" ('agents', 't_cell',): c3,\n",
" ('agents', 't_cell', 'boundary',): c4,\n",
" ('agents', 't_cell', 'internal',): c4,\n",
" ('agents', 't_cell', 'neighbors',): c4,\n",
"}\n",
"node_border_colors = {\n",
" ('agents', 't_cell', 't_cell'): 'red',\n",
"}\n",
"\n",
"plot_bigraph({'agents': {'t_cell': tcell0}}, \n",
" node_groups=node_groups,\n",
" node_fill_colors={**nodec1},\n",
" node_border_colors=node_border_colors,\n",
" **plot_settings,\n",
" filename='tcell_composite',\n",
" remove_process_place_edges=True,\n",
" # **{'out_dir': 'out','dpi': '250'}\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "5914f73d-28f3-4fd0-8dde-32b571fbee5a",
"metadata": {},
"source": [
"## Stand-alone processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ed5943a-f5f9-48b9-875a-dc91014edc85",
"metadata": {},
"outputs": [],
"source": [
"topology = composite['topology']\n",
"tcell_topology = get_in(topology, ['tumor_environment', 'agents', 'tcell_0'])\n",
"tumor_topology = get_in(topology, ['tumor_environment', 'agents', 'tumor_0'])\n",
"dendritic_topology = get_in(topology, ['tumor_environment', 'agents', 'dendritic_0'])\n",
"neighbors_topology = get_in(topology, ['tumor_environment', 'neighbors_multibody'])\n",
"diffusion_topology = get_in(topology, ['tumor_environment', 'diffusion_field'])\n",
"lymph_node_transfer_topology = get_in(topology, ['lymph_node_transfer'])"
]
},
{
"cell_type": "markdown",
"id": "df5511a9-3894-4aa6-9d11-fc6e063a5ef7",
"metadata": {},
"source": [
"### Cell Processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63ea9b11-c757-4728-8149-c991567ce7eb",
"metadata": {},
"outputs": [],
"source": [
"t_cell_spec = {'t_cell': {'_ports': tcell_topology['t_cell']}}\n",
"plot_bigraph(t_cell_spec, **plot_settings, filename='tcell_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1acbc97-e303-40fa-a6d2-4e47555e98ab",
"metadata": {},
"outputs": [],
"source": [
"tumor_spec = {'tumor': {'_ports': tumor_topology['tumor']}}\n",
"plot_bigraph(tumor_spec, **plot_settings, filename='tumor_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "569ccfea-ec94-4e8d-8af8-5be0e509a044",
"metadata": {},
"outputs": [],
"source": [
"dendritic_spec = {'dendritic_cell': {'_ports': dendritic_topology['dendritic_cell']}}\n",
"plot_bigraph(dendritic_spec, **plot_settings, filename='dendritic_process')"
]
},
{
"cell_type": "markdown",
"id": "d5d1962d-58b5-418c-bb46-9d615358abd3",
"metadata": {},
"source": [
"### Adapter processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88451f47-8c93-4143-9289-40198968b3f5",
"metadata": {},
"outputs": [],
"source": [
"local_field_spec = {'local_field': {'_ports': tcell_topology['local_field']}}\n",
"plot_bigraph(local_field_spec, **plot_settings, filename='local_field_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05697c8e-cd0d-4a4c-b291-d867d5205532",
"metadata": {},
"outputs": [],
"source": [
"division_spec = {'division': {'_ports': tcell_topology['division']}}\n",
"plot_bigraph(division_spec, **plot_settings, filename='division_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bd9c850-5b79-4676-88e0-b8218762a03d",
"metadata": {},
"outputs": [],
"source": [
"death_spec = {'death': {'_ports': tcell_topology['death']}}\n",
"plot_bigraph(death_spec, **plot_settings, filename='death_process')"
]
},
{
"cell_type": "markdown",
"id": "f0f5d4cc-7ff3-40ef-b66e-446e25c2f7a3",
"metadata": {},
"source": [
"### Environment processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e911f343-f60f-4a4f-a8f4-a38550d305e0",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"neighbors_spec = {'neighbors': {'_ports': neighbors_topology}}\n",
"plot_bigraph(neighbors_spec, **plot_settings, filename='neighbors_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0207969-ccdf-4e36-ac9c-e3d61754eb55",
"metadata": {},
"outputs": [],
"source": [
"diffusion_spec = {'diffusion_field': {'_ports': diffusion_topology}}\n",
"plot_bigraph(diffusion_spec, **plot_settings, filename='diffusion_process')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ed30164-8d6a-490b-be7a-a7fd490878b6",
"metadata": {},
"outputs": [],
"source": [
"lymph_node_transfer_spec = {'lymph_node_transfer': {'_ports': lymph_node_transfer_topology}}\n",
"plot_bigraph(lymph_node_transfer_spec, **plot_settings, filename='lymph_node_transfer_process')"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}