Analysis

lensing

mejiro.analysis.lensing.get_alpha(lens_model, kwargs_lens, scene_size, pixel_scale)[source]

Compute the deflection angle map for a given lens model over a 2D grid. The intended use case is:

plt.quiver(*get_alpha(lens_model, kwargs_lens, scene_size, pixel_scale))
mejiro.analysis.lensing.get_kappa(lens_model, kwargs_lens, scene_size, pixel_scale)[source]

Compute the convergence (kappa) map for a given lens model over a 2D grid. The scene size is calculated from overall scene size and pixel scale to match how scene size is calculated in the SyntheticImage class. This way, the same parameters will yield arrays with the same shapes and can be directly compared.

Parameters:
  • lens_model (object) – See lenstronomy documentation.

  • kwargs_lens (list of dict) – See lenstronomy documentation.

  • scene_size (float) – The physical size of the scene in angular units (often, arcseconds).

  • pixel_scale (float) – The size of each pixel.

Returns:

A 2D array containing the convergence (kappa) values evaluated on a grid covering the scene.

Return type:

np.ndarray

mejiro.analysis.lensing.get_potential(lens_model, kwargs_lens, scene_size, pixel_scale)[source]

Compute the potential map for a given lens model over a 2D grid. The scene size is calculated from overall scene size and pixel scale to match how scene size is calculated in the SyntheticImage class. This way, the same parameters will yield arrays with the same shapes and can be directly compared.

Parameters:
  • lens_model (object) – See lenstronomy documentation.

  • kwargs_lens (list of dict) – See lenstronomy documentation.

  • scene_size (float) – The physical size of the scene in angular units (often, arcseconds).

  • pixel_scale (float) – The size of each pixel.

Returns:

A 2D array containing the potential values evaluated on a grid covering the scene.

Return type:

np.ndarray

mejiro.analysis.lensing.get_subhalo_mass_function(realization, bins=10)[source]

Return the subhalo mass function from a pyHalo subhalo realization. The intended use case is quickly plotting the subhalo mass function in the following way:

plt.loglog(*get_subhalo_mass_function(realization))
Parameters:
  • realization (object) – A pyHalo realization.

  • bins (int, optional) – Number of bins to use for the mass histogram (default is 10).

Returns:

  • bin_edges (numpy.ndarray) – The edges of the mass bins (excluding the last edge), in the same units as the halo masses.

  • hist (numpy.ndarray) – The number of halos in each mass bin.

regions

mejiro.analysis.regions.annular_mask(dimx, dimy, center, r_in, r_out)[source]

snr_calculation

mejiro.analysis.snr_calculation.get_snr(exposure, snr_per_pixel_threshold=1)[source]

Calculate the signal-to-noise ratio (SNR) given an exposure, using the method of Holloway et al. (2023). First, the SNR per pixel is calculated: see get_snr_array(). Then, contiguous regions of pixels above the SNR per pixel threshold are identified. The SNR for each region is calculated in the following way:

\[\text{SNR}_\text{region} = \frac{\sum\limits_i N_{i,\,S}}{\sqrt{\sum\limits_i \left(N_{i,\,S} + N_{i,\,L} + N_{i,\,B} + N_{i,\,N}\right)}}\]

where the summations are over the pixels that comprise the region, \(N_{i,\,S}\) are the counts in pixel \(i\) due to the source galaxy, \(N_{i,\,L}\) are counts due to the lensing galaxy, \(N_{i,\,B}\) are counts due to the sky background, and \(N_{i,\,N}\) are counts due to detector noise. If multiple regions are formed, the SNR of the region with the highest SNR is taken to be the SNR of the system.

Parameters:
  • exposure (object) – Expected to have ‘source_exposure’ and ‘exposure’ attributes.

  • snr_per_pixel_threshold (float, optional) – The minimum SNR per pixel required for a pixel to be included in a region (default is 1).

Returns:

  • max_snr (float or None) – The maximum SNR found among the regions above the threshold. Returns 1 if no pixels are above the threshold, or None if no regions are found.

  • masked_snr_array (numpy.ma.MaskedArray) – The SNR array with pixels below the threshold masked.

Notes

  • Regions are defined as contiguous pixels (using a cross-shaped connectivity) above the SNR threshold.

  • If no pixels exceed the threshold, the SNR returned is 1. Any system with no pixels above the threshold is undetectable. Typically, an SNR of 20 is required for a system to be detectable.

mejiro.analysis.snr_calculation.get_snr_array(exposure)[source]

Calculate the signal-to-noise ratio (SNR) per pixel for a given exposure:

\[\frac{\text{Source}}{\sqrt{\text{Source + Lens + Noise}}}\]

Any NaN or infinite values (for example, if a pixel happens to have zero counts) are replaced with zero.

Parameters:

exposure (object) – Expected to have ‘source_exposure’ and ‘exposure’ attributes.

Returns:

An array containing the SNR values, with NaN and infinite values replaced by zero.

Return type:

np.ndarray

Raises:

ValueError – If the exposure was not created with pieces=True.

stats

mejiro.analysis.stats.chi_square(a, b)[source]

Compute the chi-square statistic between two arrays:

\[\chi^2 = \sum_{i} \frac{\left(A_{i} - B_{i}\right)^2}{B_{i}}\]
Parameters:
  • a (numpy.ndarray) – First input array. Must have the same shape as b.

  • b (numpy.ndarray) – Second input array. Must have the same shape as a.

Returns:

The chi-square statistic. If any division by zero occurs, the result will be replaced with NaN.

Return type:

float

Raises:
  • AssertionError – If the input arrays a and b do not have the same shape.

  • AssertionError – If the input array b contains any zeros.

Notes

If the input arrays are not 1-dimensional, they will be flattened before computation.

mejiro.analysis.stats.normalize(array)[source]

Normalize an array by dividing each element by the sum of all elements.

Parameters:

array (numpy.ndarray) – Input array to be normalized.

Returns:

Normalized array where the sum of all elements is 1.

Return type:

numpy.ndarray