<jupyter_start><jupyter_text>ZernikesPrysm supports two flavors of Zernike polynomials; the Fringe set up to the 49th term, and the Noll set up to the 36th term. They have identical interfaces, so only one will be shown here.Zernike notations are a subclass of [Pupil](./Pupils.ipynb), so they support the same arguments to `__init__`,<jupyter_code>%matplotlib inline
from prysm import FringeZernike, NollZernike
p = FringeZernike(samples=123, dia=456.7, wavelength=1.0, opd_unit='nm', mask='dodecagon')<jupyter_output><empty_output><jupyter_text>There are additional keyword arguments for each term, and the base (0 or 1) can be supplied. With base 0, the terms start at Z0 and range to Z48. With base 1, they start at Z1 and range to Z49 (or Z48, for Standard Zernikes). The Fringe set can also be used with unit variance via the `norm` keyword argument. Both notations also have nice print statements,<jupyter_code>p2 = FringeZernike(Z4=1, Z9=1, Z48=1, base=0, norm=True)
p2<jupyter_output><empty_output><jupyter_text>Notice that the RMS value is equal to sqrt(1^2 + 1^2 + 1^2) = sqrt(3) = 1.732 ~= 1.722. The difference of ~1% is due to the array sizes used by prysm by default, if increased, e.g. by adding `samples=1204` to the constructor, the value would converge to the analytical one.A Zernike pupil can also be initalized with an iterable (list, tuple...) of coefficients,<jupyter_code>import numpy as np
terms = np.random.rand(49)  # 49 zernike coefficients, e.g. from a wavefront sensor
fz3 = FringeZernike(terms)<jupyter_output><empty_output><jupyter_text>Zernike instances have a `truncate` method which discards terms with indices higher than n. For example,<jupyter_code>fz3.truncate(16)<jupyter_output><empty_output><jupyter_text>this is less efficient, however, than simply slicing the coefficient vector,<jupyter_code>fz4 = FringeZernike(terms[:16])<jupyter_output><empty_output><jupyter_text>and this slicing alternative should be used when one is sensitive to performance.The top few terms may be extracted,<jupyter_code>fz4.top_n(5)<jupyter_output><empty_output><jupyter_text>or the terms listed by their pairwise magnitudes and clocking angles,<jupyter_code>fz4.magnitudes<jupyter_output><empty_output><jupyter_text>These things may be (bar) plotted;<jupyter_code>fz4.barplot(orientation='h', buffer=1, zorder=3)
fz4.barplot_magnitudes(orientation='h', buffer=1, zorder=3)
fz4.barplot_topn(n=5, orientation='h', buffer=1, zorder=3)<jupyter_output><empty_output><jupyter_text>`orientation` controls the axis on which the terms are enumerated. `h` results in vertical bars, `v` is also accepted, as are horizontal and vertical. `buffer` is the number of terms’ worth of spaces left on each side. The default of 1 leaves a one bar margin. `zorder` is passed to matplotlib – the default of 3 places the bars above any gridlines, which is an aesthetic choice. Matplotlib has a general default of 1.If you would like direct access to the underlying functions, there are two paths. `prysm._zernike` contains functions for the first 49 (Fringe ordered) Zernike polynomials, for example<jupyter_code>from prysm._zernike import defocus<jupyter_output><empty_output><jupyter_text>each of these takes arguments of (rho, phi). They have names which end with _x, or _00 and _45 for the terms which have that naming convention. Zernike functions are cached by prysm's internal routines,<jupyter_code>from prysm.zernike import zcache
# 8 is the index into prysm.zernike.zernikes, which loosely follows the Fringe ordering
zcache(8, norm=True, samples=128)
zcache.clear()  # you should never have to do this unless you want to release memory<jupyter_output><empty_output><jupyter_text>This cache instance is used internally by prysm, if you modify the returned arrays in-place, you probably won’t like the result. You can create your own independent instance,<jupyter_code>from prysm.zernike import ZCache
my_zcache = ZCache()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08074690890739339, 'fraction_numerical': 0.023467070401211203, 'mean_word_length': 6.340740740740741, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '238493', 'license': 'MIT', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1251, 'n_words': 474, 'path': 'docs/source/user_guide/Zernikes.ipynb', 'repo_name': 'samkberry/prysm', 'size': '3963'}