swiftgalaxy.masks module

Mask particles to select galaxies. Supplements swiftsimio’s spatial masking.

The swiftsimio masking features are optimized for spatial masking, that is selecting regions made up of a subset of the SWIFT “top-level cells” in a simulation. swiftgalaxy masking features aim to support refining these relatively coarse spatial masks to select particles belonging to individual structures, or other arbitrary sets of particles, and therefore needs its own masking tools. In their long term the hope is to merge the two together, but for now the MaskCollection is the recommended way to define a selection of particles of different types for use with SWIFTGalaxy.

class swiftgalaxy.masks.LazyMask(mask: slice | EllipsisType | ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None, mask_function: Callable | None = None, combinable: bool = False)[source]

Bases: object

A class to hold a function to evaluate a mask until it is needed.

This class can contain either an explicitly evaluated mask (boolean array, slice, etc.) or a reference to a function that returns such a mask when called. When the mask property is accessed, if the mask is already evaluated it is returned, otherwise it is evaluated and returned.

The _evaluated attribute tracks whether the explicitly evaluated mask is available.

Parameters:
  • mask (slice, default: None) – An object that can be used to mask an array (slice, boolean array, etc.).

  • mask_function (Callable, default: None) – A reference to a function that returns a mask when called.

  • combinable (bool) – If True, it declares that for this mask data[this_mask][other_mask] is equivalent to data[this_mask[other_mask]]. This usually means that it is an array of integer indices to select from data.

property mask: slice | EllipsisType | ndarray[tuple[Any, ...], dtype[_ScalarT]] | None

Get the explicitly evaluated mask, evaluating it if necessary.

Returns:

The explicitly evaluated mask.

Return type:

slice

class swiftgalaxy.masks.MaskCollection(**kwargs: slice | EllipsisType | ndarray[tuple[Any, ...], dtype[_ScalarT]] | None | LazyMask)[source]

Bases: object

Barebones container for mask objects.

Takes a set of kwargs at initialisation and assigns their values to attributes of the object. Attempts to access a non-existent attribute returns None instead of raising an AttributeError.

This is intended to hold masks that can be applied to cosmo_array objects under the names of particle types (e.g. gas, dark_matter, etc.), but this is not checked or enforced.

Parameters:

**kwargs – Any items passed as kwargs will have their values passed to correspondingly named attributes of this object.

Notes

Note

The velociraptor.swift.swift module makes some use of a namedtuple called MaskCollection. These objects are not valid where swiftgalaxy functions expect a MaskCollection because namedtuple objects are immutable.

Examples

n_dm = 123  # suppose this is number of dark matter particles
# all these masks select all particles:
MaskCollection(
    gas=np.s_[...],
    dark_matter=np.ones(n_dm, dtype=bool),
    stars=None
)
combined_with(other_mask_collection: MaskCollection, *, sg: SWIFTGalaxy) MaskCollection[source]

Combine this MaskCollection with another.

data[this_mask.<type>.mask][other_mask.<type>.mask] and data[combined_mask.<type>.mask] are equivalent, where combined_mask = this_mask.combined_with(other_mask).

Parameters: