예제 #1
0
파일: match.py 프로젝트: YumanYIN/Pro
def match2Graph(G1,G2):
    global a

    # obtain min_edit_path and min_edit_distance
    para2, para3 = nx.optimal_edit_paths(G1, G2)
    maxOrder = 0
    for i in range(0,len(G1.nodes)):
        if G1.nodes[i]['order'] > maxOrder:
            maxOrder = G1.nodes[i]['order']

    # match 2 graph
    for i in range(0,len(para2[0][0])):
        if (para2[0][0][i][0] != None) and (para2[0][0][i][1] != None):
            G2.nodes[para2[0][0][i][1]]['order'] = G1.nodes[para2[0][0][i][0]]['order']
        elif para2[0][0][i][0] == None:
            maxOrder += 1
            G2.nodes[para2[0][0][i][1]]['order'] = maxOrder
        else:
            continue

    for paraTemp in para2:
        matchNumber = 0
        for matchNodes in paraTemp[0]:
            if matchNodes[0] != matchNodes[1]:
                matchNumber += 1

    print(para3)
예제 #2
0
def compute_path(tree1, tree2):
    g1 = convert_nx(tree1)
    g2 = convert_nx(tree2)
    g, cost = nx.optimal_edit_paths(g1, g2, node_subst_cost=node_subst_cost, node_del_cost=node_del_cost, node_ins_cost=node_ins_cost, edge_subst_cost=edge_subst_cost, edge_del_cost=edge_del_cost, edge_ins_cost=edge_ins_cost)
    path = g[0]
    #print("edit distance between %s and %s" % (tree1.smiles, tree2.smiles))
    #print_path(g1, g2, path)
    return path, g, cost
예제 #3
0
def ged_paths(g1,g2,node_weight_mode='proportional'):
    #need to incorporate edges attributes in order for ged edge costs to work correctly
    '''for e, attr in g1.edges.items():
        pdb.set_trace()
        attr['nodes'] = '{}-{}'.format(e[0],e[1])
    for e, attr in g2.edges.items():
        attr['nodes'] = '{}-{}'.format(e[0],e[1])'''

    def edge_subst_cost(gattr, hattr):
        if (gattr['relation'] == hattr['relation']):# and (gattr['nodes'] == hattr['nodes']):
            return 0
        else:
            return -1

    def node_subst_cost_proportional(uattr, vattr):
        cost = 0
        attributes = list(uattr.keys())
        unit_cost = 1/len(attributes)
        for attr in attributes:
            if(uattr[attr] != vattr[attr]): cost=cost+unit_cost
        return cost

    def node_subst_cost_atleastone(uattr, vattr):
        cost = 0
        attributes = list(uattr.keys())
        for attr in attributes:
            if(uattr[attr] != vattr[attr]): 
                cost=1
                break
        return cost

    if node_weight_mode == 'proportional':
        node_subst_cost = node_subst_cost_proportional
    elif node_weight_mode == 'atleastone':
        node_subst_cost = node_subst_cost_atleastone
    else:
        raise ValueError('Node weight mode {} not known'.format(node_weight_mode))

    return nx.optimal_edit_paths(g1, g2,
         edge_subst_cost = edge_subst_cost,
         node_subst_cost = node_subst_cost)
        g2 = loadGXL(args.g2)

        # if you want to only output the distance, you can use the graph_edit_distance as follows:

        distance = nx.graph_edit_distance(
            g1,
            g2,
            node_subst_cost=GREC.node_substitution_cost,
            node_del_cost=GREC.node_deletion_cost,
            node_ins_cost=GREC.node_insertion_cost,
            edge_subst_cost=GREC.edge_substitution_cost,
            edge_del_cost=GREC.edge_deletion_cost,
            edge_ins_cost=GREC.node_insertion_cost)

        print("optimal distance :::", distance)

        # if you want to output the distance and all the possible optimal pathz, you can use the optimal_edit_paths function

        paths, cost = nx.optimal_edit_paths(
            g1,
            g2,
            node_subst_cost=GREC.node_substitution_cost,
            node_del_cost=GREC.node_deletion_cost,
            node_ins_cost=GREC.node_insertion_cost,
            edge_subst_cost=GREC.edge_substitution_cost,
            edge_del_cost=GREC.edge_deletion_cost,
            edge_ins_cost=GREC.node_insertion_cost)

        print("the possible paths are: ", paths)
        print("distance: ", distance)
예제 #5
0
def func2(codebox1: Text, codebox2: Text):

    # codebox1 和 codebox2是窗口中的两个文本输入框
    # 函数将会从文本框中读入代码

    codeA = codebox1.get(0.0, END)  # 从文本框读入代码
    codeB = codebox2.get(0.0, END)
    codeResourceA = cp.ProcessCode(codeA)  # 创建一个文本处理的对象
    codeResourceB = cp.ProcessCode(codeB)
    codeResourceA.processComment()  # 对文本处理对象去除注释信息
    codeResourceB.processComment()
    codeResourceA.processMacro()  # 对文本处理对象替换宏定义
    codeResourceB.processMacro()
    codebox1.delete(0.0, END)  # 清空文本框中的信息
    codebox2.delete(0.0, END)
    codebox1.insert(END, codeResourceA.code)  # 用处理后的代码信息替代原来的代码
    codebox2.insert(END, codeResourceB.code)

    codeResourceA.collectFunctionInfo()  # 收集代码中函数定义的信息
    codeResourceB.collectFunctionInfo()

    codeResourceA.functionCallInfo()  # 获取代码中函数调用的信息
    codeResourceB.functionCallInfo()

    func_name_listA = codeResourceA.function_name_list  # 获取代码中定义的所有函数名
    func_name_listB = codeResourceB.function_name_list

    call_matrixA = codeResourceA.call_matrix  # 获取函数调用的矩阵
    call_matrixB = codeResourceB.call_matrix

    print(call_matrixA)
    print(call_matrixB)

    GraphA = nx.MultiDiGraph()  # 建立一个空的有向多重图
    GraphB = nx.MultiDiGraph()

    GraphA.add_nodes_from(func_name_listA)  # 从列表里添加节点
    GraphB.add_nodes_from(func_name_listB)
    for function_from in range(len(call_matrixA)):
        for function_to in range(len(call_matrixA[function_from])):
            edge_num = call_matrixA[function_from][function_to]
            if edge_num != 0:
                for count in range(edge_num):
                    GraphA.add_edge(
                        func_name_listA[function_from],
                        func_name_listA[function_to])  # 根据矩阵中的信息添加边
    GraphA_info = len(GraphA.nodes) + len(GraphA.edges)
    for function_from in range(len(call_matrixB)):
        for function_to in range(len(call_matrixB[function_from])):
            edge_num = call_matrixB[function_from][function_to]
            if edge_num != 0:
                for count in range(edge_num):
                    GraphB.add_edge(
                        func_name_listB[function_from],
                        func_name_listB[function_to])  # 根据矩阵中的信息添加边

    nx.draw(GraphA, with_labels=True, font_weight='bold')  # 绘制有向图
    plt.show()
    nx.draw(GraphB, with_labels=True, font_weight='bold')
    plt.show()
    nx.draw(GraphA, with_labels=True, font_weight='bold')  # 绘制有向图
    path, cost = nx.optimal_edit_paths(GraphA, GraphB)
    sim_rate = (GraphA_info - cost) / GraphA_info
    return sim_rate