{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# heterograph: property maps\n",
"\n",
"Property maps enable attaching arbitrary data to the graph, vertices, and edges. The `HGraph` class provides a `pmap` property for this purpose.\n",
"\n",
"Properties are represented as key-value pairs and stored in a Python dictionary (`dict`) with the following restriction:\n",
"* **Keys must be strings, while values can be of any type**.\n",
"\n",
"For this notebook, let us consider the following example."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from heterograph import *\n",
"\n",
"g=HGraph()\n",
"g.add_vx(4)\n",
"g.add_edge(0, [1, 2])\n",
"g.add_edge([1, 2], 3)\n",
"g.view()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## graph properties\n",
"\n",
"Graph properties store data associated with the entire graph, such as the results of an analysis."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap # default: no properties"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'nregions': 3, 'has_cycles': False, 'title': 'my graph'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap = { 'nregions': 3, 'has_cycles': False }\n",
"g.pmap['title'] = 'my graph'\n",
"g.pmap"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap['has_cycles']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vertex properties\n",
"\n",
"To access the property map for vertex `vx`, we simply invoke `g.pmap[vx]`. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap[0] # default: no properties for vertex 0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'vertex-A'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap[0] = {'name': 'vertex-A', 'type': 'square'}\n",
"g.pmap[0]['name']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'circle'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap[0]['type'] = 'circle'\n",
"g.pmap[0]['type']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## edge properties\n",
"\n",
"To access the property map for edge `(s, t)`, we invoke `g.pmap[(s, t)]`. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.pmap[(0, 1)] # default: no properties for edge (0, 1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"g.pmap[(0, 1)] = {'weight': 3.14}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"edge (0, 1): {'weight': 3.14}\n",
"edge (0, 2): {}\n",
"edge (1, 3): {}\n",
"edge (2, 3): {}\n"
]
}
],
"source": [
"for e in g.edges:\n",
" print(f\"edge {e}: {g.pmap[e]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## property map initialization\n",
"\n",
"As shown in previous examples, all property maps start empty. To initialize properties with default key-value pairs, use the `ginit`, `vinit`, and `einit` parameters when building an `HGraph`. These functions are invoked when creating a new graph, vertex, or edge, respectively."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'nregions': 3, 'has_cycles': False}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ginit(g):\n",
" g.pmap = { 'nregions': 0, 'has_cycles': False }\n",
"def vinit(g, v):\n",
" g.pmap[v] = { 'name': None, 'type': None }\n",
"def einit(g, e):\n",
" g.pmap[e] = { 'weight': None }\n",
"\n",
"g=HGraph(ginit=ginit, vinit=vinit, einit=einit)\n",
"g.add_vx(3)\n",
"g.add_edge(0, [1, 2])\n",
"\n",
"g.pmap['nregions'] = 3\n",
"g.pmap"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"vertex 0: {'name': None, 'type': None}\n",
"vertex 1: {'name': None, 'type': None}\n",
"vertex 2: {'name': None, 'type': None}\n"
]
}
],
"source": [
"for v in g.vertices:\n",
" print(f\"vertex {v}: {g.pmap[v]}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"edge (0, 1): {'weight': None}\n",
"edge (0, 2): {'weight': None}\n"
]
}
],
"source": [
"for e in g.edges:\n",
" print(f\"edge {e}: {g.pmap[e]}\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"vertex 0: {'name': None, 'type': None}\n",
"vertex 1: {'name': None, 'type': None}\n",
"vertex 2: {'name': None, 'type': None}\n",
"vertex 3: {'name': None, 'type': None}\n"
]
}
],
"source": [
"# when copying a graph, the property maps and\n",
"# initialisation routines are copied as well\n",
"h = g.copy()\n",
"h.add_vx()\n",
"for v in h.vertices:\n",
" print(f\"vertex {v}: {h.pmap[v]}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "metaml",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}