lenspack.utils module

UTILS MODULE

This module contains utility functions globally available to lenspack.

lenspack.utils.round_up_to_odd(x)[source]

Round up to the nearest odd integer.

lenspack.utils.convert_units(x, target)[source]

Convert or attach units to a variable.

Parameters
  • x (float) – Quantity to convert.

  • target (str) – Target units given as an acceptable astropy.units string (e.g. ‘km’).

Raises

Exception – If the conversion fails.

Examples

>>> conv(5, 'kpc')
<Quantity 5. kpc>
>>> x = 4e14
>>> x = conv(x, 'solMass')
>>> conv(x, 'kg')
<Quantity 7.95390166e+44 kg>
lenspack.utils.sigma_critical(zl, zs, cosmology)[source]

Critical surface mass density between a lens and source galaxy(-ies).

Sigma_critical = [c^2 / (4 * pi * G)] * D_os / (D_ol * D_ls)

Angular diameter distances D are calculated in a universe specified by an instance of astropy.cosmology.core.Cosmology.

Parameters
  • zl (float) – Redshift of the lens.

  • zs (array_like) – Redshift(s) of the source galaxies.

  • cosmology (astropy.cosmology.core.Cosmology) – Cosmological model.

Returns

Critical surface mass density between a lens (i.e. cluster or DM halo) and each source redshift in units of solar masses per square parsec. For sources at the redshift of the halo and below, Sigma_critical is set to np.inf.

Return type

astropy.units.quantity.Quantity

Examples

Todo

Include the option for source redshift probability distributions.

lenspack.utils.bin2d(x, y, npix=10, v=None, w=None, extent=None, verbose=False)[source]

Bin samples of a spatially varying quantity according to position.

The (weighted) average is taken of values falling into the same bin. This function is relatively general, but it is mainly used within this package to produce maps of the two components of shear from a galaxy catalog.

Parameters
  • x (array_like) – 1D position arrays.

  • y (array_like) – 1D position arrays.

  • npix (int or list or tuple as (nx, ny), optional) – Number of bins in the x and y directions. If an int N is given, use (N, N). Binning defaults to (10, 10) if not provided.

  • v (array_like, optional) – Values at positions (x, y). This can be given as many arrays (v1, v2, …) of len(x) to bin simultaneously. If None, the bin count in each pixel is returned.

  • w (array_like, optional) – Weights for v during averaging. If provided, the same weights are applied to each input v.

  • extent (array_like, optional) – Boundaries of the resulting grid, given as (xmin, xmax, ymin, ymax). If None, bin edges are set as the min/max coordinate values of the input position arrays.

  • verbose (boolean, optional) – If True, print details of the binning.

Returns

2D numpy arrays of values v binned into pixels. The number of outputs matches the number of input v arrays.

Return type

ndarray or tuple of ndarray

Examples

>>> # 100 values at random positions within the ranges -0.5 < x, y < 0.5
>>> # and binned within -1 < x, y < 1 to a (5, 5) grid.
>>> x = np.random.random(100) - 0.5
>>> y = np.random.random(100) - 0.5
>>> v = np.random.randn(100) * 5
>>> bin2d(x, y, v=v, npix=5, extent=(-1, 1, -1, 1))
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  4.43560619, -2.33308373,  0.48447844,  0.        ],
       [ 0.        ,  1.94903524, -0.29253335,  1.3694618 ,  0.        ],
       [ 0.        , -1.0202718 ,  0.37112266, -1.43062585,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])
lenspack.utils.radius2d(N, center=None, mode='exact')[source]

Distances from every pixel to a fixed center in a square matrix.

Parameters
  • N (int) – Number of pixels to a side.

  • center (array_like, optional) – Incides of the central pixel, given as (x0, y0). If not given, the center is taken to be (N / 2, N / 2) (though see mode description).

  • mode ({'exact', 'fft'}) – How to treat the case when N is even. If ‘exact’, compute distances from the true (fractional) central pixel location. If ‘fft’, use the numpy.fft.fftfreq convention such that the central pixel location is rounded up to the nearest integer.

Returns

2D matrix of distances.

Return type

numpy array

Notes

Non-integer center coordinates are not supported. If a center is provided, mode is ignored.

Examples

>>> radius2d(4, mode='exact')
array([[ 2.12132034,  1.58113883,  1.58113883,  2.12132034],
       [ 1.58113883,  0.70710678,  0.70710678,  1.58113883],
       [ 1.58113883,  0.70710678,  0.70710678,  1.58113883],
       [ 2.12132034,  1.58113883,  1.58113883,  2.12132034]])
>>> radius2d(4, mode='fft')
array([[ 2.82842712,  2.23606798,  2.        ,  2.23606798],
       [ 2.23606798,  1.41421356,  1.        ,  1.41421356],
       [ 2.        ,  1.        ,  0.        ,  1.        ],
       [ 2.23606798,  1.41421356,  1.        ,  1.41421356]])