mrf package

mrf.celestial module

class mrf.celestial.Celestial(img, mask=None, header=None, dataset='Dragonfly', scale_bar_length=5)

Bases: object

Class for Celestial object. This class is basically a celestial body from observational perspective. It has its image, header, WCS. The mask which masks out contaminations can also be stored as an attribute. Then this Celestial object can be saved to FITS file, can be shifted, resized, rotated, etc. What’s more, the user could check the image/mask/masked image simply by invoke Celestial.display_image().

This class can also be inherited to make other classes.

display_Celestial(**kwargs)

Take a peek at the masked image, using “zscale”, “arcsinh” streching and “viridis” colormap. You can change them by adding **kwargs.

Parameters:**kwargs – arguments in mrf.display.display_single.
Returns:None
display_image(**kwargs)

Take a peek at the image, using “zscale”, “arcsinh” streching and “viridis” colormap. You can change them by adding **kwargs.

Parameters:**kwargs – arguments in mrf.display.display_single.
Returns:None
display_mask(**kwargs)

Take a peek at the mask.

Parameters:**kwargs – arguments in mrf.display.display_single.
Returns:None
hscmask
image
mask
resize_Celestial(f, method='cubic', order=5, cval=0.0)

Resize the Celestial object, including both image and mask. f > 1 means the image/mask will be resampled! f < 1 means the image/mask will be degraded.

Parameters:
  • f (float) – the positive factor of zoom. If 0 < f < 1, the mask will be resized to smaller one.
  • method (str) – interpolation method. Use ‘lanczos’ or ‘spline’ or ‘iraf’. ‘Lanczos’ requires GalSim installed.
  • order (int) – the order Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

None

resize_image(f, method='cubic', order=3, cval=0.0)

Zoom/Resize the image of Celestial object. f > 1 means the image will be resampled (finer)! f < 1 means the image will be degraded.

Parameters:
  • f (float) – the positive factor of zoom. If 0 < f < 1, the image will be resized to smaller one.
  • method (str) – interpolation method. Use ‘lanczos’, ‘cubic’, ‘quintic’ or ‘iraf’. First three methods require GalSim installed. Other methods are now consistent with “iraf” results.
  • order (int) – the order Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

resized image. The “image” attribute of Celestial class will also be changed accordingly.

Return type:

resize_image (ndarray)

resize_mask(f, method='cubic', order=5, cval=0.0)

Zoom/Resize the mask of Celestial object. f > 1 means the mask will be resampled (finer)! f < 1 means the mask will be degraded.

Parameters:
  • f (float) – the positive factor of zoom. If 0 < f < 1, the mask will be resized to smaller one.
  • method (str) – interpolation method. Use ‘lanczos’, ‘cubic’, ‘quintic’ or ‘iraf’. First three methods require GalSim installed. Other methods are now consistent with “iraf” results.
  • order (int) – the order Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

resized image. The “mask” attribute of Celestial class will also be changed accordingly.

Return type:

resize_mask (ndarray)

save_to_fits(fits_file_name, data='image', overwrite=True)

Save image or mask of this Celestial object to fits file. We operate wcs directly on header!

Parameters:
  • fits_file_name (str) – File name of fits file
  • data (str) – can be ‘image’ or ‘mask’
  • overwrite (bool) – Default is True
Returns:

None

shift_Celestial(dx, dy, method='spline', order=5, cval=0.0)

Shift the Celestial object, including image and mask.

Parameters:
  • dx (float) – shift distance (in pixel) along x (horizontal). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the image “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • dy (float) – shift distance (in pixel) along y (vertical). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the image “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • method (str) – interpolation method. Use ‘spline’, lanczos’ or ‘iraf’. If using ‘iraf’, default interpolation is ‘poly3. ‘Lanczos’ requires GalSim installed.
  • order (int) – the order of Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

None

shift_image(dx, dy, method='spline', order=5, cval=0.0)

Shift the image of Celestial object. The WCS of image will also be changed.

Parameters:
  • dx (float) – shift distance (in pixel) along x (horizontal). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the image “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • dy (float) – shift distance (in pixel) along y (vertical). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the image “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • method (str) – interpolation method. Use ‘spline’, lanczos’ or ‘iraf’. If using ‘iraf’, default interpolation is ‘poly3. ‘Lanczos’ requires GalSim installed.
  • order (int) – the order of Spline or Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

shifted image, the “image” attribute of Celestial class will also be changed accordingly.

Return type:

shift_image (ndarray)

shift_mask(dx, dy, method='spline', order=5, cval=0.0)

Shift the mask of Celestial object.

Parameters:
  • dx (float) – shift distance (in pixel) along x (horizontal). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the mask “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • dy (float) – shift distance (in pixel) along y (vertical). Note that elements in one row has the same y but different x. Example: dx = 2 is to shift the mask “RIGHT” (as seen in DS9), dy = 3 is to shift the image “UP”.
  • method (str) – interpolation method. Use ‘spline’, lanczos’ or ‘iraf’. If using ‘iraf’, default interpolation is ‘poly3. ‘Lanczos’ requires GalSim installed.
  • order (int) – the order of Spline or Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

shifted mask. The “mask” attribute of Celestial class will also be changed accordingly.

Return type:

shift_mask (ndarray)

variance
class mrf.celestial.Star(img, header, starobj, colnames=['x', 'y'], halosize=40, padsize=40, mask=None, hscmask=None)

Bases: mrf.celestial.Celestial

This Star class is the inheritance of Celestial class. It represents a small cutout, which is typically a star. Other than the functions inherited from Celestial, Star object has extra functions such as centralize, mask_out_contam.

centralize(method='spline', order=5, cval=0.0)

Shift the cutout to the true position of the star using interpolation.

Parameters:
  • method (str) – interpolation method. Options are “iraf” and “lanczos”. “Lanczos” requires GalSim installed.
  • order (int) – the order of Lanczos interpolation (>0).
  • cval (float) – value to fill the edges. Default is 0.
Returns:

None

get_masked_image(cval=nan)

Mask image according to the mask.

Parameter:
cval: value to fill the void. Default is NaN, but sometimes NaN is problematic.
Returns:masked image.
Return type:imgcp (numpy 2-D array)
mask_out_contam(sigma=4.5, deblend_cont=0.0005, blowup=True, cval=nan, show_fig=True, verbose=True)

Mask out contamination in the cutout of star. Contamination may be stars, galaxies or artifacts. This function uses sep to identify and mask contamination.

Parameters:
  • sigma (float) – The sigma in SExtractor. Default is 4.5.
  • deblend_cont (float) – Deblending parameter. Default is 0.0005.
  • blowup (bool) – Whether blow up the segmentation mask by convolving a 1.5 pixel Gaussian kernel.
  • show_fig (bool) – Whether show the figure.
  • verbose (bool) – Whether print out results.
Returns:

None

sub_bkg(sigma=4.5, deblend_cont=0.0001, verbose=True)

Subtract the locally-measured background of Star object. The sky is measured by masking out objects using sep. Be cautious and be aware what you do when using this function.

Parameters:
  • sigma (float) – The sigma in SExtractor.
  • deblend_cont (float) – Deblending parameter.
  • verbose (bool) – Whether print out background value.
Returns:

None

mrf.task module

class mrf.task.Config(d)

Bases: object

Configuration class.

class mrf.task.MrfTask(config_file)

Bases: object

MRF task class. This class implements mrf.

run(dir_lowres, dir_hires_b, dir_hires_r, certain_gal_cat, output_name='mrf', verbose=True)

Run MRF task.

Parameters:
  • dir_lowres (string) – directory of input low-resolution image.
  • dir_hires_b (string) – directory of input high-resolution blue-band image (typically g-band).
  • dir_hires_r (string) – directory of input high-resolution red-band image (typically r-band).
  • certain_gal_cat (string) – directory of a catalog (in ascii format) which contains RA and DEC of galaxies which you want to retain during MRF.
  • output_name (string) – which will be the prefix of output files.
  • verbose (bool) – If True, it will make a log file recording the process.
Returns:

containing key results of this task.

Return type:

results (Results class)

set_logger(verbose=True)

Set logger for MrfTask. The logger will record the time and each output. The log file will be saved locally.

Parameters:verbose (bool) – If False, the logger will be silent.
Returns:logger (logging.logger object)
class mrf.task.Results(config)

Bases: object

Results class. Other attributes will be added by setattr().

mrf.utils module

mrf.utils.Autokernel(img_hires, img_lowres, s, d, object_cat_dir=None, frac_maxflux=0.1, fwhm_upper=14, nkernels=20, border=50, show_figure=True, logger=None)

Automatically generate kernel from an image, by detecting star position and make kernel from them. Here all images are subsampled, and all coordinates are in subsampled image’s coordinate. Notice that s and d are half-size of kernel. Typically s > d.

Parameters:
  • img_hires (Celestial class) – high resolution image
  • img_lowres (Celestial class) – low resolution image
  • s (float) – kernel half-size, in pixel
  • d (float) – PSF half-size, in pixel
  • object_cat_dir (str) – the directory of object catalog in fits. This catalog contains all stars that can be used to construct PSF of high-res image.
  • frac_maxflux (float) – only account for stars whose flux is less than frac_maxflux * maxflux
  • fwhm_upper (float) – we only select objects whose FWHMs are less than this value.
  • nkernels (int) – number of stars, from which the kernel is generated
  • border (int) – don’t select stars with in the border/edge of image
  • show_figure (bool) – Whether show the kernels and save the figure
  • logger (logging.logger object) – logger for this task.
Returns:

the median kernel generated from given image

Return type:

kernel_median (numpy 2-D array)

class mrf.utils.Config(d)

Bases: object

Configuration class.

mrf.utils.Flux_Model(img, header, b=64, f=3, sigma=2.5, minarea=3, deblend_cont=0.005, deblend_nthresh=32, save=False, output_suffix='flux_model', logger=None)

Extract sources from given image and return a flux map (not segmentation map). The flux map will be saved as ‘_flux_’ + output_suffix + ‘.fits’, along with segmentation map.

Parameters:
  • img (numpy 2-D array) – Image itself
  • header – the header of the image
  • sigma (float) – We detect objects above this sigma
  • minarea (float) – minimum area of the object, in pixels
  • deblend_cont (float) – Minimum contrast ratio used for object deblending. Default is 0.005. To entirely disable deblending, set to 1.0.
  • deblend_nthresh (float) – Number of thresholds used for object deblending. Default is 32.
  • output_suffix (str) – Suffix of output image and segmentation map.
Returns:

objects (astropy.table.Table class): Table of detected objects. segmap (numpy 2-D array): Segmentation map. im_fluxes (numpy 2-D array): Flux map.

mrf.utils.Makekernel(img_hires, img_lowres, obj, s, d, window=<photutils.psf.matching.windows.CosineBellWindow object>)

Given position, make kernel based on this position image. Here we don’t normalize PSF! Don’t use photutils.psf.creat_matching_kernel.

Parameters:
  • img_hires (2-D numpy array) – high resolution image
  • img_lowres (2-D numpy array) – low resolution image
  • x (float) – x-position of star, where kernel is made
  • y (float) – y-position of star, where kernel is made
  • s (float) – kernel half-size, in pixel
  • d (float) – PSF half-size, in pixel
Returns:

kernel (numpy 2-D array): kernel generated by given position hires_cut (numpy 2-D array): the local cutout from high resolution image lowres_cut (numpy 2-D array): the local cutout from low resolution image

mrf.utils.add_tractor_sources(obj_cat, sources, w, shape_method='manual')

Add tractor sources to the sources list.

Parameters:
  • obj_cat – astropy Table, objects catalogue.
  • sources – list, to which we will add objects.
  • w – wcs object.
  • shape_method – string, ‘manual’ or ‘decals’. If ‘manual’, it will adopt the manually measured shapes. If ‘decals’, it will adopt ‘DECaLS’ tractor shapes.
Returns:

list of sources.

Return type:

sources

mrf.utils.azimuthal_average(image, center=None, stddev=True, binsize=0.5, interpnan=False)

Calculate the azimuthally averaged radial profile. Modified based on https://github.com/keflavich/image_tools/blob/master/image_tools/radialprofile.py

Parameters:
  • imgae (numpy 2-D array) – image array.
  • center (list) – [x, y] pixel coordinates. If None, use image center. Note that x is horizontal and y is vertical, y, x = image.shape.
  • stdev (bool) – if True, the stdev of profile will also be returned.
  • binsize (float) – size of the averaging bin. Can lead to strange results if non-binsize factors are used to specify the center and the binsize is too large.
  • interpnan (bool) – Interpolate over NAN values, i.e. bins where there is no data?
Returns:

If stdev == True, it will return [radius, profile, stdev]; else, it will return [radius, profile].

mrf.utils.bright_star_mask(mask, catalog, bright_lim=17.5, r=2.0)

Mask out bright stars on the segmentation map of high resolution image, before degrading to low resolution.

Parameters:
  • mask (numpy 2-D array) – the mask.
  • catalog (astropy.table.Table object) – a catalog containing all objects detected using sep.
  • bright_lim (float) – Stars brighter than this value will be masked out.
  • r (float) – Blow-up factor of mask.
Returns:

segmentation map after removing bright stars (1 -> 0).

Return type:

segmap_cp (numpy 2-D array)

mrf.utils.circularize(img, n=14, print_g=True)

Circularize an image/kernel. Inspired by http://adsabs.harvard.edu/abs/2011PASP..123.1218A.

Parameters:
  • img (numpy 2-D array) – image
  • n (int) – times of circularization. For example, the output image will be invariant under rotation theta = 360 / 2^14 if n=14.
  • print_g (bool) – if true, a parameter describing the asymmetricness of input image. g=0 means perfectly symmetric.
Returns:

circularized image.

Return type:

img_cir (numpy 2-D array)

mrf.utils.create_matching_kernel_custom(source_psf, target_psf, window=None)

Create a kernel to match 2D point spread functions (PSF) using the ratio of Fourier transforms. Reference: https://photutils.readthedocs.io/en/stable/api/photutils.create_matching_kernel.html?highlight=create_kernel.

Parameters:
  • source_psf (numpy 2-D array) – The source PSF. The source PSF should have higher resolution (i.e. narrower) than the target PSF. source_psf and target_psf must have the same shape and pixel scale.
  • target_psf (numpy 2-D array) – The target PSF. The target PSF should have lower resolution (i.e. broader) than the source PSF. source_psf and target_psf must have the same shape and pixel scale.
  • window

    The window (or taper) function or callable class instance used to remove high frequency noise from the PSF matching kernel. Some examples include: * photutils.psf.matching.HanningWindow * photutils.psf.matching.TukeyWindow * photutils.psf.matching.CosineBellWindow * photutils.psf.matching.SplitCosineBellWindow * photutils.psf.matching.TopHatWindow

    For more information on window functions and example usage, see https://photutils.readthedocs.io/en/stable/psf_matching.html#psf-matching.

Returns:

The matching kernel to go from source_psf to target_psf.

The output matching kernel is normalized such that it sums to 1.

Return type:

kernel (numpy 2-D array)

mrf.utils.extract_obj(img, b=64, f=3, sigma=5, pixel_scale=0.168, minarea=5, deblend_nthresh=32, deblend_cont=0.005, clean_param=1.0, sky_subtract=False, flux_auto=True, flux_aper=None, show_fig=True, verbose=True, logger=None)

Extract objects for a given image using sep (a Python-wrapped SExtractor). For more details, please check http://sep.readthedocs.io and documentation of SExtractor.

Parameters:
  • img (numpy 2-D array) – input image
  • b (float) – size of box
  • f (float) – size of convolving kernel
  • sigma (float) – detection threshold
  • pixel_scale (float) – default is 0.168 (HSC pixel size). This only affect the figure scale bar.
  • minarea (float) – minimum number of connected pixels
  • deblend_nthresh (float) – Number of thresholds used for object deblending
  • deblend_cont (float) – Minimum contrast ratio used for object deblending. Set to 1.0 to disable deblending.
  • clean_param (float) – Cleaning parameter (see SExtractor manual)
  • sky_subtract (bool) – whether subtract sky before extract objects (this will affect the measured flux).
  • flux_auto (bool) – whether return AUTO photometry (see SExtractor manual)
  • flux_aper (list) – such as [3, 6], which gives flux within [3 pix, 6 pix] annulus.
Returns:

objects: astropy Table, containing the positions,

shapes and other properties of extracted objects.

segmap: 2-D numpy array, segmentation map

mrf.utils.img_cutout(img, wcs, coord_1, coord_2, size=60.0, pixel_scale=2.5, pixel_unit=False, img_header=None, prefix='img_cutout', out_dir=None, save=True)

Generate image cutout with updated WCS information. (From kungpao https://github.com/dr-guangtou/kungpao)

Parameters:
  • img (numpy 2-D array) – image array.
  • wcs (astropy.wcs.WCS object) – WCS of input image array.
  • coord_1 (float) – ra or x of the cutout center.
  • coord_2 (float) – dec or y of the cutout center.
  • size (float) – image size, in arcsec unit by default.
  • pixel_scale (float) – pixel size, in the unit of “arcsec/pixel”.
  • pixel_unit (bool) – When True, coord_1, cooord_2 becomes X, Y pixel coordinates. size will also be treated as in pixels.
  • img_header – The header of input image, typically astropy.io.fits.header object. Provide the haeder in case you can save the infomation in this header to the new header.
  • prefix (str) – Prefix of output files.
  • out_dir (str) – Directory of output files. Default is the current folder.
  • save (bool) – Whether save the cutout image.
Returns:

cutout (numpy 2-D array): the cutout image.

[cen_pos, dx, dy]: a list contains center position and dx, dy.

cutout_header: Header of cutout image.

mrf.utils.img_replace_with_noise(img, mask)

This function add Gaussian noise to the masked region. The characteristics of Gaussian is decided by sep locally measured sky value and stderr. (from kungpao https://github.com/dr-guangtou/kungpao)

Parameters:
  • img (numpy 2-D array) – image.
  • mask (numpy 2-D array) – pixels that you want to mask out should have value of ONE.
Returns:

image with mask replaced by noise

Return type:

img_noise_replace (numpy 2-D array)

mrf.utils.mask_out_certain_galaxy(segmap, header, gal_cat=None, logger=None)

Mask out certain galaxy on segmentation map.

Parameters:
  • segmap (2-D numpy array) – segmentation map, on which bright stars will be masked.
  • img (2-D numpy array) – image itselt.
  • header (astropy.io.fits.header object) – the header of this image.
  • gal_cat (astropy.table.Table object) – catalog of galaxies you want to mask out. Must have columns called ra and dec (or x and y).
Returns:

segmentation map after removing bright stars.

Return type:

segmap_cp (numpy 2-D array)

mrf.utils.mask_out_stars(segmap, img, header, method='gaia', bright_lim=15.5, catalog_dir=None)

Mask out bright stars on the segmentation map of high resolution image, before degrading to low resolution.

Parameters:
  • segmap (2-D numpy array) – segmentation map, on which bright stars will be masked.
  • img (2-D numpy array) – image itselt.
  • header – the header of this image.
  • method (str) – here three methods are provided: ‘gaia’, ‘apass’ or ‘usno’.
  • bright_lim (float) – the magnitude limit of stars to be masked out.
  • catalog_dir (str) – optional, you can provide local catalog here.
Returns:

segmentation map after removing bright stars.

Return type:

segmap_cp

mrf.utils.mask_out_stars_hsc(segmap, img, header, method='gaia', bright_lim=15.5, catalog_dir=None)

For HSC, the bleeding tails are annoying. So this function is for them. Mask out bright stars on the segmentation map of high resolution image, before degrading to low resolution.

Parameters:
  • segmap (2-D numpy array) – segmentation map, on which bright stars will be masked.
  • img (2-D numpy array) – image itselt.
  • header – the header of this image.
  • method (str) – here three methods are provided: ‘gaia’, ‘apass’ or ‘usno’.
  • bright_lim (float) – the magnitude limit of stars to be masked out.
  • catalog_dir (str) – optional, you can provide local catalog here.
Returns:

segmentation map after removing bright stars.

Return type:

segmap_cp

mrf.utils.mask_remove_cen_obj(mask)

Remove the central object from the binary 0-1 mask.

Parameters:mask (numpy 2-D array) – binary mask, in which 1 means the pixel will be masked from image.
Returns:a mask with central object removed (1 -> 0).
Return type:mask_copy (numpy 2-D array)
mrf.utils.mask_remove_obj(mask, x, y)

Remove an object from the mask given its coordinate.

Parameters:
  • mask (numpy 2-D array) – binary mask
  • x (int) – x-coordinate. (Remember here x is columns, and y is rows, follows the sep convention, not numpy one.)
  • y (int) – y-coordinate.
Returns:

the mask with certain object removed

Return type:

mask_copy (numpy 2-D array)

mrf.utils.phys_size(redshift, H0=70, Omegam=0.3, verbose=True)
Calculate the corresponding physical size per arcsec of a given redshift
in the Flat Lambda-CDM cosmology.
Parameters:
  • redshift (float) – redshift at which you want to calculate.
  • H0 (float) – Hubble constant, in the unit of km/s/Mpc.
  • Omegam (float) – density parameter of matter $\Omega_m$. It should be within [0, 1].
  • verbose (bool) – If true, it will print out the physical scale at the given redshift.
Returns:

The corresponding physical size (not comoving) per arcsec of a given redshift.

Return type:

physical_size (float)

mrf.utils.psf_bkgsub(psf, edge)

Subtract the background of PSF, estimated by the pixel value around the edges of PSF. This is different from Star.sub_bkg, in which we estimate background using sep.

Parameters:
  • psf (numpy 2-D array) – PSF itself
  • edge (int) – size of edge, within which we measure the local background
mrf.utils.query_star(img, header, method='gaia', bright_lim=15.5, catalog_dir=None)

Query stars within a given field, return a catalog.

Parameters:
  • img (numpy 2-D array) – image array.
  • header (astropy.io.fits.header object) – the header of this image.
  • method (str) – here three methods are provided: ‘gaia’, ‘apass’ or ‘usno’. “gaia” will be slower than the other two methods.
  • bright_lim (float) – the magnitude limit of stars to be masked out.
  • catalog_dir (str) – optional, you can provide local catalog here.
Returns:

star_cat (astropy.table.Table object)

mrf.utils.readStarCatalog(catalog, img, img_header, ra_dec_name=None, mag_name=None, bright_lim=15.5)
Read a given catalog (astropy Table objects). Note that the column names of RA and DEC
must be ‘ra’ and ‘dec’ or ‘RA’ and ‘DEC’. One magnitude column shuold be included and named ‘mag’ or ‘MAG’.
Parameters:
  • catalog – given catalog, must contain ra, dec and some magnitude (such as r-mag).
  • ra_dec_name – [‘ra’, ‘dec’]
  • img (2-D numpy array) – image itselt.
  • img_header – the header of this image.
  • bright_lim – the magnitude limit of stars to be masked out.
Returns:

2-D numpy table, contains ‘x’ and ‘y’ coordinate of bright stars. nx, ny: dimension of image.

Return type:

xys

mrf.utils.remove_lowsb(flux_model, conv_model, kernel, segmap, objcat_dir, SB_lim=24.0, zeropoint=30.0, pixel_size=0.83, unmask_ratio=6, gaussian_radius=1.5, gaussian_threshold=0.01, logger=None)

Remove low surface brightness features from Flux Model. For more details please read the corresponding sections in van Dokkum et al. 2019 PASP.

Parameters:
  • flux_model (numpy 2-D array) – Flux model, generated by FluxModel function.
  • conv_model (numpy 2-D array) – The flux model after convolved with MRF kernel.
  • kernel (numpy 2-D array) – MRF kernel.
  • segmap (numpy 2-D array) – Segmentation map yielded by extract_obj function.
  • objcat_dir (str) – The directory of object catalog, which is also yielded by extract_obj function.
  • SB_lim (float) – Surface brightness limit. Objects fainter than this will be masked.
  • zeropoint (float) – The photometric zeropoint of high-resolution image.
  • pixel_size (float) – Pixel size of flux_model, after rebinning high-resolution image.
  • unmask_ratio (float) – We mask out faint objects with $E<unmask_ratio$.
  • gaussian_radius (float) – A 2-D Gaussian is called to smooth the mask a little bit.
  • gaussian_threshold (float) – A 2-D Gaussian is called to smooth the mask a little bit.
  • logger (logging.logger object) – logger for this task.
Returns:

Flux model after removing low-SB features.

Return type:

im_highres (numpy 2-D array)

mrf.utils.save_to_fits(img, fits_file, wcs=None, header=None, overwrite=True)

Save numpy 2-D arrays to fits file. (from kungpao https://github.com/dr-guangtou/kungpao)

Parameters:
  • img (numpy 2-D array) – The 2-D array to be saved.
  • fits_file (str) – File name of fits file.
  • wcs (astropy.wcs.WCS object) – World coordinate system (WCS) of this image.
  • header (astropy.io.fits.header or str) – header of this image.
  • overwrite (bool) – Whether overwrite the file. Default is True.
Returns:

img_hdu (astropy.fits.PrimaryHDU object)

mrf.utils.seg_remove_cen_obj(seg)

Remove the central object from the segmentation map yielded by sep or SExtractor.

Parameters:seg (numpy 2-D array) – segmentation map
Returns:the segmentation map with central object removed
Return type:seg_copy (numpy 2-D array)
mrf.utils.seg_remove_obj(seg, x, y)

Remove an object from the segmentation given its coordinate.

Parameters:
  • seg (numpy 2-D array) – segmentation mask
  • x (int) – x-coordinate. (Remember here x is columns, and y is rows, follows the sep convention, not numpy one.)
  • y (int) – y-coordinate.
Returns:

the segmentation map with certain object removed

Return type:

seg_copy (numpy 2-D array)

mrf.utils.tractor_iteration(obj_cat, w, img_data, invvar, psf_obj, pixel_scale, shape_method='manual', kfold=4, first_num=50, fig_name=None)

Run tractor iteratively.

Parameters:
  • obj_cat – objects catalogue.
  • w – wcs object.
  • img_data – 2-D np.array, image.
  • invvar – 2-D np.array, inverse variance matrix of the image.
  • psf_obj – PSF object, defined by tractor.psf.PixelizedPSF() class.
  • pixel_scale – float, pixel scale in unit arcsec/pixel.
  • shape_method – if ‘manual’, then adopt manually measured shape. If ‘decals’, then adopt DECaLS shape from tractor files.
  • kfold – int, iteration time.
  • first_num – how many objects will be fit in the first run.
  • fig_name – string, if not None, it will save the tractor subtracted image to the given path.
Returns:

list, containing tractor model sources. trac_obj: optimized tractor object after many iterations.

Return type:

sources

mrf.display module

mrf.display.display_single(img, pixel_scale=0.168, physical_scale=None, xsize=8, ysize=8, ax=None, stretch='arcsinh', scale='zscale', contrast=0.25, no_negative=False, lower_percentile=1.0, upper_percentile=99.0, cmap=<matplotlib.colors.ListedColormap object>, scale_bar=True, scale_bar_length=5.0, scale_bar_fontsize=20, scale_bar_y_offset=0.5, scale_bar_color='w', scale_bar_loc='left', color_bar=False, color_bar_loc=1, color_bar_width='75%', color_bar_height='5%', color_bar_fontsize=18, color_bar_color='w', add_text=None, text_fontsize=30, text_y_offset=0.8, text_color='w')

Display single image using arcsinh stretching, “zscale” scaling and viridis colormap as default. This function is from kungpao https://github.com/dr-guangtou/kungpao.

Parameters:
  • img (numpy 2-D array) – The image array.
  • pixel_scale (float) – The pixel size, in unit of “arcsec/pixel”.
  • physical_scale (bool) – Whether show the image in physical scale.
  • xsize (int) – Width of the image, default = 8.
  • ysize (int) – Height of the image, default = 8.
  • ax (matplotlib.pyplot.axes object) – The user could provide axes on which the figure will be drawn.
  • stretch (str) – Stretching schemes. Options are “arcsinh”, “log”, “log10” and “linear”.
  • scale (str) – Scaling schemes. Options are “zscale” and “percentile”.
  • contrast (float) – Contrast of figure.
  • no_negative (bool) – If true, all negative pixels will be set to zero.
  • lower_percentile (float) – Lower percentile, if using scale="percentile".
  • upper_percentile (float) – Upper percentile, if using scale="percentile".
  • cmap (str) – Colormap.
  • scale_bar (bool) – Whether show scale bar or not.
  • scale_bar_length (float) – The length of scale bar.
  • scale_bar_y_offset (float) – Offset of scale bar on y-axis.
  • scale_bar_fontsize (float) – Fontsize of scale bar ticks.
  • scale_bar_color (str) – Color of scale bar.
  • scale_bar_loc (str) – Scale bar position, options are “left” and “right”.
  • color_bar (bool) – Whether show colorbar or not.
  • add_text (str) – The text you want to add to the figure. Note that it is wrapped within $\mathrm{}$.
  • text_fontsize (float) – Fontsize of text.
  • text_y_offset (float) – Offset of text on y-axis.
  • text_color (str) – Color of text.
Returns:

If the input ax is not None.

Return type:

ax

mrf.display.display_multiple(data_array, text=None, ax=None, **kwargs)

Display multiple images together using the same strecth and scale.

Parameters:
  • data_array (list) – A list containing images which are numpy 2-D arrays.
  • text (str) – A list containing strings which you want to add to each panel.
  • ax (list) – The user could provide a list of axes on which the figure will be drawn.
  • **kwargs – other arguments in display_single.
Returns:

If the input ax is not None.

Return type:

axes

mrf.display.draw_rectangles(img, catalog, colnames=['x', 'y'], header=None, ax=None, rectangle_size=[30, 30], pixel_scale=0.168, color='r', **kwargs)

Draw rectangles on an image according to a catalogue.

Parameters:
  • img (numpy 2-D array) – Image itself.
  • catalog (astropy.table.Table object) – A catalog which contains positions.
  • colnames (list) – List of string, indicating which columns correspond to positions. It can also be “ra” and “dec”, but then “header” is needed.
  • header – Header file of a FITS image containing WCS information, typically astropy.io.fits.header object.
  • ax (matplotlib.pyplot.axes object) – The user could provide axes on which the figure will be drawn.
  • rectangle_size (list of floats) – Size of rectangles, in pixel.
  • pixel_scale (float) – Pixel size, in arcsec/pixel. Needed for correct scale bar.
  • color (str) – Color of rectangles.
  • **kwargs – other arguments of display_single.
Returns:

If the input ax is not None.

Return type:

ax

mrf.display.draw_circles(img, catalog, colnames=['x', 'y'], header=None, ax=None, circle_size=30, pixel_scale=0.168, color='r', **kwargs)

Draw circles on an image according to a catalogue.

Parameters:
  • img (numpy 2-D array) – Image itself.
  • catalog (astropy.table.Table object) – A catalog which contains positions.
  • colnames (list) – List of string, indicating which columns correspond to positions. It can also be “ra” and “dec”, but then “header” is needed.
  • header – Header file of a FITS image containing WCS information, typically astropy.io.fits.header object.
  • ax (matplotlib.pyplot.axes object) – The user could provide axes on which the figure will be drawn.
  • circle_size (float) – Radius of circle, in pixel.
  • pixel_scale (float) – Pixel size, in arcsec/pixel. Needed for correct scale bar.
  • color (str) – Color of circles.
  • **kwargs – other arguments of display_single.
Returns:

If the input ax is not None.

Return type:

ax

mrf.display.display_isophote(img, ell, pixel_scale, scale_bar=True, scale_bar_length=50, physical_scale=None, text=None, ax=None, contrast=None, circle=None)

Visualize the isophotes.

Parameters:
  • img (numpy 2-D array) – Image array.
  • ell – astropy Table or numpy table, is the output of ELLIPSE.
  • pixel_scale (float) – Pixel scale in arcsec/pixel.
  • scale_bar (boolean) – Whether show scale bar.
  • scale_bar_length (float) – Length of scale bar.
  • physical_scale (float) – If not None, the scale bar will be shown in physical scale (kpc).
  • text (string) – If not None, the text will be shown in the upper left corner.
  • ax (matplotlib.pyplot.axes object) – The user could provide axes on which the figure will be drawn.
  • contrast (float) – Default contrast is 0.15.
  • circle (list of floats) – This shows several circles with different radii. Maximun three circles.
Returns:

If the input ax is not None.

Return type:

ax

mrf.display.SBP_single(ell_fix, redshift, pixel_scale, zeropoint, ax=None, offset=0.0, x_min=1.0, x_max=4.0, alpha=1, physical_unit=False, show_dots=False, show_grid=False, show_banner=True, vertical_line=None, linecolor='firebrick', linestyle='-', linewidth=3, labelsize=25, ticksize=30, label='SBP', labelloc='lower left')

Display the 1-D profiles, without showing PA and ellipticity.

Parameters:
  • ell_fix – astropy Table or numpy table, should be the output of IRAF ELLIPSE.
  • redshift (float) – redshift of the object.
  • pixel_scale (float) – pixel scale in arcsec/pixel.
  • zeropoint (float) – zeropoint of the photometry system.
  • ax (matplotlib.pyplot.axes object) – The user could provide axes on which the figure will be drawn.
  • offset (float) – offset of single surface brightness profile, in the unit of count.
  • x_min (float) – Minimum value of x-axis, in $x^{1/4}$ scale.
  • x_max (float) – Maximum value of x-axis, in $x^{1/4}$ scale.
  • alpha (float) – transparency.
  • physical_unit (bool) – If true, the figure will be shown in physical scale.
  • show_dots (bool) – If true, it will show all the data points.
  • show_grid (bool) – If true, it will show a grid.
  • vertical_line (list of floats) – positions of vertical lines. Maximum length is three.
  • linecolor (str) – Color of surface brightness profile.
  • linestyle (str) – Style of surface brightness profile. Could be “–”, “-.”, etc.
  • label (string) – Label of surface brightness profile.
Returns:

If the input ax is not None.

Return type:

ax

mrf.display.random_cmap(ncolors=256, background_color='white')

Random color maps, from kungpao https://github.com/dr-guangtou/kungpao.

Generate a matplotlib colormap consisting of random (muted) colors. A random colormap is very useful for plotting segmentation images.

Parameters
ncolors : int, optional
The number of colors in the colormap. The default is 256.
random_state : int or ~numpy.random.RandomState, optional
The pseudo-random number generator state used for random sampling. Separate function calls with the same random_state will generate the same colormap.
Returns
cmap : matplotlib.colors.Colormap
The matplotlib colormap with random colors.
Notes
Based on: colormaps.py in photutils

mrf.download module

class mrf.download.TqdmUpTo(iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, lock_args=None, nrows=None, colour=None, delay=0, gui=False, **kwargs)

Bases: tqdm.std.tqdm

Provides update_to(n) which uses tqdm.update(delta_n).

update_to(b=1, bsize=1, tsize=None)

b (int, optional): Number of blocks transferred so far [default: 1]. bsize (int, optional): Size of each block (in tqdm units) [default: 1]. tsize (int, optional): Total size (in tqdm units). If [default: None] remains unchanged.

mrf.download.megapipe_query_sql(ra, dec, size)

Return SQL query command of CFHT megapipe

Parameters:
  • ra (float) – in degree
  • dec (float) – in degree
  • size (float) – in degree
Returns:

The query URL, need to be opened by wget or curl.

Return type:

url (str)

mrf.download.get_megapipe_catalog(ra, dec, size, output_filename='_megapipe_cat.csv', overwrite=True)

Download CFHT MegaPipe frame catalog of given (ra, dec) and image size, and save in csv format.

Parameters:
  • ra (float) – in degree
  • dec (float) – in degree
  • size – use astropy.units. e.g. size = 300 * u.arcsec. If not in astropy.units form, then default units are ‘arcsec’.
  • output_filename (string) – The file will be saved here.
  • overwrite (bool) – it will overwrite the catalog if overwrite=True.
Returns:

None

mrf.download.overlap_fraction(img, header, frame, verbose=True)

Calculate the overlap between a given CFHT frame and a given image.

Parameters:
  • img (numpy 2-D array) – image array.
  • header – header of the image, typically astropy.io.fits.header object.
  • frame – This is one row of mega_cat, containing “position_bounds”.
  • verbose (bool) – Whether print out intersection percentage.
Returns:

The overlapping fraction (with respect to the input CFHT frame).

Return type:

percentage (float)

mrf.download.wget_cfht(frame, band, output_dir, output_name, overwrite=True)
mrf.download.download_cfht_megapipe(img, header, band='g', mega_cat_dir='_megapipe_cat.csv', output_dir='./', output_name='CFHT_megapipe_img', overwrite=True)

Download CFHT MegaPipe frame catalog of given (ra, dec) and image size. This could be really TIME CONSUMING!!! Typically one frame could be 500M to 2G.

Parameters:
  • img (numpy 2-D array) – input image, usually low-resolution image (such as Dragonfly).
  • header (astropy.io.fits.header object) – header of the input image.
  • band (string) – such as ‘g’ (‘G’) and ‘r’ (‘R’).
  • mega_cat_dir (string) – the directory of MegaPipe catalog, which is downloaded by get_megapipe_catalog.
  • output_dir (string) – the CFHT images will be downloaded here.
  • output_name (string) – name of CFHT images, as you can change
  • overwrite (bool) – It will overwrite the image if overwrite=True.
Returns:

None

mrf.download.download_highres(lowres_dir, high_res='hsc', band='g', overwrite=False)
Download high resolution image which overlaps with the given low resolution image.
This could be TIME CONSUMING! Typically one frame could be 500M to 2G. You also need to install unagi (https://github.com/dr-guangtou/unagi) to download HSC images.
Parameters:
  • lowres_dir (string) – the directory of input low-resolution image.
  • high_res (string) – name of high-resolution survey, now support ‘HSC’ and ‘CFHT’.
  • band (string) – name of filter, typically ‘g’ or ‘r’.
  • overwrite (bool) – it will overwrite the image if overwrite=True.
Returns:

None

mrf.imtools module

mrf.imtools.imdelete(image, iraf_path=None)

Delete fits image.

Parameters:
  • image – str, name of the image to be deleted. Can also use regex (REGular EXpression).
  • iraf_path – directory of “iraf/macosx/”.
Returns:

None

mrf.imtools.imcopy(image, output, iraf_path=None)

Copy fits image.

Parameters:
  • image – str, name of the image to be deleted. Can also use regex.
  • output – str, name of output image.
  • iraf_path – directory of “iraf/macosx/”.
Returns:

None

mrf.imtools.imshift(image, output, xshift, yshift, interp_type='poly3', boundary_type='constant', iraf_path=None)

Shift fits image, see https://iraf.net/irafhelp.php?val=imshift&help=Help+Page.

Parameters:
  • image – str, name of the image to be deleted. Can also use regex.
  • output – str, name of output image.
  • and yshift (xshift) – float, shift pixels in x and y.
  • interp_type – str, interpolant type. Available methods: “nearest”, “linear”, “poly3”, “poly5”, “spline3”, “sinc”, “drizzle”.
  • boundary_type – str, available methods: “nearest”, “constant”, “reflect” and “wrap”. If use constant, fill the value with zero.
  • iraf_path – directory of “iraf/macosx/”.
Returns:

None

mrf.imtools.magnify(image, output, xmag, ymag, x1='INDEF', x2='INDEF', y1='INDEF', y2='INDEF', interpolant='poly3', boundary='constant', fluxconserve='yes', logfile='STDOUT', iraf_path=None)

Magnify fits image, see https://iraf.net/irafhelp.php?val=imgeom.magnify&help=Help+Page.

Parameters:
  • image – str, name of the image to be deleted. Can also use regex.
  • output – str, name of output image.
  • xmag (ymag) – float, shift pixels in x and y.
  • x1 (x2) – The starting and ending coordinates in x in the input image which become the first and last pixel in x in the magnified image. The values need not be integers. If indefinite the values default to the first and last pixel in x of the input image; i.e. a value of 1 and nx.
  • y1 (y2) – The starting and ending coordinates in y in the input image which become the first and last pixel in y in the magnified image. The values need not be integers. If indefinite the values default to the first and last pixel in y of the input image; i.e. a value of 1 and ny.
  • interp_type – str, interpolant type. Available methods: “nearest”, “linear”, “poly3”, “poly5”, “spline3”, “sinc”, “lsinc”, “drizzle”.
  • boundary_type – str, available methods: “nearest”, “constant”, “reflect” and “wrap”. If use constant, fill the value with zero.
  • fluxconserve – str, whether conserve flux.
  • iraf_path – directory of “iraf/macosx/”.
Returns:

None

mrf.imtools.blkavg(image, output, b1, b2, option='sum', iraf_path=None)

Block average fits image, see https://iraf.net/irafhelp.php?val=imgeom.magnify&help=Help+Page.

Parameters:
  • image – str, name of the image to be deleted. Can also use regex.
  • output – str, name of output image.
  • b1 – block size (column).
  • b2 – block size (row).
  • option – str, choices are “sum” and “average”.
Returns:

None