def lambda_handler(event, context): if 'body' in event.keys(): event = json.loads(event["body"]) bucket = "districtr" state = event["state"].lower().replace(" ", "_") units = event["units"].lower().replace(" ", "") # district = event["dist_id"] plan_assignment = event["assignment"] keys = set(plan_assignment.keys()) key = "dual_graphs/{}_{}.json".format(state, units) try: data = s3.get_object(Bucket=bucket, Key=key) g = json_graph.adjacency_graph(json.load(data['Body'])) graph = Graph(g) assignment = { n: plan_assignment[n] if n in keys else -1 for n in graph.nodes() } part = GeographicPartition(graph, assignment) return {'statusCode': 200, 'body': json.dumps(plan_evaluation(part))} except Exception as e: print(e) return { "error": "This state/units ({}, {}) is not supported".format( event["state"], event["units"]) }
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
def graph_from_url(link): r = requests.get(url=link) data = json.loads(r.content) g = json_graph.adjacency_graph(data) graph = Graph(g) graph.issue_warnings() pos = {} for node in graph.nodes(): pos[node] = [graph.node[node]['C_X'], graph.node[node]['C_Y']] return graph
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
# return distance # link = input("Put graph link: ") # link = "https://people.csail.mit.edu/ddeford//COUNTY/COUNTY_23.json" 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_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)