networkx: how to set custom cost function?

Olha Kholod
:

I am following networkx documentation (1) and I would like to set different penalties for cost function (e.g. node_del_cost and node_ins_cost). Let say, I would like to penalize deletion/insertion of node by three points.

So far, I have created two undirected graphs that differ by labeling node C (UPDATED CODE).

import networkx as nx

G=nx.Graph()
G.add_nodes_from([("A", {'label':'CDKN1A'}), ("B", {'label':'CUL4A'}), 
    ("C", {'label':'RB1'})])

G.add_edges_from([("A","B"), ("A","C")])

H=nx.Graph()
H.add_nodes_from([("A", {'label':'CDKN1A'}), ("B", {'label':'CUL4A'}),
    ("C", {'label':'AKT'})])
H.add_edges_from([("A","B"), ("A","C")])

# arguments
# node_match – a function that returns True if node n1 in G1 and n2 in G2 should be considered equal during matching.
# ignored if node_subst_cost is specified
def node_match(node1, node2):
    return node1['label']==node2['label']

# node_subst_cost - a function that returns the costs of node substitution
# overrides node_match if specified.
def node_subst_cost(node1, node2): 
    return node1['label']==node2['label']

# node_del_cost - a function that returns the costs of node deletion
# if node_del_cost is not specified then default node deletion cost of 1 is used.
def node_del_cost(node1):
    return node1['label']==3    

# node_ins_cost - a function that returns the costs of node insertion
# if node_ins_cost is not specified then default node insertion cost of 1 is used.
def node_ins_cost(node2):
    return node2['label']==3    

paths, cost = nx.optimal_edit_paths(G, H, node_match=None, edge_match=None, 
    node_subst_cost=node_subst_cost, node_del_cost=node_del_cost, node_ins_cost=node_ins_cost, 
    edge_subst_cost=None, edge_del_cost=None, edge_ins_cost=None, 
    upper_bound=None)

# length of the path
print(len(paths))

# optimal edit path cost (graph edit distance).
print(cost)

This give me 2.0 as an optimal path cost and 7.0 as the length of the path. However, I do not fully understand why, because I set penalty to 3.0, so the edit distance is expected to be 3.

Thank you for your suggestions!

Olha

Azim Mazinani
:

As mentioned in the documentation, when you pass a node_subst_cost function as a parameter, it ignores node_match function and applies cost for any substitution operation, even though the nodes are equal. So I would suggest that first you evaluate the nodes equality in node_subst_cost function and then apply the cost accordingly:

def node_subst_cost(node1, node2):
    # check if the nodes are equal, if yes then apply no cost, else apply 3
    if node1['label'] == node2['label']:
        return 0
    return 3


def node_del_cost(node):
    return 3  # here you apply the cost for node deletion


def node_ins_cost(node):
    return 3  # here you apply the cost for node insertion


paths, cost = nx.optimal_edit_paths(
    G,
    H,
    node_subst_cost=node_subst_cost,
    node_del_cost=node_del_cost,
    node_ins_cost=node_ins_cost
)

print(cost)  # which will return 3.0

You can also do the same for edge operations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to set rmse cost function in tensorflow

From Dev

writing a custom cost function in tensorflow

From Dev

How to set colors for nodes in NetworkX?

From Dev

How to evaluate cost function for scikit learn LogisticRegression?

From Dev

How to correctly derivate quadratic cost function

From Dev

how is the efficiency and cost of nested function in scheme language

From Dev

how is the efficiency and cost of nested function in scheme language

From Dev

How to tell Python unittest to exit with custom code set in a function

From Dev

how to set Shiny::toJSON args from custom update***Input function?

From Dev

How to tell Python unittest to exit with custom code set in a function

From Dev

How to represent hypothesis function in logistic regression cost function

From Dev

Custom Unordered Set Hash Function

From Dev

Set equality with custom compare function

From Dev

GKGridGraphNode with custom traversal cost

From Dev

How do I set a cost limit in Google Developers Console

From Dev

How much data it cost to set up a TCP connection?

From Dev

How to use the function r.cost to get the least-cost path between two polygons?

From Dev

How to set custom headerlink?

From Dev

Rails 4 How do I set custom set of parameters to a user without update function?

From Dev

How to use networkx algorithm with my custom graph datastructure?

From Dev

How do I specify a custom hash function explicitly for unordered_set by passing a named function?

From Dev

How do i check if a cost function is Concave or Convex?

From Dev

How to modify the seq2seq cost function for padded vectors?

From Dev

How do I implement a cost function that requires all outputs at once

From Dev

how can i Develop Deep sparse Autoencoder cost function in TensorFlow?

From Dev

Logisitic Regression Cost Function

From Dev

Python - Cost of find() function

From Dev

How to set custom button in custom dialog box?

From Dev

How to set custom font for label in custom tableviewcelll?

Related Related

  1. 1

    how to set rmse cost function in tensorflow

  2. 2

    writing a custom cost function in tensorflow

  3. 3

    How to set colors for nodes in NetworkX?

  4. 4

    How to evaluate cost function for scikit learn LogisticRegression?

  5. 5

    How to correctly derivate quadratic cost function

  6. 6

    how is the efficiency and cost of nested function in scheme language

  7. 7

    how is the efficiency and cost of nested function in scheme language

  8. 8

    How to tell Python unittest to exit with custom code set in a function

  9. 9

    how to set Shiny::toJSON args from custom update***Input function?

  10. 10

    How to tell Python unittest to exit with custom code set in a function

  11. 11

    How to represent hypothesis function in logistic regression cost function

  12. 12

    Custom Unordered Set Hash Function

  13. 13

    Set equality with custom compare function

  14. 14

    GKGridGraphNode with custom traversal cost

  15. 15

    How do I set a cost limit in Google Developers Console

  16. 16

    How much data it cost to set up a TCP connection?

  17. 17

    How to use the function r.cost to get the least-cost path between two polygons?

  18. 18

    How to set custom headerlink?

  19. 19

    Rails 4 How do I set custom set of parameters to a user without update function?

  20. 20

    How to use networkx algorithm with my custom graph datastructure?

  21. 21

    How do I specify a custom hash function explicitly for unordered_set by passing a named function?

  22. 22

    How do i check if a cost function is Concave or Convex?

  23. 23

    How to modify the seq2seq cost function for padded vectors?

  24. 24

    How do I implement a cost function that requires all outputs at once

  25. 25

    how can i Develop Deep sparse Autoencoder cost function in TensorFlow?

  26. 26

    Logisitic Regression Cost Function

  27. 27

    Python - Cost of find() function

  28. 28

    How to set custom button in custom dialog box?

  29. 29

    How to set custom font for label in custom tableviewcelll?

HotTag

Archive