descwl.analysis module

Perform weak-lensing analysis of simulated sources.

class descwl.analysis.OverlapAnalyzer(survey, no_hsm, alpha=1)[source]

Bases: object

Analyze impact of overlapping sources on weak lensing.

Parameters:survey (descwl.survey.Survey) – Simulated survey to describe with FITS header keywords.
static add_args(parser)[source]

Add command-line arguments for constructing a new Reader.

The added arguments are our constructor parameters with ‘_’ replaced by ‘-‘ in the names. Note that constructor parameter defaults are specified here rather than in the constructor, so that they are included in command-line help.

Parameters:parser (argparse.ArgumentParser) – Arguments will be added to this parser object using its add_argument method.
add_galaxy(model, stamps, bounds)[source]

Add one galaxy to be analyzed.

Parameters:
  • model (descwl.model.Galaxy) – The galaxy model used for rendering.
  • stamps (numpy.ndarray) – Array of shape (nstamp,width,height) containing pixel values for nstamp stamps of dimensions (width,height).
  • bounds (galsim.BoundsI) – Bounds of the stamps in the full simulated survey image.
finalize(verbose, trace, calculate_bias)[source]

Finalize analysis of all added galaxies.

Parameters:
  • verbose (bool) – Print a summary of analysis results.
  • trace (callable) – Function to call for tracing resource usage. Will be called with a brief str description of each checkpoint.
Returns:

Overlap analysis results.

Return type:

OverlapResults

fit_galaxies(indices, observed_image, fixed_parameters=None)[source]

Simultaneously fit a set of galaxy parameters to an observed image.

Fits are performed on noise-free images, so there are no meaningful errors to report and only best-fit parameter values are returned. This method is intended to support systematics studies where shifts in best-fit parameters are the quantities of interest. See the descwl.render.GalaxyRenderer.draw() method for details on how the fit parameters are defined and how each galaxy model is rendered as these parameters are varied.

Parameters:
  • indices (iterable) – List of num_galaxies integer galaxy indices to include in the fit. Index values correspond to the order in which galaxies were added using add_galaxy().
  • observed_image (galsim.Image) – A GalSim image that defines the observed pixels that will be fit and the region into which galaxy models will be renderered.
  • fixed_parameters (dict) – An optional dictionary of parameter values to fix in the fit, or None if all parameters should be floating.
Returns:

Array with shape (num_galaxies,6) containing the best-fit values

of the following six parameters: df,dx,dy,ds,dg1,dg2. See the descwl.render.GalaxyRenderer.draw() method for details on how these parameters are defined.

Return type:

numpy.ndarray

Raises:

RuntimeError – Invalid galaxy index or fit did not find a minimum.

classmethod from_args(args)[source]

Create a new Reader object from a set of arguments.

Parameters:args (object) – A set of arguments accessed as a dict using the built-in vars() function. Any extra arguments beyond those defined in add_args() will be silently ignored.
Returns:A newly constructed Reader object.
Return type:Reader
class descwl.analysis.OverlapResults(survey, table, stamps, bounds, num_slices)[source]

Bases: object

Results of analyzing effects of overlapping sources on weak lensing.

Results are normally obtained by calling OverlapAnalyzer.finalize() after simulating an image or using descwl.output.Reader() to load saved simulation results.

Indices returned by the find methods below can be used to lookup analysis results via table[index] and the corresponding simulated datacube using stamps[index] and bounds[index].

Parameters:
  • survey (descwl.survey.Survey) – Simulated survey that results are based on.
  • table (astropy.table.Table) – Table of analysis results with one row per galaxy.
  • stamps (array) – Array of numpy.ndarray postage-stamp datacubes. Might be None.
  • bounds (array) – Array of galsim.BoundsI objects giving pixel coordinates of each datacube in the full simulated image. Might be None.
  • num_slices (int) – Number of datacube slices for each stamp. Ignored if stamps is None.
Raises:

RuntimeError – Image datacubes have unexpected number of slices.

add_noise(noise_seed)[source]

Add Poisson noise to the simulated survey image.

Parameters:noise_seed (int) – Random seed to use.
Raises:RuntimeError – Noise has already been added.
get_bias(selected, covariance)[source]

Return bias of the 6 parameters in vector form.

The bias is obtained from contracting the bias tensor with the appropiate covariance matrix elements.

Parameters:
  • selected (iterable) – Array of integer indices for the sources to include in the calculated matrices.
  • covariance (array) – An array containing the covariance matrix of the selected galaxies.
Returns:

bias which is a vector with dimensions (nbias)

containing the biases of the selected galaxies.

Return type:

array

get_bias_tensor(selected, covariance)[source]

Return bias tensor from the selected galaxies.

Uses the function get_bias_tensor_images() and then contracts these images to obtain the actual bias tensor.

Parameters:
  • selected (iterable) – Array of integer indices for the sources to include in the calculated matrices.
  • covariance (array) – An array containing the covariance matrix of the selected galaxies.
Returns:

bias_tensor with dimensions (ntensor,ntensor,npar) containing

bias_tensor elements.

Return type:

array

get_bias_tensor_images(index1, index2, background)[source]

Return bias tensor images for a pair of galaxies.

The bias tensor images are required in the calculation of the bias of parameters of the galaxies. Bias tensor images are derived from the first partials and second partials of the six parameters.

Parameters:
  • index1 (int) – Index of the first galaxy to use.
  • index2 (int) – Index of the second galaxy to use, which might the same as index1.
  • background (galsim.Image) – Background image that combines all sources that overlap the two galaxies and completely contains them.
Returns:

Tuple (images,overlap) where images is a numpy.ndarray with shape

(npar,npar,npar,height,width), where npar=6 and (height,width) are the dimensions of the overlap between the two galaxies, and overlap gives the bounding box of the overlap in the full survey image. Returns None,None if the two galaxies do not overlap.

Return type:

tuple

Raises:

RuntimeError – Invalid index1 or index2, or galaxies are not contained with the background image, or no partial derivative images are available.

get_fisher_images(index1, index2, background)[source]

Return Fisher-matrix images for a pair of galaxies.

Fisher matrix images are derived from the partial derivatives with respect to six parameters: total flux in detected electrons, centroid positions in x and y in arcseconds, flux-preserving radial scale (dimensionless), and shear g1,g2 with |g| = (a-b)/(a+b). Use the fisher program to display Fisher matrix images.

Parameters:
  • index1 (int) – Index of the first galaxy to use.
  • index2 (int) – Index of the second galaxy to use, which might the same as index1.
  • background (galsim.Image) – Background image that combines all sources that overlap the two galaxies and completely contains them.
Returns:

Tuple (images,overlap) where images is a numpy.ndarray with shape

(npar,npar,height,width), where npar=6 and (height,width) are the dimensions of the overlap between the two galaxies, and overlap gives the bounding box of the overlap in the full survey image. Returns None,None if the two galaxies do not overlap.

Return type:

tuple

Raises:

RuntimeError – Invalid index1 or index2, or galaxies are not contained with the background image, or no partial derivative images are available.

get_matrices(selected)[source]

Return matrices derived the from Fisher-matrix images for a set of sources.

If the Fisher matrix is not invertible or any variances are <= 0, we will drop the selected source with the lowest value of snr_iso and try again. This procedure is iterated until we get a valid covariance matrix. Matrix elements for all parameters of any sources that get dropped by this procedure will be set to zero and variances will be set to np.inf so that 1/np.sqrt(variance) = 0. Invalid covariances are generally associated with sources that are barely above the pixel SNR threshold, so this procedure should normally provide sensible values for the largest possible subset of the input selected sources. Use the fisher program to visualize matrix elements and to further study examples of invalid covariances.

Parameters:selected (iterable) – Array of integer indices for the sources to include in the calculated matrices.
Returns:
Tuple (fisher,covariance,variance,correlation) of numpy.ndarray
where variance has shape (npar,) and all other arrays are symmetric with shape (npar,npar), where npar = 6*len(selected). Matrix elements will be zero for any parameters associated with dropped sources, as described above.
Return type:tuple
get_stamp(index, datacube_index=0)[source]

Return the simulated postage stamp for a single galaxy.

Parameters:
  • index (int) – Index of the requested galaxy in our results. Use meth:find_galaxy to get the index for an identifier.
  • datacube_index (int) – Which slice of the datacube to return.
Returns:

A GalSim image for the requested galaxy. Note that the

return value is a read-only view of our internal stamps array.

Return type:

galsim.ImageView

Raises:

RuntimeError – No such galaxy in the results.

get_subimage(indices, datacube_index=0)[source]

Return simulated subimage of a set of objects.

Parameters:indices (iterable) – Indices of the objects to include in the subimage. Our select() method returns a suitable argument to use here by default (format = ‘index’).
Returns:Image of the selected objects or None if indices is empty.
Return type:galsim.Image
Raises:RuntimeError – An index is out of range.
match_sextractor(catalog_name, column_name='match')[source]

Match detected objects to simulated sources.

Parameters:
  • catalog_name (str) – Name of an ASCII catalog in SExtractor-compatible format, and containing X_IMAGE, Y_IMAGE columns and num_found rows.
  • column_name (str) – Name of a column in our table data member to fill with the detected object index matched to each simulated source, or -1 if no match is found. Overwrites any exisiting column or creates a new one. Does nothing if column_name is None or ‘’.
Returns:

Tuple detected,matched,indices,distance where detected is the detected catalog as

a astropy.table.Table, matched is an array of num_found booleans indicating whether each object was matched with a simulated object, indices is an array of num_matched = np.count_nonzero(matched) integers giving row numbers in our table attribute for each simulated match, and distance is an array of num_matched separation distances in arcseconds between matched and simulated objects.

Return type:

tuple

select(*selectors, **options)[source]

Select objects.

This function is implemented using eval() so should only be used when the source of the selector strings is trusted.

Parameters:
  • selectors (str,...) – A sequence of one or more selection strings, each consisting of a boolean expression of column table names, e.g. ‘snr_iso > 5’. The special strings ‘ALL’ and ‘NONE’ do what you would expect.
  • mode (str) – The string ‘and’ or ‘or’ to specify how multiple selectors should be combined.
  • format (str) – The string ‘mask’ or ‘index’ to specify the format of the returned array. A mask is an array of booleans with one entry per source. The alternative index array of integers lists the selected indices in increasing order and might be empty. The mask format is useful for performing your own logical operations on the returned array. The index format is useful for iterating over the selected objects. Both formats can be used to slice the catalog table to extract only the selected rows.
Returns:

A numpy array of booleans or integers, depending

on the value of the format input argument.

Return type:

numpy.ndarray

Raises:

RuntimeError – A selector uses an undefined variable name, the method was passed an unexpected mode or format string, or these results have no catalog table.

slice_labels = ['dflux', 'dx', 'dy', 'ds', 'dg1', 'dg2']
descwl.analysis.make_inv_positions()[source]
descwl.analysis.make_positions()[source]