Approximation module#

Approximation subpackage for napari-stress.

class napari_stress.approximation.EllipsoidExpander#

Expand a set of points to fit an ellipsoid using least squares fitting.

The ellipsoid equation is of the form:

\[Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gx + Hy + Iz = 1\]

where A, B, C, D, E, F, G, H, I are the coefficients of the ellipsoid equation and x, y, z are the coordinates of the points. The parameters of this equation are fitted to the input points using least squares fitting.

fit(points: 'napari.types.PointsData')#

Fit an ellipsoid to a set of points using leaast square fitting.

expand(points: 'napari.types.PointsData')#

Project a set of points onto their respective position on the fitted ellipsoid.

fit_expand(points: 'napari.types.PointsData')#

Fit an ellipsoid to a set of points and then expand them.

Examples

# Instantiate and fit an ellipsoid expander to a set of points
expander  = EllipsoidExpander()
expander.fit(points)

# Expand the points on the fitted ellipsoid
fitted_points = expander.expand(points)
property axes_#

The lengths of the axes of the ellipsoid.

Returns:

axes_ – The lengths of the axes of the ellipsoid.

Return type:

np.ndarray

property center_#

The center of the ellipsoid.

Returns:

center_ – The center of the ellipsoid.

Return type:

np.ndarray

property coefficients_#

The coefficients of the fitted ellipsoid

Returns:

coefficients_ – The coefficients of the ellipsoid equation. The coefficients are of the form (3, 2, 3); The first dimension represents the three axes of the ellipsoid (major, medial and minor). The second dimension represents the components of the ellipsoid vectors (base point and direction vector). The third dimension represents the dimension of the space (z, y, x).

Return type:

napari.types.VectorsData

property properties#

Get properties of the expansion.

Returns:

properties – Dictionary containing properties of the expansion with following keys:

  • residuals: np.ndarray

    Residual euclidian distance between input points and expanded points.

  • maximum_mean_curvature: float

    Maximum mean curvature of the ellipsoid.

  • minimum_mean_curvature: float

    Minimum mean curvature of the ellipsoid.

The maximum and minimum curvatures \(H_{max}\) and \(H_{min}\) are calculated as follows:

\[ \begin{align}\begin{aligned}H_{max} = 1 / (2 * a^2) + 1 / (2 * b^2)\\H_{min} = 1 / (2 * b^2) + 1 / (2 * a^2)\end{aligned}\end{align} \]

where a, b are the largest and smallest axes of the ellipsoid, respectively.

Return type:

dict

class napari_stress.approximation.SphericalHarmonicsExpander(max_degree: int = 5, expansion_type: str = 'cartesian', normalize_spectrum: bool = True)#

Expand a set of points using spherical harmonics.

The spherical harmonics expansion is performed using the following equation:

\[f(\theta, \phi) = \sum_{l=0}^{L} \sum_{m=-l}^{l} a_{lm} Y_{lm}(\theta, \phi)\]

where \(a_{lm}\) are the spherical harmonics coefficients and \(Y_{lm}(\theta, \phi)\) are the spherical harmonics functions. The expansion is performed in the spherical coordinate system.

Parameters:
  • max_degree (int) – Maximum degree of spherical harmonics expansion.

  • expansion_type (str) – Type of expansion to perform. Can be either ‘cartesian’ or ‘radial’.

  • normalize_spectrum (bool) – Normalize power spectrum sum to 1.

fit(points: 'napari.types.PointsData')#

Fit spherical harmonics to input data. If expansion_type is ‘cartesian’, the input points are converted to ellipsoidal coordinates (latitude/longitude) before fitting and each coordinate (z, y, z) is fitted separately. Hence, the derived coefficients are of shape (3, max_degree + 1, max_degree + 1). If expansion_type is ‘radial’, the input points are converted to radial coordinates ($math:rho, theta, phi$) before fitting. Only the radial coordinate is fitted, hence the derived coefficients are of shape `(1, max_degree + 1, max_degree + 1).

expand(points: 'napari.types.PointsData')#

Expand input points using spherical harmonics.

fit_expand(points: 'napari.types.PointsData')#

Fit spherical harmonics to input data and then expand them.

property coefficients_#

The coefficients of the spherical harmonics expansion.

Returns:

coefficients_ – The coefficients of the spherical harmonics expansion. The shape of the coefficients depends on the type of expansion. If the expansion type is ‘cartesian’, the coefficients are of shape (3, max_degree + 1, max_degree + 1). If the expansion type is ‘radial’, the coefficients are of shape (1, max_degree + 1, max_degree + 1).

Return type:

np.ndarray

property properties#

Get properties of the expansion.

Returns:

properties – Dictionary containing properties of the expansion with following keys:

  • residuals: np.ndarray

    Residual euclidian distance between input points and expanded points.

  • power_spectrum: np.ndarray

    Power spectrum of spherical harmonics coefficients. If ‘normalize_spectrum’ is set to True, the power spectrum is normalized to sum to 1.

    The spectrum \(P_l\) is calculated as follows:

    \[P_l = \sum_{m=-l}^{l} |a_{lm}|^2\]

    where \(a_{lm}\) are the spherical harmonics coefficients.

Return type:

dict

napari_stress.approximation.expand_points_on_ellipse(fitted_ellipsoid: VectorsData, pointcloud: PointsData) PointsData#

Expand a pointcloud on the surface of a fitted ellipse.

This function takes a ellipsoid (in the form of the major axes) and a pointcloud from which the ellipsoid was derived. The coordinates of the points in the pointcloud are then transformed to their corresponding locations on the surface of the fitted ellipsoid.

Parameters:
  • fitted_ellipsoid (VectorsData)

  • pointcloud (PointsData)

Return type:

PointsData

Deprecated since version 0.3.3: This will be removed in 0.4.0. Use approximateion.EllipseExpander instead.

napari_stress.approximation.expand_points_on_fitted_ellipsoid(points: napari.types.PointsData) napari.types.PointsData#

Project a set of points on a fitted ellipsoid.

Parameters:

points (napari.types.PointsData) – The points to project.

Returns:

projected_points – The projected points.

Return type:

napari.types.PointsData

napari_stress.approximation.expand_spherical_harmonics(points: napari.types.PointsData, max_degree: int = 5) napari.types.LayerDataTuple#

Expand points using spherical harmonics.

Parameters:
  • points (np.ndarray) – Points to be expanded.

  • max_degree (int) – Maximum degree of spherical harmonics expansion.

Returns:

expanded_points – Expanded points.

Return type:

np.ndarray

napari_stress.approximation.least_squares_ellipsoid(points: PointsData) VectorsData#

Fit ellipsoid to points with a last-squares approach.

This function takes a pointcloud and fits an ellipsoid to it using a least-squares approach. The ellipsoid is returned as a set of vectors representing the major and minor axes of the ellipsoid.

Parameters:

points (PointsData)

Returns:

VectorsData

Return type:

Major/minor axis of the ellipsoid

Deprecated since version 0.3.3: This will be removed in 0.4.0. Use approximateion.EllipseExpander instead.

napari_stress.approximation.normals_on_ellipsoid(points: PointsData) VectorsData#

Fits an ellipsoid and calculates the normals vectors.

This function takes a pointcloud and calculates the normals on the ellipsoid fitted to the pointcloud.

Parameters:

points (PointsData)

Returns:

VectorsData

Return type:

Normals on the ellipsoid