def set_up_graph(link):
    """
    The function used to convert online json file to adjacency graph
    Parameters:
        link (string): the link for online json file
    Returns:
        Graph: graph with the information
        mean: the mean value used for partition
    """
    link = "https://people.csail.mit.edu/ddeford//COUNTY/COUNTY_13.json"
    r = requests.get(link)
    data = json.loads(r.content)
    g = json_graph.adjacency_graph(data)
    graph = Graph(g)
    graph.issue_warnings()

    horizontal = []
    node_degree = []

    # find the node with degree 1 or 2 and remove it
    for node in graph.nodes():
        graph.nodes[node]["pos"] = [
            graph.node[node]['C_X'], graph.node[node]['C_Y']
        ]
        horizontal.append(graph.node[node]['C_X'])
        if graph.degree(node) == 1 or graph.degree(node) == 2:
            node_degree.append(node)

    # remove node with degree 1 or 2 since it will impact the outcome of graph
    for i in node_degree:
        graph.remove_node(i)

    # calculate mean value for partition
    mean = sum(horizontal) / len(horizontal)
    return graph, mean
Example #2
0
def graph_from_url_processing(link):
    r = requests.get(url=link)
    data = json.loads(r.content)
    g = json_graph.adjacency_graph(data)
    graph = Graph(g)
    graph.issue_warnings()
    for node in graph.nodes():
        graph.nodes[node]["pos"] = [graph.nodes[node]['C_X'], graph.nodes[node]['C_Y'] ]
    deg_one_nodes = []
    for v in graph:
        if graph.degree(v) == 1:
            deg_one_nodes.append(v)
    for node in deg_one_nodes:
        graph.remove_node(node)
    return graph
graph.issue_warnings()

horizontal = []
node_degree_1 = []
# for x in graph.nodes():
#     graph.nodes[x]["pos"] = np.array(x[0], x[1])
for node in graph.nodes():
    graph.nodes[node]["pos"] = [
        graph.node[node]['C_X'], graph.node[node]['C_Y']
    ]
    horizontal.append(graph.node[node]['C_X'])
    if graph.degree(node) == 1 or graph.degree(node) == 2:
        node_degree_1.append(node)

for i in node_degree_1:
    graph.remove_node(i)

mean = sum(horizontal) / len(horizontal)

# m= 5
# graph = nx.grid_graph([m,m])
# graph.name = "grid_size:" + str(m)
# for x in graph.nodes():
#
#     graph.nodes[x]["pos"] = np.array([x[0], x[1]])

###graph = depth_k_refine(graph,0)
##graph = depth_k_barycentric(graph, 4)
draw_with_location(graph)
#graph = compute_rotation_system(graph)
#This makes it so that the graph has the rotation system around each vertex