def generate_random_watershed_solve_request_from_graph(g, context, pct_tmnt=0.5): graph_dict = {"graph": clean_graph_dict(g)} treatment_facility_nodes = [] treatment_site_nodes = [] land_surface_nodes = [] for n in g: if g.in_degree(n) >= 1 and numpy.random.random() < pct_tmnt: treatment_facility_nodes.append(n) elif g.in_degree(n) >= 1 and numpy.random.random() < pct_tmnt: treatment_site_nodes.append(n) else: land_surface_nodes.append(n) treatment_facility_dict = generate_random_treatment_facility_request( treatment_facility_nodes, context) treatment_site_dict = generate_random_treatment_site_request( treatment_site_nodes, context) land_surface_dict = generate_random_land_surface_request( land_surface_nodes, context) request = { **graph_dict, **treatment_facility_dict, **treatment_site_dict, **land_surface_dict, } return request
def solution_sequence_response(client): min_branch_size = [2, 6, 10, 50] n_graphs = [1, 3, 5, 10] min_max = [(3, 4), (10, 11), (20, 40)] responses = {} for bs, ngraph, minmax in product(min_branch_size, n_graphs, min_max): g = generate_n_random_valid_watershed_graphs(ngraph, *minmax) payload = json.dumps(clean_graph_dict(g)) route = API_LATEST + "/network/solution_sequence" response = client.post(route + f"?min_branch_size={bs}", data=payload) responses[(bs, ngraph, minmax)] = response if all([minmax == (10, 11), ngraph == 3, bs == 6]): result_route = response.json()["result_route"] client.get(result_route + "/img?media_type=svg") time.sleep(0.5) yield responses
def named_subgraph_responses(client): route = API_LATEST + "/network/subgraph" responses = {} slow_graph = clean_graph_dict(nx.gnr_graph(200, p=0.05, seed=42)) nodes = [{"id": "3"}, {"id": "29"}, {"id": "18"}] init_post_requests = [ # name, file or object, is-fast ("subgraph_response_fast", get_payload("network_subgraph_request.json"), True), ( "subgraph_response_slow", json.dumps(dict(graph=slow_graph, nodes=nodes)), False, ), ] for name, payload, isfast in init_post_requests: response = client.post(route, data=payload) responses[name] = response result_route = response.json()["result_route"] if isfast: # trigger the svg render here so it's ready to get later. client.get(result_route + "/img?media_type=svg") time.sleep(0.5) yield responses
def test_clean_graph_object(graph_obj_isvalid): graph_obj, isvalid = graph_obj_isvalid clean_graph_dict = utils.clean_graph_dict(graph_obj) assert all([isinstance(n["id"], str) for n in clean_graph_dict["nodes"]]) assert all([isinstance(e["source"], str) for e in clean_graph_dict["edges"]]) assert all([isinstance(e["target"], str) for e in clean_graph_dict["edges"]]) clean_graph_obj = nx.relabel_nodes(graph_obj, lambda x: str(x)) _g = utils.graph_factory(clean_graph_dict) assert nx.is_isomorphic(_g, clean_graph_obj)
def named_validation_responses(client): route = API_LATEST + "/network/validate" responses = {} slow_valid = json.dumps( clean_graph_dict(nx.gnr_graph(15000, p=0.05, seed=42))) slow_invalid = json.dumps(clean_graph_dict(nx.gnc_graph(15000, seed=42))) init_post_requests = [ ("valid_graph_response_fast", get_payload("network_validate_is_valid.json")), ( "invalid_graph_response_fast", get_payload("network_validate_is_invalid_cycle.json"), ), ("valid_graph_response_slow", slow_valid), ("invalid_graph_response_slow", slow_invalid), ] for name, payload in init_post_requests: response = client.post(route, data=payload) responses[name] = response yield responses
def generate_random_graph_request(n_nodes, seed=0): # pragma: no cover g = nx.gnr_graph(n=n_nodes, p=0.0, seed=seed) graph_dict = clean_graph_dict(g) return {"graph": graph_dict}