def compute_f2_edge(graph, vertex_a, vertex_b, self_max=False): """ Compute feature f2 of Sun et al. for a specific edge""" out_weights = graph_utils.get_edge_weights(graph, vertex_a) in_weights = graph_utils.get_edge_weights(graph, vertex_b, out=False) edge_weight = graph_utils.get_edge_weight(graph, vertex_a, vertex_b) numerator = edge_weight - np.min(out_weights) if not self_max: denominator = np.max(in_weights) - np.min(in_weights) else: denominator = np.partition(in_weights, -2)[-2] - np.min(in_weights) return numerator / denominator
def compute_f1_vertex(graph, vertex, self_max=False): """ Compute feature f1 of Sun et al. for a specific vertex """ """ If the self-weight is the largest weight, set self_max true """ out_weights = graph_utils.get_edge_weights(graph, vertex) numerator = out_weights - np.min(out_weights) if not self_max: denominator = np.max(out_weights) - np.min(out_weights) else: denominator = np.partition(out_weights, -2)[-2] - np.min(out_weights) return numerator / denominator
def compute_f3_edge(graph, vertex_a, vertex_b, self_max=False): """ Compute feature f3 of Sun et al. for a specific edge""" out_weights = graph_utils.get_edge_weights(graph, vertex_a) edge_weight = graph_utils.get_edge_weight(graph, vertex_a, vertex_b) if not self_max: numerator = edge_weight - np.mean(out_weights) denominator = np.max(out_weights) - np.min(out_weights) else: numerator = edge_weight - np.partition(out_weights, -2)[:-1].mean() denominator = np.partition(out_weights, -2)[-2] - np.min(out_weights) return numerator / denominator
def write_edge_weights_full(fid, graph): """ Write the edge weights in full matrix form """ num_nodes = len(graph.nodes) num_edges = len(graph.edges) if num_edges != num_nodes**2: raise AssertionError( "Not all edges are in the graph, cannot write in full!") for node in graph.nodes: weights = graph_utils.get_edge_weights(graph, node) write_edges_weights(fid, weights)
def compute_f2_vertex(graph, vertex, self_max=False): """ Compute feature f2 of Sun et al. for a specific vertex """ """ If the self-weight is the largest weight, set self_max true """ """ NOTE that this is the transpose of the usual order """ in_weights = graph_utils.get_edge_weights(graph, vertex, out=False) minimum = np.min(in_weights) numerator = in_weights - minimum if not self_max: denominator = np.max(in_weights) - np.min(in_weights) else: denominator = np.partition(in_weights, -2)[-2] - minimum return numerator / denominator
def compute_ff_edges(graph): """ Compute the weight divided by the min right neighbour weight """ min_vertex = np.min(graph.nodes) weights = 1 + np.array(graph_utils.get_weights(graph)) mins = [ 1 + np.min(graph_utils.get_edge_weights(graph, node, out=False)) for node in graph.nodes ] return np.array([ mins[edge[1] - min_vertex] / weight for weight, edge in zip(weights, graph.edges) ])
def compute_fc_edges(graph): """ Compute the weight divided by the max right neighbour weight """ min_vertex = np.min(graph.nodes) weights = 1 + np.array(graph_utils.get_weights(graph)) maxes = [ 1 + np.max(graph_utils.get_edge_weights(graph, node, out=False)) for node in graph.nodes ] return np.array([ weight / maxes[edge[1] - min_vertex] for weight, edge in zip(weights, list(graph.edges)) ])
def write_edge_weights_triangular(fid, graph): """ Write the edge weights in full matrix form """ num_nodes = len(graph.nodes) num_edges = len(graph.edges) min_vertex = np.min(graph.nodes) if num_edges != num_nodes * (num_nodes - 1) / 2: raise AssertionError( "Wrong number of edges in the graph, cannot write triangular!") for node in graph.nodes: weights = graph_utils.get_edge_weights(graph, node)[node - min_vertex:] write_edges_weights(fid, weights)