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:
objectA 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
maskproperty is accessed, if the mask is already evaluated it is returned, otherwise it is evaluated and returned.The
_evaluatedattribute 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 maskdata[this_mask][other_mask]is equivalent todata[this_mask[other_mask]]. This usually means that it is an array of integer indices to select fromdata.
- class swiftgalaxy.masks.MaskCollection(**kwargs: slice | EllipsisType | ndarray[tuple[Any, ...], dtype[_ScalarT]] | None | LazyMask)[source]
Bases:
objectBarebones 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
Noneinstead of raising anAttributeError.This is intended to hold masks that can be applied to
cosmo_arrayobjects 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.swiftmodule makes some use of anamedtuplecalledMaskCollection. These objects are not valid whereswiftgalaxyfunctions expect aMaskCollectionbecausenamedtupleobjects 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
MaskCollectionwith another.data[this_mask.<type>.mask][other_mask.<type>.mask]anddata[combined_mask.<type>.mask]are equivalent, wherecombined_mask = this_mask.combined_with(other_mask).- Parameters:
other_mask_collection (MaskCollection) – The other mask collection to combine with this one.
sg (SWIFTGalaxy) – The
SWIFTGalaxyto use to look up particle count metadata.