Density.centered#

Density.centered(cutoff=0)[source]#

Shifts the data center of mass to the center of the data array using linear interpolation. The box size of the returned Density object is at least equal to the box size of the class instance.

Parameters:
cutofffloat, optional

Only elements in data larger than cutoff will be considered for computing the new box. By default considers only positive elements.

Returns:
Density

A centered copy of the class instance.

NDArray

The offset between array center and center of mass.

Notes

Should any axis of the class instance data array be smaller than the return value of Density.minimum_enclosing_box(), the size of the internal data array is adapted to avoid array elements larger than cutoff to fall outside the data array.

Examples

Density.centered() returns a tuple containing a centered version of the current Density instance, as well as an array with translations. The translation corresponds to the shift between the original and current center of mass.

>>> import numpy as np
>>> from tme import Density
>>> dens = Density(np.ones((5,5,5)))
>>> centered_dens, translation = dens.centered(0)
>>> translation
array([0., 0., 0.])

Density.centered() extended the Density.data attribute of the current Density instance and modified Density.origin accordingly.

>>> centered_dens
Origin: (-2.0, -2.0, -2.0), sampling_rate: (1, 1, 1), Shape: (9, 9, 9)

Density.centered() achieves centering via zero-padding and rigid-transform of the internal Density.data attribute. centered_dens is sufficiently large to represent all rotations of the Density.data attribute, such as ones obtained from tme.matching_utils.get_rotation_matrices().

>>> from tme.matching_utils import get_rotation_matrices
>>> rotation_matrix = get_rotation_matrices(dim = 3 ,angular_sampling = 10)[0]
>>> rotated_centered_dens = centered_dens.rigid_transform(
>>>     rotation_matrix = rotation_matrix,
>>>     order = None
>>> )
>>> print(centered_dens.data.sum(), rotated_centered_dens.data.sum())
125.0 125.0