imcascade.fitter

Module Contents

Classes

Fitter

A Class used fit images with MultiGaussModel

Functions

initialize_fitter(im, psf[, mask, err, x0, y0, re, …])

Function used to help Initialize Fitter instance from simple inputs

fitter_from_ASDF(file_name[, init_dict, bounds_dict])

Function used to initalize a fitter from a saved asdf file

Attributes

log2pi

imcascade.fitter.log2pi
imcascade.fitter.initialize_fitter(im, psf, mask=None, err=None, x0=None, y0=None, re=None, flux=None, psf_oversamp=1, sky_model=True, log_file=None, readnoise=None, gain=None, exp_time=None, num_components=None, component_widths=None, log_weight_scale=True)

Function used to help Initialize Fitter instance from simple inputs

Parameters
  • im (str or 2D Array) – The image or cutout to be fit with imcascade. If a string is given, it is interpretted as the location of a fits file with the cutout in it’s first HDU. Otherwise is a 2D numpy array of the data to be fit

  • psf (str, 2D Array or None) – Similar to above but for the PSF. If not using a PSF, the use None

  • mask (2D array (optional)) – Sources to be masked when fitting, if none is given then one will be derrived

  • err (2D array (optional)) – Pixel errors used to calculate the weights when fitting. If none is given will use readnoise, gain and exp_time if given, or default to sep derrived rms

  • x0 (float (optional)) – Inital guess at x position of center, if not will assume the center of the image

  • y0 (float (optional)) – Inital guess at y position of center, if not will assume the center of the image

  • re (float (optional)) – Inital guess at the effective radius of the galaxy, if not given will estimate using sep kron radius

  • flux (float (optional)) – Inital guess at the flux of the galaxy, if not given will estimate using sep flux

  • psf_oversamp (float (optional)) – Oversampling of PSF given, default is 1

  • sky_model (boolean (optional)) – Whether or not to model sky as tilted-plane, default is True

  • log_file (str (optional)) – Location of log file

  • readnoise,gain,exp_time (float,float,float (all optional)) – The read noise (in electrons), gain and exposure time of image that is used to calculate the errors and therefore pixel weights. Only used if err = None. If these parameters are also None, then will estimate pixel errors using sep rms map.

Returns

Fitter – Returns intialized instance of imcascade.fitter.Fitter which can then be used to fit galaxy and analyze results.

Return type

imcascade.fitter.Fitter

imcascade.fitter.fitter_from_ASDF(file_name, init_dict={}, bounds_dict={})

Function used to initalize a fitter from a saved asdf file

This can be useful for re-running or for initializing a series of galaxies beforehand and then transferring to somewhere else or running in parallel

Parameters
  • file_name (str) – location of asdf file containing saved data. Often this is a file created by Fitter.save_results

  • init_dict (dict (optional)) – Dictionary specifying initial guesses for least_squares fitting to be passed to Fitter instance.

  • bounds_dict (dict (optional)) – Dictionary specifying bounds for least_squares fitting to be passed to Fitter instance.

class imcascade.fitter.Fitter(img, sig, psf_sig, psf_a, weight=None, mask=None, sky_model=True, sky_type='tilted-plane', render_mode='hybrid', log_weight_scale=True, verbose=True, psf_shape=None, init_dict={}, bounds_dict={}, log_file=None)

Bases: imcascade.mgm.MultiGaussModel

A Class used fit images with MultiGaussModel

This is the main class used to fit imcascade models

Parameters
  • img (2D Array) – Data to be fit, it is assumed to be a cutout with the object of interest in the center of the image

  • sig (1D Array) – Widths of Gaussians to be used in MultiGaussModel

  • psf_sig (1D array, None) – Width of Gaussians used to approximate psf

  • psf_a (1D array, None) – Weights of Gaussians used to approximate psf If both psf_sig and psf_a are None then will run in Non-psf mode

  • weight (2D Array, optional) – Array of pixel by pixel weights to be used in fitting. Must be same shape as ‘img’ If None, all the weights will be set to 1.

  • mask (2D Array, optional) – Array with the same shape as ‘img’ denoting which, if any, pixels to mask during fitting process. Values of ‘1’ or ‘True’ values for the pixels to be masked. If set to ‘None’ then will not mask any pixels. In practice, the weights of masked pixels is set to ‘0’.

  • sky_model (bool, optional) – If True will incorperate a tilted plane sky model. Reccomended to be set to True

  • sky_type (str, 'tilted-plane' or 'flat') – Function used to model sky. Default is tilted plane with 3 parameters, const bkg and slopes in each directin. ‘flat’ uses constant background model with 1 parameter.

  • render_mode ('hybrid', 'erf' or 'gauss') – Option to decide how to render models. ‘erf’ analytically computes the integral over the pixel of each profile therefore is more accurate but more computationally intensive. ‘gauss’ assumes the center of a pixel provides a reasonble estimate of the average flux in that pixel. ‘gauss’ is faster but far less accurate for objects which vary on O(pixel size), so use with caution. ‘hybrid’ is the defualt, uses ‘erf’ for components with width < 5 to ensure accuracy and uses ‘gauss’ otherwise as it is accurate enough and faster. Also assumes all flux > 5 sigma for components is 0.

  • log_weight_scale (bool, optional) – Wether to treat weights as log scale, Default True

  • verbose (bool, optional) – If true will log and print out errors

  • psf_shape (dict, Optional) – Dictionary containg at ‘q’ and ‘phi’ that define the shape of the PSF. Note that this slows down model rendering significantly so only reccomended if neccesary.

  • init_dict (dict, Optional) – Dictionary specifying initial guesses for least_squares fitting. The code is desigined to make ‘intelligent’ guesses if none are provided

  • bounds_dict (dict, Optional) – Dictionary specifying boundss for least_squares fitting and priors. The code is desigined to make ‘intelligent’ guesses if none are provided

resid_1d(params)

Given a set of parameters returns the 1-D flattened residuals when compared to the Data, to be used in run_ls_min Function

Parameters

params (Array) – List of parameters to define model

Returns

resid_flatten – 1-D array of the flattened residuals

Return type

array

run_ls_min(ls_kwargs={})

Function to run a least_squares minimization routine using pre-determined inital guesses and bounds.

Utilizes the scipy least_squares routine (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.least_squares.html)

Parameters

ls_kwargs (dict, optional) – Optional list of arguments to be passes to least_squares routine

Returns

min_param – Returns a 1D array containing the optimized parameters that describe the best fit model.

Return type

1D array

set_up_express_run(set_params=None)

Function to set up ‘express’ run using pre-rendered images with a fixed x0,y0, phi and q. Sets class attribute ‘express_gauss_arr’ which is needed to run dynesty or emcee in express mode

Parameters

set_params (len(4) array-like, optional) – Parameters (x0,y0,q,phi) to use to per-render images. If None will call run_ls_min() or use stored min_res to find parameters.

Returns

express_gauss_arr – Returns a 3-D with pre-rendered images based on input parameters

Return type

array (shape[0],shape[1], Ndof_gauss)

make_express_model(exp_params)

Function to generate a model for a given set of paramters, specifically using the pre-renedered model for the ‘express’ mode

Parameters

exo_params (Array) – List of parameters to define model. Length is Ndof_gauss + Ndof_sky since the structural parameters (x0,y0,q, PA) are set

Returns

model – Model image based on input parameters

Return type

2D-array

chi_sq(params)

Function to calculate chi_sq for a given set of paramters

Parameters

params (Array) – List of parameters to define model

Returns

chi^2 – Chi squared statistic for the given set of parameters

Return type

float

log_like(params)

Function to calculate the log likeliehood for a given set of paramters

Parameters

params (Array) – List of parameters to define model

Returns

log likeliehood – log likeliehood for a given set of paramters, defined as -0.5*chi^2

Return type

float

ptform(u)

Prior transformation function to be used in dynesty ‘full’ mode

Parameters

u (array) – array of random numbers from 0 to 1

Returns

x – array containing distribution of parameters from prior

Return type

array

log_like_exp(exp_params)

Function to calculate the log likeliehood for a given set of paramters, specifically using the pre-renedered model for the ‘express’ mode

Parameters

exo_params (Array) – List of parameters to define model. Length is Ndof_gauss + Ndof_sky since the structural parameters (x0,y0,q, PA) are set

Returns

log likeliehood – log likeliehood for a given set of paramters, defined as -0.5*chi^2

Return type

float

ptform_exp_ls(u)

Prior transformation function to be used in dynesty ‘express’ mode using gaussian priors defined by the results of the least_squares minimization

Parameters

u (array) – array of random numbers from 0 to 1

Returns

x – array containing distribution of parameters from prior

Return type

array

ptform_exp_lin_cauchy(u)

Prior transformation function to be used in dynesty ‘express’ mode using gaussian priors defined by the results of the least_squares minimization

Parameters

u (array) – array of random numbers from 0 to 1

Returns

x – array containing distribution of parameters from prior

Return type

array

ptform_exp_unif(u)

Prior transformation function to be used in dynesty ‘express’ mode using unifrom priors defined by self.lb and self.ub

Parameters

u (array) – array of random numbers from 0 to 1

Returns

x – array containing distribution of parameters from prior

Return type

array

run_dynesty(method='full', sampler_kwargs={}, run_nested_kwargs={}, prior='min_results')

Function to run dynesty to sample the posterior distribution using either the ‘full’ methods which explores all paramters, or the ‘express’ method which sets the structural parameters.

Parameters
Returns

Posterior – posterior distribution derrived. If method is ‘express’, the first 4 columns, containg x0, y0, PA and q, are all the same and equal to values used to pre-render the images

Return type

Array

save_results(file_name)

Function to save results after run_ls_min, run_dynesty and/or run_emcee is performed. Will be saved as an ASDF file.

Parameters

file_name (str) – Str defining location of where to save data

Returns

dict_saved – Dictionary containing all the save quantities

Return type

dict