Density.trim_box#

Density.trim_box(cutoff, margin=0)[source]#

Computes a rectangle with sufficient dimension that encloses all values of the internal data array larger than the specified cutoff, expanded by the specified margin.

The output can be passed to Density.adjust_box() to crop the internal data array.

Parameters:
cutofffloat

The threshold value for determining the minimum enclosing box. Default is 0.

marginint, optional

The margin to add to the box dimensions. Default is 0.

Returns:
tuple

A tuple containing slice objects representing the box.

Raises:
ValueError

If the cutoff is larger than or equal to the maximum density value.

Examples

The following will compute the bounding box that encloses all values in the example array that are larger than zero:

>>> import numpy as np
>>> from tme import Density
>>> dens = Density(np.array([0,1,1,1,0]))
>>> dens.trim_box(0)
(slice(1, 4, None),)

The resulting tuple can be passed to Density.adjust_box() to trim the current Density instance:

>>> dens.adjust_box(dens.trim_box(0))
>>> dens.data.shape
(3,)