SlicerMapper#
- class SlicerMapper(data, detector_shape=(4096, 4096), pixel_size=10, xy_units='mm', **kwargs)[source]#
Bases:
objectMap between detector pixel coordinates and IFS slicer coordinates.
The mapping is derived from spot calibration data that relates each slice, field position along the slice (from -1 to 1) and wavelength to a physical
(x, y)position on the detector. Linear interpolators built from this data are used to project scene cubes onto the detector and to de-project detector images back to the sky plane.Internally, wavelengths are stored in Angstrom and detector positions in mm, with (0, 0) at the detector center.
- Parameters:
data (
pandas.DataFrame) – Calibration data, with one row per spot and columns for the slice id, field position, wavelength and(x, y)detector position (column names can be set through**kwargs).detector_shape (
tupleofint, optional) – Shape of the detector in pixels(ny, nx). Default is (4096, 4096).pixel_size (
float, optional) – Size of a detector pixel, in micrometers. Default is 10.xy_units (
str, optional) – Unit of the(x, y)coordinates indata(an astropy unit name). Default is"mm".**kwargs – Column names and wavelength unit of
data:wavelength_units(default"micrometer"),x(default"x"),y(default"y"),sliceid(default"slice"),fieldpos(default"fieldpos") andwavelength(default"lbda").
See also
from_dataBuild the mapper from a calibration file.
Methods
Extract a cube of deprojected data from the deprojection DataFrame.
Interpolate the calibration data on a slice-field-wavelength grid.
Create a
SlicerMapperfrom a calibration file or DataFrame.Get the detector pixel positions of slice-field-wavelength coordinates.
Get the contour edges of specified slices.
Get the bounding box of specified slice(s).
Paint the footprint of a slice onto a detector image.
Convert physical coordinates (mm) to detector pixel coordinates.
Convert detector pixel coordinates to physical coordinates (mm).
Project a data cube onto the detector plane.
Project a Lazuli target simulation onto the detector plane.
Project slice image(s) onto the detector.
Display slices on the detector.
Convert slice, field position and wavelength into detector pixels.
Convert detector pixels into slice, field position and wavelength.
Attributes
Calibration data (
pandas.DataFrame).Detector properties, as a dict with
"shape"((ny, nx)in pixels) and"pixel_size"(micrometers).Interpolator from detector (x, y) [mm] to (slice id, field position, wavelength [A]).
Interpolator from (slice id, field position, wavelength [A]) to detector (x, y) [mm].
Column names and units of the calibration data, as given at initialization.
Number of unique slice ids in the calibration data.
- deproject_cubeof(key, deproj_df=None, nslices=58, nfieldpos=58, nlbda=571)[source]#
Extract a cube of deprojected data from the deprojection DataFrame.
- Parameters:
key (
str) – Column name to extract from the deprojected DataFrame.deproj_df (
pandas.DataFrameorNone, optional) – Deprojected DataFrame, as returned bydeproject_data. If None, the one stored bydeproject_data(..., inplace=True)is used. Default is None.nslices (
int, optional) – Number of slices. Default is 58.nfieldpos (
int, optional) – Number of field positions. Default is 58.nlbda (
int, optional) – Number of wavelength points. Default is 571.
- Returns:
numpy.ndarray– 3D array of shape(nlbda, nfieldpos, nslices).- Raises:
ValueError – If no deprojected DataFrame is provided or stored.
Notes
The
keycolumn is reshaped as(nslices, nfieldpos, nlbda)before moving the axes, whiledeproject_dataorders its rows with the field position varying the slowest, then the slice id, then the wavelength.
- deproject_data(sliceid, fieldpos, lbda, key=None, inplace=False)[source]#
Interpolate the calibration data on a slice-field-wavelength grid.
For each combination of
sliceid,fieldposandlbda, the detector position is computed, and thekeycolumns ofdataare linearly interpolated (in the detector plane) at that position.- Parameters:
sliceid (array_like) – Slice ids.
fieldpos (array_like) – Field positions along the slice (from -1 to 1).
lbda (array_like) – Wavelengths in Angstrom.
key (
strorlistofstrorNone, optional) – Column name(s) ofdatato interpolate. If None, all the columns are used. Default is None.inplace (
bool, optional) – If True, also store the result in the mapper, for later use bydeproject_cubeof. Default is False.
- Returns:
pandas.DataFrame– One row per grid point (seemesh_kwargsfor the ordering), with columnssliceid,fieldpos,lbda,xpixel,ypixeland the interpolatedkeycolumn(s).
- classmethod from_data(data, xy_units='mm', **kwargs)[source]#
Create a
SlicerMapperfrom a calibration file or DataFrame.- Parameters:
data (
strorpandas.DataFrame) – Path to a space-separated CSV file (e.g.mapping_spotdata.csvinPACKAGE_PATH) or DataFrame containing the calibration data.xy_units (
str, optional) – Unit of the(x, y)coordinates. Default is"mm".**kwargs – Goes to
SlicerMapper.
- Returns:
SlicerMapper– Mapper built from the calibration data.
- get_pixel_positions(coordinates)[source]#
Get the detector pixel positions of slice-field-wavelength coordinates.
- Parameters:
coordinates (
pandas.DataFrameor array_like) – Coordinates(sliceid, fieldpos, wavelength)(wavelength in Angstrom), as a DataFrame with these 3 columns (in that order) or an array of shape(N, 3).- Returns:
numpy.ndarray– Detector pixel coordinates(x, y), of shape(N, 2). NaN outside of the calibration domain.
- get_slice_contours(sliceid, lbda_range=[3500, 17000], out_format='numpy', units='pixels', slice_edge=[-1, 1], nbin_pos=5, nbin_lbda=10, combined=False)[source]#
Get the contour edges of specified slices.
The contour is the convex hull of the detector positions of a
nbin_pos x nbin_lbdagrid of field positions and wavelengths.- Parameters:
sliceid (
intor array_like) – Slice id(s) to get contours for.lbda_range (
listoffloat, optional) – Wavelength range[min, max]in Angstrom. Default is[3_500, 17_000].out_format (
str, optional) – Output format:"shapely"or"geometry"for a shapely polygon,"numpy"or"array"for the vertex coordinates. Default is"numpy".units (
str, optional) – Units of the output coordinates:"pixels"(or"pxl","pixel") for detector pixels,"physical"or"mm"for mm from the detector center. Default is"pixels".slice_edge (
listoffloat, optional) – Range of field positions along the slice. Default is[-1, 1].nbin_pos (
int, optional) – Number of points along the field position. Default is 5.nbin_lbda (
int, optional) – Number of points along wavelength. Default is 10.combined (
bool, optional) – If True and several slices are given, return a single contour enclosing all of them. If False, return one contour per slice. Default is False.
- Returns:
shapely.Polygonornumpy.ndarrayorlist– Contour polygon, or array of its(x, y)vertices of shape(nvertices, 2), depending onout_format. If several slices are given andcombinedis False, a list with one such result per slice.- Raises:
ValueError – If
unitsorout_formatare invalid.
Notes
nbin_posandnbin_lbdaare not propagated whencombinedis False and several slices are given (the defaults are used).
- get_slice_inbox(sliceid, lbda_range=[3500, 17000], units='pixels')[source]#
Get the bounding box of specified slice(s).
- Parameters:
sliceid (
intor array_like) – Slice id(s) to get the bounding box for.lbda_range (
listoffloat, optional) – Wavelength range[min, max]in Angstrom. Default is[3500, 17000].units (
str, optional) – Units of the output:"pixels"or"physical"/"mm". Default is"pixels".
- Returns:
numpy.ndarray– ForNslices, array of shape(2, N, 2): axis 0 is(x, y), axis 1 the slice and axis 2(min, max).
See also
get_slice_contoursThe slice contours used to get the box.
- inpaint_slice_onto_image(sliceid, image, fillvalue=1, oversampling=3, **kwargs)[source]#
Paint the footprint of a slice onto a detector image.
The slice contour is turned into a polygon and every pixel whose centre falls inside it is set to
fillvalue. The test is run on an oversampled grid and rebinned, so that the pixels straddling the edge of the slice end up with the fraction of their area it covers.- Parameters:
sliceid (
int) – Identifier of the slice to paint.image (
numpy.ndarray) – 2D detector image, modified in place.fillvalue (
float, optional) – Value given to the pixels fully covered by the slice. Default is 1.oversampling (
int, optional) – Oversampling factor per axis, used to estimate the partial coverage of the edge pixels. Default is 3.**kwargs – Goes to
get_slice_contours(withunits="pixels"andout_format="numpy", the defaults).
- Returns:
numpy.ndarray– The inputimage, with the slice footprint painted in.
See also
get_slice_contoursThe slice contour this paints.
Notes
imageis modified in place, the returned array is the input object and not a copy. Only the bounding box of the slice is touched, but it is overwritten rather than added to, so painting slices whose bounding boxes overlap will erase the earlier ones.
- physical_to_pixels(a_physical, from_center=True)[source]#
Convert physical coordinates (mm) to detector pixel coordinates.
- Parameters:
a_physical (
numpy.ndarray) – Physical coordinates(x, y)in mm, of shape(..., 2).from_center (
bool, optional) – If True, the physical coordinates are relative to the detector center, anddetector_shape / 2is added to the output pixel coordinates. Default is True.
- Returns:
numpy.ndarray– Pixel coordinates, with the same shape as the input.
- pixels_to_physical(a_pixels, from_center=True)[source]#
Convert detector pixel coordinates to physical coordinates (mm).
- Parameters:
a_pixels (
numpy.ndarray) – Pixel coordinates(x, y), of shape(..., 2).from_center (
bool, optional) – If True,detector_shape / 2is subtracted from the pixel coordinates, such that the physical coordinates are relative to the detector center. Default is True.
- Returns:
numpy.ndarray– Physical coordinates in mm, with the same shape as the input.
Notes
If
a_pixelsis a floatnumpy.ndarrayandfrom_centeris True, it is modified in place.
- project_lazulicube(cube, which, lbda, fill_value=0, **kwargs)[source]#
Project a data cube onto the detector plane.
- Parameters:
cube (
numpy.ndarray) – Data cube to project, of shape(nlbda, 58, nspatial).which (
str) – Which slices to project:"fine"or"narrow"for slices 1-58,"medium"or"wide"for slices 59-116.lbda (
numpy.ndarray) – Wavelengths in Angstrom of the cube spectral axis.fill_value (
float, optional) – Value of the pixels with no projected signal. Default is 0.**kwargs – Goes to
project_slice.
- Returns:
numpy.ndarray– 2D detector image.- Raises:
ValueError – If
whichis not one of the available options.
- project_lazulitarget(lazulitarget, psf_profile='airy', switch_off=[], **kwargs)[source]#
Project a Lazuli target simulation onto the detector plane.
The cubes of both the narrow (slices 1 to 58) and wide (slices 59 to 116) fields are simulated and projected, and the resulting images are summed.
- Parameters:
lazulitarget (
slicersim.lazuli.VirtualLazuliTarget) – Lazuli target, e.g. aLazuliSupernova.psf_profile (
str, optional) – PSF profile used to simulate the cubes. Default is"airy".switch_off (
listofstr, optional) – Scene contributions to switch off. Default is[].**kwargs – Goes to
get_cube.
- Returns:
numpy.ndarray– 2D detector image combining both the narrow and wide fields.
- project_slice(sliceid, sliceimg, lbda, oversample=(5, 2), fill_value=0)[source]#
Project slice image(s) onto the detector.
The slice images are oversampled (by repetition), each oversampled element is mapped to its detector pixel, and the values falling into the same pixel are summed.
- Parameters:
sliceid (
intor array_like) – Id(s) of the slice(s), matching the second axis ofsliceimg.sliceimg (
numpy.ndarray) – Image of the slices, of shape(nlbda, nslices, nspatial), the spatial axis spanning field positions -1 to 1.lbda (
numpy.ndarray) – Wavelengths in Angstrom of the spectral axis ofsliceimg.oversample (
tupleofint, optional) –(spectral_oversampling, spatial_oversampling)factors applied to the image before projection. Default is (5, 2).fill_value (
float, optional) – Initial value of the detector pixels, onto which the projected values are added. If NaN, the pixels with no (or zero) signal are set to NaN. Default is 0.
- Returns:
numpy.ndarray– 2D image of shapedetector["shape"].- Raises:
ValueError – If the shape of
sliceimgdoes not match the lengths ofsliceidorlbda.
- show_slices(sliceid, which='lbda', ax=None, **kwargs)[source]#
Display slices on the detector.
- Parameters:
sliceid (
intor array_like) – Slice id(s) to display.which (
str, optional) – What to color by:"lbda"for wavelength,"id"or"sliceid"for slice id. Default is"lbda".ax (
matplotlib.axes.Axesortupleofmatplotlib.axes.Axes, optional) – Axes to plot on. If a tuple of 2 axes, the second is used for the colorbar. If None, a new figure is created. Default is None.**kwargs – Goes to the internal plotting method:
cmap,lbda_min,lbda_max(Angstrom),nlbda(forwhich="lbda"), and otherwise tomatplotlib.patches.Polygon.
- Returns:
matplotlib.figure.Figure– The figure containing the plot.- Raises:
ValueError – If
whichis not recognized.
- slice_pos_wave_to_xy(sliceid, fieldpos, lbda)[source]#
Convert slice, field position and wavelength into detector pixels.
- Parameters:
sliceid (
floator array_like) – Coordinates, broadcast to the shape ofsliceid(wavelength in Angstrom). For instance,sliceid=[1, 10, 80], fieldpos=-1, lbda=8000is understood as field position -1 at 8000 A for each of the 3 slices, whilesliceid=[10, 80], fieldpos=[0, -1], lbda=8000is understood as field position 0 for slice 10 and -1 for slice 80, each at 8000 A.fieldpos (
floator array_like) – Coordinates, broadcast to the shape ofsliceid(wavelength in Angstrom). For instance,sliceid=[1, 10, 80], fieldpos=-1, lbda=8000is understood as field position -1 at 8000 A for each of the 3 slices, whilesliceid=[10, 80], fieldpos=[0, -1], lbda=8000is understood as field position 0 for slice 10 and -1 for slice 80, each at 8000 A.lbda (
floator array_like) – Coordinates, broadcast to the shape ofsliceid(wavelength in Angstrom). For instance,sliceid=[1, 10, 80], fieldpos=-1, lbda=8000is understood as field position -1 at 8000 A for each of the 3 slices, whilesliceid=[10, 80], fieldpos=[0, -1], lbda=8000is understood as field position 0 for slice 10 and -1 for slice 80, each at 8000 A.
- Returns:
numpy.ndarray– Detector pixel coordinates(x, y), of shape(2, N)(or(2,)for a single point).
See also
xy_to_slice_pos_waveThe inverse conversion.
- xy_to_slice_pos_wave(x, y)[source]#
Convert detector pixels into slice, field position and wavelength.
- Parameters:
x (
floator array_like) – Detector pixel coordinates,ybeing broadcast to the shape ofx. For instance,x=[1, 10], y=90is understood asy=90for bothx=1andx=10, whilex=[1, 10], y=[80, 90]is understood as the two points(1, 80)and(10, 90).y (
floator array_like) – Detector pixel coordinates,ybeing broadcast to the shape ofx. For instance,x=[1, 10], y=90is understood asy=90for bothx=1andx=10, whilex=[1, 10], y=[80, 90]is understood as the two points(1, 80)and(10, 90).
- Returns:
numpy.ndarray–(sliceid, fieldpos, lbda)coordinates (wavelength in Angstrom), of shape(3, N). NaN outside of the calibration domain.
See also
slice_pos_wave_to_xyThe inverse conversion.
- data#
Calibration data (
pandas.DataFrame).
- detector#
Detector properties, as a dict with
"shape"((ny, nx)in pixels) and"pixel_size"(micrometers).
- interp_2d_to_3d#
Interpolator from detector (x, y) [mm] to (slice id, field position, wavelength [A]).
A
scipy.interpolate.LinearNDInterpolator, built at first access.
- interp_3d_to_2d#
Interpolator from (slice id, field position, wavelength [A]) to detector (x, y) [mm].
A
scipy.interpolate.LinearNDInterpolator, built at first access.
- keys#
Column names and units of the calibration data, as given at initialization.
- nslices#
Number of unique slice ids in the calibration data.