Coverage for /Users/estefania/python_playground/membrane-curvature/membrane_curvature/curvature.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1r"""
2.. role:: raw-math(raw) :format: latex html
4--------------------
5Curvature
6--------------------
8In MembraneCurvature, we calculate Gaussian and mean curvature from a cloud of points.
10Gaussian curvature is defined by
12.. math:: K = \frac{\partial_{xx}\partial_{yy}-\partial_{xy}^2}
13 {(1+\partial_x^2+\partial_y^2)^2}.
15Mean curvature is defined by
17.. math:: H =
18 \frac{(1+\partial_x^2)\partial_{yy}+(1+\partial_y^2)\partial_{xx}-2\partial_x\partial_y\partial_{xy}}
19 {2(1+\partial_x^2+\partial_y^2)^{3/2}}.
22Notes
23---------
25Since the mean curvature calculates the arithmetic mean of two
26principal curvatures, the default units of :math:`H` are Å\ :sup:`-1`.
27On the other hand, Gaussian curvature calculates the geometric mean of the
28two principal curvatures. Therefore, the default units of :math:`K` are Å\ :sup:`-2`.
29In general, units of mean curvature are [length] :sup:`-1`,
30and units of Gaussian curvature are [length] :sup:`-2`.
32.. warning::
34 Numpy cannot calculate the gradient for arrays with inner array of
35 `length==1` unless `axis=0` is specified. Therefore in the functions here included
36 for mean and Gaussian curvature, shape of arrays must be at least (2,2).
37 In general, to calculate a numerical gradients shape of arrays must be >=(`edge_order` +
38 1).
41Functions
42---------
44"""
46import numpy as np
49def gaussian_curvature(Z):
50 """
51 Calculate gaussian curvature from Z cloud points.
54 Parameters
55 ----------
56 Z: np.ndarray.
57 Multidimensional array of shape (n,n).
60 Returns
61 -------
62 K : np.ndarray.
63 The result of gaussian curvature of Z. Returns multidimensional
64 array object with values of gaussian curvature of shape `(n, n)`.
66 """
68 Zy, Zx = np.gradient(Z)
69 Zxy, Zxx = np.gradient(Zx)
70 Zyy, _ = np.gradient(Zy)
72 K = (Zxx * Zyy - (Zxy ** 2)) / (1 + (Zx ** 2) + (Zy ** 2)) ** 2
74 return K
77def mean_curvature(Z):
78 """
79 Calculates mean curvature from Z cloud points.
82 Parameters
83 ----------
84 Z: np.ndarray.
85 Multidimensional array of shape (n,n).
88 Returns
89 -------
90 H : np.ndarray.
91 The result of gaussian curvature of Z. Returns multidimensional
92 array object with values of gaussian curvature of shape `(n, n)`.
94 """
96 Zy, Zx = np.gradient(Z)
97 Zxy, Zxx = np.gradient(Zx)
98 Zyy, _ = np.gradient(Zy)
100 H = (1 + Zx**2) * Zyy + (1 + Zy**2) * Zxx - 2 * Zx * Zy * Zxy
101 H = H / (2 * (1 + Zx**2 + Zy**2)**(1.5))
103 return H