Exemplo n.º 1
0
def create_node_raking_from_diff_matrix(diff: pd.DataFrame,
                                        removed_nodes: [int],
                                        graph: gc.Graph,
                                        save_info: sl.MemoryAccess,
                                        save: bool = True) -> []:
    utils.assure_same_labels([diff])

    labels = diff.index.values.tolist()
    dim = len(labels)

    # init sums
    node_pos_sums = {}
    node_neg_sums = {}

    for label in labels:
        node_pos_sums[label] = 0
        node_neg_sums[label] = 0

    # sum values up
    for i in range(dim):
        for j in range(i):
            label1 = labels[i]
            label2 = labels[j]

            value = diff.at[label1, label2]
            if value > 0:
                node_pos_sums[label1] += value
                node_pos_sums[label2] += value
            else:
                node_neg_sums[label1] += value
                node_neg_sums[label2] += value

    pos_list = list(map(lambda x: (x, node_pos_sums[x]), node_pos_sums))
    neg_list = list(map(lambda x: (x, node_neg_sums[x]), node_neg_sums))

    complete_list = list(
        map(lambda x: (x, node_pos_sums[x] - node_neg_sums[x]), node_pos_sums))

    pos_list.sort(key=lambda x: -x[1])
    neg_list.sort(key=lambda x: x[1])
    complete_list.sort(key=lambda x: -x[1])

    if save:
        save_info.save_node_raking(removed_nodes, pos_list,
                                   list(graph.neighbours(removed_nodes[-1])))

    neighbours = list(graph.neighbours(removed_nodes[0]))
    pos_list_labels = list(map(lambda x: x[0] in neighbours, pos_list))

    neg_list_labels = list(map(lambda x: x[0] in neighbours, neg_list))
    complete_list_labels = list(
        map(lambda x: x[0] in neighbours, complete_list))

    return pos_list, pos_list_labels, neg_list, neg_list_labels, complete_list, complete_list_labels
Exemplo n.º 2
0
def create_target_vector(row_labels: [], graph: gc.Graph,
                         node_to_predict: int) -> pd.DataFrame:
    """
    creates the target vector for classifier
    :param row_labels: labels the target vector should be created
    :param graph: graph including the removed node
    :param node_to_predict: the node that is removed in the 2. embedding
    :return:
    """

    neighbours_of_removed_node = graph.neighbours(node_to_predict)

    target = pd.DataFrame(False, row_labels, ["y"])

    for neighbour in neighbours_of_removed_node:

        # this prevents an error in case the original graph is used while 2 nodes are removed in the labels
        # and they are connected
        if neighbour in row_labels:
            target.loc[neighbour] = True

    return target
def _get_avg_degree_of_neighbours(graph: gc.Graph, node: int):
    neighbours_mean = (list(
        map(lambda n: graph.degree(n), list(graph.neighbours(node)))))
    return sum(neighbours_mean) / len(neighbours_mean)