nbodykit.algorithms.fof

Functions

centerofmass(label, pos, boxsize[, comm])

Calulate the center of mass of particles of the same label.

count(label[, comm])

Count the number of particles of the same label.

equiv_class(labels, values, op[, …])

apply operation to equivalent classes by label, on values

fof(source, linking_length, comm, periodic, …)

Run Friends-of-friends halo finder.

fof_catalog(source, label, comm[, position, …])

Catalog of FOF groups based on label from a parent source

fof_find_peaks(source, label, comm[, …])

Find position of the peak (maximum) from a given column for a fof result.

replacesorted(arr, sorted, b[, out])

replace a with corresponding b in arr

Classes

FOF(source, linking_length, nmin[, …])

A friends-of-friends halo finder that computes the label for each particle, denoting which halo it belongs to.

class nbodykit.algorithms.fof.FOF(source, linking_length, nmin, absolute=False, periodic=True, domain_factor=1)[source]

A friends-of-friends halo finder that computes the label for each particle, denoting which halo it belongs to.

Friends-of-friends was first used by Davis et al 1985 to define halos in hierachical structure formation of cosmological simulations. The algorithm is also known as DBSCAN in computer science. The subroutine here implements a parallel version of the FOF.

The underlying local FOF algorithm is from kdcount.cluster, which is an adaptation of the implementation in Volker Springel’s Gadget and Martin White’s PM.

Results are computed when the object is inititalized. See the documenation of run() for the attributes storing the results.

For returning a CatalogSource of the FOF halos, see find_features() and for computing a halo catalog with added analytic information for a specific redshift and cosmology, see to_halos().

Parameters
  • source (CatalogSource) – the source to run the FOF algorithm on; must support ‘Position’

  • linking_length (float) – the linking length, either in absolute units, or relative to the mean particle separation

  • nmin (int) – halo with fewer particles are ignored

  • absolute (bool, optional) – If True, the linking length is in absolute units, otherwise it is relative to the mean particle separation; default is False

Methods

find_features(self[, peakcolumn])

Based on the particle labels, identify the groups, and return the center-of-mass CMPosition, CMVelocity, and Length of each feature.

run(self)

Run the FOF algorithm.

to_halos(self, particle_mass, cosmo, redshift)

Return a HaloCatalog, holding the center-of-mass position and velocity of each FOF halo, as well as the properly scaled mass, for a given cosmology and redshift.

find_features(self, peakcolumn=None)[source]

Based on the particle labels, identify the groups, and return the center-of-mass CMPosition, CMVelocity, and Length of each feature.

If a peakcolumn is given, the PeakPosition and PeakVelocity is also calculated for the particle at the peak value of the column.

Data is scattered evenly across all ranks.

Returns

a source holding the (‘CMPosition’, ‘CMVelocity’, ‘Length’) of each feature; optionaly, PeakPosition, PeakVelocity are also included if peakcolumn is not None

Return type

ArrayCatalog

run(self)[source]

Run the FOF algorithm. This function returns nothing, but does attach several attributes to the class instance:

  • attr:labels

  • max_labels

Note

The labels array is scattered evenly across all ranks.

labels

an array the label that specifies which FOF halo each particle belongs to

Type

array_like, length: size

max_label

the maximum label across all ranks; this represents the total number of FOF halos found

Type

int

to_halos(self, particle_mass, cosmo, redshift, mdef='vir', posdef='cm', peakcolumn='Density')[source]

Return a HaloCatalog, holding the center-of-mass position and velocity of each FOF halo, as well as the properly scaled mass, for a given cosmology and redshift.

The returned catalog also has default analytic prescriptions for halo radius and concentration.

The data is scattered evenly across all ranks.

Parameters
  • particle_mass (float) – the particle mass, which is used to convert the number of particles in each halo to a total mass

  • cosmo (nbodykit.cosmology.core.Cosmology) – the cosmology of the catalog

  • redshift (float) – the redshift of the catalog

  • mdef (str, optional) – string specifying mass definition, used for computing default halo radii and concentration; should be ‘vir’ or ‘XXXc’ or ‘XXXm’ where ‘XXX’ is an int specifying the overdensity

  • posdef (str, optional) – position, can be cm (center of mass) or peak (particle with maximum value on a column)

  • peakcolumn (str , optional) – when posdef is ‘peak’, this is the column in source for identifying particles at the peak for the position and velocity.

Returns

a HaloCatalog at the specified cosmology and redshift

Return type

HaloCatalog

nbodykit.algorithms.fof.centerofmass(label, pos, boxsize, comm=<mpi4py.MPI.Intracomm object at 0x7f71a6039270>)[source]

Calulate the center of mass of particles of the same label.

The center of mass is defined as the mean of positions of particles, but care has to be taken regarding to the periodic boundary.

This is a collective operation, and after the call, all ranks will have the position of halos.

Parameters
  • label (array_like (integers)) – Halo label of particles, >=0

  • pos (array_like (float, 3)) – position of particles.

  • boxsize (float or None) – size of the periodic box, or None if no periodic boundary is assumed.

  • comm (MPI.Comm) – communicator for the collective operation.

Returns

hpos – the center of mass position of the halos.

Return type

array_like (float, 3)

nbodykit.algorithms.fof.count(label, comm=<mpi4py.MPI.Intracomm object at 0x7f71a6039270>)[source]

Count the number of particles of the same label.

This is a collective operation, and after the call, all ranks will have the particle count.

Parameters
  • label (array_like (integers)) – Halo label of particles, >=0

  • comm (MPI.Comm) – communicator for the collective operation.

Returns

count – the count of number of particles in each halo

Return type

array_like

nbodykit.algorithms.fof.equiv_class(labels, values, op, dense_labels=False, identity=None, minlength=None)[source]

apply operation to equivalent classes by label, on values

Parameters
  • labels (array_like) – the label of objects, starting from 0.

  • values (array_like) – the values of objects (len(labels), …)

  • op (numpy.ufunc) – the operation to apply

  • dense_labels (boolean) – If the labels are already dense (from 0 to Nobjects - 1) If False, numpy.unique() is used to convert the labels internally

Returns

the value of each equivalent class

Return type

result

Examples

>>> x = numpy.arange(10)
>>> print equiv_class(x, x, numpy.fmin, dense_labels=True)
[0 1 2 3 4 5 6 7 8 9]
>>> x = numpy.arange(10)
>>> v = numpy.arange(20).reshape(10, 2)
>>> x[1] = 0
>>> print equiv_class(x, 1.0 * v, numpy.fmin, dense_labels=True, identity=numpy.inf)
[[  0.   1.]
 [ inf  inf]
 [  4.   5.]
 [  6.   7.]
 [  8.   9.]
 [ 10.  11.]
 [ 12.  13.]
 [ 14.  15.]
 [ 16.  17.]
 [ 18.  19.]]
nbodykit.algorithms.fof.fof(source, linking_length, comm, periodic, domain_factor, logger)[source]

Run Friends-of-friends halo finder.

Friends-of-friends was first used by Davis et al 1985 to define halos in hierachical structure formation of cosmological simulations. The algorithm is also known as DBSCAN in computer science. The subroutine here implements a parallel version of the FOF.

The underlying local FOF algorithm is from kdcount.cluster, which is an adaptation of the implementation in Volker Springel’s Gadget and Martin White’s PM. It could have been done faster.

Parameters
  • source (CatalogSource) – the input source of particles; must support ‘Position’ column; source.attrs['BoxSize'] is also used

  • linking_length (float) – linking length in data units. (Usually Mpc/h).

  • comm (MPI.Comm) – The mpi communicator.

Returns

minid – A unique label of each position. The label is not ranged from 0.

Return type

array_like

nbodykit.algorithms.fof.fof_catalog(source, label, comm, position='Position', velocity='Velocity', initposition='InitialPosition', peakcolumn=None, periodic=True)[source]

Catalog of FOF groups based on label from a parent source

This is a collective operation – the returned halo catalog will be equally distributed across all ranks

Notes

This computes the center-of-mass position and velocity in the same units as the corresponding columns source

Parameters
  • source (CatalogSource) – the parent source of particles from which the center-of-mass position and velocity are computed for each halo

  • label (array_like) – the label for each particle that identifies which halo it belongs to

  • comm (MPI.Comm) – the mpi communicator. Must agree with the datasource

  • position (str, optional) – the column name specifying the position

  • velocity (str, optional) – the column name specifying the velocity

  • initposition (str, optional) – the column name specifying the initial position; this is only computed if available

  • peakcolumn (str, optional) – if not None, find PeakPostion and PeakVelocity based on the value of peakcolumn

Returns

catalog – A 1-d array of type ‘Position’, ‘Velocity’, ‘Length’. The center mass position and velocity of the FOF halo, and Length is the number of particles in a halo. The catalog is sorted such that the most massive halo is first. catalog[0] does not correspond to any halo.

Return type

array_like

nbodykit.algorithms.fof.fof_find_peaks(source, label, comm, position='Position', column='Density')[source]

Find position of the peak (maximum) from a given column for a fof result.

nbodykit.algorithms.fof.replacesorted(arr, sorted, b, out=None)[source]

replace a with corresponding b in arr

Parameters
  • arr (array_like) – input array

  • sorted (array_like) – sorted

  • b (array_like) –

  • out (array_like,) – output array

  • Result

  • ------

  • newarr (array_like) – arr with a replaced by corresponding b

Examples

>>> print replacesorted(numpy.arange(10), numpy.arange(5), numpy.ones(5))
[1 1 1 1 1 5 6 7 8 9]