pysdic.ZernikePolynomialsCache#

The Zernike polynomials \(Z_{n,m}(\rho, \theta)\) of a pysdic.ZernikeDistortion only depend on the points and on the ellipse of definition \((R_x, R_y, x_0, y_0)\), not on the coefficients. When a distortion is applied several times to the same points with different coefficients, the polynomials can be computed once and reused with a ZernikePolynomialsCache, attached to the distortion with the property polynomials_cache.

ZernikePolynomialsCache class#

class ZernikePolynomialsCache(max_entries=2)[source]#

Bases: object

A cache of the values (and derivatives) of the Zernike polynomials at sets of points, to share between pysdic.ZernikeDistortion objects (see their polynomials_cache).

The Zernike polynomials \(Z_{n,m}(\rho, \theta)\) only depend on the points and on the ellipse of definition \((R_x, R_y, x_0, y_0)\), not on the coefficients of the distortion. When a distortion is applied several times to the same points (e.g. the iterations of a calibration where only the distortion is optimized), the polynomials are computed once and reused. Several distortions can share the same cache, for example the distortions of different orders evaluated at the same points.

An entry is identified by the points (hash and exact comparison with a copy of the points) and the ellipse (exact comparison of \(R_x, R_y, x_0, y_0\)):

  • The polynomials are stored up to the highest order computed: an entry of order \(N\) also serves the orders \(N' \leq N\) (the first polynomials in the OSA/ANSI order).

  • The derivatives \(\partial Z / \partial x\) and \(\partial Z / \partial y\) are stored only when they are requested (Jacobian dx of the distortion): an entry without derivatives is computed again (and replaced) when they are needed.

  • The entries are kept in the order of their last use: when the cache is full, the least recently used entry is removed.

Note

The cache is only used by pysdic.ZernikeDistortion.apply(). The undistortion (invert) evaluates the polynomials at points that change at each Newton iteration: it never uses the cache.

Important

The memory of an entry is about \(8 N_p (2 + D P)\) bytes for \(N_p\) points and \(P\) polynomials, with \(D = 1\) (values) or \(D = 3\) (with derivatives), e.g. about 115 MB for 627 264 points and \(P = 21\) (order 5) without derivatives. Call clear() (or remove the cache from the distortions) to free it.

Parameters:

max_entries (Integral, optional (default: 2)) – The maximum number of entries (sets of points) kept, at least 1. For a stereo pair whose two views share the same distortion, use at least 2 (one entry per view).

Examples

 1from pysdic import ZernikeDistortion, ZernikePolynomialsCache
 2
 3cache = ZernikePolynomialsCache(max_entries=2)
 4distortion = ZernikeDistortion(n_zernike=5, radius_x=1.5, polynomials_cache=cache)
 5distortion.set_zero()
 6
 7distortion.apply(points)  # The polynomials are computed and stored
 8distortion.parameters = new_parameters
 9distortion.apply(points)  # The polynomials are reused
10
11distortion.polynomials_cache = None  # No cache anymore
12cache.clear()  # Free the memory

Usage#

The typical use is the calibration of a distortion alone: the extrinsic does not change, so the normalized points of a view are the same at each iteration, only the coefficients are optimized. In a measurement, the points move at each iteration: the cache is useless.

import pysdic

cache = pysdic.ZernikePolynomialsCache(max_entries=2)  # One entry per view (L and R share the distortion)
distortion.polynomials_cache = cache                   # Or ZernikeDistortion(..., polynomials_cache=cache)

...                                                    # Optimization of the coefficients of the distortion

print(cache)                                           # n_entries, nbytes, hits, misses
distortion.polynomials_cache = None                    # No cache anymore
cache.clear()                                          # Free the memory

The rules of the cache:

  • No cache by default: the polynomials are computed at each call unless a cache is set explicitly. The cache never changes the results.

  • Shareable: several distortions (e.g. of different orders, with the same ellipse) can use the same cache. It is not serialized and not copied (a copy of the distortion has no cache).

  • Identification of an entry: the points (hash and exact comparison with a stored copy) and the ellipse (exact comparison). An entry of order \(N\) also serves the orders \(N' \leq N\); the derivatives \(\partial Z / \partial x\) and \(\partial Z / \partial y\) are stored only when requested (Jacobian dx).

  • LRU: at most max_entries entries are kept, the least recently used one is removed first. max_entries can be changed at any time.

  • Only for apply: the undistortion (invert) evaluates the polynomials at points that change at each Newton iteration and never uses the cache.

Important

The memory of an entry is about \(8 N_p (2 + D P)\) bytes for \(N_p\) points and \(P\) polynomials, with \(D = 1\) (values) or \(D = 3\) (with the derivatives), e.g. about 115 MB for 627 264 points at the order 5 without derivatives. Call ZernikePolynomialsCache.clear() (or remove the cache from the distortions) when it is not needed anymore.

Implemented methods and attributes#

ZernikePolynomialsCache.max_entries

[Get/Set] The maximum number of entries (at least 1).

ZernikePolynomialsCache.n_entries

[Get] The number of entries in the cache.

ZernikePolynomialsCache.nbytes

[Get] The memory used by the entries, in bytes.

ZernikePolynomialsCache.hits

[Get] The number of evaluations served by the cache since its creation (or the last clear()).

ZernikePolynomialsCache.misses

[Get] The number of evaluations that computed the polynomials since its creation (or the last clear()).

ZernikePolynomialsCache.clear()

Remove all the entries (and reset the numbers of hits and misses).

See also

ZernikeDistortion, the distortion using the cache.