Example #1
0
def test_multinetwork_subsample(network_params, samples: int, min_radii: float):
    """
    Test MultiNetwork.subsample.
    """
    assert min_radii > 0.0
    assert samples > 0
    networks = [Network(**kwargs) for kwargs in network_params]
    multi_network = MultiNetwork(networks=tuple(networks))

    subsamples = multi_network.subsample(min_radii=min_radii, samples=samples)

    assert isinstance(subsamples, list)
    assert len(networks) * samples == len(subsamples)

    for subsample in subsamples:
        if subsample.error:
            raise subsample.result
    identifiers = [subsample.identifier for subsample in subsamples]
    assert not any(subsample.error for subsample in subsamples)

    for network in networks:
        assert network.name in identifiers

    gathered = subsampling.gather_subsample_descriptions(subsample_results=subsamples)

    assert isinstance(gathered, list)

    assert len(gathered) > 0
Example #2
0
def multinetwork_plot_multi_length_distribution(
    network_params,
    using_branches=False,
    automatic_cut_offs=True,
    plot_truncated_data=True,
):
    """
    Create and test multi_network plot_multi_length_distribution.
    """
    # using_branches = False
    # automatic_cut_offs = True
    networks = [
        Network(**kwargs, determine_branches_nodes=using_branches)
        for kwargs in network_params
    ]
    multi_network = MultiNetwork(networks=tuple(networks))

    mld = multi_network.multi_length_distributions(using_branches=using_branches)

    assert len(mld.distributions) == len(network_params)

    mld, polyfit, fig, ax = multi_network.plot_multi_length_distribution(
        using_branches=using_branches,
        automatic_cut_offs=automatic_cut_offs,
        plot_truncated_data=plot_truncated_data,
    )

    assert isinstance(mld, length_distributions.MultiLengthDistribution)
    assert isinstance(polyfit, length_distributions.Polyfit)
    assert isinstance(fig, Figure)
    assert isinstance(ax, Axes)
    return mld, fig, ax
Example #3
0
def multinetwork_plot_multi_length_distribution(
    network_params, using_branches=False, cut_distributions=True
):
    """
    Create and test multi_network plot_multi_length_distribution.
    """
    # using_branches = False
    # cut_distributions = True
    networks = [
        Network(**kwargs, determine_branches_nodes=using_branches)
        for kwargs in network_params
    ]
    multi_network = MultiNetwork(networks=tuple(networks))

    mld = multi_network.multi_length_distributions(
        using_branches=using_branches, cut_distributions=cut_distributions
    )

    assert len(mld.distributions) == len(network_params)

    mld, fig, ax = multi_network.plot_multi_length_distribution(
        using_branches=using_branches, cut_distributions=cut_distributions
    )

    assert isinstance(mld, length_distributions.MultiLengthDistribution)
    assert isinstance(fig, Figure)
    assert isinstance(ax, Axes)
    return mld, fig, ax
Example #4
0
def test_multinetwork_methods(
    network_params, using_branches, automatic_cut_offs, tmp_path: Path
):
    """
    Test MultiNetwork methods generally.
    """

    networks = [
        Network(**params, determine_branches_nodes=using_branches)
        for params in network_params
    ]

    multi_network = MultiNetwork(tuple(networks))

    mlds, polyfits, figs, axes = multi_network._plot_azimuth_set_lengths(
        automatic_cut_offs=automatic_cut_offs,
        using_branches=using_branches,
        plot_truncated_data=True,
    )

    for name, fig in zip(mlds, figs):
        output_path = general.save_fig(fig=fig, results_dir=tmp_path, name=name)
        logging.info(f"Saved plot to {output_path}.")

    assert isinstance(mlds, dict)
    assert isinstance(polyfits, list)
    assert isinstance(figs, list)
    assert isinstance(axes, list)
    assert isinstance(figs[0], Figure)
    assert isinstance(axes[0], Axes)
    assert isinstance(polyfits[0], length_distributions.Polyfit)

    # Test basic_network_descriptions_df

    if not using_branches:
        return
    connection_frequency_new = "C FREQ"
    columns = {
        general.NAME: (None, str),
        general.Param.AREA.value.name: (None, float),
        general.Param.TRACE_MEAN_LENGTH.value.name: (None, float),
        general.Param.BRANCH_MEAN_LENGTH.value.name: (None, float),
        general.Param.FRACTURE_INTENSITY_P21.value.name: (None, float),
        general.Param.CONNECTION_FREQUENCY.value.name: (
            connection_frequency_new,
            float,
        ),
    }
    basic_network_descriptions_df = multi_network.basic_network_descriptions_df(
        columns=columns
    )
    assert isinstance(basic_network_descriptions_df, pd.DataFrame)
    assert general.NAME not in basic_network_descriptions_df.index.values
    assert general.Param.AREA.value.name in basic_network_descriptions_df.index.values

    # Test renaming
    for old_column, (new_column, _) in columns.items():
        if new_column is not None:
            assert new_column in basic_network_descriptions_df.index.values
            assert old_column not in basic_network_descriptions_df.index.values
Example #5
0
def test_multinetwork_plot_azimuth_set_lengths(
    network_params, using_branches, cut_distributions, tmp_path: Path
):
    """
    Test MultiNetwork._plot_azimuth_set_lengths.
    """

    networks = [
        Network(**params, determine_branches_nodes=using_branches)
        for params in network_params
    ]

    multi_network = MultiNetwork(tuple(networks))

    mlds, figs, axes = multi_network._plot_azimuth_set_lengths(
        cut_distributions=cut_distributions, using_branches=using_branches
    )

    for name, fig in zip(mlds, figs):
        output_path = general.save_fig(fig=fig, results_dir=tmp_path, name=name)
        logging.info(f"Saved plot to {output_path}.")

    assert isinstance(mlds, dict)
    assert isinstance(figs, list)
    assert isinstance(axes, list)
    assert isinstance(figs[0], Figure)
    assert isinstance(axes[0], Axes)
Example #6
0
def perf_profile(
    do_validation: bool = True,
    do_network: bool = True,
    do_grid: bool = True,
    do_subsampling: bool = False,
):
    """
    Profile ``fractopo`` performance.
    """
    if not all(path.exists()
               for path in (SAMPLE_TRACES_PATH, SAMPLE_AREA_PATH)):
        logging.error(f"Current path: {Path.cwd().resolve()}")
        raise FileNotFoundError("Expected sample to exist at:"
                                f" {(SAMPLE_TRACES_PATH, SAMPLE_AREA_PATH)}")

    traces = read_geofile(SAMPLE_TRACES_PATH)
    area = read_geofile(SAMPLE_AREA_PATH)
    name = SAMPLE_AREA_PATH.stem
    snap_threshold = 0.001

    if do_validation:
        validator = Validation(
            traces=traces,
            area=area,
            SNAP_THRESHOLD=snap_threshold,
            name=name,
            allow_fix=True,
        )
        validator.run_validation()

    if do_network:
        network = Network(
            trace_gdf=traces,
            area_gdf=area,
            snap_threshold=snap_threshold,
            name=name,
            circular_target_area=True,
            truncate_traces=True,
            determine_branches_nodes=True,
        )

        network.numerical_network_description()
        if do_grid:
            network.contour_grid(cell_width=2.0)

        if do_subsampling:
            multi_network = MultiNetwork((network, ))
            multi_network.subsample(min_radii=5.0, samples=5)
Example #7
0
def test_multinetwork_plot_ternary(network_params, tmp_path):
    """
    Test MultiNetwork.plot_xyi and plot_branch.
    """

    networks = [
        Network(**params, determine_branches_nodes=True) for params in network_params
    ]

    multi_network = MultiNetwork(tuple(networks))

    for plot_func in ("plot_xyi", "plot_branch"):

        fig, ax, tax = getattr(multi_network, plot_func)()

        assert isinstance(fig, Figure)
        assert isinstance(ax, Axes)
        assert isinstance(tax, TernaryAxesSubplot)

        tmp_path = Path(tmp_path)
        tmp_path.mkdir(exist_ok=True)
        plot_path = tmp_path / f"{plot_func}.png"
        fig.savefig(plot_path, bbox_inches="tight")
        logging.info(f"Saved {plot_func} plot to {plot_path.absolute()}.")
Example #8
0
# Note that azimuth sets are explicitly set here for both networks. They are
# user-defined though the Network will use default azimuth sets if not set by
# the user.

import geopandas as gpd

from fractopo import Network

kb11_network = Network(
    name="KB11",
    trace_gdf=gpd.read_file(
        "https://raw.githubusercontent.com/nialov/"
        "fractopo/master/tests/sample_data/KB11/KB11_traces.geojson"),
    area_gdf=gpd.read_file(
        "https://raw.githubusercontent.com/nialov/"
        "fractopo/master/tests/sample_data/KB11/KB11_area.geojson"),
    truncate_traces=True,
    circular_target_area=False,
    determine_branches_nodes=True,
    snap_threshold=0.001,
    # Explicitly set azimuth sets
    azimuth_set_names=("N-S", "E-W"),
    azimuth_set_ranges=((135, 45), (45, 135)),
)

hastholmen_network = Network(
    name="Hastholmen",
    trace_gdf=gpd.read_file(
        "https://raw.githubusercontent.com/nialov/"
        "fractopo/master/tests/sample_data/hastholmen_traces_validated.geojson"
    ),
    area_gdf=gpd.read_file(
Example #9
0
# Load kb11_network network from examples/example_networks.py
from example_networks import kb11_network

# Import Network class from fractopo
from fractopo import Network
from fractopo.analysis.parameters import plot_parameters_plot

# Make kb7_network here
kb7_network = Network(
    name="KB7",
    trace_gdf=gpd.read_file(
        "https://raw.githubusercontent.com/nialov/"
        "fractopo/master/tests/sample_data/KB7/KB7_traces.geojson"),
    area_gdf=gpd.read_file(
        "https://raw.githubusercontent.com/nialov/"
        "fractopo/master/tests/sample_data/KB7/KB7_area.geojson"),
    truncate_traces=True,
    circular_target_area=False,
    determine_branches_nodes=True,
    snap_threshold=0.001,
)

# %%
# Geometric and topological Network parameters
# --------------------------------------------
#
# All parameters are accessible through an attribute

kb11_parameters = kb11_network.parameters
kb7_parameters = kb7_network.parameters