SlicerMapper#

class SlicerMapper(data, detector_shape=(4096, 4096), pixel_size=10, xy_units='mm', **kwargs)[source]#

Bases: object

Map 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 (tuple of int, 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 in data (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") and wavelength (default "lbda").

See also

from_data

Build the mapper from a calibration file.

Methods

deproject_cubeof

Extract a cube of deprojected data from the deprojection DataFrame.

deproject_data

Interpolate the calibration data on a slice-field-wavelength grid.

from_data

Create a SlicerMapper from a calibration file or DataFrame.

get_pixel_positions

Get the detector pixel positions of slice-field-wavelength coordinates.

get_slice_contours

Get the contour edges of specified slices.

get_slice_inbox

Get the bounding box of specified slice(s).

inpaint_slice_onto_image

Paint the footprint of a slice onto a detector image.

physical_to_pixels

Convert physical coordinates (mm) to detector pixel coordinates.

pixels_to_physical

Convert detector pixel coordinates to physical coordinates (mm).

project_lazulicube

Project a data cube onto the detector plane.

project_lazulitarget

Project a Lazuli target simulation onto the detector plane.

project_slice

Project slice image(s) onto the detector.

show_slices

Display slices on the detector.

slice_pos_wave_to_xy

Convert slice, field position and wavelength into detector pixels.

xy_to_slice_pos_wave

Convert detector pixels into slice, field position and wavelength.

Attributes

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]).

interp_3d_to_2d

Interpolator from (slice id, field position, wavelength [A]) to detector (x, y) [mm].

keys

Column names and units of the calibration data, as given at initialization.

nslices

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.DataFrame or None, optional) – Deprojected DataFrame, as returned by deproject_data. If None, the one stored by deproject_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 key column is reshaped as (nslices, nfieldpos, nlbda) before moving the axes, while deproject_data orders 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, fieldpos and lbda, the detector position is computed, and the key columns of data are 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 (str or list of str or None, optional) – Column name(s) of data to 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 by deproject_cubeof. Default is False.

Returns:

pandas.DataFrame – One row per grid point (see mesh_kwargs for the ordering), with columns sliceid, fieldpos, lbda, xpixel, ypixel and the interpolated key column(s).

classmethod from_data(data, xy_units='mm', **kwargs)[source]#

Create a SlicerMapper from a calibration file or DataFrame.

Parameters:
  • data (str or pandas.DataFrame) – Path to a space-separated CSV file (e.g. mapping_spotdata.csv in PACKAGE_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.DataFrame or 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_lbda grid of field positions and wavelengths.

Parameters:
  • sliceid (int or array_like) – Slice id(s) to get contours for.

  • lbda_range (list of float, 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 (list of float, 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.Polygon or numpy.ndarray or list – Contour polygon, or array of its (x, y) vertices of shape (nvertices, 2), depending on out_format. If several slices are given and combined is False, a list with one such result per slice.

Raises:

ValueError – If units or out_format are invalid.

Notes

nbin_pos and nbin_lbda are not propagated when combined is 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 (int or array_like) – Slice id(s) to get the bounding box for.

  • lbda_range (list of float, 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 – For N slices, array of shape (2, N, 2): axis 0 is (x, y), axis 1 the slice and axis 2 (min, max).

See also

get_slice_contours

The 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 (with units="pixels" and out_format="numpy", the defaults).

Returns:

numpy.ndarray – The input image, with the slice footprint painted in.

See also

get_slice_contours

The slice contour this paints.

Notes

image is 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, and detector_shape / 2 is 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 / 2 is 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_pixels is a float numpy.ndarray and from_center is 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 which is 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:
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 (int or array_like) – Id(s) of the slice(s), matching the second axis of sliceimg.

  • 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 of sliceimg.

  • oversample (tuple of int, 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 shape detector["shape"].

Raises:

ValueError – If the shape of sliceimg does not match the lengths of sliceid or lbda.

show_slices(sliceid, which='lbda', ax=None, **kwargs)[source]#

Display slices on the detector.

Parameters:
  • sliceid (int or 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.Axes or tuple of matplotlib.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 (for which="lbda"), and otherwise to matplotlib.patches.Polygon.

Returns:

matplotlib.figure.Figure – The figure containing the plot.

Raises:

ValueError – If which is not recognized.

slice_pos_wave_to_xy(sliceid, fieldpos, lbda)[source]#

Convert slice, field position and wavelength into detector pixels.

Parameters:
  • sliceid (float or array_like) – Coordinates, broadcast to the shape of sliceid (wavelength in Angstrom). For instance, sliceid=[1, 10, 80], fieldpos=-1, lbda=8000 is understood as field position -1 at 8000 A for each of the 3 slices, while sliceid=[10, 80], fieldpos=[0, -1], lbda=8000 is understood as field position 0 for slice 10 and -1 for slice 80, each at 8000 A.

  • fieldpos (float or array_like) – Coordinates, broadcast to the shape of sliceid (wavelength in Angstrom). For instance, sliceid=[1, 10, 80], fieldpos=-1, lbda=8000 is understood as field position -1 at 8000 A for each of the 3 slices, while sliceid=[10, 80], fieldpos=[0, -1], lbda=8000 is understood as field position 0 for slice 10 and -1 for slice 80, each at 8000 A.

  • lbda (float or array_like) – Coordinates, broadcast to the shape of sliceid (wavelength in Angstrom). For instance, sliceid=[1, 10, 80], fieldpos=-1, lbda=8000 is understood as field position -1 at 8000 A for each of the 3 slices, while sliceid=[10, 80], fieldpos=[0, -1], lbda=8000 is 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_wave

The inverse conversion.

xy_to_slice_pos_wave(x, y)[source]#

Convert detector pixels into slice, field position and wavelength.

Parameters:
  • x (float or array_like) – Detector pixel coordinates, y being broadcast to the shape of x. For instance, x=[1, 10], y=90 is understood as y=90 for both x=1 and x=10, while x=[1, 10], y=[80, 90] is understood as the two points (1, 80) and (10, 90).

  • y (float or array_like) – Detector pixel coordinates, y being broadcast to the shape of x. For instance, x=[1, 10], y=90 is understood as y=90 for both x=1 and x=10, while x=[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_xy

The 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.