pynbody.halo.ahf.AHFCatalogue#
- class pynbody.halo.ahf.AHFCatalogue(sim, filename=None, make_grp=None, get_all_parts=None, use_iord=None, ahf_basename=None, dosort=None, only_stat=None, write_fpos=True, halo_numbers='ahf', ignore_missing_substructure=True, **kwargs)[source]#
Bases:
HaloCatalogueClass to handle catalogues produced by Amiga Halo Finder (AHF).
Changed in version 2.7.0: Unless it is using particle IDs and the newer file format, AHF expresses halo membership as offsets within the snapshot’s families, and the catalogue now refuses to load against a partially loaded snapshot, raising a
PartialLoadingNotSupportedError; previously the wrong particles were returned. Where particle IDs are used, halos which extend beyond the loaded particles are reported throughcomplete_keys().- Attributes:
baseThe snapshot object that this halo catalogue is based on.
Methods
complete_keys([load_all_if_required])Return an array of the halo numbers which can actually be retrieved from the snapshot.
contains(halo_number)Deprecated alias; instead of
h.contains(number)usenumber in h.from_portable_state(state, sim)Recreate a halo catalogue from a state dictionary, attaching it to the specified simulation.
get_complete_mask([load_all_if_required])Return a boolean mask, in halo index order, of the halos which can be retrieved from the snapshot.
get_dummy_halo(halo_number)Return a DummyHalo object containing only the halo properties, no particle information
get_group_array([family, use_index, fill_value])Return an array with an integer for each particle in the simulation, indicating the halo of that particle.
Express the entire catalogue as numpy arrays and python primitives.
get_properties_all_halos([with_units])Returns a dictionary of properties for all halos.
get_properties_one_halo(halo_number)Returns a dictionary of properties for a single halo, given a halo_number.
is_complete(halo_number[, load_all_if_required])Return True if the specified halo can actually be retrieved from the snapshot.
Iterate over all subclasses of this class, recursively.
iter_subclasses_with_priority(priority)Iterate over all subclasses, starting with the given priorities
keys()Return an array of all halo numbers in the catalogue, whether or not they can be loaded.
load_all()Loads all halos, which is normally more efficient if a large fraction of them will be accessed.
load_copy(halo_number)Load a fresh SimSnap with only the particles in specified halo
physical_units([distance, velocity, mass, ...])Converts all arrays' units to be consistent with the distance, velocity, mass basis units specified.
Deprecated alias for
load_all()- __init__(sim, filename=None, make_grp=None, get_all_parts=None, use_iord=None, ahf_basename=None, dosort=None, only_stat=None, write_fpos=True, halo_numbers='ahf', ignore_missing_substructure=True, **kwargs)[source]#
Initialize an AHFCatalogue.
- Parameters:
sim (SimSnap) – the simulation snapshot to which this catalogue refers
filename (str | pathlib.Path) – specify a path to an AHF halo catalog. Note that AHF actually outputs multiple files; you can specify the path to the
AHF_halosorAHF_particlesfile and the code will infer the other filenames from this. Alternatively you can specify the path up to theAHF_prefix and the code will similarly infer the full set of filenames.halo_numbers (str, optional) –
specify how to number the halos. Options are:
’ahf’ (default): use the halo numbers written in the AHF halos file if present, or a zero-based indexing
’file-order’: zero-based indexing of the halos
’v1’: one-based indexing of the halos, compatible with pynbody v1 default behaviour
’length-order’: sort by the number of particles in each halo, with the halo with most particles being halo 0
’length-order-v1’: as length-order, but indexing from halo 1, compatible with
dosort=Truein pynbody v1
ignore_missing_substructure (bool, optional) – If True (default), the code will not raise an exception if the substructure file is missing or corrupt. If False, it will raise an exception.
use_iord (bool, optional) – if True, the particle IDs in the Amiga catalogue are taken to refer to the iord array. If False, they are the particle offsets within the file. If None, the parameter defaults to True for GadgetSnap, False otherwise.
write_fpos (bool, optional) – If True (default), the code will attempt to write a file containing the starting positions of each halo’s particle information within the AHF_particles file. If False, it will not attempt to write this file. This file is used to speed up loading of particle information for individual halos when
load_all()is not called. If :meth`load_all` is called, there is no benefit to writing the file and better performance is obtained by usingwrite_fpos=False.ahf_basename (str, optional) – Deprecated way to specify the location of the catalogue
make_grp – Deprecated. If True a ‘grp’ array is created in the underlying snapshot specifying the lowest level halo that any given particle belongs to. If it is False, no such array is created; if None, the behaviour is determined by the configuration system.
get_all_parts – Deprecated; use the
load_all()method instead.dosort – Deprecated; equivalent to
halo_numbers='length-order'only_stat – Deprecated; this keyword is now ignored. To obtain halo information without loading the particles, use the methods
get_properties_one_halo()orget_properties_all_halos().
- property base#
The snapshot object that this halo catalogue is based on.
- complete_keys(load_all_if_required=True) NDArray[int]#
Return an array of the halo numbers which can actually be retrieved from the snapshot.
This is a subset of
keys(), in the same order, from which halos referring to particles that are not present in the snapshot have been excluded. Accessing such halos raises anIncompleteHaloError; this normally happens only if the snapshot has been partially loaded.Note that halo properties remain available for all halos, including incomplete ones; it is only access to the particles which fails.
Some catalogue formats express halo membership through an array covering only the particles which were loaded, such as a halo number per particle, and so are unable to tell whether the halo finder assigned further particles which are absent. These report all their halos as complete, and issue a RuntimeWarning to that effect. (Formats which instead identify their particles by position within the snapshot cannot be used with a partially loaded snapshot at all; they refuse to load.)
Added in version 2.7.0.
- Parameters:
load_all_if_required (bool) – Establishing which halos are complete requires the particle lists for all halos, so
load_all()is called if it has not been already. If this is undesirable (e.g. because the catalogue is very large), pass False to raise a RuntimeError instead.
- contains(halo_number: int) bool#
Deprecated alias; instead of
h.contains(number)usenumber in h.
- classmethod from_portable_state(state: dict, sim: SimSnap) HaloCatalogue#
Recreate a halo catalogue from a state dictionary, attaching it to the specified simulation.
Added in version 2.7.0.
- Parameters:
state (dict) – A dictionary previously returned by
get_portable_state(), possibly having been transferred from another process.sim (
SimSnap) – The simulation to attach the catalogue to. It must have the same particle ordering as the simulation the state was generated from, since halo membership is expressed as offsets into it.
- Returns:
A halo catalogue which behaves like the original, but does not refer to the halo finder’s files.
- Return type:
- get_complete_mask(load_all_if_required=True) NDArray[bool]#
Return a boolean mask, in halo index order, of the halos which can be retrieved from the snapshot.
This is the underlying information from which
complete_keys()andis_complete()are derived. Since it is in index order, it can be used to filter the arrays returned byget_properties_all_halos(). (See the class documentation for the distinction between halo numbers and indices.)See
complete_keys()for the meaning of theload_all_if_requiredargument. The returned array is cached and read-only.Added in version 2.7.0.
- get_dummy_halo(halo_number) DummyHalo#
Return a DummyHalo object containing only the halo properties, no particle information
The units of the properties match those of the corresponding
Haloobject; seeget_properties_one_halo().
- get_group_array(family=None, use_index=False, fill_value=-1)#
Return an array with an integer for each particle in the simulation, indicating the halo of that particle.
If there are multiple levels (i.e. subhalos), the number returned corresponds to the lowest level, i.e. the smallest subhalo.
- Parameters:
family (str, optional) – If specified, return only the group array for the specified family.
use_index (bool, optional) – If True, return the halo index rather than the halo number. (See the class documentation for the distinction between halo numbers and indices.)
fill_value (int, optional) – The value to fill for particles not in any halo.
- get_portable_state() dict#
Express the entire catalogue as numpy arrays and python primitives.
The result is a dictionary whose values are numpy arrays, python primitives (such as strings and integers), or nested dictionaries of the same. Nothing in it is tied to the present process, or to the halo finder’s files, so it may be transferred elsewhere – for example to another process, by putting the arrays into shared memory. The recipient does not need to know the role of any individual array; it needs only to reproduce the structure at the other end, where
from_portable_state()turns it back into a live halo catalogue attached to a specified simulation.The state includes the halo numbering, the particles belonging to each halo, any information about particles that are missing from the snapshot (see
IncompleteHaloError), whether this catalogue is able to detect such particles at all (_can_determine_completeness, so that a catalogue which cannot does not appear to have become able to once transferred), and the halo finder properties that are available fromget_properties_all_halos(). Properties which are only available one halo at a time, i.e. those provided by catalogues which implementget_properties_one_halo()withoutget_properties_all_halos(), are not included.Note that halo membership is expressed as offsets into the snapshot, so the state is only meaningful alongside a snapshot with the same particle ordering as the present one.
Added in version 2.7.0.
- get_properties_all_halos(with_units=True) dict[source]#
Returns a dictionary of properties for all halos.
If with_units is True, the properties are returned as SimArrays with units if possible. Otherwise, numpy arrays are returned.
Note that the returned properties are in contiguous arrays, and as a result may be in a different order to the halo numbers which are used to access individual halos. To map between halo numbers and properties, use the .number_mapper object; or access individual property dictionaries by halo number using get_properties_one_halo.
- get_properties_one_halo(halo_number) dict#
Returns a dictionary of properties for a single halo, given a halo_number.
The units of the returned properties match those of the corresponding
Haloobject. Ifphysical_units()has been called persistently on the simulation or on this catalogue, the properties are converted to those physical units. Otherwise, they are in the units provided by the halo finder.Changed in version 2.7.2: Properties now always match the units of the corresponding
Halo. Previously, whether a persistentphysical_units()conversion was applied depended on how the catalogue had been accessed.
- is_complete(halo_number, load_all_if_required=True) bool#
Return True if the specified halo can actually be retrieved from the snapshot.
See
complete_keys()for more information, including the meaning of theload_all_if_requiredargument.Added in version 2.7.0.
- classmethod iter_subclasses() Iterable[type]#
Iterate over all subclasses of this class, recursively.
This is used by HaloCatalogue and SimSnap to find a suitable loader for a given file.
- classmethod iter_subclasses_with_priority(priority: Iterable[str | type]) Iterable[type]#
Iterate over all subclasses, starting with the given priorities
The priorities can be provided either as a string or a class
- keys() NDArray[int]#
Return an array of all halo numbers in the catalogue, whether or not they can be loaded.
If the snapshot has been partially loaded, some of these halos may refer to particles that are not present, and cannot be retrieved; see
complete_keys().The returned array is read-only, since it may be a view of the catalogue’s internal numbering.
Changed in version 2.7.0: A read-only numpy array is returned. Previously this was whatever the catalogue’s number mapper held, and was writable, so code which modified it in place must now take a copy first.
- load_all()#
Loads all halos, which is normally more efficient if a large fraction of them will be accessed.
- load_copy(halo_number)#
Load a fresh SimSnap with only the particles in specified halo
This relies on the underlying SimSnap being capable of partial loading.
- physical_units(distance='kpc', velocity='km s^-1', mass='Msol', persistent=True, convert_parent=False)#
Converts all arrays’ units to be consistent with the distance, velocity, mass basis units specified.
- Parameters:
distance (string (default = 'kpc')) – The distance unit to convert to.
velocity (string (default = 'km s^-1')) – The velocity unit to convert to.
mass (string (default = 'Msol')) – The mass unit to convert to.
persistent (boolean (default = True)) – Apply units change to future lazy-loaded arrays if True.
convert_parent (boolean (default = True)) – Propagate units change from a halo catalogue to a parent snapshot. See note below.
Note
The option convert_parent is only applicable to
Haloobjects. It is ignored by all other objects, includingpynbody.snapshot.simsnap.SimSnap,pynbody.snapshot.subsnap.SubSnap, andpynbody.halo.HaloCatalogueobjects.When
physical_unitsis called on apynbody.halo.Haloand convert_parent is True, no immediate action is taken on thepynbody.halo.Haloitself; rather the request is passed upwards to thepynbody.halo.HaloCatalogue.The catalogue object then calls
physical_unitson the parent snapshot and on all cached halos, settingconvert_parent=Falseso that the units change is then applied to thepynbody.halo.Haloobject itself.This ensures that unit changes propagate through to properties of all halos. Most users will not need to worry about this subtlety; things should ‘just work’ if you ignore the convert_parent option.
- precalculate()#
Deprecated alias for
load_all()