Fourier Surface

Added in version 2.0.0.

With surface_method='fourier', MembraneCurvature derives a surface by fitting a truncated 2D Fourier series to the \(z\) coordinates of the reference atoms. The truncation is set by the maximum mode indices along \(x\) and \(y\) with fourier_m and fourier_n.

The fitted parameters are the Fourier coefficients: the mean term \(A_{00}\), and a cosine and a sine amplitude, \(A_{mn}\) and \(B_{mn}\), for each retained mode. MembraneCurvature evaluates the basis functions at the atom positions to build a design matrix, with one row per atom and one column per coefficient, and solves the resulting linear least squares system for the coefficients.

The fitted series is evaluated at the centre of every bin on a regular n_x_bins x n_y_bins grid to form the height field. Because a Fourier series is a continuous function, every bin gets a value regardless of how many atoms fall inside it, hence avoiding NaN values. Mean and Gaussian curvature are then calculated on the same grid.

Note

The Fourier method computes partial derivatives analytically from the fitted series. Unlike the binning methods, it does not rely on finite differences and therefore avoids artifacts at the edges of the box.

Warning

Use fourier_m = fourier_n = 2, the default values, unless you have a clear reason to resolve shorter wavelengths.

For more details on the Fourier method, refer to the 2.1 Fourier method section in the Algorithm page.

References

The Fourier method in MembraneCurvature uses the Fourier surface modeling [CAG2009] and molecular Fourier shape descriptors [JMG1988] as conceptual references:

[CAG2009]

Shen et al., Fourier method for large-scale surface modeling and registration, Computers & Graphics (2009), doi: 10.1016/j.cag.2009.03.002.

[JMG1988]

Leicester et al., Description of molecular surface shape using Fourier descriptors, Journal of Molecular Graphics (1988), doi: 10.1016/0263-7855(88)85008-2.

Functions

membrane_curvature.fourier_surface._bin_centre_mesh(Lx: float, Ly: float, n_x_bins: int, n_y_bins: int) → tuple[ndarray, ndarray][source]

Relative-coordinate mesh at bin centres.

Bin centres at (i + 1/2) L / n_bins for each axis, compatible with the periodic domain [0, L).

Parameters:
  • Lx (float) – Period length in \(x\).

  • Ly (float) – Period length in \(y\).

  • n_x_bins (int) – Number of bins along \(x\).

  • n_y_bins (int) – Number of bins along \(y\).

Returns:

  • X_rel (ndarray) – Mesh of relative \(x\) coordinates, shape (n_x_bins, n_y_bins).

  • Y_rel (ndarray) – Mesh of relative \(y\) coordinates, same shape as X_rel.

Raises:

ValueError – If n_x_bins or n_y_bins is not a positive integer. The public entry points already validate this with validate_positive_bin_counts().

membrane_curvature.fourier_surface._build_fourier_matrix(x_positions: ndarray, y_positions: ndarray, box_length_x: float, box_length_y: float, fourier_modes: list[tuple[int, int]]) → ndarray[source]

Build the least-squares design matrix from the Fourier basis functions.

Each row is an atom position \((x_i, y_i)\) and each column is a basis function evaluated at that position: a constant column for \(A_{00}\), then a \(\cos(k \cdot r)\) and \(\sin(k \cdot r)\) pair per mode, with wavevectors \(k = (2\pi m / L_x, 2\pi n / L_y)\).

Parameters:
  • x_positions (ndarray) – \(x\) coordinates of points within one periodic box (same shape as y_positions).

  • y_positions (ndarray) – \(y\) coordinates of points within one periodic box.

  • box_length_x (float) – Period length \(L_x\).

  • box_length_y (float) – Period length \(L_y\).

  • fourier_modes (list of tuple of int) – Non-redundant (m, n) indices, e.g. from fourier_mode_list().

Returns:

design_matrix – Basis evaluated at each point. dtype is numpy.float64.

Return type:

ndarray, shape (n_points, 1 + 2 * n_modes)

Raises:

ValueError – If x_positions and y_positions do not have the same shape.

membrane_curvature.fourier_surface._compute_wavevector(mode_x, mode_y, kx_base, ky_base)[source]

Wavevector components for a single mode.

Builds \((k_x, k_y) = (m\, k_{x,\mathrm{base}}, n\, k_{y,\mathrm{base}})\) used in the phase \(k_x x + k_y y\) for that mode.

Parameters:
  • mode_x (int) – Mode index \(m\).

  • mode_y (int) – Mode index \(n\).

  • kx_base (float) – Reciprocal step \(2\pi / L_x\).

  • ky_base (float) – Reciprocal step \(2\pi / L_y\).

Returns:

  • kx (float) – mode_x * kx_base.

  • ky (float) – mode_y * ky_base.

membrane_curvature.fourier_surface._eval_fourier_surface(X_rel: ndarray, Y_rel: ndarray, Lx: float, Ly: float, A00: float, coeffs: dict[tuple[int, int], tuple[float, float]], *, derivatives: Literal[False]) → tuple[ndarray][source]
membrane_curvature.fourier_surface._eval_fourier_surface(X_rel: ndarray, Y_rel: ndarray, Lx: float, Ly: float, A00: float, coeffs: dict[tuple[int, int], tuple[float, float]], *, derivatives: Literal[True]) → tuple[ndarray, ndarray, ndarray, ndarray, ndarray, ndarray]

Evaluate the fitted Fourier sum on a relative-coordinate grid.

Reconstructs \(Z(x,y) = A_{00} + \sum_{(m,n)} A_{mn}\cos\phi_{mn} + B_{mn}\sin\phi_{mn}\) with \(\phi_{mn} = k_x x + k_y y\). When derivatives is True, also accumulates the analytic first and second partial derivatives of the same sum, used for Monge-gauge curvature.

Parameters:
  • X_rel (ndarray) – Relative \(x\) coordinates on the periodic domain (same shape as Y_rel).

  • Y_rel (ndarray) – Relative \(y\) coordinates on the periodic domain.

  • Lx (float) – Period length in \(x\) (same units as X_rel).

  • Ly (float) – Period length in \(y\).

  • A00 (float) – Mean term \(A_{00}\).

  • coeffs (dict[tuple[int, int], tuple[float, float]]) – Mode amplitudes (A, B) from _fourier_fit_from_atoms() / _unpack_coefficients().

  • derivatives (bool) – If False, return (Z,). If True, return (Z, fx, fy, fxx, fyy, fxy).

Returns:

If derivatives is False, the 1-tuple (Z,) where Z matches the shape of X_rel. If derivatives is True, the 6-tuple with each grid matching X_rel.

Return type:

tuple of ndarray

Notes

The derivatives=False branch returns (Z,), a 1-tuple and not a bare ndarray, to keep a single return type (tuple[ndarray, ...]) regardless of the flag. Callers therefore unwrap with [0].

membrane_curvature.fourier_surface._fourier_fit_from_atoms(positions: ndarray, x_range: tuple[float, float], y_range: tuple[float, float], M: int, N: int, rcond: float | None = None) → tuple[float, float, float, dict[tuple[int, int], tuple[float, float]], ndarray][source]

Fit Fourier coefficients to atom heights, without evaluating on a grid.

Builds the design matrix, solves for \(A_{00}\) and the mode amplitudes, and returns the domain sizes and coefficients needed to evaluate the surface on a grid.

Parameters:
  • positions (ndarray, shape (n_atoms, 3)) – Atom coordinates in the same length unit as the box. The third column (positions[:, 2]) is the height \(z\).

  • x_range, y_range (tuple of float) – (min, max) domain extents.

  • M, N (int) – Maximum Fourier mode indices (see fourier_mode_list()).

  • rcond (float or None, optional) – Relative cutoff for small singular values in _solve_design_least_squares_svd().

Returns:

  • Lx (float) – Period length \(L_x = x_{\max} - x_{\min}\).

  • Ly (float) – Period length \(L_y = y_{\max} - y_{\min}\).

  • A00 (float) – Mean height coefficient \(A_{00}\).

  • coeffs (dict[tuple[int, int], tuple[float, float]]) – Mapping (m, n) -> (A, B) cosine and sine coefficients per mode.

  • theta (ndarray, shape (n_coefficients,)) – Coefficient vector (index 0 is \(A_{00}\), then cosine/sine pairs in fourier_mode_list() order).

Raises:

ValueError – If positions cannot be converted to a float64 array or has the wrong shape, if x_range or y_range have non-positive width, or if M or N are negative.

Warns:

UserWarning – If the least-squares system is rank-deficient or underdetermined.

membrane_curvature.fourier_surface._harmonic_height_and_phi_derivatives(A: float, B: float, cos_phase: ndarray, sin_phase: ndarray) → tuple[ndarray, ndarray, ndarray][source]

Height and \(\phi\)-derivatives for one mode \(h(\phi)=A\cos\phi+B\sin\phi\).

Derivatives are taken with respect to the phase \(\phi\), not with respect to \(x\) and \(y\). _eval_fourier_surface() turns them into spatial derivatives with the chain rule, multiplying by \(k_x\) and \(k_y\).

Parameters:
  • A, B (float) – Cosine and sine coefficients for this mode.

  • cos_phase (ndarray) – \(\cos\phi\), same shape as sin_phase.

  • sin_phase (ndarray) – \(\sin\phi\).

Returns:

  • h (ndarray) – Contribution \(A\cos\phi + B\sin\phi\).

  • h_phi (ndarray) – First derivative \(\partial h/\partial \phi\).

  • h_phiphi (ndarray) – Second derivative \(\partial^2 h/\partial \phi^2\).

membrane_curvature.fourier_surface._solve_design_least_squares_svd(design_matrix: ndarray, targets: ndarray, rcond: float | None = None) → tuple[ndarray, int, ndarray][source]

Least-squares solution with numpy.linalg.svd() and singular-value truncation.

Solves min ||design_matrix @ theta - targets||_2 with a relative singular-value cutoff rcond (values \(s \le rcond \cdot s_{\max}\) are treated as zero). When the system is rank-deficient, returns the minimum Euclidean-norm coefficient vector among least-squares minimizers.

Parameters:
  • design_matrix (ndarray, shape (n_rows, n_columns)) – Design matrix with rows corresponding to atom positions and columns corresponding to the basis functions.

  • targets (ndarray, shape (n_rows,)) – Target heights.

  • rcond (float or None, optional) – Relative cutoff for small singular values. None uses a heuristic cutoff based on machine precision and the problem size.

Returns:

  • theta (ndarray, shape (n_columns,)) – Fitted coefficients, one per column of the design matrix. The number of columns is the number of Fourier parameters.

  • rank (int) – Number of singular values above the cutoff.

  • singular_values (ndarray) – Singular values of design_matrix.

Raises:

ValueError – If targets does not have shape (n_rows,).

membrane_curvature.fourier_surface._unpack_coefficients(theta: ndarray, modes: list[tuple[int, int]]) → tuple[float, dict[tuple[int, int], tuple[float, float]]][source]

Split the least-squares coefficient vector into the mean and per-mode amplitudes.

Index 0 is \(A_{00}\). Remaining entries are cosine/sine pairs in modes order.

Parameters:
  • theta (ndarray, shape (n_coefficients,)) – Least-squares solution vector: mean at index 0, then pairs (A_{mn}, B_{mn}) in the same order as modes.

  • modes (list of tuple of int) – Mode list from fourier_mode_list() (or equivalent ordering).

Returns:

  • A00 (float) – Constant (mean) term.

  • coeffs (dict) – Mapping (m, n) -> (A, B) cosine and sine coefficients per mode.

membrane_curvature.fourier_surface.fourier_height_derivatives_from_atoms(positions: ndarray, x_range: tuple[float, float], y_range: tuple[float, float], n_x_bins: int, n_y_bins: int, M: int, N: int, rcond: float | None = None) → tuple[ndarray, ndarray, ndarray, ndarray, ndarray, ndarray][source]

Fourier height and analytic partial derivatives on bin centres.

Same pipeline as fourier_height_from_atoms(), and also evaluates the analytic first and second partial derivatives of the fitted series.

Parameters:
  • positions (ndarray, shape (n_atoms, 3)) – Atom coordinates. The third column (positions[:, 2]) is the height \(z\).

  • x_range, y_range (tuple of float) – (min, max) domain extents.

  • n_x_bins, n_y_bins (int) – Output grid size at bin centres.

  • M, N (int) – Maximum Fourier mode indices, see fourier_mode_list().

  • rcond (float or None, optional) – Relative cutoff for small singular values in _solve_design_least_squares_svd().

Returns:

Z, fx, fy, fxx, fyy, fxy – Each array has shape (n_x_bins, n_y_bins).

Return type:

ndarray

Raises:

ValueError – If n_x_bins or n_y_bins is not a positive integer, if positions cannot be converted to a float64 array or has the wrong shape, if x_range or y_range have non-positive width, or if M or N are negative.

Warns:

UserWarning – If the least-squares system is rank-deficient or underdetermined.

membrane_curvature.fourier_surface.fourier_height_from_atoms(positions: ndarray, x_range: tuple[float, float], y_range: tuple[float, float], n_x_bins: int, n_y_bins: int, M: int, N: int, rcond: float | None = None) → ndarray[source]

Fourier height field on bin centres.

Fits the Fourier coefficients to the atom heights with _fourier_fit_from_atoms(), then evaluates the fitted series at bin centres.

Parameters:
  • positions (ndarray, shape (n_atoms, 3)) – Atom coordinates in the same length unit as the box. The third column (positions[:, 2]) is the height \(z\).

  • x_range, y_range (tuple of float) – (min, max) domain extents.

  • n_x_bins, n_y_bins (int) – Output grid size at bin centres.

  • M, N (int) – Maximum Fourier mode indices, see fourier_mode_list().

  • rcond (float or None, optional) – Relative cutoff for small singular values in _solve_design_least_squares_svd().

Returns:

Z – Fitted surface height.

Return type:

ndarray, shape (n_x_bins, n_y_bins)

Raises:

ValueError – If n_x_bins or n_y_bins is not a positive integer, if positions cannot be converted to a float64 array or has the wrong shape, if x_range or y_range have non-positive width, or if M or N are negative.

Warns:

UserWarning – If the least-squares system is rank-deficient or underdetermined.

membrane_curvature.fourier_surface.fourier_mode_list(M: int, N: int) → list[tuple[int, int]][source]

Non-redundant 2D Fourier modes for a real-valued function on a periodic domain.

Modes are \((m, n)\) where:

  • \(m=1..M\), \(n=-N..N\) (all n retained, no symmetry to exploit)

  • \(m=0\), \(n=1..N\) only (\(F(0,-n) = F(0,n)^*\) makes n<0 redundant)

The \((0,0)\) mean is excluded and handled separately as \(A_{00}\).

Parameters:
  • M (int) – Maximum positive mode index in \(x\).

  • N (int) – Maximum mode index magnitude in \(y\).

Returns:

modes – Non-redundant (m, n) indices, excluding (0, 0).

Return type:

list of tuple of int

Raises:

ValueError – If M or N is negative.

membrane_curvature.fourier_surface.n_fourier_parameters(M: int, N: int) → int[source]

Total number of scalar Fourier parameters for the truncated series.

Uses the mode list from fourier_mode_list(): one scalar parameter for the mean term \(A_{00}\) and two parameters per non-zero mode (cosine and sine coefficients). The total number of parameters is n = 1 + 2 * len(fourier_mode_list(M, N)).

Parameters:
  • M (int) – Maximum positive mode index in \(x\) (see fourier_mode_list()).

  • N (int) – Maximum mode index magnitude in \(y\).

Returns:

n – Total number of scalar Fourier parameters 1 + 2 * n_modes.

Return type:

int