# General utilities¶

## Utilities¶

This unit contains a number of useful constants and functions for analyzing SPARTA output:

 isArray(var) Tests whether a variable var is iterable or not. findNearestIndexSorted(d, value) Find the index of a value in an array, where the array is sorted ascendingly. findNearestIndices(d, values) Find the indices of a number of values in an array, where the array is sorted ascendingly. findRedshiftIndex(fn[, a, z]) Returns the closest index in a SPARTA file to a given redshift of scale factor. findIndices(set_all, set_find[, algorithm]) Find the indices of one set of integers in another. findIndicesMultiD(set_all, set_find[, algorithm]) Find the indices of one set of integers in another, higher-dimensional array.
sparta.utils.RHO_CRIT_0_KPC3 = 277.4848

The critical density of the universe at z = 0 in units of $$M_{\odot} h^2 / kpc^3$$.

sparta.utils.isArray(var)

Tests whether a variable var is iterable or not.

Parameters
var: array_like

Variable to be tested.

Returns
is_array: boolean

Whether var is a numpy array or not.

sparta.utils.findNearestIndexSorted(d, value)

Find the index of a value in an array, where the array is sorted ascendingly.

Parameters
d: array_like

Array of values.

value: float

The value to search.

Returns
idx: int

The index where the array value is closest.

sparta.utils.findNearestIndices(d, values)

Find the indices of a number of values in an array, where the array is sorted ascendingly.

Parameters
d: array_like

Array of values.

values: array_like

An array of values to search.

Returns
idx: array_like

The indices where the array values are closest to values. Has the same dimensions as values.

sparta.utils.findRedshiftIndex(fn, a=None, z=None)

Returns the closest index in a SPARTA file to a given redshift of scale factor.

Note that this function does not check whether the given redshift was within the range of redshifts in the SPARTA file.

Parameters
a: float

Scale factor (either a or z need to be passed).

z: float

Redshift (either a or z need to be passed).

Returns
idx: int

Index of closest redshift bin in SPARTA file.

a: float

The scale factor at the chosen snapshot.

z: float

The redshift at the chosen snapshot.

sparta.utils.findIndices(set_all, set_find, algorithm='auto')

Find the indices of one set of integers in another.

This function is useful for finding the indices of a set of IDs in another large array of IDs. For example, the load() function does not return halos in the requested order if halo_ids are passed to the function. This function makes it easy to obtain the original ordering.

Two algorithms are implemented: the standard np.where() function which searches each ID separately, and an algorithm based on np.searchsorted(). The latter is faster when set_find is large (greater than 100). By default, the best algorithm is chosen.

Parameters
set_all: array_like

A set of unique integer numbers (does not need to be sorted).

set_find: array_like

The numbers to be found in set_all (does not need to be sorted, and must be smaller or equal in size to set_all).

algorithm: str

If auto, the function automatically chooses the best algorithm. If individual, the np.where() function is used. If array, the np.searchsorted() function is used.

Returns
idx_find: array_like

Array of indices of the same size as set_find, pointing to set_all.

sparta.utils.findIndicesMultiD(set_all, set_find, algorithm='auto')

Find the indices of one set of integers in another, higher-dimensional array.

Like findIndices, but allows set_all to have higher dimensionality. Only the index in the first dimension is returned. This is useful for index arrays that have a time axis to them, where we do not care at what time an ID is found.

Parameters
set_all: array_like

A set of unique integer numbers (does not need to be sorted).

set_find: array_like

The numbers to be found in set_all (does not need to be sorted, and must be smaller or equal in size to set_all).

algorithm: str

If auto, the function automatically chooses the best algorithm. If individual, the np.where() function is used. If array, the np.searchsorted() function is used.

Returns
idx_find: array_like

Array of indices of the same size as set_find, pointing to set_all.