{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Convolvables\n", "\n", "Prysm features a rich implemention of Linear Shift Invariant (LSI) system theory. Under this mathematical ideal, the transfer function is the product of the Fourier transform of a cascade of components, and the spatial distribution of intensity is the convolution of a cascade of components. These features are usually used to blur objects or images with Point Spread Functions (PSFs), or model the transfer function of an opto-electronic system. Within prysm there is a class `Convolvable` which objects and PSFs inherit from. You should rarely need to use the base class, except when subclassing it with your own models or objects or loading a source image.\n", "\n", "The built-in convolvable objects are Slits, Pinholes, Tilted Squares, and Siemens Stars. There are also two components, PixelAperture and OLPF, used for system modeling." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from prysm import Convolvable, Slit, Pinhole, TiltedSquare, SiemensStar, PixelAperture, OLPF" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = Slit(width=1, orientation='crossed') # diameter, um\n", "p = Pinhole(width=1)\n", "t = TiltedSquare(angle=8, background='white', sample_spacing=0.05, samples=256) # degrees\n", "star = SiemensStar(spokes=32, sinusoidal=False, background='white', sample_spacing=0.05, samples=256)\n", "pa = PixelAperture(width_x=5) # diameter, um\n", "ol = OLPF(width_x=5*0.66)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Objects that take a background parameter will be black-on-white for background=white, or white-on-black for background=black. Two objects are convolved via the `conv` method, which returns self on a new Convolvable instance and is chainable," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# monstrosity = s.conv(p).conv(t).conv(star).conv(pa).conv(ol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some models require sample spacing and samples parameters while others do not. This is because prysm has many methods of executing an FFT-based Fourier domain convolution under the hood. If an object has a known analytical Fourier transform, the class has an `analytic_ft` method which has abscissa units of reciprocal microns. If the analytic FT is present, it is used in lieu of numerical data. Models that have analytical Fourier transforms also accept sample_spacing and samples parameters, which are used to define a grid in the spatial domain. If two objects with analytical Fourier transforms are convolved, the output grid will have the finer sample spacing of the two inputs, and the larger span or window width of the two inputs.\n", "\n", "The Convolvable constructor takes only four parameters," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "x = y = np.linspace(-20,20,256)\n", "z = np.random.uniform(size=256**2).reshape(256,256)\n", "c = Convolvable(data=z, x=x, y=y, has_analytic_ft=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`has_analytic_ft` has a default value of `False`.\n", "\n", "Minimal labor is required to subclass Convolvable. For example, the Pinhole implemention is simply:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# need from prysm import mathops as m if you want to actually use this\n", "\n", "class Pinhole(Convolvable):\n", " def __init__(self, width, sample_spacing=0.025, samples=0):\n", " self.width = width\n", "\n", " # produce coordinate arrays\n", " if samples > 0:\n", " ext = samples / 2 * sample_spacing\n", " x, y = m.linspace(-ext, ext, samples), m.linspace(-ext, ext, samples)\n", " xv, yv = m.meshgrid(x, y)\n", " w = width / 2\n", " # paint a circle on a black background\n", " arr = m.zeros((samples, samples))\n", " arr[m.sqrt(xv**2 + yv**2) < w] = 1\n", " else:\n", " arr, x, y = None, m.zeros(2), m.zeros(2)\n", "\n", " super().__init__(data=arr, x=x, y=y, has_analytic_ft=True)\n", "\n", " def analytic_ft(self, x, y):\n", " xq, yq = m.meshgrid(x, y)\n", " # factor of pi corrects for jinc being modulo pi\n", " # factor of 2 converts radius to diameter\n", " rho = m.sqrt(xq**2 + yq**2) * self.width * 2 * m.pi\n", " return m.jinc(rho).astype(config.precision)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is less than 20 lines long.\n", "\n", "Convolvable objects have a few convenience properties and methods. `slice_x` and its y variant exist and behave the same as slices on subclasses of `OpticalPhase` such as `Pupil` and `Interferogram`. `plot_slice_xy` also works the same way. `shape` is a convenience wrapper for `.data.shape`, and `support_x`, `support_y`, and `support` mimic the equivalent diameter properties on `OpticalPhase`.\n", "\n", "`show` and `show_fourier` behave the same way as `plot2d` methods found throughout `prysm`, except there are xlim and ylim parameters, which may be either single values, taken to be symmetric axis limits, or length-2 iterables of lower and upper limits.\n", "\n", "Finally, `Convolvable` objects may be initialized from images," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from prysm import sample_files\n", "c = Convolvable.from_file(sample_files('boat.png'), scale=5) # plate scale in um -- 5microns/pixel\n", "c.data /= 255 # when initializing from files, normalization is left to the user\n", "c.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and written out as 8 or 16-bit images," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = 'foo.png' # or jpg, any format imageio can handle\n", "c.save(p, nbits=16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In practical use, one will generally only use the conv, from_file, and save methods with any degree of regularity. The complete API documentation is below. Attention should be paid to the docstring of conv, as it describes some of the details associated with convolutions in prysm, their accuracy, and when they are used." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(c.conv)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }