Esempio n. 1
0
def network_analyze(
    traces: gpd.GeoDataFrame,
    area: gpd.GeoDataFrame,
    name: str,
    coverage_gdf: gpd.GeoDataFrame,
    circle_radius: float,
) -> Tuple[Network, pd.Series]:
    """
    Analyze traces and area for results.
    """
    network = Network(
        trace_gdf=traces,
        area_gdf=area,
        name=name,
        determine_branches_nodes=True,
        snap_threshold=0.001,
    )
    points = network.representative_points()
    if not len(points) == 1:
        raise ValueError("Expected one target area representative point.")
    point = points[0]
    amount_of_coverage = assess_coverage(target_centroid=point,
                                         coverage_gdf=coverage_gdf,
                                         radius=circle_radius)
    describe_df = describe_random_network(
        network=network,
        target_centroid=point,
        name=name,
        amount_of_coverage=amount_of_coverage,
        radius=circle_radius,
    )

    return network, describe_df.iloc[0]
Esempio n. 2
0
def test_network(
    traces,
    area,
    name,
    determine_branches_nodes,
    truncate_traces,
    snap_threshold,
    circular_target_area,
    file_regression,
    data_regression,
):
    """
    Test Network object creation and attributes with general datasets.

    Tests for regression and general assertions.
    """
    network = Network(
        trace_gdf=traces,
        area_gdf=area,
        name=name,
        determine_branches_nodes=determine_branches_nodes,
        truncate_traces=truncate_traces,
        snap_threshold=snap_threshold,
        trace_length_set_names=("a", "b"),
        branch_length_set_names=("A", "B"),
        trace_length_set_ranges=((0.1, 1), (1, 2)),
        branch_length_set_ranges=((0.1, 1), (1, 2)),
        circular_target_area=circular_target_area,
    )

    assert area.shape[0] == len(network.representative_points())
    numerical_description = network.numerical_network_description()
    assert isinstance(numerical_description, dict)
    for key, item in numerical_description.items():
        assert isinstance(key, str)
        if not isinstance(item, (int, float, str)):
            assert isinstance(item.item(), (int, float))

    cut_off = 1.0
    num_description_explicit_cut_offs = network.numerical_network_description(
        trace_lengths_cut_off=cut_off, branch_lengths_cut_off=cut_off
    )
    for label in ("branch", "trace"):
        key = label + (
            " "
            + length_distributions.Dist.POWERLAW.value
            + " "
            + length_distributions.CUT_OFF
        )
        assert num_description_explicit_cut_offs[key] == 1.0

    network_target_areas = network.target_areas
    assert isinstance(network_target_areas, list)
    assert all(
        [isinstance(val, (Polygon, MultiPolygon)) for val in network_target_areas]
    )

    network_attributes = dict()
    for attribute in ("node_counts", "branch_counts"):
        # network_attributes[attribute] = getattr(network, attribute)
        for key, value in getattr(network, attribute).items():
            if not np.isnan(value):
                network_attributes[key] = int(value)

        for key, value in numerical_description.items():
            if isinstance(value, (float, int)):
                network_attributes[key] = round(
                    value.item() if hasattr(value, "item") else value, 2
                )

    data_regression.check(network_attributes)

    if determine_branches_nodes and network.branch_gdf.shape[0] < 500:

        sorted_branch_gdf = network.branch_gdf.sort_index()
        assert isinstance(sorted_branch_gdf, gpd.GeoDataFrame)
        # Do not check massive branch counts
        file_regression.check(sorted_branch_gdf.to_json(indent=1))
        network_extensive_testing(
            network=network, traces=traces, area=area, snap_threshold=snap_threshold
        )

    trace_intersects = network.trace_intersects_target_area_boundary
    assert isinstance(trace_intersects, np.ndarray)
    assert trace_intersects.dtype in ("int32", "int64")
    branch_intersects = network.branch_intersects_target_area_boundary
    assert isinstance(branch_intersects, np.ndarray)
    assert network.branch_intersects_target_area_boundary.dtype in ("int32", "int64")