pynbody.analysis.profile.Profile#

class pynbody.analysis.profile.Profile(sim, load_from_file=False, ndim=2, type='lin', calc_x=None, weight_by='mass', **kwargs)[source]#

Bases: object

Generates profiles of specified quantities as a function of radius or other binning quantity.

Any quantity known in the SimSnap can be profiled, meaning that the mean value of that quantity in each bin is calculated.

See also

For more information and example usage, see the Profiles tutorial.

To define profiles of new quantities, see profile_property().

Implicit averaging: If an array ar is defined in the underlying SimSnap, then a profile of ar can be accessed as p['ar'] where p is a Profile object. For example, p['vr'] gives the radial velocity profile. Implicitly, this is averaged over all particles in each bin, weighted by mass (unless an alternate weighting scheme is passed to the weight_by keyword argument of the constructor).

Dispersions: One may append _rms or _disp to the name of a defined array to get the root-mean-square or dispersion profile, respectively. For example, p['vr_rms'] gives the root-mean-square radial velocity profile, while p['vr_disp'] gives the radial velocity dispersion profile. By definition, p['vr_disp']**2 is the same as p['vr_rms']**2 - p['vr']**2.

Derivatives: One may also prepend d_ to the name of a defined array to get the derivative, e.g. p['d_temp'] gives the radial temperature gradient.

Non-array profiles: Profiles can be defined that do not directly correspond to the average over an array in the snapshot. Examples include density, mass and mass_enc. These are implemented as functions in the pynbody.analysis.profile module; you can therefore find a list of available profiles by looking at the functions there. These profiles can be accessed in the same way as array profiles, e.g. p['density']. For profiles that take an argument, such as sb, this is passed in with an underscore e.g. p['sb_b'] for b-band surface brightnesses.

Storing profiles: Use the write() function to write the current profiles with all the necessary information to a file. Initialize a profile with the load_from_file=True keyword to automatically load a previously saved profile. The filename is chosen automatically and corresponds to a hash generated from the positions of the particles used in the profile. This is to ensure that you are always looking at the same set of particles, centered in the same way. It also means you must use the same centering method if you want to reuse a saved profile.

Changed in version 2.0: The method create_particle_array has been removed. Its behaviour was poorly defined in v1, and not believed to be widely used.

Methods

create_particle_array(profile_name[, ...])

Interpolate the profile back onto the particles

derivable_keys()

Returns a list of possible profiles

families()

Returns the family of particles used

keys()

Returns a listing of available profile types

profile_property(fn)

Function decorator to define a new profile property.

write()

Writes all the vital information of the profile to a file.

__init__(sim, load_from_file=False, ndim=2, type='lin', calc_x=None, weight_by='mass', **kwargs)[source]#

Initialise a profile, determining the binning quantity and bin size.

The constructor generates the bins without actually calculating any profiles. The profiles are calculated lazily when requested.

Parameters:
  • sim (pynbody.snapshot.SimSnap) – The simulation snapshot to generate a profile for

  • ndim (int, optional:) – Specifies whether it’s a 2D or 3D profile - in the 2D case, the bins are generated in the xy plane

  • type (str, optional:) – Specifies whether bins should be spaced linearly (‘lin’, default), logarithmically (‘log’) or contain equal numbers of particles (‘equaln’)

  • rmin (float, optional:) – Minimum value to consider (left-hand-edge of lowest bin). Default is the minimum value of the binning quantity.

  • rmax (float, optional:) – Maximum value to consider (right-hand-edge of highest bin). Default is the maximum value of the binning quantity.

  • nbins (int, optional:) – Number of bins to use. Default is 100.

  • bins (array-like, optional:) – Predefined bin edges in units of the binning quantity. If this keyword is set, the values of the keywords type, nbins, rmin and rmax will be ignored.

  • calc_x (function, optional:) – Function to use to calculate the value for binning. If None it defaults to the radial distance from origin (in either 2 or 3 dimensions), but you can specify this function to return any value you want for making profiles along arbitrary axes. Depending on your function, the units of certain profiles (such as density) might not make sense.

  • weight_by (str, optional:) – Name of the array to use for weighting averages across particles in each bin. Default is ‘mass’.

create_particle_array(profile_name, particle_name=None, log_x_interpolation=None, log_y_interpolation=None, target_simulation=None)[source]#

Interpolate the profile back onto the particles

For example, calling create_particle_array('density') will create a new array in the simulation called ‘density’ which is the density of each particle according to the profile.

Parameters:
  • profile_name (str) – The name of the profile to interpolate

  • particle_name (str, optional) – The name of the new array to create. If not specified, it will be the same as the profile_name.

  • log_x_interpolation (bool, optional) – If True, interpolate in log space for the x-axis; if False, don’t. If None, perform log interpolation if all bin centres are positive.

  • log_y_interpolation (bool, optional) – If True, interpolate in log space for the y-axis; if False, don’t. If None, perform log interpolation if all profile values are positive.

  • target_simulation (pynbody.SimSnap, optional) – The simulation to create the new array in. If not specified, the array will be created in the current simulation. Specifying another simulation is helpful e.g. if you want to interpolate the profile onto a different set of particles.

derivable_keys()[source]#

Returns a list of possible profiles

families()[source]#

Returns the family of particles used

keys()[source]#

Returns a listing of available profile types

static profile_property(fn)[source]#

Function decorator to define a new profile property.

For example,

@Profile.profile_property
def x_squared(pro):
    return pro['x']**2

would define a new profile property ‘x_squared’ which is the square of the ‘x’ profile. This can then be accessed as pro['x_squared'] for any profile object pro.

write()[source]#

Writes all the vital information of the profile to a file.

To recover the profile, initialize a profile with the load_from_file=True keyword to automatically load a previously saved profile. The filename is chosen automatically and corresponds to a hash generated from the positions of the particles used in the profile. This is to ensure that you are always looking at the same set of particles, centered in the same way. It also means you must use the same centering method if you want to reuse a saved profile.