fractopo.analysis.length_distributions module

Utilities for analyzing and plotting length distributions for line data.

class fractopo.analysis.length_distributions.Dist(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Enums of powerlaw model types.

EXPONENTIAL = 'exponential'
LOGNORMAL = 'lognormal'
POWERLAW = 'power_law'
TRUNCATED_POWERLAW = 'truncated_power_law'
class fractopo.analysis.length_distributions.LengthDistribution(lengths, area_value, using_branches, name='', _automatic_fit=None)

Bases: object

Dataclass for length distributions.

area_value: float
property automatic_fit: Fit | None

Get automatic powerlaw Fit.

generate_distributions(cut_off=1e-18)

Generate ccdf and truncated length data with cut_off.

lengths: ndarray
manual_fit(cut_off)

Get manual powerlaw Fit.

name: str = ''
using_branches: bool
class fractopo.analysis.length_distributions.MultiLengthDistribution(distributions, using_branches, fitter=<function numpy_polyfit>, cut_offs=None, _fit_to_multi_scale_lengths=None, _normalized_distributions=None, _optimized=False)

Bases: object

Multi length distribution.

cut_offs: Optional[List[float]] = None
distributions: List[LengthDistribution]
fitter(log_ccm)

Fit numpy polyfit to data.

Return type:

Tuple[float, float]

property names: List[str]

Get length distribution names.

normalized_distributions(automatic_cut_offs)

Create normalized and truncated lengths and ccms.

Return type:

Tuple[List[ndarray], List[ndarray], List[ndarray], List[ndarray], List[ndarray]]

optimize_cut_offs(shgo_kwargs=None, scorer=<function mean_squared_log_error>)

Get cut-off optimized MultiLengthDistribution.

Return type:

Tuple[MultiScaleOptimizationResult, MultiLengthDistribution]

optimized_multi_scale_fit(scorer, shgo_kwargs)

Use scipy.optimize.shgo to optimize fit.

Return type:

MultiScaleOptimizationResult

plot_multi_length_distributions(automatic_cut_offs, plot_truncated_data, scorer=<function mean_squared_log_error>)

Plot multi-scale length distribution.

Return type:

Tuple[Polyfit, Figure, Axes]

using_branches: bool
class fractopo.analysis.length_distributions.MultiScaleOptimizationResult(polyfit: Polyfit, cut_offs: ndarray, optimize_result: OptimizeResult, x0: ndarray, bounds: ndarray, proportions_of_data: List[float])

Bases: NamedTuple

Results of scipy.optimize.shgo on length data.

bounds: ndarray

Alias for field number 4

cut_offs: ndarray

Alias for field number 1

optimize_result: OptimizeResult

Alias for field number 2

polyfit: Polyfit

Alias for field number 0

proportions_of_data: List[float]

Alias for field number 5

x0: ndarray

Alias for field number 3

class fractopo.analysis.length_distributions.Polyfit(y_fit: ndarray, m_value: float, constant: float, score: float, scorer: Callable[[ndarray, ndarray], float])

Bases: NamedTuple

Results of a polyfit to length data.

constant: float

Alias for field number 2

m_value: float

Alias for field number 1

score: float

Alias for field number 3

scorer: Callable[[ndarray, ndarray], float]

Alias for field number 4

y_fit: ndarray

Alias for field number 0

class fractopo.analysis.length_distributions.SilentFit(data, discrete=False, xmin=None, xmax=None, verbose=True, fit_method='Likelihood', estimate_discrete=True, discrete_approximation='round', sigma_threshold=None, parameter_range=None, fit_optimizer=None, xmin_distance='D', xmin_distribution='power_law', **kwargs)

Bases: Fit

Wrap powerlaw.Fit for the singular purpose of silencing output.

Silences output both to stdout and stderr.

fractopo.analysis.length_distributions.all_fit_attributes_dict(fit)

Collect ‘all’ fit attributes into a dict.

Return type:

Dict[str, float]

fractopo.analysis.length_distributions.apply_cut_off(lengths, ccm, cut_off=1e-18)

Apply cut-off to length data and associated ccm.

>>> lengths = np.array([2, 4, 8, 16, 32])
>>> ccm = np.array([1. , 0.8, 0.6, 0.4, 0.2])
>>> cut_off = 4.5
:rtype: :sphinx_autodoc_typehints_type:`\:py\:data\:\`\~typing.Tuple\`\\ \\\[\:py\:class\:\`\~numpy.ndarray\`\, \:py\:class\:\`\~numpy.ndarray\`\]`
>>> apply_cut_off(lengths, ccm, cut_off)
(array([ 8, 16, 32]), array([0.6, 0.4, 0.2]))
fractopo.analysis.length_distributions.calculate_exponent(alpha)

Calculate exponent from powerlaw.alpha.

fractopo.analysis.length_distributions.calculate_fitted_values(log_lengths, m_value, constant)

Calculate fitted values of y.

Return type:

ndarray

fractopo.analysis.length_distributions.cut_off_proportion_of_data(fit, length_array)

Get the proportion of data cut off by powerlaw cut off.

If no fit is passed the cut off is the one used in automatic_fit.

Return type:

float

fractopo.analysis.length_distributions.describe_powerlaw_fit(fit, length_array, label=None)

Compose dict of fit powerlaw attributes and comparisons between fits.

Return type:

Dict[str, float]

fractopo.analysis.length_distributions.distribution_compare_dict(fit)

Compose a dict of length distribution fit comparisons.

Return type:

Dict[str, float]

fractopo.analysis.length_distributions.fit_to_multi_scale_lengths(ccm, lengths, fitter=<function numpy_polyfit>, scorer=<function mean_squared_log_error>)

Fit np.polyfit to multiscale length distributions.

Returns the fitted values, exponent and constant of fit within a Polyfit instance.

Return type:

Polyfit

fractopo.analysis.length_distributions.numpy_polyfit(log_lengths, log_ccm)

Fit numpy polyfit to data.

Return type:

Tuple[float, float]

fractopo.analysis.length_distributions.optimize_cut_offs(cut_offs, distributions, fitter, scorer, *_)

Optimize multiscale fit.

Requirements for the optimization function are that the function must take one argument of 1-d array and return a single float. It can take static arguments (distributions, fitter).

Return type:

float

fractopo.analysis.length_distributions.plot_distribution_fits(length_array, label, using_branches, use_probability_density_function, cut_off=None, fit=None, fig=None, ax=None, fits_to_plot=(Dist.EXPONENTIAL, Dist.LOGNORMAL, Dist.POWERLAW), plain=False)

Plot length distribution and powerlaw fits.

If a powerlaw.Fit is not given it will be automatically determined (using the optionally given cut_off).

Return type:

Tuple[Optional[Fit], Figure, Axes]

fractopo.analysis.length_distributions.plot_fit_on_ax(ax, fit, fit_distribution, use_probability_density_function)

Plot powerlaw model to ax.

Return type:

None

fractopo.analysis.length_distributions.plot_multi_distributions_and_fit(truncated_length_array_all, ccm_array_normed_all, full_length_array_all, full_ccm_array_normed_all, cut_offs, names, polyfit, using_branches, plot_truncated_data)

Plot multi-scale length distribution.

Return type:

Tuple[Figure, Axes]

fractopo.analysis.length_distributions.scikit_linear_regression(log_lengths, log_ccm)

Fit using scikit LinearRegression.

Return type:

Tuple[float, float]

fractopo.analysis.length_distributions.setup_ax_for_ld(ax, using_branches, indiv_fit, use_probability_density_function, plain=False)

Configure ax for length distribution plots.

Parameters:
  • ax (Axes) – Ax to setup.

  • using_branches (bool) – Are the lines in the axis branches or traces.

  • indiv_fit (bool) – Is the plot single-scale or multi-scale.

  • use_probability_density_function (bool) – Whether to use complementary cumulative distribution function

  • plain (bool) – Should the stylizing be kept to a minimum.

fractopo.analysis.length_distributions.setup_length_dist_legend(ax)

Set up legend for length distribution plots.

Used for both single and multi distribution plots.

fractopo.analysis.length_distributions.sort_and_log_lengths_and_ccm(lengths, ccm)

Preprocess lengths and ccm.

Sorts them and calculates their natural logarithmic.

Return type:

Tuple[ndarray, ndarray]

fractopo.analysis.length_distributions.sorted_lengths_and_ccm(lengths, area_value)

Get (normalized) complementary cumulative number array.

Give area_value as None to not normalize.

>>> lengths = np.array([2, 4, 8, 16, 32])
>>> area_value = 10.0
:rtype: :sphinx_autodoc_typehints_type:`\:py\:data\:\`\~typing.Tuple\`\\ \\\[\:py\:class\:\`\~numpy.ndarray\`\, \:py\:class\:\`\~numpy.ndarray\`\]`
>>> sorted_lengths_and_ccm(lengths, area_value)
(array([ 2,  4,  8, 16, 32]), array([0.1 , 0.08, 0.06, 0.04, 0.02]))
>>> lengths = np.array([2, 4, 8, 16, 32])
>>> area_value = None
>>> sorted_lengths_and_ccm(lengths, area_value)
(array([ 2,  4,  8, 16, 32]), array([1. , 0.8, 0.6, 0.4, 0.2]))