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.

Args:
arr : ndarray
Numpy ndarray
Kwargs:
passed to scipy.stats.describe
Returns:
output of scipy.stats.describe
dragons.munge.munge.edges_to_centers(edges, width=False)[source]

Convert evenly spaced bin edges to centers.

Args:
edges : ndarray
bin edges
Kwargs:
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.

Args:
mass : array
an array of ‘masses’
volume : float
volume of simulation cube/subset
bins : int or list or str
If bins is a string, then it must be one of:
‘blocks’ : use bayesian blocks for dynamic bin widths
‘knuth’ : use Knuth’s rule to determine bins
‘scott’ : use Scott’s rule to determine bins
‘freedman’ : use the Freedman-diaconis rule to determine bins
Kwargs:
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 np.histogram call
Returns:

array of [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.

Notes:
The code to generate the bin_widths is taken from astroML.hist
dragons.munge.munge.ndarray_to_dataframe(arr, drop_vectors=False)[source]

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

Args:
arr : ndarray
Numpy ndarray
Kwargs:
drop_vectors : bool
only include single value datatypes in output DataFrame
Returns:
df : DataFrame
Pandas DataFrame
dragons.munge.munge.power_spectrum(grid, side_length, n_bins)[source]

Calculate the dimensionless power spectrum of a grid (G):

\[\begin{split}\Delta = \frac{k^3}{2\pi^2 V} <|\hat G|^2>\end{split}\]
Args:
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
Returns:
kmean : ndarray
The mean wavenumber of each bin
power : ndarray
The dimensionless power (\(\Delta\))
uncert : ndarray
The standard deviation of the power within each k bin
dragons.munge.munge.pretty_print_dict(d, fmtlen=30)[source]

Pretty print a dictionary, dealing with recursion.

Args:
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.

Args:
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
Kwargs:
filt : string
The name of the filter. Currently only “tophat” (real space) is implemented. More filters will be added over time.
Returns:
smoothed_grid : ndarray
The smoothed grid
dragons.munge.regrid.regrid()

Downgrade the resolution of a 3 dimensional grid.

Args:

old_grid (np.ndarray[float32, ndim=3]) : Grid to be resampled

n_cell (int) : n cells per dimension of new grid

Returns:
New, degraded resolution grid.