munge

A collection of functions for doing common processing tasks.

dragons.munge.munge.describe(arr, **kwargs)[source]

Run scipy.stats.describe and produce legible output.

Parameters:
  • arr (ndarray) – Numpy ndarray

  • **kwargs – passed to scipy.stats.describe

Return type:

output of scipy.stats.describe

dragons.munge.munge.edges_to_centers(edges, width=False)[source]

Convert evenly spaced bin edges to centers.

Parameters:
  • edges (ndarray) – bin edges

  • width (bool) – also return the bin width

Returns:

  • centers (ndarray) – bin centers (size = edges.size-1)

  • bin_width (float) – only returned if width = True

dragons.munge.munge.mass_function(mass, volume, bins, range=None, poisson_uncert=False, return_edges=False, **kwargs)[source]

Generate a mass function.

Parameters:
  • mass (ndarray) – an array of ‘masses’

  • volume (float) – volume of simulation cube/subset

  • bins (int or list or str) – passed to numpy.histogram

  • range (len=2 list or array) – range of data to be used for mass function

  • poisson_uncert (bool) – return poisson uncertainties in output array (default: False)

  • return_edges (bool) – return the bin_edges (default: False)

  • **kwargs – passed to numpy.histogram

Returns:

[bin centers, mass function vals] If poisson_uncert=True then array has 3rd column with uncertainties. If return_edges=True then the bin edges are also returned.

Return type:

array

dragons.munge.munge.ndarray_to_dataframe(arr, drop_vectors=False)[source]

Convert numpy ndarray to a pandas DataFrame, dealing with N(>1) dimensional datatypes.

Parameters:
  • arr (ndarray) – Numpy ndarray

  • drop_vectors (bool) – only include single value datatypes in output DataFrame

Returns:

df – Pandas DataFrame

Return type:

DataFrame

dragons.munge.munge.power_spectrum(grid, side_length, n_bins, dimensional=False)[source]

Calculate the dimensionless and dimensional power spectra of a grid (G):

\[ \begin{align}\begin{aligned}\Delta^2 (k) = \frac{k^3 V}{2\pi^2} <|\hat G|^2>_k\\P(k) = <|\hat G|^2> V\end{aligned}\end{align} \]
Parameters:
  • grid (ndarray) – The grid from which to construct the power spectrum

  • side_length (float) – The side length of the grid (assumes all side lengths are equal)

  • n_bins (int) – The number of k bins to use

  • dimensional (Boolean (optional)) – Switch for calculating dimensional power spectrum Default is False

Returns:

  • kmean (ndarray) – The mean wavenumber of each bin

  • power (ndarray) – The dimensionless power (\(\Delta^2 (k)\))

  • uncert (ndarray) – The uncertainty of the dimensionless power within each k bin

  • power_dim (ndarray) – The dimensional power (returned iff dimensional = True) (\(P(k)\))

  • uncert_dim (ndarray) – The uncertainty of the dimensional power within each k bin (returned iff dimensional = True)

dragons.munge.munge.pretty_print_dict(d, fmtlen=30)[source]

Pretty print a dictionary, dealing with recursion.

Parameters:
  • d (dict) – the dictionary to print

  • fmtlen (int) – maximum length of dictionary key for print formatting

dragons.munge.munge.smooth_grid(grid, side_length, radius, filt='tophat')[source]

Smooth a grid by convolution with a filter.

Parameters:
  • grid (ndarray) – The grid to be smoothed

  • side_length (float) – The side length of the grid (assumes all side lengths are equal)

  • radius (float) – The radius of the smoothing filter

  • filt (string, optional) – The name of the filter. Currently only “tophat” (real space) is implemented. More filters will be added over time.

Returns:

smoothed_grid – The smoothed grid.

Return type:

ndarray

dragons.munge.regrid.regrid(old_grid, n_cell)

Downgrade the resolution of a 3 dimensional grid.

Parameters:
  • (np.ndarray[float32 (old_grid)

  • ndim=3]) (Grid to be resampled)

  • (int) (n_cell)

Return type:

New, degraded resolution grid.