slmsuite.hardware.slms.screenmirrored.ScreenMirrored
- class ScreenMirrored(display_number, bitdepth=8, verbose=True, **kwargs)[source]
Bases:
SLM
Wraps a
pyglet
window for displaying data to an SLM.Caution
This class currently supports SLMs with 8-bit precision or less. In the future, this class will support 16-bit SLMs using RG color.
Important
Many SLM manufacturers provide an SDK for interfacing with their devices. Using a python wrapper for these SDKs is recommended, instead of or in supplement to this class, as there often is functionality additional to a mirrored screen (e.g. USB for changing settings) along with device-specific optimizations.
Note
There are a variety of python packages that support blitting images onto a fullscreen display.
Simple DirectMedia Layer (SDL) wrappers:
pygame
(link), which also supports OpenGL. Only supports one screen.sdl2
(readthedocs) through thePySDL2
package. Requires additional libraries.
Open Graphics Library (OpenGL) wrappers:
moderngl
(readthedocs), an OpenGL wrapper focusing on a pythonic interface for core OpenGL functions.OpenGL
(link) through thePyOpenGL
/PyOpenGL_accelerate
package, a very light OpenGL wrapper.pyglet
(readthedocs), a light OpenGL wrapper.
GUI Library wrappers:
gi
(readthedocs), through thePyGObject
package wrappingGTK
and other GUI libraries.pyqt6
(link), through thePyQt6
package wrapping the version 6Qt
GUI library.tkinter
(link), included in standardpython
, wrapping theTcl
/Tk
GUI library.wx
(link), through thewxPython
package wrapping thewxWidgets
GUI library.slmpy
(GitHub) useswx
.
slmsuite
usespyglet
as the default display package.pyglet
is generally more capable than the mentioned SDL wrappers while immediately supporting features such as detecting connected displays which low-level packages likeOpenGL
andmoderngl
do not have.pyglet
allows us to interact more directly with the display hardware without the additional overhead that is found in GUI libraries. Most importantly,pyglet
is well documented.However, it might be worthwhile in the future to look back into SDL options, as SDL surfaces are closer to the pixels than OpenGL textures, so greater speed might be achievable (even without loading data to the GPU as a texture). Another potential improvement could come from writing
cupy
datastructures toOpenGL
textures directly, without using the CPU as an intermediary. There is some precedent for transferring data fromCUDA
(on whichcupy
is based) toOpenGL
, thoughcupy
does not currently directly support this.Important
ScreenMirrored
uses a double-buffered and vertically synchronized (vsync)OpenGL
context. This is to prevent “tearing” resulting from data being modified during a display write: rather, all monitor writes are synchronized such that clean frames are always displayed. This feature is similar to theisImageLock
flag inslmpy
, but is implemented a bit closer to the hardware.- window
Fullscreen window used to send information to the SLM.
- Type
pyglet.window.Window
- tex_shape_ratio
Ratio between the SLM shape and the (power-2-padded) texture stored in
OpenGL
.- Type
(int, int)
- buffer
Memory used to load data to the
OpenGL
memory. Of typenp.uint8
.- Type
numpy.ndarray
- cbuffer
Array of length
prod(shape) * 4
(4 bytes per RGBA). Maps to the same memory asbuffer
. Used to load data to the texture.- Type
pyglet.gl.GLubyte
- texture
Identifier for the texture loaded into
OpenGL
memory.- Type
pyglet.gl.GLuint
Methods
Closes frame.
Get information about the available displays, their indexes, and their sizes.
Loads vendor-provided phase correction from file, setting
phase_correction
.Fourier transforms the wavefront calibration's measured amplitude to find the expected diffraction-limited perfomance of the system in
"knm"
space.Sets
measured_amplitude
used for hologram generation in the absence of a proper wavefront calibration.Approximates the expected radius of farfield spots in the
"kxy"
basis based on the near-field amplitude distributionmeasured_amplitude
.Checks, cleans, and adds to data, then sends the data to the SLM and potentially waits for settle.
- __init__(display_number, bitdepth=8, verbose=True, **kwargs)[source]
Initializes a
pyglet
window for displaying data to an SLM.Caution
slmsuite
makes use of user-supplied knowledge of SLM pixel size:SLM.dx_um
,SLM.dy_um
. Be sure these values are correct.Caution
An SLM designed at 1064 nm can be used for an application at 780 nm by passing
wav_um=.780
andwav_design_um=1.064
, thus causing the SLM to use only a fraction (780/1064) of the full dynamic range. Be sure these values are correct. Note that there are some performance losses from using this modality (seewrite()
).Caution
There is some subtlety to complex display setups with Linux. Working outside the default display is currently not implemented.
- Parameters
display_number (int) – Monitor number for frame to be instantiated upon.
verbose (bool) – Whether or not to print extra information.
**kwargs – See
SLM.__init__()
for permissible options.
References
- 14
- static info(verbose=True)[source]
Get information about the available displays, their indexes, and their sizes.
- Parameters
verbose (bool) – Whether or not to print display information.
- Returns
The number and geometry of each display.
- Return type
list of (int, (int, int, int, int)) tuples
- load_vendor_phase_correction(file_path)[source]
Loads vendor-provided phase correction from file, setting
phase_correction
. By default, this is interpreted as an image file and is padded or unpadded to the shape of the SLM. Subclasses should implement vendor-specific routines for loading and interpreting the file (e.g.Santec
loads a .csv).- Parameters
file_path (str) – File path for the vendor-provided phase correction.
- Returns
phase_correction
, the vendor-provided phase correction.- Return type
numpy.ndarray
- point_spread_function_knm(padded_shape=None)[source]
Fourier transforms the wavefront calibration’s measured amplitude to find the expected diffraction-limited perfomance of the system in
"knm"
space.- Parameters
padded_shape ((int, int) OR None) – The point spread function changes in resolution depending on the padding. Use this variable to provide this padding. If
None
, do not pad.- Returns
The point spread function of shape
padded_shape
.- Return type
numpy.ndarray
- set_measured_amplitude_analytic(radius, units='norm')[source]
Sets
measured_amplitude
used for hologram generation in the absence of a proper wavefront calibration.FourierSLM
includes capabilities for wavefront calibration viawavefront_calibrate()
. This process also measures the amplitude of the source on the SLM and stores this inmeasured_amplitude
.measured_amplitude
is used for better refinement of holograms during numerical optimization. If one does not have a camera to use forwavefront_calibrate()
, this method allows the user to set an approximation of the source amplitude based on an assumed \(1/e\) amplitude (\(1/e^2\) power) Gaussian beam radius.- Parameters
radius (float) – Radius in normalized units to assume for the source Gaussian beam.
units (str in {"norm", "nm", "um", "mm", "m"}) – Units for the given radius.
- Returns
- Return type
numpy.ndarray
- spot_radius_kxy()[source]
Approximates the expected radius of farfield spots in the
"kxy"
basis based on the near-field amplitude distributionmeasured_amplitude
.- Returns
Average radius of the farfield spot.
- Return type
float
- write(phase, phase_correct=True, settle=False)[source]
Checks, cleans, and adds to data, then sends the data to the SLM and potentially waits for settle. This method calls the SLM-specific private method
_write_hw()
which transfers the data to the SLM.Warning
Subclasses implementing vendor-specific software should not overwrite this method. Subclasses should overwrite
_write_hw()
instead.Caution
The sign on
phase
is flipped before converting to integer data. This is to convert between the ‘increasing value ==> increasing voltage (= decreasing phase delay)’ convention in most SLMs andslmsuite
’s ‘increasing value ==> increasing phase delay’ convention. As a result, zero phase will appear entirely white (255 for an 8-bit SLM), and increasing phase will darken the displayed pattern. If integer data is passed, this data is displayed directly and the sign is not flipped.Important
The user does not need to wrap (e.g.
numpy.mod(data, 2*numpy.pi)
) the passed phase data, unless they are pre-caching data for speed (see below).write()
uses optimized routines to wrap the phase (see the private method_phase2gray()
). Which routine is used depends onphase_scaling
:phase_scaling
is one.Fast bitwise integer modulo is used. Much faster than the other routines which depend on
numpy.mod()
.phase_scaling
is less than one.In this case, the SLM has more phase tuning range than necessary. If the data is within the SLM range
[0, 2*pi/phase_scaling]
, then the data is passed directly. Otherwise, the data is wrapped by \(2\pi\) using the very slownumpy.mod()
. Try to avoid this in applications where speed is important.phase_scaling
is more than one.In this case, the SLM has less phase tuning range than necessary. Processed the same way as the
phase_scaling
is less than one case, with the important exception that phases (after wrapping) between2*pi/phase_scaling
and2*pi
are set to zero. For instance, a sawtooth blaze would be truncated at the tips.
Caution
After scale conversion, data is
floor()
ed to integers withnp.copyto
, rather than rounded to the nearest integer (np.around()
equivalent). While this is irrelevant for the average user, it may be significant in some cases. If this behavior is undesired consider either:write()
integer data directly or modifying the behavior of the private method_phase2gray()
in a pull request. We have not been able to find an example ofnp.copyto
producing undesired behavior, but will change this if such behavior is found.- Parameters
phase (numpy.ndarray or None) –
Phase data to display in units of \(2\pi\), unless the passed data is of integer type and the data is applied directly.
If
None
is passed towrite()
, data is zeroed.If the array has a larger shape than the SLM shape, then the data is cropped to size in a centered manner (
unpad
).If integer data is passed with the same type as
display
(np.uint8
for <=8-bit SLMs,np.uint16
otherwise), then this data is directly passed to the SLM, without going through the “phase delay to grayscale” conversion defined in the private method_phase2gray()
. In this situation,phase_correct
is ignored. This is error-checked such that bits with greater significance than the bitdepth of the SLM are zero (e.g. the final 6 bits of 16 bit data for a 10-bit SLM). Integer data with type different fromdisplay
leads to a TypeError.
Usually, an exact stored copy of the data passed by the user under
phase
is stored in the attributephase
. However, in cases wherephase_scaling
not one, this copy is modified to include how the data was wrapped. If the data was cropped, then the cropped data is stored, etc. If integer data was passed, the equivalent floating point phase is computed and stored in the attributephase
.phase_correct (bool) – Whether or not to add
phase_correction
tophase
.settle (bool) – Whether to sleep for
settle_time_s
.
- Returns
display
, the integer data sent to the SLM.- Return type
numpy.ndarray
- Raises
TypeError – If integer data is incompatible with the bitdepth or if the passed phase is otherwise incompatible (not a 2D array or smaller than the SLM shape, etc).