def test_solve_watershed_stable_with_subsets(contexts, watershed_graph, initial_node_data): g, data = watershed_graph, deepcopy(initial_node_data) context = contexts["default"] nx.set_node_attributes(g, data) solve_watershed_loading(g, context) # from the previous solution, we only need the keys which contain the accumulated keys. # keys = ["_direct", "_upstream", "_cumul", "_inflow", "_discharged"] reqd_min_attrs = attrs_to_resubmit( [data for n, data in g.nodes(data=True)]) prev_solve_data = { n: {k: dct[k] for k in dct.keys() if k in reqd_min_attrs} for n, dct in g.nodes(data=True) } # check single dirty nodes for dirty_node in g.nodes(): dirty_nodes = [dirty_node] subg = nx.DiGraph(g.subgraph(get_subset(g, nodes=dirty_nodes)).edges) # always send the info that was sent the first time nx.set_node_attributes( subg, {k: v for k, v in data.items() if k in subg.nodes()}) nx.set_node_attributes( subg, {k: v for k, v in prev_solve_data.items() if k not in dirty_nodes}) solve_watershed_loading(subg, context) check_graph_data_equal(g, subg) # check multiple dirty nodes numpy.random.seed(42) for dirty_nodes in [ numpy.random.choice(g.nodes(), size=size, replace=False) for size in [2, 4, 6, 8, 10] ]: subg = nx.DiGraph(g.subgraph(get_subset(g, nodes=dirty_nodes)).edges) # always send the info that was sent the first time nx.set_node_attributes( subg, {k: v for k, v in data.items() if k in subg.nodes()}) nx.set_node_attributes( subg, {k: v for k, v in prev_solve_data.items() if k not in dirty_nodes}) solve_watershed_loading(subg, context) check_graph_data_equal(g, subg)
def test_post_solve_watershed_stable(client, watershed_requests, watershed_responses, watershed_test_case): size, pct_tmnt, dirty_nodes = watershed_test_case watershed_request = watershed_requests[size, pct_tmnt] post_response = watershed_responses[size, pct_tmnt] results = post_response.json()["data"]["results"] reqd_min_attrs = attrs_to_resubmit(results) previous_results = { "previous_results": [{k: dct[k] for k in dct.keys() if k in reqd_min_attrs + ["node_id"]} for dct in results] } g = graph_factory(watershed_request["graph"]) subg = nx.DiGraph(g.subgraph(get_subset(g, nodes=dirty_nodes)).edges) subgraph = {"graph": nxGraph_to_dict(subg)} new_request = deepcopy(watershed_request) new_request.update(subgraph) new_request.update(previous_results) payload = json.dumps(new_request) route = config.API_LATEST + "/watershed/solve" response = client.post(route, data=payload) subgraph_results = response.json()["data"]["results"] check_subgraph_response_equal(subgraph_results, results)
def test_stable_watershed_stable_subgraph_solutions(contexts, watershed_requests, watershed_test_case): n_nodes, pct_tmnt, dirty_nodes = watershed_test_case watershed_request = deepcopy(watershed_requests[(n_nodes, pct_tmnt)]) context = contexts["default"] response_dict = solve_watershed( watershed=watershed_request, treatment_pre_validated=False, context=context, ) results = response_dict["results"] reqd_min_attrs = attrs_to_resubmit(results) previous_results = { "previous_results": [{k: dct[k] for k in dct.keys() if k in reqd_min_attrs + ["node_id"]} for dct in results] } g = graph_factory(watershed_request["graph"]) # this subgraph is empty, has no data. subg = nx.DiGraph(g.subgraph(get_subset(g, nodes=dirty_nodes)).edges) subgraph = {"graph": nxGraph_to_dict(subg)} new_request = deepcopy(watershed_request) new_request.update(subgraph) new_request.update(previous_results) subgraph_response_dict = solve_watershed( watershed=new_request, treatment_pre_validated=False, context=context, ) subgraph_results = subgraph_response_dict["results"] check_subgraph_response_equal(subgraph_results, results)
def network_subgraphs(graph: Dict[str, Any], nodes: List[Dict[str, Any]]) -> Dict[str, Any]: _graph = thin_graph_dict(graph) node_ids = [node["id"] for node in nodes] G = graph_factory(_graph) subset = get_subset(G, node_ids) sub_g = G.subgraph(subset) subgraph_nodes = [{ "nodes": [{ "id": n } for n in nodes] } for nodes in nx.weakly_connected_components(sub_g)] result: Dict[str, Any] = {"graph": _graph} result.update({"requested_nodes": nodes}) result.update({"subgraph_nodes": subgraph_nodes}) return result
def test_network_algo_get_subset_dag(g): for node in g.nodes(): subset = get_subset(g, node) assert len(subset) <= len(g)
def test_network_algo_get_subset(graph, nodes, exp): subset = get_subset(graph, nodes) assert subset == set(exp)