halotools.mock_observables.mean_delta_sigma(galaxies, particles, effective_particle_masses, rp_bins, period=None, verbose=False, num_threads=1, approx_cell1_size=None, approx_cell2_size=None, per_object=False)[source]

Calculate \(\Delta\Sigma(r_p)\), the galaxy-galaxy lensing signal as a function of projected distance.

The mean_delta_sigma function calculates \(\Delta\Sigma(r_p)\) by calculating the excess surface density of particles in cylinders surrounding the input galaxies. The input particles should be a random downsampling of particles in the same simulation snapshot as the model galaxies.

By using the effective_particle_masses argument, the function works equally well with DM-only simulations as with hydro simulations that include multiple species of particles with different masses and/or different downsampling rates.

Example calls to this function appear in the documentation below.

See also Galaxy Catalog Analysis Example: Galaxy-galaxy lensing.


Numpy array of shape (num_gal, 3) containing 3-d positions of galaxies. Length units are comoving and assumed to be in Mpc/h, here and throughout Halotools.

See the Formatting your xyz coordinates for Mock Observables calculations documentation page for instructions on how to transform your coordinate position arrays into the format accepted by the galaxies and particles arguments.


Numpy array of shape (num_ptcl, 3) containing 3-d positions of particles.

Length units are comoving and assumed to be in Mpc/h, here and throughout Halotools.

effective_particle_massesfloat or ndarray

Float or array storing the effective mass of each particle in units of Msun with h=1 units.

If passing in an ndarray, must be of shape (num_ptcl, ), one array element for every particle.

If passing in a single float, it will be assumed that every particle has the same mass (as is the case in a typical DM-only simulation).

The effective mass is simply the actual mass multiplied by the downsampling rate. For example, if your simulation has a particle mass of 10**8 and you are using a sample of particles that have been randomly downsampled at a 1% rate, then your effective particle mass will be 10**10.

See the Examples section below for how this can be calculated from Halotools-provided catalogs.


Numpy array of shape (num_rbins, ) of projected radial boundaries defining the bins in which the result is calculated.

Length units are comoving and assumed to be in Mpc/h, here and throughout Halotools.


Length-3 sequence defining the periodic boundary conditions in each dimension. If you instead provide a single scalar, Lbox, period is assumed to be the same in all Cartesian directions.

Length units are comoving and assumed to be in Mpc/h, here and throughout Halotools.

per_objectbool, optional

Boolean flag specifying whether the function will return the per-object lensing signal. Default is False, in which the returned array will be an average over the entire sample. If True, the returned ndarray will have shape (num_gal, num_rbins)

num_threadsint, optional

Number of threads to use in calculation, where parallelization is performed using the python multiprocessing module. Default is 1 for a purely serial calculation, in which case a multiprocessing Pool object will never be instantiated. A string ‘max’ may be used to indicate that the pair counters should use all available cores on the machine.

approx_cell1_sizearray_like, optional

Length-3 array serving as a guess for the optimal manner by how points will be apportioned into subvolumes of the simulation box. The optimum choice unavoidably depends on the specs of your machine. Default choice is to use Lbox/10 in each dimension, which will return reasonable result performance for most use-cases. Performance can vary sensitively with this parameter, so it is highly recommended that you experiment with this parameter when carrying out performance-critical calculations.

approx_cell2_sizearray_like, optional

Analogous to approx_cell1_size, but for sample2. See comments for approx_cell1_size for details.


Numpy array of shape (num_rbins-1, ) storing \(\Delta\Sigma(r_p)\) in comoving units of \(h M_{\odot} / {\rm Mpc}^2\) assuming h=1.

If per_object is True, Delta_Sigma will instead have shape (num_gal, num_rbins)


For demonstration purposes we will calculate mean_delta_sigma using a mock catalog generated with the FakeSim that is generated on-the-fly.

>>> from halotools.sim_manager import FakeSim
>>> halocat = FakeSim()

Now let’s populate this halo catalog with mock galaxies.

>>> from halotools.empirical_models import PrebuiltHodModelFactory
>>> model = PrebuiltHodModelFactory('leauthaud11', threshold = 11.)
>>> model.populate_mock(halocat)

Now we retrieve the positions of our mock galaxies and transform the arrays into the shape of the ndarray expected by the mean_delta_sigma function. We transform our x, y, z points into the array shape used by the pair-counter by taking the transpose of the result of numpy.vstack. This boilerplate transformation is used throughout the mock_observables sub-package:

>>> x = model.mock.galaxy_table['x']
>>> y = model.mock.galaxy_table['y']
>>> z = model.mock.galaxy_table['z']
>>> galaxies = np.vstack((x, y, z)).T

The return_xyz_formatted_array function also performs this same transformation, and can also be used to place mock galaxies into redshift-space for additional observational realism.

Let’s do the same thing for a set of particle data:

>>> px = model.mock.ptcl_table['x']
>>> py = model.mock.ptcl_table['y']
>>> pz = model.mock.ptcl_table['z']
>>> particles = np.vstack((px, py, pz)).T

The default Halotools catalogs come with ~1e6 particles. Using this many particles may be overkill: in many typical use-cases, the mean_delta_sigma function converges at the percent-level using an order of magnitude fewer particles. The code below shows how to (optionally) downsample these particles using a Halotools convenience function.

>>> from halotools.utils import randomly_downsample_data
>>> num_ptcls_to_use = int(1e4)
>>> particles = randomly_downsample_data(particles, num_ptcls_to_use)
>>> particle_masses = np.zeros(num_ptcls_to_use) + halocat.particle_mass
>>> total_num_ptcl_in_snapshot = halocat.num_ptcl_per_dim**3
>>> downsampling_factor = total_num_ptcl_in_snapshot/float(len(particles))
>>> effective_particle_masses = downsampling_factor * particle_masses
>>> rp_bins = np.logspace(-1, 1, 10)
>>> period = model.mock.Lbox
>>> ds = mean_delta_sigma(galaxies, particles, effective_particle_masses, rp_bins, period)

Take care with the units. The values for \(\Delta\Sigma\) returned by the mean_delta_sigma functions are in comoving units of \(h M_{\odot} / {\rm Mpc}^2\) assuming h=1, whereas the typical units used to plot \(\Delta\Sigma\) are in physical units of \(M_{\odot} / {\rm pc}^2\) using the value of little h appropriate for your assumed cosmology.

The code shown above demonstrates how to calculate \(\Delta\Sigma\) via the excess surface density of mass using the z-axis as the axis of projection. However, it may be useful to project along the other Cartesian axes, for example to help beat down sample variance. While the mean_delta_sigma function is written to always use the “third” dimension as the projection axis, you can easily hack the code to project along, say, the y-axis by simply transposing your y- and z-coordinates when you pack them into a 2-d array:

>>> particles = np.vstack((px, pz, py)).T
>>> galaxies = np.vstack((x, z, y)).T

Using the above particles and galaxies and otherwise calling the mean_delta_sigma function as normal will instead calculate the surface mass density by projecting along the y-axis.