Search code examples
python-3.xnetworkxscikit-image

What does merge_func in merge_hierarchicals of future.graph module do?


checking the example provided in the documentation: https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_rag_merge.html#sphx-glr-auto-examples-segmentation-plot-rag-merge-py

I can not understand well what the variable merge_func function does. In example2 it even does nothing and everything seems to work well.

example 2: https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_boundary_merge.html#sphx-glr-auto-examples-segmentation-plot-boundary-merge-py


Solution

  • Probably it should have a different name. This is why this functionality is in future: to enable us to experiment with the interface.

    merge_func is a function that takes in the graph and two nodes being merged, and updates the destination node accordingly. In the first example, the information about the graph is stored in the nodes, so the new node needs to be updated. In the second example, all relevant information is in the graph edges, so we don't need to do anything to the new node when it is merged.

    weight_func is the equivalent function for edges: given two nodes being merged and a neighbor of the newly formed node, what are the attributes of the new edge?

    My suggestion for the future based on your question is:

    • rename the functions to edge_merge_func and node_merge_func
    • have both return dictionaries rather than have one return a dictionary and the other one perform operations in-place
    • allow each to be None so that users don't have to provide them even if they do nothing.

    I hope all that makes sense!