Exemplo n.º 1
0
 def fit_sparsify(self, graph, iterations, weight="weight"):
     """ Compute the sparsification features for the graph """
     
     sparse_graph = self.copy_graph_blank(graph)
     edges = self.run_sparsify(graph, iterations=iterations, weight=weight)
     for num, edge_set in enumerate(edges):
         weighted_edges = [edge + ((num + 1) / iterations, ) for edge in edge_set]
         sparse_graph.add_weighted_edges_from(weighted_edges)
     return graph_utils.get_weights(sparse_graph)
Exemplo n.º 2
0
def define_edge_objective(variable_dict, graph, perturb=False):
    """ Define all terms for the edge objective """

    weights = np.array(graph_utils.get_weights(graph))
    if perturb:
        perturbs = ((weights - weights.mean())/weights.mean()) ** 2
        perturbs  = np.clip(perturbs * 2 -1, -1, 1)
        randoms = np.random.uniform(low=0, high=0.3, size=len(weights))
        perturbs = np.ones_like(randoms)
        weights = weights + weights * perturbs * randoms
    variables = [variable_dict["x_{},{}".format(*edge)] for edge in graph.edges]
    return [var * weight for (var, weight) in zip(variables, weights)]
Exemplo n.º 3
0
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)
    ])
Exemplo n.º 4
0
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))
    ])
Exemplo n.º 5
0
def plot_graph(graph,
               edges=None,
               weights=None,
               weight="weight",
               coord_dict=None,
               withlabels=True):
    """ Plot the given graph, plotting the given edges and weights (default all) """

    coord_dict = coord_dict or generate_coord_dict(graph)

    if edges is None:
        edges = graph_utils.get_edges(graph)
    if weights is None:
        weights = weights or graph_utils.get_weights(graph, weight=weight)

    check_indices(coord_dict, edges)

    plot_vertices(coord_dict, withlabels=withlabels)
    plot_edges(coord_dict, edges, weights)
Exemplo n.º 6
0
def plot_weighted_graph(graph,
                        weight="weight",
                        title="Random Weighted Graph Plot"):
    """ Plot the given graph, randomly positioning nodes """

    coords = nx.spring_layout(graph)

    xs = [coords[key][0] for key in coords.keys()]
    ys = [coords[key][1] for key in coords.keys()]

    plt.scatter(xs, ys, marker="x", color="purple")

    for a, b, k in zip(xs, ys, coords.keys()):
        plt.text(a, b, k)

    edges, weights = graph_utils.get_edges(graph), graph_utils.get_weights(
        graph, weight=weight)

    for (edge, weight) in zip(edges, weights):
        xs = (coords[edge[0]][0], coords[edge[1]][0])
        ys = (coords[edge[0]][1], coords[edge[1]][1])
        plt.plot(xs, ys, 'k-', lw=2, color="black", alpha=weight)
Exemplo n.º 7
0
def compute_f12_edges(graph):
    """ Compare the edges to the mean edge value """

    weights = np.array(graph_utils.get_weights(graph))
    return weights.flatten() / (np.max(weights) - np.min(weights))
Exemplo n.º 8
0
def compute_f10_edges(graph):
    """ Compare the edges to the max edge value """

    weights = np.array(graph_utils.get_weights(graph))
    return weights.flatten() / np.max(weights)
Exemplo n.º 9
0
def compute_fd_edges(graph):
    """ Compute the weight divided by the global max """

    weights = np.array(graph_utils.get_weights(graph))
    global_min = weights.min()
    return (1 + global_min) / (1 + weights)