lib.samples.mesh.Mesh

class lib.samples.mesh.Mesh(mesh, dims, edges=None, nodes=None, isdensity=True)

Bases: cosmopipe.lib.utils.BaseClass

Class that represents a mesh. TODO: decide what to do with MPI… scatter mesh? Gather mesh?

Initialize Mesh.

Parameters
  • mesh (array) – Mesh array.

  • dims (list) – List of labels (one for each mesh dimension).

  • edges (list, default=None) – List of edges (one array for each mesh dimension). If None, use nodes instead. Mesh shape must be length of edges - 1 along each dimension.

  • nodes (list, default=None) – List of mesh nodes (one array for each mesh dimension).

  • isdensity (bool, default=True) – Whether to normalize binned (weighted) samples by mesh volume (True).

Methods

argmax

Return coordinates of maximum along axes axes or dimensions dims.

argmean

Return mean coordinates along axes axes or dimensions dims.

copy

Return shallow copy of self.

deepcopy

Return deep copy.

from_samples

Initialize mesh from samples.

from_state

Instantiate and initalize class with state dictionary.

get_axes

Return axes (axes) corresponding to dimensions dims.

get_levels

Return density threshold(s) corresponding to confidence level(s) targets.

get_sigmas

Return density thresholds corresponding to n-sigmas confidence levels sigmas.

integrate

Integrate mesh along axes axes or dimensions dims.

interpolate

Interpolate mesh at provided points.

is_mpi_broadcast

is_mpi_gathered

is_mpi_root

is_mpi_scattered

load

Load class in numpy binary format from disk.

load_auto

If different formats are possible, this method should between them based on file name extension.

log_critical

log_debug

log_error

log_info

log_warning

max

Return mesh maximum value.

mnodes

Return meshgrid of nodes along axes axes or dimensions dims.

ranges

Return mesh extension along axes axes or dimensions dims.

save

Save class to disk.

save_auto

If different formats are possible, this method should between them based on file name extension.

save_txt

Save mesh as ASCII file.

volume

Return volume array along axes axes or dimensions dims.

widths

Return cell sizes along axes axes or dimensions dims.

Attributes

logger

mpiattrs

MPI attributes

mpicomm

mpiroot

mpistate

ndim

Return number of dimensions.

shape

Return mesh shape.

__add__(other)

Addition of two meshes.

__call__(axes=None)

Return mesh reduced to axes axes or dimensions dims. Mesh is integrated along other dimensions.

argmax(axes=None)

Return coordinates of maximum along axes axes or dimensions dims.

argmean(axes=None)

Return mean coordinates along axes axes or dimensions dims.

copy()

Return shallow copy of self.

deepcopy()

Return deep copy.

classmethod from_samples(samples, weights=None, dims=None, bins=30, method='cic', bw_method='scott')

Initialize mesh from samples. TODO: scattered samples?

Parameters
  • samples (array) – Array of positions, with coordinates along first axes.

  • weights (array, default=None) – Sample weights, defaults to 1.

  • dims (list) – List of labels (one for each mesh dimension).

  • bins (int, default=30) – Number of bins i.e. mesh nodes to use along each dimension.

  • method (string) –

    Method to interpolate (weighted) samples on mesh, either:

    • ’gaussian_kde’: Gaussian kernel density estimation, based on scipy.stats.gaussian_kde

    • ’cic’ : Cloud-in-Cell assignment

    • ’histo’ : simple binning.

  • bw_method (string, default='scott') – If method is 'gaussian_kde', method to determine KDE bandwidth, see scipy.stats.gaussian_kde.

Returns

mesh

Return type

Mesh

classmethod from_state(state, mpiroot=0, mpicomm=None)

Instantiate and initalize class with state dictionary.

get_axes(dims=None)

Return axes (axes) corresponding to dimensions dims.

get_levels(targets)

Return density threshold(s) corresponding to confidence level(s) targets.

get_sigmas(sigmas)

Return density thresholds corresponding to n-sigmas confidence levels sigmas.

Parameters

sigmas (int, list, array) – Sigmas to get density thresholds for. If scalar, return thresholds up to (including) sigmas.

Returns

levels – Density thresholds.

Return type

array

integrate(axes=None)

Integrate mesh along axes axes or dimensions dims.

interpolate(points, method='linear')

Interpolate mesh at provided points.

Parameters
  • points (array) – Array of points, with coordinates along last axis.

  • method (string) – Interpolation method, ‘linear’ or ‘nearest’ (see scipy.interpolate.interpn())

Returns

values – Mesh values at interpolated points.

Return type

array

classmethod load(filename, mpiroot=0, mpicomm=None)

Load class in numpy binary format from disk. If the loaded state contains __class__ and that exists in cls._registry, return instance of cls._registry[__class__] (instead of cls).

load_auto(*args, **kwargs)

If different formats are possible, this method should between them based on file name extension.

max()

Return mesh maximum value.

mnodes(axes=None)

Return meshgrid of nodes along axes axes or dimensions dims.

property mpiattrs

MPI attributes

property ndim

Return number of dimensions.

ranges(axes=None)

Return mesh extension along axes axes or dimensions dims.

save(filename)

Save class to disk.

save_auto(*args, **kwargs)

If different formats are possible, this method should between them based on file name extension.

save_txt(filename, header='', fmt='%.8e', delimiter=' ', **kwargs)

Save mesh as ASCII file. TODO: needs major improvement, e.g. what about dimension names?

Parameters
  • filename (string) – File name where to save mesh.

  • kwargs (dict) – Arguments for numpy.savetxt().

property shape

Return mesh shape.

volume(axes=None)

Return volume array along axes axes or dimensions dims.

widths(axes=None)

Return cell sizes along axes axes or dimensions dims.