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---------
24Numpy cannot calculate the gradient for arrays with inner array of
25`length==1` unless `axis=0` is specified. Therefore in the functions here included
26for mean and Gaussian curvature, shape of arrays must be at least (2,2).
27In general, to calculate a numerical gradients shape of arrays must be >=(`edge_order` +
281).
31Functions
32---------
34"""
36import numpy as np
39def gaussian_curvature(Z):
40 """
41 Calculate gaussian curvature from Z cloud points.
44 Parameters
45 ----------
46 Z: np.ndarray.
47 Multidimensional array of shape (n,n).
50 Returns
51 -------
52 K : np.ndarray.
53 The result of gaussian curvature of Z. Returns multidimensional
54 array object with values of gaussian curvature of shape `(n, n)`.
56 """
58 Zy, Zx = np.gradient(Z)
59 Zxy, Zxx = np.gradient(Zx)
60 Zyy, _ = np.gradient(Zy)
62 K = (Zxx * Zyy - (Zxy ** 2)) / (1 + (Zx ** 2) + (Zy ** 2)) ** 2
64 return K
67def mean_curvature(Z):
68 """
69 Calculates mean curvature from Z cloud points.
72 Parameters
73 ----------
74 Z: np.ndarray.
75 Multidimensional array of shape (n,n).
78 Returns
79 -------
80 H : np.ndarray.
81 The result of gaussian curvature of Z. Returns multidimensional
82 array object with values of gaussian curvature of shape `(n, n)`.
84 """
86 Zy, Zx = np.gradient(Z)
87 Zxy, Zxx = np.gradient(Zx)
88 Zyy, _ = np.gradient(Zy)
90 H = (Zx**2 + 1) * Zyy - 2 * Zx * Zy * Zxy + (Zy**2 + 1) * Zxx
91 H = -H / (2 * (Zx**2 + Zy**2 + 1)**(1.5))
93 return H