def test_manhattan_norm(coord1, coord2, epsilon): node0 = Node(0, LabelNodeLetter(*coord1)) node1 = Node(1, LabelNodeLetter(*coord2)) edit_cost = EditCostLetter(1., 1., 1., 1., 'manhattan') result = edit_cost.cost_substitute_node(node0, node1) arr1 = np.array(coord1) arr2 = np.array(coord2) assert abs(result - np.linalg.norm(arr1 - arr2, 1)) < epsilon assert result == np.linalg.norm(arr1 - arr2, 1)
def define_graphs(): ged = GED(EditCostLetter(1., 1., 1., 1., 'manhattan')) n, m = 4, 3 graph_source = Graph('gr_source', 'gr_source.gxl', n) graph_target = Graph('gr_target', 'gr_targe.gxl', m) # Init graph source: add nodes and edges graph_source.add_node(Node(0, LabelNodeLetter(1, 0))) graph_source.add_node(Node(1, LabelNodeLetter(2, 0))) graph_source.add_node(Node(2, LabelNodeLetter(1, 0))) graph_source.add_node(Node(3, LabelNodeLetter(3, 0))) graph_source.add_edge(Edge(0, 1, LabelEdge(0))) graph_source.add_edge(Edge(1, 2, LabelEdge(0))) graph_source.add_edge(Edge(1, 3, LabelEdge(0))) graph_source.add_edge(Edge(2, 3, LabelEdge(0))) # Init graph target: add nodes and edges graph_target.add_node(Node(0, LabelNodeLetter(3, 0))) graph_target.add_node(Node(1, LabelNodeLetter(2, 0))) graph_target.add_node(Node(2, LabelNodeLetter(2, 0))) graph_target.add_edge(Edge(0, 1, LabelEdge(0))) graph_target.add_edge(Edge(1, 2, LabelEdge(0))) return ged, graph_source, graph_target
def test_letter_alpha_0_5(graphs, graph_source_target, accuracy): graph_source, graph_target = [ graph for graph in graphs if graph.name in graph_source_target ] cst_cost_node = 0.9 cst_cost_edge = 2.3 edit_cost = EditCostLetter(cst_cost_node, cst_cost_node, cst_cost_edge, cst_cost_edge, 'euclidean') edit_cost_alpha = EditCostLetter(cst_cost_node, cst_cost_node, cst_cost_edge, cst_cost_edge, 'euclidean', alpha=0.5) ged = GED(edit_cost) ged_alpha = GED(edit_cost_alpha) results = ged_alpha.compute_edit_distance(graph_source, graph_target) expected = ged.compute_edit_distance(graph_source, graph_target) / 2. assert (results - expected) < accuracy
def test_alpha_0_25(define_graphs): graph_source, graph_target = define_graphs edit_cost = EditCostLetter(1., 1., 1., 1., 'euclidean') edit_cost_alpha = EditCostLetter(1., 1., 1., 1., 'euclidean', alpha=0.25) ged = GED(edit_cost) ged_alpha = GED(edit_cost_alpha) cost = ged.compute_edit_distance(graph_source, graph_target) cost_alpha = ged_alpha.compute_edit_distance(graph_source, graph_target) expected_cost_alpha = 2. expected_C = np.array([[2., 1., 1., 1., np.inf, np.inf, np.inf], [1., 0., 0., np.inf, 1., np.inf, np.inf], [2., 1., 1., np.inf, np.inf, 1., np.inf], [0., 1., 1., np.inf, np.inf, np.inf, 1.], [1., np.inf, np.inf, 0., 0., 0., 0.], [np.inf, 1., np.inf, 0., 0., 0., 0.], [np.inf, np.inf, 1., 0., 0., 0., 0.]]) expected_C_alpha = expected_C / 4 expected_C_star_alpha = np.array( [[0.5, 1., 0.25, 1., np.inf, np.inf, np.inf], [1.75, 0.75, 1.5, np.inf, 2.5, np.inf, np.inf], [1.25, 0.25, 1., np.inf, np.inf, 1.75, np.inf], [0.75, 0.25, 1., np.inf, np.inf, np.inf, 1.75], [1., np.inf, np.inf, 0., 0., 0., 0.], [np.inf, 1.75, np.inf, 0., 0., 0., 0.], [np.inf, np.inf, 1., 0., 0., 0., 0.]]) # expected_C_star_alpha = expected_C_star / 2 print(ged_alpha.C_star.base) assert np.array_equal(np.asarray(ged_alpha.C), expected_C_alpha) assert np.array_equal(np.asarray(ged_alpha.C_star), expected_C_star_alpha) assert cost_alpha == expected_cost_alpha
def test_with_verified_data(letter_graphs, dataframe_letter, graph_source_target, accuracy): gr_name_src, gr_name_trgt = [name[0] + '/' + name for name in graph_source_target] graph_source, graph_target = [graph for graph in letter_graphs if graph.name in graph_source_target] # print(graph_source) # print(graph_target) cst_cost_node = 0.9 cst_cost_edge = 2.3 ged = GED(EditCostLetter(cst_cost_node, cst_cost_node, cst_cost_edge, cst_cost_edge, 'euclidean')) results = ged.compute_edit_distance(graph_source, graph_target) expected = dataframe_letter.loc[gr_name_src, gr_name_trgt] np.set_printoptions(precision=2) print(np.asarray(ged.C)) print(np.asarray(ged.C_star)) print(f'res {results}') print(f'exp {expected}') print(f'###### diff {results - expected}') # assert results == expected assert (results - expected) < accuracy
def test_simple_alpha(define_graphs): graph_source, graph_target = define_graphs # edit_cost = EditCostLetter(1., 1., 1., 1., 'euclidean') edit_cost_alpha = EditCostLetter(1., 1., 1., 1., 'euclidean', alpha=0.5) # ged = GED(edit_cost) ged_alpha = GED(edit_cost_alpha) # cost = ged.compute_edit_distance(graph_source, graph_target) cost_alpha = ged_alpha.compute_edit_distance(graph_source, graph_target) expected_cost_alpha = 2. expected_C = np.array([[2., 1., 1., 1., np.inf, np.inf, np.inf], [1., 0., 0., np.inf, 1., np.inf, np.inf], [2., 1., 1., np.inf, np.inf, 1., np.inf], [0., 1., 1., np.inf, np.inf, np.inf, 1.], [1., np.inf, np.inf, 0., 0., 0., 0.], [np.inf, 1., np.inf, 0., 0., 0., 0.], [np.inf, np.inf, 1., 0., 0., 0., 0.]]) expected_C_alpha = expected_C / 2 expected_C_star = np.array([[2., 2., 1., 2., np.inf, np.inf, np.inf], [3., 1., 2., np.inf, 4., np.inf, np.inf], [3., 1., 2., np.inf, np.inf, 3., np.inf], [1., 1., 2., np.inf, np.inf, np.inf, 3.], [2., np.inf, np.inf, 0., 0., 0., 0.], [np.inf, 3., np.inf, 0., 0., 0., 0.], [np.inf, np.inf, 2., 0., 0., 0., 0.]]) expected_C_star_alpha = expected_C_star / 2 print(ged_alpha.C.base) assert np.array_equal(np.asarray(ged_alpha.C), expected_C_alpha) assert np.array_equal(np.asarray(ged_alpha.C_star), expected_C_star_alpha) assert cost_alpha == expected_cost_alpha