pyMOE package
Submodules
pyMOE.aperture module
mask.py
Definition of Class Aperture
- class pyMOE.aperture.Aperture(x, y)
Bases:
object- Class Aperture:
Creates an Aperture object that is an homogenous array of values corresponding to the transfer function matrix across the aperture
- Args:
- x:
Vector for the x axis
- y:
Vector for the y axis
- Methods:
- aperture:
returns the aperture
- shape:
returns the shape of the aperture
- discretize(levels)
Discretizes the aperture to the number of levels
- property shape
- class pyMOE.aperture.ApertureField(x, y)
Bases:
object- Class Aperture:
Creates an Aperture object that is an homogenous array of complex values corresponding to the transfer function matrix across the aperture
- Args:
- x:
Vector for the x axis
- y:
Vector for the y axis
- Methods:
- aperture:
returns the complex aperture array
- amplitude:
sets or returns the amplitude of the aperture array
- phase:
sets or returns the phase of the aperture array
- unwrap:
retursn the unwrapped phase
- shape:
returns the shape of the aperture
- property amplitude
- property phase
- property shape
- property unwrap
pyMOE.dither module
dither.py Module for dithering of masks/images
- pyMOE.dither.dither_img(input_img, output_filename, plotting=False)
Returns a make dithered image from input_img, save to output_img.
- Args:
- inputcv2:
input image as a 2D grid (use cv2 img)
- output_filename:
filename of image to be written
- plotting:
binary value, if True shows the plot, defaults to False
- pyMOE.dither.floyd_steinberg(input_img, plot=False)
Applies Floyd-Steinberg dithering algorithm to input image.
- Args:
- input_img:
input image as a 2D grid (use cv2 img)
- plot:
binary value, if True shows the plot, defaults to False
pyMOE.export module
export.py Module containing several functions to export masks to gds.
- pyMOE.export.grayim2gds(infile, outfile, pixelx, pixely, cellname, level, layer=0, datatype=0, verbose=False)
(void) Transforms one image (converted to grayscale) into a gds, using cv2
- Args:
- infile:
input IMAGE file (on extension that cv2 can read ), e.g. “image.png”
- outfile:
output GDS file, e.g. “image.gds”
- pixelx:
pixel size in x, in um
- pixely:
pixel size in y, in um
- cellname:
string with cellname, e.g. “TOP”
- level:
int from 0 to 255, pixels gray value to be passed to GDS
- layer:
(optional) gray level, defaults to 0
- datatype:
(optional) gds datatype, defaults to 0
- verbose:
(optional) defaults to False, if True prints
infilxe = “image.png” outfilxe = “image.gds” pixelx = 1 #um pixely = 1 #um grayim2gds(infilxe, outfilxe, pixelx, pixely,”TOP”, 0)
- pyMOE.export.grayim2gds_writer(infile, outfile, pixelx, pixely, cellname, level, layer=0, datatype=0, verbose=False)
(void) Transforms one image (converted to grayscale) into a gds, using cv2 by default adds the image to (layer, datatype) = (0,0)
- Args:
- infile:
input IMAGE file (on extension that cv2 can read ), e.g. “image.png”
- outfile:
output GDS file, e.g. “image.gds”
- pixelx:
pixel size in x, in um
- pixely:
pixel size in y, in um
- cellname:
string with cellname, e.g. “TOP”
- level:
int from 0 to 255 (0 = black, 255=white) , pixels gray value to be passed to GDS
- layer:
(optional) gray level, defaults to 0
- datatype:
(optional) gds datatype, defaults to 0
- verbose:
(optional) defaults to False, if True prints
infilxe = “image.png” outfilxe = “image.gds” cellname = “TOP” #name of the gds cell graycolor = 0 #black pixels pixelx = 1 #um pixely = 1 #um
grayim2gds_writer(infilxe, outfilxe, pixelx, pixely,cellname, graycolor, verbose=True)
- pyMOE.export.grayim2gds_writer_frac(infile, outfile, pixelx, pixely, cellname, level, nm=None, layer=0, datatype=0, verbose=False)
(void) Transforms one image (converted to grayscale) into a gds, using cv2
By default adds the image to (layer, datatype) = (0,0)
- Args:
- infile:
input IMAGE file (on extension that cv2 can read ), e.g. “image.png”
- outfile:
output GDS file, e.g. “image.gds”
- pixelx:
pixel size in x, in um
- pixely:
pixel size in y, in um
- cellname:
string with cellname, e.g. “TOP”
- level:
int from 0 to 255 (0 = black, 255=white) , pixels gray value to be passed to GDS
- nm:
(optional) If nm is given fractionates the image, nr of pixels for each fractioned part (should be multiple of pixel sizes), defaults to None
- layer:
(optional) gray level, defaults to 0
- datatype:
(optional) gds datatype, defaults to 0
- verbose:
(optional) defaults to False, if True prints
infilxe = “image.png” outfilxe = “image.gds” pixelx = 1 #um pixely = 1 #um cellname = “TOP” #name of the gds cell graycolor = 0 #black pixels frac = 250 #size of frac pixels in the image
grayim2gds_writer_frac(infilxe, outfilxe, pixelx, pixely, cellname, graycolor, frac, verbose=True)
- pyMOE.export.grayim2gds_writer_klops(infile, output_filename, pixelx, pixely, cellname, level, layer=0, datatype=0, verbose=False)
(void) Transforms one image (converted to grayscale) into a gds, using cv2 for reading the image by default adds the image to (layer, datatype) = (0,0)
- Args:
- infile:
input IMAGE file (on extension that cv2 can read ), e.g. “image.png”
- output_filename:
intermediate GDS file
- pixelx:
pixel size in x, in um
- pixely:
pixel size in y, in um
- cellname:
string with cellname, e.g. “TOP”
- level:
int from 0 to 255 (0 = black, 255=white) , pixels gray value to be passed to GDS
- layer:
(optional) gray level, defaults to 0
- datatype:
(optional) gds datatype, defaults to 0
- verbose:
(optional) defaults to False, if True prints
pyMOE.gds_klops module
gds_klops.py Module containing several functions for operations with/within mask files (e.g. gds)
These functions might require pya, gdspy, gdstk or gdshelpers libraries. Functions using gdspy and/or pya are exemplified within the notebooks.
####IMPORTANT: these functions work on the klayout-0.26.11 version ####There might be some inconsistencies for later versions of pya library
- pyMOE.gds_klops.cell_wpol(cs, cellname)
Transforms contourf into GDS2 using gdshelpers ‘cs’ = contours FROM matplotlif contourf function ‘cellname’ = string cellname, e.g. ‘TOP’ Returns: cell with polygon, multipolygon array
- pyMOE.gds_klops.cell_wpol_gdspy(cs, cellname, prec=1e-06, mpoints=1000000000.0)
Cell made with cut polygons from the z profile
- Args:
- cs:
contours FROM matplotlif contourf function
- cellname:
string cellname, e.g. ‘TOP’
- Returns:
[0] gdspy library [1] cell with polygons
##By default the levels start at 0 and go to the number of levels in the contourplot
- pyMOE.gds_klops.cell_wpol_gdspy_fast(cs, cellname, prec=1e-06, mpoints=1000000000.0)
Cell made with cut polygons from the z profile
- Args:
- cs:
contours FROM matplotlif contourf function
- cellname:
string cellname, e.g. ‘TOP’
- Returns:
[0] gdspy library [1] cell with polygons
##By default the levels start at 0 and go to the number of levels in the contourplot
- pyMOE.gds_klops.cell_wpol_gdstk(cs, cellname)
Transforms contourf into GDS2 using gdstk
- Args:
- cs:
contours FROM matplotlif contourf function
- cellname:
string cellname, e.g. ‘TOP’
- Returns:
[0] gdstk library [1] cell with polygons
- pyMOE.gds_klops.change_layers(fstgds_filename, fst_cellname, layerspol, new_layers, output_filename)
(void) Transforms layers from the source layer into the destination #by default considers datatypes are int(0), set datatypes to 0 function can be used before
- Args:
- fstgds_filename:
string filename of gds to read
- fst_cellname:
string name of cell in the gds
- layerspol:
array of the layers of the gds file
- new_layers:
array of destination layers - MUST HAVE THE SAME CORRESPONDENCE
- output_filename:
string filename of output gds
- pyMOE.gds_klops.correct_gds(inputfilename_gds, outputfilename_dxf)
(void) corrects gds to be able to read/write gds files
- pyMOE.gds_klops.diffs_layers_arrays(readfile, cellname, layerspol1, datatypes1, layerspol2, datatypes2, outfile)
(void) Sequentially makes the difference from one layer to the other, from layerspol1 to layerspol2
- Args:
- readfile:
string filename of input gds
- cellname:
string name of cell
- layerspol1:
numpy array with all layer that will be substracted
- datatypes1:
numpy array with all datatypes that will be subtracted
- layerspol2:
numpy array with all layer where we will subtract
- datatypes2:
numpy array with all datatypes where we will substract
- outfile:
string filename of output gds
- pyMOE.gds_klops.gds_to_dxf(inputfilename_gds, outputfilename_dxf)
(void) writes to file (it is named to be dxf… other extensions also work)
- pyMOE.gds_klops.import_gds(fstgds_filename, fst_cellname, fst_layer_nr, fst_datatype_nr, sndgds_filename, snd_cellname, snd_layer_nr, snd_datatype_nr, output_filename, clear_gds=True)
(void) Imports shapes from fst gds file INTO snd gds file
- Args:
- Ngds_filename:
string filename of N(=fst,snd) gds
- N_cellname:
string name of cell in N(=fst,snd) gds
- N_layer_nr:
int layer number in N(=fst,snd) gds
- N_datatype_nr:
int datatype number in N(=fst,snd) gds
- output_filename:
string filename of output gds
- clear_gds:
clear gds, before inserting shapes, defaults to True
- pyMOE.gds_klops.inspect_gds2layers_gdstk(filename)
Get all layer polygons from gds filename =’filename’
- Returns:
[0] gstk lib object with all information of cell, layers and shapes [1] pol_dict, dictionary of gdstk Polygons {(layer_nr, datatype_nr): [Polygon1, Polygon2,…]} [2] nmpy array with (layer_nr, datatype_nr) [3] list with (layer_nr, datatype_nr)
- pyMOE.gds_klops.instance_array(cell_name, input_filename, transx, transy, nr_inst_X, nr_inst_Y, pitx, pity, output_filename)
(void) Makes array of existing object(s) in the gds
- Args:
- cell_name:
string name of cell
- input_filename:
string name of the input gds
- transx:
translation vector in x in um
- transy:
translation vector in y in um
- nr_inst_X:
int number of repeated objects in x
- nr_inst_Y:
int number of repeated objects in y
- pitx:
int pitch in x in um
- pity:
int pitch in y in um
- output_filename:
string filename of output gds
- pyMOE.gds_klops.merge_layer(readfile, cellname, layer_nr, datatype_nr, outputfile)
(void) Merges all shapes within gds (only tested gds with single layer)
- Args:
- readfile:
string filename of input gds
- cellname:
string name of cell
- layer_nr:
int layer number
- datatype_nr:
int datatype number
- outputfile:
string filename of output gds
- pyMOE.gds_klops.rescale_layout(readfile, cellname, factor, outfile, divfactor=1, newcellname=None, verbose=True)
(void) Rescales the layout
- Args:
- readfile:
string filename of input gds
- cellname:
string name of cell in the readfile
- factor:
int with scaling factor multiply
- outfile:
string filename of output gds
- divfactor:
int with scaling factor division, defaults to 1
- newcellname:
string name of the cell in the outfile, defaults to cellname
- pyMOE.gds_klops.reset_datatypes(fstgds_filename, fst_cellname, fst_layer_nr, fst_datatype_nr, snd_layer_nr, snd_datatype_nr, output_filename)
(void) resets datatypes
- Args:
- Ngds_filename:
string filename of N(=fst,snd) gds
- N_cellname:
string name of cell in N(=fst,snd) gds
- N_layer_nr:
int layer number in N(=fst,snd) gds
- N_datatype_nr:
int datatype number in N(=fst,snd) gds
- output_filename:
string filename of output gds
- pyMOE.gds_klops.rotate_layout(readfile, cellname, angle, outputfile, transx=0, transy=0)
rotate layout by angle, inspired in https://www.klayout.de/forum/discussion/2143/import-a-gds-then-make-into-a-cell-to-rotate
translation with vector (transx, transy), default = (0,0)
pyMOE.gdsconverter module
gdsconverter.py GDS converter module
- class pyMOE.gdsconverter.GDSMask(mask, units=1e-06, precision=1e-09, verbose=True)
Bases:
object- Class GDSMask:
Creates a class to integrate the GDS library and layout corresponding to a mask aperture. Receives an aperture and provides methods to calculate the corresponding GDS layout
- Args:
- mask:
aperture object
- Methods:
- levels:
number of unique discretized levels in mask
- layers:
list of layers in layout
- cells:
list of cells in layout
- total_polygons:
total number of polygons in layout in all layers
- total_vertices:
total number of vertices in all polygons in all layers
- create_layout():
Creates layout (raster or vector or edges etc todo)
- merge(layer):
merges polygons in layer
- plot():
plots mask
- viewer():
Opens LayoutViewer of gdspy
- save_gds(filename):
saves layout to gds file
- property aperture
- create_layout(mode='raster', cellname='TOP', merge=False, break_vertices=250)
Creates GDS layout of the discretized aperture
- Args:
- mode:
default Raster. (can also accept contour)
- cellname:
name of the topcell to include all the merged polygons
- merge:
default False. If True, will merge the individual pixel polygons
- break_vertices:
threshold value to speed up the merging of polygons
- Returns:
- gdslib:
l ibrary with topcell will update the class internal gdslib
- property levels
- property total_polygons
- property total_vertices
- view_layout()
Runs gdspy.LayoutViewer
- write_gds(filename, cells=None, timestamp=None, binary_cells=None)
Writes layout to gds file using gdspy library
- pyMOE.gdsconverter.cell_wpol_gdspy(cs, cellname, prec=1e-06, mpoints=1000000000.0)
###Initially coming from gds_klops ###TODO: migrate functions from the gds_klops (implemented with various libraries) to gdsp y
Cell made with cut polygons from the z profile
- Args:
- cs:
contours FROM matplotlif contourf function
- cellname:
string cellname, e.g. ‘TOP’
- Returns:
- [0]:
gdspy library,
- [1]:
cell with polygons
##By default the levels start at 0 and go to the number of levels in the contourplot
- pyMOE.gdsconverter.change_layers_gdspy(fstgds_filename, new_cellname, layerspol, new_layers, output_filename)
Transforms layers from the source layer (layerpol) into the destination (new_layers) By default considers datatypes are int(0), set datatypes to 0 function can be used before Assumes that we have the polygons in the top level of the input gds
- Args:
- fstgds_filename:
string filename of gds to read
- new_cellname:
string name of cell in the gds
- layerspol:
array of the layers of the gds file, if it is not the same, leaves the absent layers untouched
- new_layers:
array of destination layers - MUST HAVE THE SAME CORRESPONDENCE
- output_filename:
string filename of output gds
- pyMOE.gdsconverter.count_vertices(pols)
Counts vertices of polygons
- Args:
- pols:
polygon or polygon set
- Returns:
number of vertices
- pyMOE.gdsconverter.merge_polygons(polygons, layer=0, assume_non_overlap=True, break_vertices=250, verbose=True)
Merge polygons function receives a list of polygons or polygon set and will iteratively merge consecutive polygons, assuming to be in the same layer. If we assume that polygons are sequentially located in the matrix, then it is expected that consecutive polygons can be merged, while polygons far apart are probably not in the same cluster.
The boolean operation compares the new polygon with the existing operand, and it becomes slower with more polyons/vertices already included in the merged set. To optimize this, we consider a break_vertices threshold where the merged set of polygons is broken into a separate list to continue the merging. This will result in a merged set that could be smaller, but is much faster to execute, scaling with N instead of N^2.
- Args:
- polygons:
must be a list of polygons, polygonset, rectangles etc that the boolean operation accepts
- layer:
default 0 and ignored. The merged list of merged polygons will have the same layer as the input polygons (assumed the same for all)
- assume_non_overlap:
default True, if True the function will break the merged list of polygons when reaching break_vertices
- break_vertices:
approximate number of vertices to consider in the boolean operation before breaking the list.
- verbose:
default True. Prints the progress bar.
- Returns:
list of merged polygons or polygonsets.
pyMOE.generate module
generate.py Module containing several functions to construct arbitrary aperture masks
- pyMOE.generate.aperture_add(aperture1, aperture2)
Adds two apertures
- pyMOE.generate.aperture_multiply(aperture1, aperture2)
Multiply two apertures
- pyMOE.generate.aperture_operation(aperture1, aperture2, operand)
Executes the operation on the apertures 1 and 2. Both apertures must have the same spatial distribution and shape.
- Args:
- aperture1:
First Aperture
- aperture2:
Second Aperture
- operand:
numpy operand function to consider
- Returns:
- aperture:
Aperture with result of operation
- pyMOE.generate.aperture_rotate(aperture_in, angle, pivot=None, background=0)
Rotates image around the pivot point, using the scipy ndimage (default applies spline interpolation + rotation of 2D array) https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.rotate.html
- Args:
- aperture_in:
input aperture object
- angle:
rotation angle in degrees
- pivot:
coordinates of pivot point (center of rotation) in the img 2D array, defaults to center of aperture
- background:
background value where rotation is done, by default 0
- Returns:
Rotated 2D array, with padding
- pyMOE.generate.aperture_subtract(aperture1, aperture2)
Subtracts two apertures
- pyMOE.generate.arbitrary_aperture_function(aperture, function, center=(0, 0), **function_args)
Updates aperture and returns phase mask calculated based on function
- Args:
- aperture:
mask of type Aperture
- function:
function to calculate the phase on
- **function_args:
additional arguments to pass onto the function
- Returns:
- aperture:
aperture with fresnel phase
- pyMOE.generate.arbitrary_multilayer_mask(mode, N_pixels, xsize, ysize, n, fname, *args, filename=None, plotting=False, prec=1e-06, mpoints=1000000000.0, zlevs=[], grid=None, **kwargs)
###TO BE DEPRECATED IN NEXT MAJOR RELEASE (use generate.arbitrary_aperture_function)
returns a “contour” mask of the arbitrary fname function
parameters: mode = ‘gdspyfast’, ‘gdspy’, ‘gdshelper’ N_pixels = nr of pixels (or points) , by default the results 2D array is N_pixels by N_pixels xsize = size in x in um ysize = size in y in um n = number of gray levels fname = function name (e.g. lensfres(x,y,x0,y0, args) , where args will be given as *args) *args = arguments fname, excluding the [x,y,x0,y0] params
optional: filename = string with mask output into GDS (default None) plotting = True, shows the mask (default False) prec = precision of the gdspy boolean operation (default 1e-6 um) mpoints = max_points of the gdspy polygon (default 1e9 points) zlevs = array of the phase levels grid = 2D array with a meshgrid
Example of use: arbitrary_multilayer_mask(5000, 500,500, 10, lensfres, fo=5000, wavelength=0.6328, filename=”fresnel_phase_plate.gds”, plotting=True ,prec = 1e-6, mpoints = 1e9 )
- pyMOE.generate.arbitrary_phase_mask(mode, N_pixels, xsize, ysize, n, fname, *args, filename=None, plotting=False, prec=1e-06, mpoints=1000000000.0, zlevs=[], grid=None, **kwargs)
###TO BE DEPRECATED IN NEXT MAJOR (use generate.arbitrary_aperture_function)
returns a “phase mask” (2D array of the phase IN RADIANS) from arbitrary COMPLEX PHASE function fname given as argument
parameters: mode = ‘gdspyfast’, ‘gdspy’, ‘gdshelper’ N_pixels = nr of pixels (or points) , by default the results 2D array is N_pixels by N_pixels xsize = size in x in um ysize = size in y in um n = number of gray levels fname = function name (e.g. lensfres(x,y,x0,y0, args) , where args will be given as *args) *args = arguments fname, excluding the [x,y,x0,y0] params
optional: filename = string with mask output into GDS (default None) plotting = True, shows the mask (default False) prec = precision of the gdspy boolean operation (default 1e-6 um) mpoints = max_points of the gdspy polygon (default 1e9 points) zlevs = array of the phase levels grid = 2D array with a meshgrid
Examples of use: #Should take around ~30 s for any of these arbitrary_phase_mask(5000, 500,500, 10, lensfres, fo=5000, wavelength=0.6328, filename=”fresnel_phase_plate.gds”, plotting=True ,prec = 1e-6, mpoints = 1e9 )
arbitrary_phase_mask(5000, 500,500, 60, spiral, L=1, filename=”spiral_phase_plate.gds”, plotting=True ,prec = 1e-12, mpoints = 1e9 )
- pyMOE.generate.boundary_from_function()
- pyMOE.generate.circular_aperture(aperture, radius, center=(0, 0))
Updates aperture and returns 2D circular aperture mask
- Args:
- aperture:
mask of type Aperture
- radius:
radius of the circle aperture
- center:
default (x0=0, y0=0) center of circle
- Returns:
- aperture:
aperture with circular amplitude
- pyMOE.generate.clip_aperture(aperture_in, new_dx0_1, new_dx0_2, new_dy0_1, new_dy0_2)
Clips the aperture between [new_dx0_1, new_dx0_2] and [new_dy0_1, new_dy0_2] limits
- Args:
- aperture_in:
input aperture object
- new_dx0_1, new_dx0_2:
limits in x
- new_dy0_1, new_dy0_2:
limits in y
- Returns:
Clipped aperture with limits [new_dx0_1, new_dx0_2] and [new_dy0_1, new_dy0_2]
- pyMOE.generate.clip_aperture_within(aperture_in, new_dx0_1, new_dx0_2, new_dy0_1, new_dy0_2)
Clips the aperture by a rectangular aperture
- Args:
- aperture_in:
input aperture object
- new_dx0_1, new_dx0_2:
limits in x
- new_dy0_1, new_dy0_2:
limits in y
- Returns:
Clipped aperture_in with the rectangle
- pyMOE.generate.create_aperture_from_array(array, pixel_size, center=False)
Creates an aperture from the given array, where each pixel is of pixel_size
- Args:
- array:
2D numpy array of a mask
- pixel_size:
absolute value for each pixel, as a tuple (x,y)
- center:
if True, will center the image at the origin
- Returns:
- aperture:
aperture with the mask inserted
- pyMOE.generate.create_empty_aperture(xmin, xmax, N_x, ymin, ymax, N_y)
Creates an empty aperture max of the mesh dimensions provided
- Args:
- xmin, xmax:
range for x
- N_x:
number of x points
- ymin, ymax:
range for y
- N_y:
number of y points
- Returns:
- mask:
empty Aperture
- pyMOE.generate.create_empty_aperture_from_aperture(aperture)
Creates an empty aperture with the same spatial dimensions of the given aperture
- Args:
- aperture:
aperture
- Returns:
- aperture:
empty Aperture of same spatial dimensions
- pyMOE.generate.create_scale(N_pixels, nsz, ngs)
returns a 2D array with a scale of successive gray levels
- Args:
- N_pixels:
nr of pixels
- nsz:
division in size
- ngs:
nr of gray levels
- pyMOE.generate.fresnel_phase(aperture, focal_length, wavelength, radius=None, center=(0, 0))
Updates aperture and returns Fresnel phase mask
- Args:
- aperture:
mask of type Aperture
- focal_length:
design focal length
- wavelength:
design wavelength
- radius:
if defined, truncates the fresnel phase to inside this radius
- Returns:
- aperture:
aperture with fresnel phase
- pyMOE.generate.fresnel_phase_mask(N_pixels, foc, wavelength, xsize, ysize, n, filename=None, plotting=False, prec=1e-06, mpoints=1000000000.0, grid=None)
###TO BE DEPRECATED IN NEXT MAJOR RELEASE (use generate.fresnel_phase)
returns a Fresnel “phase mask” (2D array of the phase IN RADIANS) parameters: N_pixels = nr of pixels , by default the results 2D array is N_pixels by N_pixels foc = focal length in um wavelength = wavelength in um xsize = size in x in um ysize = size in y in um n = number of gray levels
optional: filename = string with mask output into GDS (default None) plotting = True, shows the mask (default False) prec = precision of the gdspy boolean operation (default 1e-6) mpoints = max_points of the gdspy polygon (default 1e9) grid = 2D array with a meshgrid
Example of use: fresnel_phase_mask(N_pixels = 5000, foc = 5000, wavelength = 0.6328 , xsize = 500, ysize =500, n=10, filename=’fresnel_phase_mask.gds’, plotting=True ) #Should take around ~30 s
- pyMOE.generate.fresnel_zone_plate_aperture(aperture, focal_length, wavelength, radius=None, center=(0, 0))
Updates aperture and returns Fresnel zone plate aperture
- Args:
- aperture:
mask of type Aperture
- focal_length:
design focal length
- wavelength:
design wavelength
- radius:
if defined, truncates the fresnel phase to inside this radius
- Returns:
- aperture:
aperture with fresnel zone plate
- pyMOE.generate.fzp_mask(N_pixels, foc, wavelength, xsize, ysize, filename, plotting=False, grid=None)
###TO BE DEPRECATED IN NEXT MAJOR (use generate.fresnel_zone_plate_aperture)
returns a fresnel zone plate (as a numpy 2D array) N_pixels = nr of pixels foc = focal length in um wavelength = wavelength in um xsize = size in x in um ysize = size in y in um filename = string with mask image name ‘image.png’
Optional: plotting=True, shows the mask grid = 2D array with a meshgrid
Example of use:
fzp_mask(N_pixels = 50, foc = 5000 , wavelength = 0.6328 , xsize = 500, ysize = 500, filename = ‘fresnel.png’, plotting=True )
- pyMOE.generate.makegrid(N_pixels, xsize, ysize)
Creates a square meshgrid of N_pixels by N_pixels in with arrays till xsize and ysize
- Args:
- N_pixels:
nr of pixels , by default the results 2D array is N_pixels by N_pixels
- xsize:
size in x
- ysize:
size in y
- Returns:
Meshgrid (XX, YY)
- pyMOE.generate.rectangular_aperture(aperture, width, height, corner=None, center=None)
Updates aperture and returns 2D rectangular aperture mask
- Args:
- aperture:
aperture of type Aperture
- width, height:
width and height of the rectangle
- corner:
if given, sets the lower left corner of the rectangle
- center:
if given, sets the center of the rectangle
- Returns:
- aperture:
aperture with rectangular amplitude
- pyMOE.generate.resize_linear(image_matrix, new_height: int, new_width: int)
Perform a pure-numpy linear-resampled resize of an image. https://stackoverflow.com/questions/48121916/numpy-resize-rescale-image
- Args:
- image_matrix:
input 2D array (image)
- new_height:
integer, height of the image
- new_width:
integer, width of the image
- Returns:
- output_image:
resized 2D array (image)
- pyMOE.generate.truncate_aperture_radius(aperture, radius, center=(0, 0), truncate_value=0)
Truncates the aperture to inside the circle of radius at center
- Args:
- aperture:
mask to be truncated
- radius:
radius to select the region
- center:
center points tuple of the circle
- truncate_value:
value to truncate the mask, by default 0
- Returns:
aperture
pyMOE.holograms module
Library module for hologram generation and visualiation
- class pyMOE.holograms.Field(a, b)
Bases:
objectClass to define complex fields with methods for amplitude and phase
- property amplitude
- property intensity
- property phase
- pyMOE.holograms.algorithm_Gerchberg_Saxton(target_intensity, iterations=3, levels=None, input_phase=None, source_beam=None, verbose=True)
Creates hologram phase mask to generate target intensity using Gerchberg Saxton Algorithm.
U0 - input field U1 - calculated far-field
- Args:
- target_intensity:
2D array of intensity values
- levels:
Scalar or array: levels to consider in the phase mask as physical constraint. If None, it does not discretize.
- input_phase:
If given, uses this input_phase as starting phase instead of random.
- Returns:
- phase_mask:
2D phase mask
- error_list:
list of errors measured in each iteration
- pyMOE.holograms.calculate_phase_farfield(phase, source_beam=None)
Calculates a proportional far field of a given phase aperture and source_beam
TODO Fraunhofer propagation
- Args:
- phase:
phase mask
- source_beam:
if not given, assumes constant intensity=1
- Returns
- far_field:
far field amplitude
pyMOE.importing module
importing.py Module containing functions to import gds files, inspect them in matplotlib and save gds files as image files
- pyMOE.importing.gds2img(infile, outfile, norm, rescaled=0, verbose=False)
(void) plots the gds for inspection in python matplotlib and saves the figure to image file Note: if plotting different gds file together, please make sure they are aligned (e.g. centered at origin)
- Args:
- infile:
string gds filename (e.g. ‘yolo.gds’)
- outfile:
string img filename (e.g. ‘yolo.tiff’)
- norm:
maximum level of gray (e.g. 128)
- rescaled:
if !=0 will rescale the layout
- verb:
if True, shows verbose, defaults to False
- pyMOE.importing.inspect_gds2(filename, colors, rescale=0, **kwargs)
Plots the gds for inspection in python matplotlib, only with the borders of the features Note: if plotting different gds file together, please make sure they are aligned (e.g. centered at origin)
- Args:
- filename:
string gds filename (e.g. ‘yolo.gds’)
- color:
string color name for plotting
- rescale:
int rescaling factor for all points in mask, by default is 0
- pyMOE.importing.inspect_gds2layers(filename, norm, rescale=0, verbose=False, **kwargs)
Returns cell, polygon dictionary, (xmin,xmax) and (ymin,ymax) for representation of the gds layers into a grayscale image
- Args:
- filename:
string gds filename (e.g. ‘yolo.gds’)
- norm:
maximum level of gray (e.g. 128)
- verbose:
if True show some information while doing the operations. defaults false
for **kwargs, add ‘axes = subplot’, where subplot has been previously defined as subplot = fig.add_subplot(111) with ‘import matplotlib.pyplot as plt’ and ‘fig = plt.figure()’
- pyMOE.importing.inspect_gds2layersplt(filename, norm, rescale=0, verbose=False, **kwargs)
Returns cell, polygon dictionary, (xmin,xmax) and (ymin,ymax) for representation of ALL gds layers into a grayscale image
- Args:
- filename:
string gds filename (e.g. ‘yolo.gds’)
- norm:
maximum level of gray (e.g. 128)
- verbose:
if True show some information while doing the operations. defaults false
for **kwargs, add ‘axes = subplot’, where subplot has been previously defined as subplot = fig.add_subplot(111) with ‘import matplotlib.pyplot as plt’ and ‘fig = plt.figure()’
- pyMOE.importing.makesubplot(x, y, *argv, **kwargs)
Makes a subplot object for plotting
pyMOE.metas module
metas.py Module containing functions to create metasurfaces from phase masks
- pyMOE.metas.metasurface_from_phase(xsiz, ysiz, pixelx, pixely, p, aperture_vals, topcellname, outfilen, gdspyelements='pillar', verbose=False, rotation=None, scaling=None, grid='square', mindim=0.05, smallerdim=0, largest_phase=None)
Transform a 2D array (aperture_vals) representing the phase into a 2D metasurface and saves it to gds
- Args:
- xsiz:
x size of aperture in x in um
- ysiz:
y size of aperture size in y in um
- pixelx:
pixel size in x in um
- pixely:
pixel size in y in um
- p:
periodicity in um
- aperture_vals:
2D array with the phase
- topcellname:
string with name of top cell, e.g. ‘TOP’
- oufilen:
string filename of output gds
- gdspyelements:
gdspy element to be used as individual meta-element (also accepts array of such elements for iteration, with same dimension as unique values in aperture_vals). If == ‘pillar’ (default) -> gdspy circle with 1 um diameter
- verbose:
if True, prints during execution
- rotation:
array with the rotation angles of unique meta-elements (1:1 correspondence with unique aperture_vals values!), Rotation angle is anti-clockwise in radians. If None (default), sets the rotation angle = 0 for all elements.
- scaling:
array with the scaling factor of unique meta-element (1:1 correspondence with unique aperture_vals values!), Scaling factor is with respect to the dimension of the individual meta-element. If None (default), sets scaling factor to 1.0 for all elements.
- grid:
Type of grid, options are ‘square’ or ‘hex’. Default is ‘square’. Please make sure the aperture_vals have been evaluated in an hexagonal grid, to make sure the values match.
- mindim:
clipping scaling factor (cannot scale below a certain value, to avoid very small elements)
- smallerdim:
lowest scaling factor
- largest_phase:
largest phase in the phase mask. If None, takes the maximum of aperture_vals
- Returns:
None
- pyMOE.metas.metasurface_from_phase_instances(xsiz, ysiz, pixelx, pixely, p, aperture_vals, topcellname, outfilen, gdspyelements='pillar', infile=None, verbose=False, rotation=None, scaling=None, grid='square', mindim=0.05, smallerdim=0, tempfile='temp.gds', largest_phase=None)
Transform a 2D array (aperture_vals) representing the phase into a 2D metasurface using instances (from pya) package and saves it to gds
- Args:
- xsiz:
x size of aperture in x in um
- ysiz:
y size of aperture size in y in um
- pixelx:
pixel size in x in um
- pixely:
pixel size in y in um
- p:
periodicity in um
- aperture_vals:
2D array with the phase
- topcellname:
string with name of top cell, e.g. ‘TOP’
- oufilen:
string filename of output gds
- gdspyelements:
gdspy element to be used as individual meta-element (also accepts array of such elements for iteration, with same dimension as unique values in aperture_vals). If == ‘pillar’ (default) -> gdspy circle with 1 um diameter. If ‘infile’ is provided, ignores this design.
- infile:
string with filename to be used as meta-element
- verbose:
if True, prints during execution
- rotation:
array with the rotation angles of unique meta-elements (1:1 correspondence with unique aperture_vals values!), Rotation angle is anti-clockwise in radians. If None (default), sets the rotation angle = 0 for all elements.
- scaling:
array with the scaling factor of unique meta-element (1:1 correspondence with unique aperture_vals values!), Scaling factor is with respect to the dimension of the individual meta-element. If None (default), sets scaling factor to 1.0 for all elements.
- grid:
Type of grid, options are ‘square’ or ‘hex’. Default is ‘square’. Please make sure the aperture_vals have been evaluated in an hexagonal grid, to make sure the values match.
- mindim:
clipping scaling factor (cannot scale below a certain value, to avoid very small elements)
- smallerdim:
lowest scaling factor
- tempfile:
string with name of a temporary file that will be used to have the individual elements and make the instances
- largest_phase:
largest phase in the phase mask. If None, takes the maximum of aperture_vals
- Returns:
None
pyMOE.plotting module
plotting.py Module for plotting apertures ans save them as image files
- pyMOE.plotting.plot_aperture(aperture, scale=None, colorbar=True, only_plot=False, filename=None, **kwargs)
Plots the given aperture
- Args:
- aperture:
Aperture object of the mask
- colorbar:
True/False flag to plot colorbars
- only_plot:
if True, only shows image without labels and axes
- filename:
if provided, saves figure to filename
###available output file extensions are same as opencv https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html
- pyMOE.plotting.plot_field(aperture, which='both', scale=None, colorbar=True, only_plot=False, filename=None, **kwargs)
Plots the given aperture
- Args:
- aperture:
Aperture object of the mask
- which:
default “both”, “amplitude” or “phase”
- colorbar:
True/False flag to plot colorbars
- only_plot:
if True, only shows image without labels and axes
- filename:
if provided, saves figure to filename
###available output file extensions are same as opencv https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html
- pyMOE.plotting.save_mask_plot(maskcir, xsize, ysize, filename)
pyMOE.propagate module
propagate.py
This module had origin in the “propopt” repository module with same name -> For more information, examples of use and validation tests,
please see link for repository : https://github.com/cunhaJ/propopt
Here, “propopt” code has been modified and extended for use with apertures and masks of “pyMOE”
- pyMOE.propagate.Fraunhofer_criterion(aperturesiz, wavelength)
Calculation of “Fraunhofer distance” , Goodman exp 4-27
- Args:
- aperturesiz:
size of the aperture in m
- wavelength:
wavelength of the aperture in m
- Returns:
Fraunhofer distance
- pyMOE.propagate.Fresnel_num(width, wavelength, zdist)
Calculation of Fresnel number, Goodman pag 85
- Args:
- width:
size of the aperture in m
- wavelength:
wavelength of the aperture in m
- zdist:
distance to the screen in m
- Returns:
Fresnel number
- pyMOE.propagate.RS_intXY(zs, mask, npixmask, pixsizemask, npixscreen, dxscreen, dyscreen, wavelength, verbose=False)
Calculates the RS int of the first kind returns Escreen (complex electric field at obs screen), Iscreen (intensity at obs screen), iplot (the actual intensity)
- Args:
- zs:
distance to screen [m]
- mask:
Electric field at the mask, complex valued 2D function
- npixmask:
number of pixels on the side of the mask
- pixsizemask:
size of pixel of the mask [m]
- npixscreen:
number of pixels on the side of the screen (observation)
- dxscreen:
max_x of the screen [m], the screen range is [-dxscreen, dxscreen]
- dyscreen:
max_y of the screen [m], the screen range is [-dyscreen, dyscreen]
- wavelength:
wavelength of the light [m]
- verbose:
defaults to False, if True prints
- pyMOE.propagate.RS_intXY_kernel(dmask, nps, npm, xs, ys, zs, xm, ym, zm, k, E0m, verbose)
- pyMOE.propagate.RS_intYZ(zmin, zmax, nzs, yfixed, mask, npixmask, pixsizemask, npixscreen, dxscreen, dyscreen, wavelength, nind, verbose=False)
Calculates the RS_int in the of the first kind, taking information about mask, distance to screen, and screen information returns Escreen (complex electric field at obs screen), Iscreen (intensity at obs screen), iplot (the actual intensity)
Args:
- [zmin, zmax]:
distance range limits in z [m]
- nzs:
number of points along the optical axis
- yfixed:
y fixed coordinates
- mask:
Electric field at the mask, complex valued 2D function
- npixmask:
number of pixels on the side of the mask
- pixsizemask:
size of pixel of the mask [m]
- npixscreen:
number of pixels on the side of the screen (observation)
- dxscreen:
max_x of the screen [m], the screen range is [-dxscreen, dxscreen]
- dyscreen:
max_y of the screen [m], the screen range is [-dyscreen, dyscreen]
- nind:
scaling refractive index
- wavelength:
wavelength of the light [m]
- verbose:
defaults to False, if True prints
- pyMOE.propagate.RS_intYZ_kernel(dmask, npm, nzs, npixscreen, xfixed, ys, zarray, xm, ym, zm, k, E0m, verbose)
- pyMOE.propagate.RS_intZZ(zmin, zmax, nzs, xfixed, yfixed, mask, npixmask, pixsizemask, npixscreen, dxscreen, dyscreen, wavelength, nind, verbose=False)
Calculates the RS_int in the of the first kind, taking information about mask, distance to screen, and screen information returns Escreen (complex electric field at obs screen), Iscreen (intensity at obs screen), iplot (the actual intensity)
- Args:
- [zmin, zmax]:
distance range limits in z [m]
- nzs:
number of points along the optical axis
- xfixed, yfixed:
x and y fixed coordinates
- mask:
Electric field at the mask, complex valued 2D function
- npixmask:
number of pixels on the side of the mask
- pixsizemask:
size of pixel of the mask [m]
- npixscreen:
number of pixels on the side of the screen (observation)
- dxscreen:
max_x of the screen [m], the screen range is [-dxscreen, dxscreen]
- dyscreen:
max_y of the screen [m], the screen range is [-dyscreen, dyscreen]
- nind:
scaling refractive index
- wavelength:
wavelength of the light [m]
- verbose:
defaults to False, if True prints
- pyMOE.propagate.RS_intZZ_kernel(dmask, npm, nzs, xs, ys, zs, xm, ym, zm, k, E0m, verbose)
- pyMOE.propagate.fraunhofer(z, mask, npixmask, pixsizemask, npixscreen, dxscreen, dyscreen, wavelength)
Calculate Fraunhofer approximation, following Goodman exp 4-25
- Args:
- z:
distance to the observation plane in m
- mask:
2D map (x-y plane) npixel vs npixel mask
- npixmask:
number of pixels of the mask
- pixsizemask:
size of the pixel at the mask in m
- npixscreen:
size of the pixel at the screen
- dxscreen:
x-size of the screen in m
- dyscreen:
y-size of the screen in m
- wavelength:
wavelength in m
returns 2D map (x-y plane) with abs of Electric field at distance z
- pyMOE.propagate.fraunhofer_kernel(k, xm, ym, mask, z, wavelength)
- pyMOE.propagate.fresnel(z, mask, npixmask, pixsizemask, npixscreen, dxscreen, dyscreen, wavelength)
Calculate Fresnel approximation, following Goodman exp 4-17
- Args:
- z:
distance to the observation plane in m
- mask:
2D map (x-y plane) npixel vs npixel mask
- npixmask:
number of pixels of the mask
- pixsizemask:
size of the pixel at the mask in m
- npixscreen:
size of the pixel at the screen
- dxscreen:
x-size of the screen in m
- dyscreen:
y-size of the screen in m
- wavelength:
wavelength in m
returns 2D map (x-y plane) with abs of Electric field at distance z
- pyMOE.propagate.fresnel_kernel(k, xm, ym, z, mask)
pyMOE.sag_functions module
sag_functions.py Module containing several sag or phase functions for various optical elements
- pyMOE.sag_functions.fresnel_lens_phase(XX, YY, focal_length, wavelength)
returns the COMPLEX PHASE of a fresnel lens with input meshgrid (x,y) with center at (x0,y0)
- Args:
- XX:
x array from meshgrid
- YY:
y array from meshgrid
- focal_length:
focal distance
- wavelength:
wavelength of design
Note: for angle (in rad), call numpy.angle(…)
- pyMOE.sag_functions.monkey_saddle(x, y, a, b)
returns a COMPLEX PHASE monkey saddle function
- Args:
- x:
x array from meshgrid
- y:
y array from meshgrid
- a:
arbitrary parameter
- b:
arbitrary parameter
- pyMOE.sag_functions.saddle(x, y, a, b)
returns a COMPLEX PHASE saddle function
- Args:
- x:
x array from meshgrid
- y:
y array from meshgrid
- a:
arbitrary parameter
- b:
arbitrary parameter
- pyMOE.sag_functions.spiral(x, y, L)
returns a spiral COMPLEX PHASE with input meshgrid (x,y) with center at (x0,y0)
- Args:
- x:
x array from meshgrid
- y:
y array from meshgrid
- L:
topological charge
- Returns
spiral phase
pyMOE.utils module
utils.py Module containing utility functions
- class pyMOE.utils.Timer(name=None)
Bases:
objectTimer helper class to calculated elapsed time of chunk of code, from https://stackoverflow.com/a/5849861/7996766
- pyMOE.utils.create_levels(start, end, levels)
Creates linear levels list with N
- pyMOE.utils.digitize_array_to_bins(array, levels)
Digitizes the given array to within the number of levels provided
- Args:
- array:
input array of values
- levels:
integer number of levels to consider or array of levels
- Returns:
- bins:
bins corresponding to the levels
- digitized:
digitized array
- To do:
Consider the midpoint selection in the future
- pyMOE.utils.discretize_array(array, levels)
- pyMOE.utils.mean_squared_error(new, original)
Calculates mean squared error between the new array and the original
- pyMOE.utils.progress_bar(progress, bar_length=20, bar_character='#')
Progress bar. Writes a progress bar in place in the output
- Args:
- progress:
value between 0 and 1
- bar_length:
number of characters to consider in the bar
- pyMOE.utils.simpson2d(f, ax, bx, ay, by)
Implements Simpson method for calculating a double integral in 2D array f
- Arguments:
- f:
2D array to calculate integral
- [ax, bx]:
limits of integration in x, [lower, upper]
- [ay, by]:
limits of integration in y, [lower, upper]