Ejemplo n.º 1
0
def return_indexed_graph(lambda_cluster_keys):
    adjacency_list = {}
    degree_map = {k: 0 for k in range(len(lambda_cluster_keys))}
    for i in range(len(lambda_cluster_keys)):
        vertex_i = set(flatten(map(lambda x: (x[0][0], x[0][2]), lambda_cluster_keys[i])))
        for j in range(i):
            common_nodes = set.intersection(vertex_i, flatten(map(lambda x: (x[0][0], x[0][1]), lambda_cluster_keys[j])))
            if (len(common_nodes) > 0):
                adjacency_list[(i, j)] = common_nodes
                degree_map[i] = degree_map[i]+1
                adjacency_list[(j, i)] = common_nodes
                degree_map[j] = degree_map[j]+1
    return (lambda_cluster_keys, adjacency_list, degree_map)
Ejemplo n.º 2
0
def simple_tree_print_all(tree):
    print("Score = "+str(tree["totalScore"])+" -- "+str(set(flatten(flatten(map(lambda x : map(lambda y: [y[0], y[2]] , x['p']),  tree["tree"]))))))
Ejemplo n.º 3
0
def simple_tree_string(tree):
    return ("|".join(unique(filter(lambda z : (z[:1] == "E"), flatten(flatten(map(lambda x : map(lambda y: [y[0], y[2]] , x['p']),  tree["tree"])))))))+"\t"+("|".join(unique(filter(lambda z : (z[:2] == "VM") or (z[:2] == "RM"), flatten(flatten(map(lambda x : map(lambda y: [y[0], y[2]] , x['p']),  tree["tree"])))))))+"\t"+str(tree["totalScore"])+"\n"
Ejemplo n.º 4
0
def simple_tree_print(tree):
    print("Score = "+str(tree["totalScore"])+" -- "+str(set(filter(lambda z : (z[:2] == "VM") or (z[:2] == "RM"), flatten(flatten(map(lambda x : map(lambda y: [y[0], y[2]] , x['p']),  tree["tree"])))))))
Ejemplo n.º 5
0
def lambda_align(data_rectified, query_unrectified, query_rectified,
                 entrypoint_map):
    """
    :param data_rectified:      data path to be aligned to the query
    :param query_unrectified:   query in its simplest triple formulation from the undirected representation
    :param entrypoint_map:      Elements that need to be present as perfect matches within the data
    :return:                    It returns a pair ((a,b), c), where
                                * a, is the data path that was be aligned
                                * b, is the set of the node alignments to the query
                                * c, is the score associated from both the vertex and edge edit distance
    """
    ## First step: check how many paths were aligned
    ## This if a first approximation of the alignments that will still provide us the edge edit distance
    edge_label_align = {}
    query_edge_match = 0
    edge_matches = 0
    while (query_edge_match < len(query_unrectified)):
        noMatch = True
        for i in range(len(data_rectified)):
            ## If I have no more alignments, then I'm done
            if (query_edge_match >= len(query_unrectified)):
                noMatch = False
                break
            ## If the labels do match (I'm going to refine the alignment in the next step)
            if (data_rectified[i][1] == query_rectified[query_edge_match][1]):
                # try:
                #     if (len(data_rectified) == 2) and (data_rectified[0][1] == 'Conflict.Attack_Place') and (data_rectified[1][1] == 'Conflict.Attack_Target'):
                #         print("loco")
                # except:
                #     pass
                noMatch = False
                edge_label_align[query_edge_match] = data_rectified[i]
                query_edge_match += 1
                edge_matches += 1
        if noMatch:
            query_edge_match += 1

    edge_plus_distance = len(data_rectified) - edge_matches
    edge_minus_distance = len(query_unrectified) - edge_matches

    ## Evaluate the node alignment using the distinct variables from the query
    node_align = {
        vertex: set()
        for vertex in set(
            flatten(map(lambda x: [x[0], x[2]], query_unrectified)))
    }
    for query_edge_id in edge_label_align:
        datum_rectified = edge_label_align[query_edge_id]
        variables = query_rectified[query_edge_id]
        node_align[variables[0]].add(datum_rectified[0])
        node_align[variables[2]].add(datum_rectified[2])

    ## Now, evaluating the vertex edit distance.
    ## 1) The vertex key in node_align is not aligned with the path if either its associated value list is empty or the
    ##    entrypoint was not matched with the entrypoint
    vertex_minus_distance = len(
        list(
            filter(
                lambda x: len(x[1]) == 0 or (x[0] in entrypoint_map and len(
                    set.intersection(x[1], entrypoint_map[x[0]])) == 0),
                node_align.items())))

    ## 2) The added vertices are the ones that are more than the parts that are already aligned.
    vertex_plus_distance = fold(
        operator.add,
        map(lambda x: len(x[1]) - 1,
            filter(lambda x: len(x[1]) > 1, node_align.items())), 0)
    ##    In addition to that, I must add the vertices that have not been matched with the element from the query
    for subpath in noneReplace(
            map(lambda x: None
                if x in edge_label_align.values() else x, data_rectified)):
        vertex_plus_distance += len(set(flatten(subpath)[1:-1]))
    ##    In addition to that, I must consider the nodes not aligned with the entrypoint
    #for ep in entrypoint_map:
    #    if (ep in node_align.keys()):
    #        s = set.difference(set(node_align[ep]), {ep})
    #        if (len(s) > 0):
    #            vertex_plus_distance += len(s)
    #        elif ((len(s) == 0) and (ep in entrypoint_map) and (ep not in entrypoint_map[ep])):
    #            vertex_minus_distance += 1

    finalScore = edge_plus_distance + edge_minus_distance + vertex_minus_distance + vertex_plus_distance
    #if (finalScore == 0):
    #    #print ("SCORE="+str()+" --\t"+str(list(map(lambda x: x[1], data_rectified)))+" E+ = "+str(edge_plus_distance)+ " E- = "+str(edge_minus_distance)+ " V+ = "+str(vertex_plus_distance)+" V- ="+str(vertex_minus_distance))
    #if (finalScore == 1):
    #    print("Score of "+str(finalScore)+" for path "+str(data_rectified)+" for cluster "+str(query_rectified))
    return (data_rectified, node_align, finalScore)