Example #1
0
def test_multilengthdistribution_plot(
    list_of_length_arrays,
    list_of_area_values,
    names,
    automatic_cut_offs,
    using_branches,
    num_regression,
):
    """
    Test MultiLengthDistribution plot_multi_length_distributions.
    """
    assert isinstance(list_of_length_arrays, list)
    assert isinstance(list_of_area_values, list)
    assert isinstance(names, list)
    assert len(list_of_length_arrays) == len(list_of_area_values) == len(names)
    multi_length_distribution = length_distributions.MultiLengthDistribution(
        distributions=[
            length_distributions.LengthDistribution(
                name=name,
                lengths=lengths,
                area_value=area_value,
                using_branches=using_branches,
            )
            for name, lengths, area_value in zip(
                names, list_of_length_arrays, list_of_area_values
            )
        ],
        using_branches=using_branches,
    )

    polyfit, fig, ax = multi_length_distribution.plot_multi_length_distributions(
        automatic_cut_offs=automatic_cut_offs, plot_truncated_data=True
    )
    plt.close()

    mld = multi_length_distribution

    (
        truncated_length_array_all,
        ccm_array_normed_all,
        _,
        _,
    ) = mld.normalized_distributions(
        automatic_cut_offs=automatic_cut_offs,
    )

    assert isinstance(fig, Figure) and isinstance(ax, Axes)
    assert isinstance(truncated_length_array_all, list)
    assert isinstance(ccm_array_normed_all, list)

    num_regression.check(
        {
            "concatted_lengths": np.concatenate(truncated_length_array_all),
            "concatted_ccm": np.concatenate(ccm_array_normed_all),
            "fitted_y_values": polyfit.y_fit,
            "m_value": np.array([polyfit.m_value]),
            "constant": np.array([polyfit.constant]),
        },
        default_tolerance=dict(atol=1e-4, rtol=1e-4),
    )
def test_multilengthdistribution_plot(
    list_of_length_arrays,
    list_of_area_values,
    names,
    cut_distributions,
    using_branches,
    num_regression,
):
    """
    Test MultiLengthDistribution plot_multi_length_distributions.
    """
    assert isinstance(list_of_length_arrays, list)
    assert isinstance(list_of_area_values, list)
    assert isinstance(names, list)
    assert len(list_of_length_arrays) == len(list_of_area_values) == len(names)
    multi_length_distribution = length_distributions.MultiLengthDistribution(
        distributions=[
            length_distributions.LengthDistribution(
                name=name,
                lengths=lengths,
                area_value=area_value,
                using_branches=using_branches,
            ) for name, lengths, area_value in zip(
                names, list_of_length_arrays, list_of_area_values)
        ],
        using_branches=using_branches,
        cut_distributions=cut_distributions,
    )

    fig, ax = multi_length_distribution.plot_multi_length_distributions()

    mld = multi_length_distribution

    assert isinstance(fig, Figure) and isinstance(ax, Axes)
    assert isinstance(mld.truncated_length_array_all, list)
    assert isinstance(mld.ccm_array_normed_all, list)
    assert isinstance(mld.fitted_y_values, np.ndarray)
    assert isinstance(mld.m_value, float)
    assert isinstance(mld.constant, float)
    assert isinstance(mld.concatted_lengths, np.ndarray)
    assert isinstance(mld.concatted_ccm, np.ndarray)

    num_regression.check(
        {
            "concatted_lengths": mld.concatted_lengths,
            "concatted_ccm": mld.concatted_ccm,
            "fitted_y_values": mld.fitted_y_values,
            "m_value": np.array([mld.m_value]),
            "constant": np.array([mld.constant]),
        },
        default_tolerance=dict(atol=1e-4, rtol=1e-4),
    )
Example #3
0
    def multi_length_distributions(
            self, using_branches: bool
    ) -> length_distributions.MultiLengthDistribution:
        """
        Get MultiLengthDistribution of all networks.
        """
        distributions_dict = self.network_length_distributions(
            using_branches=using_branches, using_azimuth_sets=False)
        distributions = [
            distributions_dict[network.name][network.name]
            for network in self.networks
        ]

        multi_distribution = length_distributions.MultiLengthDistribution(
            distributions=distributions,
            using_branches=using_branches,
        )
        return multi_distribution
Example #4
0
def test_plot_mld_optimized(distributions):
    """
    Test plot_mld_optimized.
    """
    mld = length_distributions.MultiLengthDistribution(
        distributions=distributions, using_branches=False
    )

    opt_result, opt_mld = mld.optimize_cut_offs()

    polyfit, fig, ax = opt_mld.plot_multi_length_distributions(
        automatic_cut_offs=False, plot_truncated_data=True
    )

    assert isinstance(opt_result, length_distributions.MultiScaleOptimizationResult)
    assert isinstance(polyfit, length_distributions.Polyfit)
    assert isinstance(fig, Figure) and isinstance(ax, Axes)

    plt.close()
Example #5
0
    def set_multi_length_distributions(
        self, using_branches: bool
    ) -> Dict[str, length_distributions.MultiLengthDistribution]:
        """
        Get set-wise multi-length distributions.
        """
        distributions_dict = self.network_length_distributions(
            using_branches=using_branches, using_azimuth_sets=True)

        mlds = dict()
        for set_name in self.networks[0].azimuth_set_names:
            set_lengths: List[length_distributions.LengthDistribution] = []
            for lds in distributions_dict.values():
                set_lengths.append(lds[set_name])

            mld = length_distributions.MultiLengthDistribution(
                distributions=set_lengths,
                using_branches=using_branches,
            )
            mlds[set_name] = mld
        return mlds
Example #6
0
def test_fit_to_multi_scale_lengths_fitter_comparisons(
    distributions: List[length_distributions.LengthDistribution],
    automatic_cut_offs: bool,
):
    """
    Test fit_to_multi_scale_lengths.
    """
    mld = length_distributions.MultiLengthDistribution(
        distributions=distributions, using_branches=False
    )

    (
        truncated_length_array_all,
        ccm_array_normed_all,
        # All length and ccm data ignored
        _,
        _,
    ) = mld.normalized_distributions(automatic_cut_offs=automatic_cut_offs)
    concatted_lengths, concatted_ccm = np.concatenate(
        truncated_length_array_all
    ), np.concatenate(ccm_array_normed_all)

    # Fit with np.polyfit
    numpy_polyfit_vals = length_distributions.fit_to_multi_scale_lengths(
        lengths=concatted_lengths,
        ccm=concatted_ccm,
        fitter=length_distributions.numpy_polyfit,
    )

    # Fit with scikit LinearRegression
    linear_regression_vals = length_distributions.fit_to_multi_scale_lengths(
        lengths=concatted_lengths,
        ccm=concatted_ccm,
        fitter=length_distributions.scikit_linear_regression,
    )

    for val in [*numpy_polyfit_vals, *linear_regression_vals]:
        # Polyfit attributes should be floats, array and callables (scorer func)
        assert isinstance(val, (np.ndarray, float)) or callable(val)
def test_fit_to_multi_scale_lengths_fitter_comparisons(
    distributions: List[length_distributions.LengthDistribution], ):
    """
    Test fit_to_multi_scale_lengths.
    """
    mld = length_distributions.MultiLengthDistribution(
        distributions=distributions,
        cut_distributions=True,
        using_branches=False)

    numpy_polyfit_vals = length_distributions.fit_to_multi_scale_lengths(
        lengths=mld.concatted_lengths,
        ccm=mld.concatted_ccm,
        fitter=length_distributions.numpy_polyfit,
    )
    linear_regression_vals = length_distributions.fit_to_multi_scale_lengths(
        lengths=mld.concatted_lengths,
        ccm=mld.concatted_ccm,
        fitter=length_distributions.scikit_linear_regression,
    )

    for val in [*numpy_polyfit_vals, *linear_regression_vals]:
        assert isinstance(val, (np.ndarray, float))
mpl.rcParams["figure.figsize"] = (5, 5)
mpl.rcParams["font.size"] = 8

# %%
# Collect LengthDistributions into MultiLengthDistribution
# ------------------------------------------------------------------

networks = [kb11_network, hastholmen_network, lidar_200k_network]

distributions = [
    netw.trace_length_distribution(azimuth_set=None) for netw in networks
]

mld = length_distributions.MultiLengthDistribution(
    distributions=distributions,
    using_branches=False,
    fitter=length_distributions.scikit_linear_regression,
)

# %%
# Use scipy.optimize.shgo to optimize cut-off values
# ------------------------------------------------------------------

# See https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.shgo.html
# for potential keyword arguments to pass to the shgo call
# shgo_kwargs are passed as is to ``scipy.optimize.shgo``.
shgo_kwargs = dict(sampling_method="sobol", )

# Choose loss function for optimization. Here r2 is chosen to get a visually
# sensible result but it is generally ill-suited for optimizing cut-offs of
# power-law distributions.