Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def test_watershed_solve_sequence(contexts, watershed_requests, n_nodes,
                                  pct_tmnt):

    watershed_request = deepcopy(watershed_requests[n_nodes, pct_tmnt])
    context = contexts["default"]

    g = graph_factory(watershed_request["graph"])

    initial_results = land_surface_loading(watershed_request,
                                           False,
                                           context=context)["summary"]
    db = pandas.DataFrame(initial_results).set_index("node_id")

    _node_list = solution_sequence(
        watershed_request["graph"],
        16)["solution_sequence"]["parallel"][0]["series"]
    node_list = [[n["id"] for n in nl["nodes"]] for nl in _node_list]

    presults = []  # no initial results, obvs
    for branch_nodes in node_list:

        # this subgraph is empty, has no data.
        subg = nx.DiGraph(g.subgraph(branch_nodes).edges)
        subgraph = {"graph": nxGraph_to_dict(subg)}
        reqd_min_attrs = attrs_to_resubmit(presults)
        previous_results = {
            "previous_results": [{
                k: dct[k]
                for k in dct.keys() if k in reqd_min_attrs + ["node_id"]
            } for dct in presults if dct["node_id"] in subg.nodes()]
        }

        subg_request = deepcopy(watershed_request)
        subg_request.update(subgraph)
        subg_request.update(previous_results)

        subgraph_response_dict = solve_watershed(
            subg_request,
            False,
            context=context,
        )
        subgraph_results = subgraph_response_dict["results"]

        presults.extend(subgraph_results)
        db = db.combine_first(
            pandas.DataFrame(subgraph_results).set_index("node_id"))

    response_dict = solve_watershed(
        watershed=watershed_request,
        treatment_pre_validated=False,
        context=context,
    )
    results = response_dict["results"] + response_dict["leaf_results"]

    check_db = pandas.DataFrame(results).set_index("node_id").sort_index(0)
    check_results_dataframes(db.sort_index(0), check_db)
Exemplo n.º 3
0
def test_round_trip_dict_to_dict(graph_dict_isvalid):
    graph_dict, isvalid = graph_dict_isvalid

    # round trips back to dictionaries are only possible if graph is directed.
    # this is because if the graph is undirected, then the 'source' and 'target'
    # keys _might_ be swapped.
    # if graph_dict['directed']:
    _g = utils.graph_factory(graph_dict)
    rt_graph_dict = utils.nxGraph_to_dict(_g)
    assert is_equal_subset(graph_dict, rt_graph_dict)
Exemplo n.º 4
0
def test_thin_graph_dict_roundtrip_graph_to_graph(graph_obj_isvalid):

    graph_obj, isvalid = graph_obj_isvalid

    _g = utils.thin_graph_dict(utils.nxGraph_to_dict(graph_obj))

    rt_Graph = utils.graph_factory(_g)

    assert rt_Graph.is_directed() == graph_obj.is_directed()
    assert rt_Graph.is_multigraph() == graph_obj.is_multigraph()
    assert nx.is_isomorphic(rt_Graph, graph_obj)
Exemplo n.º 5
0
def test_thin_graph_dict_roundtrip_dict_to_dict(graph_dict_isvalid, check):

    graph_dict, isvalid = graph_dict_isvalid

    _graph_dict = utils.thin_graph_dict(graph_dict)

    _g = utils.graph_factory(_graph_dict)
    rt_graph_dict = utils.nxGraph_to_dict(_g)

    # if it's in the input, check that it's in the output too.
    if graph_dict.get(check, None) is not None:
        assert is_equal_subset(rt_graph_dict[check], graph_dict[check])
Exemplo n.º 6
0
def test_round_trip_graph_to_graph(graph_obj_isvalid):
    graph_obj, isvalid = graph_obj_isvalid

    _g = utils.nxGraph_to_dict(graph_obj)

    G = utils.graph_factory(_g)

    assert nx.is_isomorphic(G, graph_obj)  # same structure nodes and edges

    for node, data in G.nodes(data=True):  # same node attrs
        assert data == graph_obj.nodes[node]

    if G.is_multigraph():
        for s, t, k, data in G.edges(data=True, keys=True):  # same edge attrs
            assert is_equal_subset(graph_obj.edges[(s, t, k)], data)
    else:
        for s, t, data in G.edges(data=True):  # same edge attrs
            assert is_equal_subset(graph_obj.edges[(s, t)], data)
Exemplo n.º 7
0
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)