def evaluateDynamic_changed_LinkPrediction(graph,
                                           embedding,
                                           rounds,
                                           edges_add,
                                           edges_rm,
                                           n_sample_nodes=None,
                                           no_python=False,
                                           is_undirected=True,
                                           sampling_scheme="u_rand"):
    nodes = []
    for e in edges_add[0]:
        nodes.append(e[0])
        nodes.append(e[1])

    # for e in edges_rm[0]:
    #   nodes.append(e[0])
    #   nodes.append(e[1])

    nodes = list(np.unique(nodes))
    # pdb.set_trace()

    test_digraph, node_l = graph_util.sample_graph(graph, len(nodes), nodes)
    estimated_adj = embedding.predict_next_adj(node_l)

    predicted_edge_list = evaluation_util.getEdgeListFromAdjMtx(
        estimated_adj, is_undirected=is_undirected, edge_pairs=None)

    MAP = metrics.computeMAP(predicted_edge_list, test_digraph)
    prec_curv, _ = metrics.computePrecisionCurve(predicted_edge_list,
                                                 test_digraph)

    return (MAP, prec_curv)
def evaluateDynamicLinkPrediction_TIMERS(graph,
                                         embedding,
                                         t,
                                         rounds,
                                         n_sample_nodes=None,
                                         no_python=False,
                                         is_undirected=True,
                                         sampling_scheme="u_rand"):
    node_l = None
    if n_sample_nodes:
        if sampling_scheme == "u_rand":
            test_digraph, node_l = graph_util.sample_graph(
                graph, n_sample_nodes)
        else:
            test_digraph, node_l = graph_util.sample_graph_rw_int(
                graph, n_sample_nodes)
    estimated_adj = embedding.predict_next_adj(t, node_l)

    predicted_edge_list = evaluation_util.getEdgeListFromAdjMtx(
        estimated_adj, is_undirected=is_undirected, edge_pairs=None)

    MAP = metrics.computeMAP(predicted_edge_list, test_digraph)
    prec_curv, _ = metrics.computePrecisionCurve(predicted_edge_list,
                                                 test_digraph)
    return (MAP, prec_curv)
def evaluateDynamicLinkPrediction(graph,
                                  embedding,
                                  rounds,
                                  n_sample_nodes=None,
                                  no_python=False,
                                  is_undirected=True,
                                  sampling_scheme="u_rand"):
    node_l = None
    if n_sample_nodes:
        if sampling_scheme == "u_rand":
            test_digraph, node_l = graph_util.sample_graph(
                graph,
                n_sample_nodes
            )
        else:
            test_digraph, node_l = graph_util.sample_graph_rw_int(
                graph,
                n_sample_nodes
            )

    else:
        test_digraph, node_l = graph_util.sample_graph(
            graph,
            n_sample_nodes)
    estimated_adj = embedding.predict_next_adj(node_l)
    print(len(estimated_adj), np.shape(estimated_adj))

    predicted_edge_list = evaluation_util.getEdgeListFromAdjMtx(
        estimated_adj,
        is_undirected=is_undirected,
        edge_pairs=None
    )
    
    print(len(predicted_edge_list), np.shape(predicted_edge_list), len(test_digraph.edges()),
              np.shape(test_digraph.edges()))

    # pdb.set_trace()

    MAP = computeMAP(predicted_edge_list, test_digraph)
    prec_curv, _ = computePrecisionCurve(
        predicted_edge_list,
        test_digraph
    )
    return (MAP, prec_curv)
示例#4
0
def expGR(digraph,
          graph_embedding,
          X,
          n_sampled_nodes,
          rounds,
          res_pre,
          m_summ,
          file_suffix=None,
          is_undirected=True,
          sampling_scheme="rw"):
    print('\tGraph Reconstruction')
    n_sampled_nodes = int(n_sampled_nodes)
    summ_file = open('%s_%s.grsumm' % (res_pre, m_summ), 'w')
    summ_file.write('Method\t%s\n' % metrics.getMetricsHeader())
    if digraph.number_of_nodes() <= n_sampled_nodes:
        rounds = 1
    MAP = [None] * rounds
    prec_curv = [None] * rounds
    err = [None] * rounds
    err_b = [None] * rounds
    n_nodes = [None] * rounds
    n_edges = [None] * rounds
    for round_id in range(rounds):
        if sampling_scheme == "u_rand":
            sampled_digraph, node_l = graph_util.sample_graph(
                digraph, n_sampled_nodes=n_sampled_nodes)
        else:
            sampled_digraph, node_l = graph_util.sample_graph_rw_int(
                digraph, n_sampled_nodes=n_sampled_nodes)
        n_nodes[round_id] = sampled_digraph.number_of_nodes()
        n_edges[round_id] = sampled_digraph.number_of_edges()
        print('\t\tRound: %d, n_nodes: %d, n_edges:%d\n' %
              (round_id, n_nodes[round_id], n_edges[round_id]))
        sampled_X = X[node_l]
        # sampled_X = np.expand_dims(sampled_X,axis=1)
        MAP[round_id], prec_curv[round_id], err[round_id], err_b[round_id] = \
            evaluateStaticGraphReconstruction(sampled_digraph,
                                              graph_embedding,
                                              sampled_X,
                                              node_l,
                                              file_suffix= file_suffix,
                                              is_undirected=is_undirected
                                              )
    try:
        summ_file.write('Err: %f/%f\n' % (np.mean(err), np.std(err)))
        summ_file.write('Err_b: %f/%f\n' % (np.mean(err_b), np.std(err_b)))
    except TypeError:
        pass
    summ_file.write('%f/%f\t%s\n' %
                    (np.mean(MAP), np.std(MAP),
                     metrics.getPrecisionReport(prec_curv[0], n_edges[0])))
    pickle.dump([n_nodes, n_edges, MAP, prec_curv, err, err_b],
                open('%s_%s.gr' % (res_pre, m_summ), 'wb'))
    return np.mean(np.array(MAP))
def evaluateDynamic_changed_LinkPrediction(graph,
                                           embedding,
                                           rounds,
                                           edges_add,
                                           edges_rm,
                                           n_sample_nodes=None,
                                           no_python=False,
                                           is_undirected=True,
                                           sampling_scheme="u_rand"):
    """Function to evaluate dynamic changed link prediction
           
           Attributes:
               graph (Object): Networkx Graph Object
               embedding (object): Algorithm for learning graph embedding.
               edges_add (list): list of edges to be added.
               edges_rm (list): list of edges to be removed.
               n_sampled_nodes (int): List of sampled nodes.
               train_ratio_init (float): sample to be used for training and testing.
               rounds (int): Number of times to run the experiment
               m_summ (str): summary to be used to save the result.
               is_undirected (bool): Flag to denote if the graph is directed.
               sampling_scheme(str): sampling scheme for selecting the nodes.

            Returns:
                ndarray: Mean Average precision
    """
    nodes = []
    for e in edges_add[0]:
        nodes.append(e[0])
        nodes.append(e[1])

    # for e in edges_rm[0]:
    #   nodes.append(e[0])
    #   nodes.append(e[1])

    nodes = list(np.unique(nodes))
    # pdb.set_trace()

    test_digraph, node_l = graph_util.sample_graph(graph, len(nodes), nodes)
    estimated_adj = embedding.predict_next_adj(node_l)

    predicted_edge_list = evaluation_util.getEdgeListFromAdjMtx(
        estimated_adj, is_undirected=is_undirected, edge_pairs=None)

    MAP = metrics.computeMAP(predicted_edge_list, test_digraph)
    prec_curv, _ = metrics.computePrecisionCurve(predicted_edge_list,
                                                 test_digraph)

    return (MAP, prec_curv)
def evaluateDynamicLinkPrediction(graph,
                                  embedding,
                                  rounds,
                                  n_sample_nodes=None,
                                  no_python=False,
                                  is_undirected=True,
                                  sampling_scheme="u_rand"):
    """Function to evaluate Dynamic Link Prediction
           
           Attributes:
               graph (Object): Networkx Graph Object
               embedding (object): Algorithm for learning graph embedding
               n_sample_nodes (list): sampled nodes
               is_undirected (bool): Flag to denote if the graph is directed.
               sampling_scheme (str): Sampling scheme to be used. 

            Returns:
                ndarray: MAP, precision curve
    """
    node_l = None
    if n_sample_nodes:
        if sampling_scheme == "u_rand":
            test_digraph, node_l = graph_util.sample_graph(
                graph, n_sample_nodes)
        else:
            test_digraph, node_l = graph_util.sample_graph_rw_int(
                graph, n_sample_nodes)
    estimated_adj = embedding.predict_next_adj(node_l)
    print(len(estimated_adj), np.shape(estimated_adj))

    predicted_edge_list = evaluation_util.getEdgeListFromAdjMtx(
        estimated_adj, is_undirected=is_undirected, edge_pairs=None)
    print(len(predicted_edge_list), np.shape(predicted_edge_list),
          len(test_digraph.edges()), np.shape(test_digraph.edges()))
    # pdb.set_trace()

    MAP = metrics.computeMAP(predicted_edge_list, test_digraph)
    prec_curv, _ = metrics.computePrecisionCurve(predicted_edge_list,
                                                 test_digraph)
    return (MAP, prec_curv)
def expGR(digraph,
          graph_embedding,
          X,
          n_sampled_nodes,
          rounds,
          res_pre,
          m_summ,
          file_suffix=None,
          is_undirected=True,
          sampling_scheme="rw"):
    """Function to evaluate graph reconstruction
           
           Attributes:
               digraph (Object): Networkx Graph Object
               graph_embedding (object): Algorithm for learning graph embedding
               X_stat (ndarray): Embedding values of the graph.
               n_sampled_nodes (int): Total number of nodes.
               rounds (int): Number of times to run the experiment
               res_pre (str): prefix to be used to store the result. 
               m_summ (str): summary to be used to save the result.
               file_suffix (str): Suffix for file name.
               is_undirected (bool): Flag to denote if the graph is directed.
               sampling_scheme(str): sampling scheme for selecting the nodes.

            Returns:
                ndarray: Mean Average precision
    """
    print('\tGraph Reconstruction')
    n_sampled_nodes = int(n_sampled_nodes)
    summ_file = open('%s_%s.grsumm' % (res_pre, m_summ), 'w')
    summ_file.write('Method\t%s\n' % metrics.getMetricsHeader())
    if digraph.number_of_nodes() <= n_sampled_nodes:
        rounds = 1
    MAP = [None] * rounds
    prec_curv = [None] * rounds
    err = [None] * rounds
    err_b = [None] * rounds
    n_nodes = [None] * rounds
    n_edges = [None] * rounds
    for round_id in range(rounds):
        if sampling_scheme == "u_rand":
            sampled_digraph, node_l = graph_util.sample_graph(
                digraph, n_sampled_nodes=n_sampled_nodes)
        else:
            sampled_digraph, node_l = graph_util.sample_graph_rw_int(
                digraph, n_sampled_nodes=n_sampled_nodes)
        n_nodes[round_id] = sampled_digraph.number_of_nodes()
        n_edges[round_id] = sampled_digraph.number_of_edges()
        print('\t\tRound: %d, n_nodes: %d, n_edges:%d\n' %
              (round_id, n_nodes[round_id], n_edges[round_id]))
        sampled_X = X[node_l]
        # sampled_X = np.expand_dims(sampled_X,axis=1)
        MAP[round_id], prec_curv[round_id], err[round_id], err_b[round_id] = \
            evaluateStaticGraphReconstruction(sampled_digraph,
                                              graph_embedding,
                                              sampled_X,
                                              node_l,
                                              file_suffix= file_suffix,
                                              is_undirected=is_undirected
                                              )
    try:
        summ_file.write('Err: %f/%f\n' % (np.mean(err), np.std(err)))
        summ_file.write('Err_b: %f/%f\n' % (np.mean(err_b), np.std(err_b)))
    except TypeError:
        pass
    summ_file.write('%f/%f\t%s\n' %
                    (np.mean(MAP), np.std(MAP),
                     metrics.getPrecisionReport(prec_curv[0], n_edges[0])))
    pickle.dump([n_nodes, n_edges, MAP, prec_curv, err, err_b],
                open('%s_%s.gr' % (res_pre, m_summ), 'wb'))
    return np.mean(np.array(MAP))