{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Region Adjacency Graphs (RAGs)\n\nThis example demonstrates the use of the `merge_nodes` function of a Region\nAdjacency Graph (RAG). The `RAG` class represents an undirected weighted graph\nwhich inherits from :obj:`networkx.Graph` class. When a new node is formed by\nmerging two nodes, the edge weight of all the edges incident on the resulting\nnode can be updated by a user defined function `weight_func`.\n\nThe default behaviour is to use the smaller edge weight in case of a conflict.\nThe example below also shows how to use a custom function to select the larger\nweight instead.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import skimage as ski\nimport networkx as nx\nfrom matplotlib import pyplot as plt\nimport numpy as np\n\n\ndef max_edge(g, src, dst, n):\n    \"\"\"Callback to handle merging nodes by choosing maximum weight.\n\n    Returns a dictionary with `\"weight\"` set as either the weight between\n    (`src`, `n`) or (`dst`, `n`) in `g` or the maximum of the two when\n    both exist.\n\n    Parameters\n    ----------\n    g : RAG\n        The graph under consideration.\n    src, dst : int\n        The vertices in `g` to be merged.\n    n : int\n        A neighbor of `src` or `dst` or both.\n\n    Returns\n    -------\n    data : dict\n        A dict with the \"weight\" attribute set the weight between\n        (`src`, `n`) or (`dst`, `n`) in `g` or the maximum of the two when\n        both exist.\n    \"\"\"\n\n    w1 = g[n].get(src, {'weight': -np.inf})['weight']\n    w2 = g[n].get(dst, {'weight': -np.inf})['weight']\n    return {'weight': max(w1, w2)}\n\n\ndef display(g, title):\n    \"\"\"Displays a graph with the given title.\"\"\"\n    pos = nx.circular_layout(g)\n    plt.figure()\n    plt.title(title)\n    nx.draw(g, pos)\n    nx.draw_networkx_labels(g, pos)\n    nx.draw_networkx_edge_labels(g, pos, font_size=20)\n\n\ng = ski.graph.RAG()\ng.add_edge(1, 2, weight=10)\ng.add_edge(2, 3, weight=20)\ng.add_edge(3, 4, weight=30)\ng.add_edge(4, 1, weight=40)\ng.add_edge(1, 3, weight=50)\n\n# Assigning dummy labels.\nfor n in g.nodes():\n    g.nodes[n]['labels'] = [n]\n\ngc = g.copy()\n\ndisplay(g, \"Original Graph\")\n\ng.merge_nodes(1, 3)\ndisplay(g, \"Merged with default (min)\")\n\ngc.merge_nodes(1, 3, weight_func=max_edge, in_place=False)\ndisplay(gc, \"Merged with max without in_place\")\n\nplt.show()"
      ]
    }
  ],
  "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.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}