pawlikMorphLSST package

Submodules

pawlikMorphLSST.Image module

This package can be extended by sub classing Image and implementing the required methods, and _IMAGE_TYPE.

class pawlikMorphLSST.Image.Image(filename=None)

Bases: abc.ABC

Abstract base class for images

abstract getHeader()
abstract getImage()
pawlikMorphLSST.Image.readImage(filename: str, ra: float, dec: float, npix=128, header=False)
Helper function that can be used to read images directly without need

to manually create Image class.

Parameters
  • filename (sty) – location of image to read

  • ra (float) – RA, right ascension of object of interest in image

  • dec (float) – DEC, declination of object of interest in image

  • npix (int, optional) – Size of cutout to return from larger image. Default is 128

  • header (bool, optional) – If true return header information as well. Default is False

Returns

  • img (np.ndarray, 2D, float) – Cutout image.

  • If header=True then also returns the header from the FITS file.

class pawlikMorphLSST.Image.sdssImage(*args, **kwargs)

Bases: pawlikMorphLSST.Image.Image

Class for SDSS images, as ingested by standard ‘method’

getHeader() → astropy.io.fits.header.Header

Returns the image header

Returns

self.header – The header for the cutout.

Return type

astropy.io.fits.Header

getImage() → numpy.ndarray

Returns the cutout image

Returns

self.image – The cutout image centered on the view provided in setview.

Return type

np.ndarray, float (2D)

setView(ra=None, dec=None, npix=128)
Get the correct view in larger image, and create the cutout on the

correct view

Parameters
  • ra (float) – RA position

  • dec (float) – DEC position

  • npix (int) – Size to make cutout

class pawlikMorphLSST.Image.lsstImage(*args, **kwargs)

Bases: pawlikMorphLSST.Image.Image

Class for SDSS images ingested via LSST dataButler.

Some metadata not available, this includes wcs, and pixel value conversion information (bscale, bzero etc). Shouldn’t really recreate butler on each image call…

This code is far from the optimal way to read images. https://github.com/LSSTScienceCollaborations/StackClub

The above source maybe of help for future developer.

getHeader()
getImage()
setView(ra, dec, run, camCol, field, filter='r', npix=128)

pawlikMorphLSST.apertures module

pawlikMorphLSST.apertures.makeaperpixmaps(npix: int, folderpath=None) → None

Writes the aperture binary masks out after calculation.

Parameters
  • npix (int) – Width of aperture image.

  • folderpath (Pathlib object) – Path to the folder where the aperture masks should be saved.

Returns

Return type

None

pawlikMorphLSST.apertures.distarr(npixx: int, npixy: int, cenpix: numpy.ndarray) → numpy.ndarray

Creates an array of distances from given centre pixel.

Near direct translation of IDL code.

Parameters
  • npixx (int) – Number of x pixels in the aperture mask.

  • npixy (int) – Number of y pixels in the aperture mask.

  • cenpix (np.ndarray) – Location of central pixels.

Returns

array of distances.

Return type

np.ndarray

pawlikMorphLSST.apertures.subdistarr(npix: int, nsubpix: int, cenpix: List[int]) → numpy.ndarray

Writes the aperture binary masks out after calculation.

Near direct translation of IDL code.

Parameters
  • npix (int) – Number of pixels in the aperture mask.

  • nsubpix (int) – Number of subpixels.

  • cenpix (List[int]) – Location of central pixels.

Returns

Array of sub-distances.

Return type

np.ndarray

pawlikMorphLSST.apertures.apercentre(apermask: numpy.ndarray, pix: numpy.ndarray) → numpy.ndarray

Function that centers a precomputed aperture mask on a given pixel.

Parameters
  • apermask (np.ndarray) – Aperture mask that is to be centred.

  • pix (List[int]) – Central pixel indicies.

Returns

mask – Returns aperture mask centered on central pixel, pix.

Return type

np.ndarray

pawlikMorphLSST.apertures.aperpixmap(npix: int, rad: float, nsubpix: int, frac: float) → numpy.ndarray

Calculate aperture binary mask.

Calculates the aperture binary mask through pixel sampling knowing the aperture radius and number of subpixels.

Near direct translation of IDL code.

Parameters
  • npix (int) – Width of aperture image.

  • rad (float) – Radius of the aperture.

  • nsubpix (int) – Number of subpixels

  • frac (float) – Fraction of something… Maybe due to Petrosian magnitude?

Returns

Numpy array that stores the mask.

Return type

np.ndarry

pawlikMorphLSST.asymmetry module

pawlikMorphLSST.asymmetry.minapix(image: numpy.ndarray, mask: numpy.ndarray, apermask: numpy.ndarray, starMask=None) → List[int]

Find the pixel that minimises the asymmetry parameter, A

Selects a range of candidate centroids within the brightest region that compromises of 20% of the total flux within object. Then measures the asymmetry of the image under rotation around that centroid. Then picks the centroid that yields the minimum A value.

Parameters
  • image (np.ndarray) – Image that the minimum asymmetry pixel is to be found in.

  • mask (np.ndarray) – Precomputed mask that describes where the object of interest is in the image

  • apermask (np.ndarray) – Precomputed aperture mask

  • starMask (np.ndarray) – Precomputed mask that masks stars that interfere with object measurement

Returns

Centroid – The minimum asymmetry pixel position.

Return type

List[int]

pawlikMorphLSST.asymmetry.calcA(img: numpy.ndarray, pixmap: numpy.ndarray, apermask: numpy.ndarray, centroid: List[int], angle: float, starMask=None, noisecorrect=False) → List[float]

Function to calculate A, the asymmetry parameter.

\[A=\frac{\sum\left|I_0-I_{\theta}\right|}{2\sum I_0}-A_{bgr}\]

Where \(I_0\) is the original image, \(I_{\theta}\) is the image rotated by \(\theta\) degrees, and \(A_{bgr}\) is the asymmerty of the sky.

See Conselice et al. for full details.

Near direct translation of IDL code.

Parameters
  • img (np.ndarray) – Image to be analysed.

  • pixmap (np.ndarray) – Mask that covers object of interest.

  • apermask (np.ndarray) – Array of the aperture mask image.

  • centroid (np.ndarray) – Pixel position of the centroid to be used for rotation.

  • angle (float) – Angle to rotate object, in degrees.

  • starMask (np.ndarray) – Precomputed mask that masks stars that interfere with object measurement

  • noisecorrect (bool, optional) – Default value False. If true corrects for background noise

Returns

A, Abgr – Returns the asymmetry value and its background value.

Return type

List(float)

pawlikMorphLSST.asymmetry.calculateAsymmetries(image: numpy.ndarray, pixelmap: numpy.ndarray) → Tuple[float]

helper function to calculate all asymmetries

Parameters
  • image (np.ndarray, 2d float) – image of a galaxy for which the asymmetries should be calculated.

  • pixelmap (np.ndarray, 2d uint8) – Pixel mask of the galaxy calculated from image.

Returns

A, As, As90 – The calculated asymmetry values.

Return type

Tuple, float

pawlikMorphLSST.casgm module

Module contains routines to calculate CAS parameters, as well as Gini, M20, R20, R80.

pawlikMorphLSST.casgm.gini(image: numpy.ndarray, mask: numpy.ndarray) → float

Calculation of the Gini index of a Galaxy.

\[g = \frac{1}{2 \bar{X} n(n-1)} \sum (2i - n - 1) \left|X_i\right|\]

Where \(\bar{X}\) is the mean over all intensities n is the total number of pixels \(X_i\) are the pixel intensities in increasing order

see Lotz et al. 2004 https://doi.org/10.1086/421849

Parameters
  • image (float, 2d np.ndarray) – Image from which the Gini index shall be calculated

  • mask (int, 2D np.ndarray) – TMask which contains the galaxies pixels

Returns

G – The Gini index.

Return type

float

pawlikMorphLSST.casgm.m20(image: numpy.ndarray, mask: numpy.ndarray) → float

Calculate the M20 statistic.

\[M_{20} = log_{10} \left(\frac{\sum M_i} {M_{tot}}\right)\]
\[While \sum f_i < 0.2 f_{tot}\]
\[M_{tot} = \sum M_i = \sum f_i [(x - x_c)^2 + (y - y_c)^2]\]

see Lotz et al. 2004 https://doi.org/10.1086/421849

Adapted from statmorph: https://github.com/vrodgom/statmorph

Parameters
  • image (float, 2d np.ndarray) – Image of galaxy

  • mask (float [0. - 1.], 2d np.ndarray) – Mask which contains the pixels belonging to the galaxy of interest.

Returns

m20 – M20 statistic

Return type

float

pawlikMorphLSST.casgm.concentration(r20: float, r80: float) → float

Calculation of the concentration of light in a galaxy.

\[C = 5log_{10}(\frac{r_{80}}{r_{20}})\]

see Lotz et al. 2004 https://doi.org/10.1086/421849

Parameters
  • r20 (float) – Radius at 20% of light

  • r80 (float) – Radius at 80% of light

Returns

C – The concentration index

Return type

float

pawlikMorphLSST.casgm.smoothness(image: numpy.ndarray, mask: numpy.ndarray, centroid: List[float], Rmax: float, r20: float, sky: float) → float

Calculate the smoothness or clumpiness of the galaxy of interest.

\[S = \frac{\sum \left|I - I_s\right| - B_s} {\sum \left|I\right|}\]

Where I is the image \(I_s\) is the smoothed image \(B_s\) is the background smoothness

see Lotz et al. 2004 https://doi.org/10.1086/421849

Parameters
  • image (float, 2d np.ndarray) – Image of galaxy

  • mask (float [0. - 1.], 2d np.ndarray) – Mask which contains the pixels belonging to the galaxy of interest.

  • centroid (List[float]) – Pixel location of the brightest pixel in galaxy.

  • Rmax (float) – Distance from brightest pixel to furthest pixel in galaxy

  • r20 (float) – Distance from brightest pixel to radius at which 20% of light of galaxy is enclosed.

  • sky (float) – Value of the sky background.

Returns

Result – The smoothness or clumpiness parameter, S.

Return type

float

pawlikMorphLSST.casgm.calcR20_R80(image: numpy.ndarray, centroid: List[float], radius: float) → Tuple[float, float]

Calculation of \(r_{20}\), and \(r_{80}\)

Parameters
  • image (float, 2d np.ndarray) – Image of galaxy

  • centroid (List[float]) – Location of the brightest pixel

  • radius (float) – Radius in which to measure galaxy light out to.

Returns

r20, r80 – The radii where 20% and 80% light falls within

Return type

Tuple[float, float]

pawlikMorphLSST.casgm.calculateCSGM(image: numpy.ndarray, mask: numpy.ndarray, skybgr: float) → Tuple[float]

Helper function that calculates the CSGM parameters

Parameters
  • image (np.ndarray, 2D float) – Image of a galxy for which the CSGM parameters are to be calculated

  • mask (np.ndarray, 2D uint8) – Image for which only the pixels that belong to the galaxy in “image” are “hot”.

  • skybgr (float) – The value of the sky background in the given image

Returns

C, S, gini, m20 – The CSGM parameters

Return type

Tuple[float]

pawlikMorphLSST.diagnostic module

pawlikMorphLSST.diagnostic.make_figure(result: Type[pawlikMorphLSST.result.Result], folder: bool, save=False, show=False) → None

Function plots results from image analysis.

Plots two or four images. Top row: original image and object map with stars overplotted if any. bottom row: Sersic fit and residual with stars overplotted if any.

Parameters
  • result (Type[Result]) – Data class container of calculated results. Must have clean image and pixelmap in order to run this function.

  • folder (bool) – If True then adjusts path to read file from.

  • save (bool, optional) – If true function saves generated figure.

  • show (bool, optional) – If true open interactive matplotlib plot.

Returns

Return type

None

pawlikMorphLSST.engines module

pawlikMorphLSST.gaussfitter module

Copyright (c) 2009-2013, Adam Ginsburg

All rights reserved.

Redistribution and use in source and binary forms, with or without modification ,are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice,this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name Adam Ginsburg nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Code adapted from Adam Ginsburg’s gaussfitter routines https://github.com/keflavich/gaussfitter

pawlikMorphLSST.gaussfitter.moments(data: numpy.ndarray, angle_guess=90.0) → List[float]

Returns (height, amplitude, x, y, width_x, width_y, rotation angle) the gaussian parameters of a 2D distribution by calculating its moments.

Parameters
  • data (np.ndarray) – data from which the Gaussian parameters will be calculated

  • angle_guess (float, optional) – Guess of the angle of the Gaussian, defual 5 degrees

Returns

params – List of parameters of the Gaussian.

Return type

List[floats]

pawlikMorphLSST.gaussfitter.twodgaussian(xydata, offset: float, amplitude: float, xo: float, yo: float, sigma_x: float, sigma_y: float, theta: float)

Returns a 2d gaussian function of the form:

\[x' = x \cos(\theta) - y \sin(\theta)\]
\[y' = x \sin(\theta)+ y \cos(\theta)\]

\(\theta\) should be in degrees

\[g = b + a exp^{\left(-\frac{((x-x_0)/x_w)^2 + ((y-y_0)/y_w)^2 }{2} \right)}\]
inpars = [b,a,center_x,center_y,width_x,width_y,rota]

(b is background height, a is peak amplitude)

where x and y are the input parameters of the returned function, and all other parameters are specified by this function

Parameters
  • xydata (List[float], List[float]) – Stack of x and y values values. xydata[0] is x and xydata[1] is y

  • offset (float) – Offset or height of the Gaussian distribution.

  • amplitude (float) – Amplitude of Gaussian.

  • yo (xo,) – Centre point of Gaussian distribution.

  • sigma_y (sigma_x,) – Standard deviation of Gaussian distribution in x and y directions.

  • theta (float) – Angle of Gaussian distribution.

Returns

g – List of computed Gaussian distribution. Array is 1D so that function is compatible with Scpiy’s curve_fit. Parameters are: Height/offset, amplitude, xo, yo, sigx, sigy, theta

Return type

List[float]

pawlikMorphLSST.helpers module

pawlikMorphLSST.helpers.getLocation(folder)

Helper function that returns a Path object containg the output folder

Parameters

folder (str) – Path to a folder for output files.

Returns

outfolder – Path to folder where data from analysis will be saved.

Return type

Path object

pawlikMorphLSST.helpers.analyseImage(info: List[Union[float, str]], *args) → List[Union[float, str]]

Helper function that calculates CASGM including As and AS90

Parameters

info (List[str, float, float]) – List of filename, RA, and DEC

Returns

A, As, AS90, C, S, gini, M20, filename , RA, DEC

Return type

Tuple[float, float, float, float, float, float, float, str, float, float]

pawlikMorphLSST.helpers.getFiles(file: str)
pawlikMorphLSST.helpers.getFilesLSST(file: str, folder: str)

pawlikMorphLSST.imageutils module

pawlikMorphLSST.imageutils.maskstarsSEG(image: numpy.ndarray) → numpy.ndarray
‘Cleans’ image of external sources.

For example will remove all stars that do not interfere with the object of interest.

Parameters

image (np.ndarray) – Image to be cleaned.

Returns

imageClean – Image cleaned of external sources.

Return type

np.ndarray

pawlikMorphLSST.imageutils.maskstarsPSF(image: numpy.ndarray, objs: List, header, skyCount: float, numSigmas=5.0, adaptive=True, sky_err=0.0) → numpy.ndarray

Use the PSF to estimate stars radius, then masks them out.

Parameters
  • image (np.ndarray) – Image that is to be masked of nuisance stars.

  • objs (List[float, float, str, float]) – List of objects. [RA, DEC, type, psfMag_r]

  • header (astropy.io.fits.header.Header) – The header of the current image. Contains information on PSF and various other parameters.

  • skyCount (float) – Sky background in counts.

  • numSigmas (optional, float) – Number of sigmas that the stars radius should extend to

Returns

mask – Array that masks stars on original image.

Return type

np.ndarray

pawlikMorphLSST.main module

pawlikMorphLSST.objectMasker module

pawlikMorphLSST.objectMasker.objectOccluded(mask: numpy.ndarray, radec: Tuple[float, float], catalogue: str, header, galaxy=False, cosmicray=False, unknown=False) → Tuple[bool, List[float]]

Function gets list of objects near the object of interest, and determines if that objects light occludeds the object of interest light.

Parameters
  • mask (np.ndarray) – Object mask

  • radec (Tuple[float, float]) – Tuple of ra, dec

  • catalogue (str) – Name of object catalogue to check against Expected format is objID: float, ra: float, dec: float, type: str

  • header – fits image header

  • galaxy (bool, optional) – Option to include galaxy objects

  • cosmicray (bool, optional) – Option to include cosmic rays

  • unknown (bool, optional) – Option to include unknown objects

Returns

Returns true alongside list of objects that occlude object mask. Otherwise returns false and an empty list

Return type

Tuple[bool, List[float]]

pawlikMorphLSST.pixmap module

pawlikMorphLSST.pixmap.pixelmap(image: numpy.ndarray, threshold: float, filterSize: int, starMask=None) → numpy.ndarray

Calculates an object binary mask.

This is acheived using a mean filter, 8 connected pixels, and a given threshold.

Parameters
  • image (np.ndarray) – Image from which the binary mask is calculated.

  • threshold (float) – Threshold for calculating 8 connectedness

  • filterSize (int) – Size of the mean filter. Must be odd

  • starMask (optional, None or np.ndarray) – Mask that mask out nuisance stars

Returns

objectMask – Calculated binary object mask

Return type

np.ndrray

pawlikMorphLSST.pixmap.calcMaskedFraction(oldMask: numpy.ndarray, starMask: numpy.ndarray, cenpix: List[float]) → float

Calculates the fraction of pixels that are masked.

Parameters
  • oldMask (np.ndarray) – Mask containing object of interest.

  • starMask (np.ndarray) – Mask containing location of star

  • cenpix (List[float]) – Centre of asymmetry in pixels.

Returns

Fraction of object pixels masked by stars

Return type

float

pawlikMorphLSST.pixmap.calcRmax(image: numpy.ndarray, mask: numpy.ndarray) → float

Function to calculate the maximum extent of a binary pixel map

Parameters
  • image (float, 2d np.ndarray) – Image of galaxy.

  • mask (np.ndarray) – Binary pixel mask

Returns

rmax – the maximum extent of the pixelmap

Return type

float

pawlikMorphLSST.pixmap.checkPixelmapEdges(mask: numpy.ndarray) → bool

Flag image if galaxy pixels are on edge of image.

Parameters

mask (np.ndarray) – pixelmap to check

Returns

flag – If true then the pixelmap hits the edge of the image

Return type

bool

pawlikMorphLSST.result module

class pawlikMorphLSST.result.Result(file: str, outfolder: Any, occludedFile: str, pixelMapFile: Any = '', cleanImage: Any = '', starMask: Any = '', objList: Any = <factory>, A: List[float] = <factory>, As: List[float] = <factory>, As90: List[float] = <factory>, rmax: float = -99, apix: Tuple[float] = (-99.0, -99.0), sky: float = -99.0, sky_err: float = 99.0, fwhms: List[float] = <factory>, theta: float = -99.0, r20: float = -99.0, r80: float = -99.0, C: float = -99.0, gini: float = -99.0, m20: float = -99.0, S: float = -99.0, sersic_amplitude: float = -99.0, sersic_r_eff: float = -99.0, sersic_n: float = -99.0, sersic_x_0: float = -99.0, sersic_y_0: float = -99.0, sersic_ellip: float = -99.0, sersic_theta: float = -99.0, time: float = 0.0, star_flag: bool = False, maskedPixelFraction: float = -99.0, objectEdge: bool = False)

Bases: object

Data class that stores the results of image analysis.

A: List[float] = None

Calculated asymmetry value, format [A, A_error]

As: List[float] = None

Calculated shape asymmetry value, format [As, As_error]

As90: List[float] = None

Calculated shape asymmetry 90 value, format [As90, As90_error]

C: float = -99.0

Concentraion value

S: float = -99.0

Smoothness value.

apix: Tuple[float] = (-99.0, -99.0)

Asymmetry (A) minimised central pixel

cleanImage: Any = ''

path to clean image.

file: str = None

Filename of image

fwhms: List[float] = None

FWHM’s of the fitted 2D Gaussian

gini: float = -99.0

Gini index

m20: float = -99.0

M20 value

maskedPixelFraction: float = -99.0

Fraction of pixels masked due to occluding star/object.

objList: Any = None

List of objects RA, DECs that occlude objects segmentation map.

objectEdge: bool = False

If true then the segmentation map extends to an edge of the image.

occludedFile: str = None

Filename of output data for objects that occlude with segmentation map.

outfolder: Any = None

Output folder for saving data

pixelMapFile: Any = ''

Path to segmentation map

r20: float = -99.0

Radius in which 20% of total light flux is contained

r80: float = -99.0

Radius in which 80% of total light flux is contained

rmax: float = -99

Maxmimum radius of the segmentation map

sersic_amplitude: float = -99.0

Sersic amplitude.

sersic_ellip: float = -99.0

Sersic ellipticity.

sersic_n: float = -99.0

Sersic index.

sersic_r_eff: float = -99.0

Sersic effective radius

sersic_theta: float = -99.0

Sersic rotation.

sersic_x_0: float = -99.0

Sersic x centre

sersic_y_0: float = -99.0

Sersic y centre

sky: float = -99.0

Sky background value.

sky_err: float = 99.0

Sky background error.

starMask: Any = ''

path to star mask

star_flag: bool = False

If true means that there is a star in the catalogue occluding the objects segmentation map

theta: float = -99.0

Theta of the fitted 2D Gaussian

time: float = 0.0

Time taken to analyse image.

write(objectfile)

Write out result as a row to a csv file

pawlikMorphLSST.sersic module

pawlikMorphLSST.sersic.fitSersic(image: numpy.ndarray, centroid: List[float], fwhms: List[float], theta: float, starMask=None)

Function that fits a 2D sersic function to an image of a Galaxy.

Parameters
  • image (np.ndarray) – image to which a 2D Sersic function will be fit

  • centroid (List[float]) – Centre of object of interest

  • fwhms (List[float]) – Full width half maximums of object

  • theta (float) – rotation of object anticlockwise from positive x axis

  • starMask (np.ndarray) – Mask contains star locations.

Returns

Parameters – Collection of best fit parameters for the 2D sersic function

Return type

astropy.modeling.Model object

pawlikMorphLSST.skyBackground module

pawlikMorphLSST.skyBackground.skybgr(img: numpy.ndarray, largeImage=None, file=None, imageSource=None) → Tuple[float, float, List[float], float]

Helper function for calculating skybgr

Parameters
  • img (np.ndarray) – image from which a sky background will be calculated

  • file (Path object, optional) – Path to image

  • largeImage (np.ndarray) – If not None, algorithm uses larger image to estimate sky background.

  • imageSource (str, optional) – Telescope source of the image. Default is SDSS

Returns

  • sky (float) – Estimation of the sky background value in counts

  • sky_err (float) – Error in sky background value in counts

  • fwhms (List[float]) – FWHM in x and y direction of the fitted Gaussian.

  • theta (float) – Angle of the fitted Gaussian in radians measured from the +ive x axis anticlockwise

Module contents

A package to analyse images of galaxies in to determine various morphological properties.