Density.adjust_box#

Density.adjust_box(box, pad_kwargs={})[source]#

Adjusts Density.data and Density.origin according to the provided box.

Parameters:
boxtuple of slices

Description of how each axis of Density.data should be sliced.

pad_kwargs: dict, optional

Parameter dictionary passed to numpy.pad.

Examples

The following demonstrates the ability of Density.adjust_box() to extract a subdensity from the current Density instance. Density.adjust_box() not only operats on Density.data, but also modifies Density.origin according to box.

>>> import numpy as np
>>> from tme import Density
>>> dens = Density(np.ones((5, 5)))
>>> box = (slice(1, 4), slice(2, 5))
>>> dens.adjust_box(box)
>>> dens
Origin: (1.0, 2.0), sampling_rate: (1, 1), Shape: (3, 3)

Density.adjust_box() can also extend the box of the current Density instance. This is achieved by negative start or stops that exceed the dimension of the current Density.data array.

>>> box = (slice(-1, 10), slice(2, 10))
>>> dens.adjust_box(box)
>>> dens
Origin: (0.0, 4.0), sampling_rate: (1, 1), Shape: (11, 8)

However, do note that only the start coordinate of each slice in box can be negative.

>>> box = (slice(-1, 10), slice(2, -10))
>>> dens.adjust_box(box)
>>> dens
Origin: (-1.0, 6.0), sampling_rate: (1, 1), Shape: (11, 0)