diskpy.pdmath package

Module contents

Created on Thu Jul 16 14:25:56 2015

@author: ibackus

diskpy.pdmath.extrap1d(x, y)[source]

Calculates a linear interpolation of x and y and does a linear extrapolation for points outside of x and y. Uses scipy.interpolate.interp1d

diskpy.pdmath.meshinterp(xedges, y, z, kind=’linear’, bounds_error=False, fill_value=0, assume_sorted=True)[source]

Generates a 2D interpolating function for z defined on a non-uniform mesh Handles units

Parameters:

xedges : array

1D array defining the x bin edges, monotonically increasing

y : array

2D array defining y values. shape (nx, ny), where nx is the number of xedges and ny is the number of y-points at each x-bin So, y[i, :] are the monotonically increasing y values at xedges[i]

z : array

2D array of z(x,y). shape (nx, ny) = y.shape

kind : str

(optional) Sets the kind of interpolation to perform [see scipy.interpolate.interp1d]

bounds_error : bool

(optional) Flag to raise error if values outside of y are called [see scipy.interpolate.interp1d]

fill_value : float

(optional) Sets the value to fill with if bounds_error = True [see scipy.interpolate.interp1d]

assume_sorted : bool

[see scipy.interpolate.interp1d]

Returns:

meshspline(x, y): callable interpolation function

Function which can be called on x, y pairs to give the interpolated value of z. Values outside of the range of y are set to fill_value. x values outside the range of xedges are set to the boundary of xedges

diskpy.pdmath.smoothstep(x, degree=5, rescale=False)[source]

Calculates a smooth step function y(x) evaluated at the data points x. x should be a numpy array or float.

y(x) is a polynomial of order ‘degree’ (default is 5). degree must be an odd number between 3 and 25 (inclusive). The higher the order, the sharper the step is.

y(x) is defined by:
y(0) = 0 y(1) = 1 The first (degree - 1)/2 derivatives are 0 at y = 0,1

* ARGUMENTS *

  • x * Points at which to evaluate the smoothstep
  • degree * Degree of the smooth step. Must be odd number between 3 and 25
    default = 5
  • rescale * Rescale x to be between 0 and 1. Default = False. If True,
    x MUST be an array (greater than length 1)

* RETURNS *

diskpy.pdmath.digitize_threshold(x, min_per_bin=0, bins=10)[source]

Digitizes x according to bins, similar to numpy.digitize, but requires that there are at least min_per_bin entries in each bin. Bins that do not have enough entries are combined with adjacent bins until they meet the requirement.

ARGUMENTS

x : array_like
Input array to be binned. Must be 1-dimensional
min_per_bin : int
Minimum number of entries per bin. Default = 0
bins : int or sequence of scalars, optional
[same as for np.histogram] If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.

RETURNS

A tuple containing: ind : array_like

Indices of the bin each element of x falls into, such that: bin_edges[i] <= x[i] < bin_edges[i+1] (See np.digitize, this uses the same convention)
bin_edges: array_like
The edges of the bins
diskpy.pdmath.binned_mean(x, y, bins=10, nbins=None, binedges=None, weights=None, weighted_bins=False, ret_bin_edges=False)[source]

Bins y according to x and takes the average for each bin.

bins can either be an integer (the number of bins to use) or an array of binedges. bins will be overridden by nbins or binedges

Optionally (for compatibility reasons) if binedges is specified, the x-bins are defined by binedges. Otherwise the x-bins are determined by nbins

If weights = None, equal weights are assumed for the average, otherwise weights for each data point should be specified

y_err (error in y) is calculated as the standard deviation in y for each bin, divided by sqrt(N), where N is the number of counts in each bin

IF weighted_bins is True, the bin centers are calculated as a center of mass

NaNs are ignored for the input. Empty bins are returned with nans

RETURNS a tuple of (bin_centers, y_mean, y_err) if ret_bin_edges=False else, Returns (bin_edges, y_mean, y_err)

diskpy.pdmath.kepler_pos(pos, vel, t, Mstar, order=10)[source]

Estimate position at future time t assuming an elliptical keplerian orbit

diskpy.pdmath.resolvedbins(x, y, minbins=200, ftol=None)[source]

Generates a sub-view of x that allows y to be resolved up to ftol. Since f will only be sampled at x, x should be very (overly) high resolution.

Parameters:

x : array or SimArray

initial bin positions to try. should be many more than required

y : array or SimArray

y(x), same shape as x. the function values to resolve

minbins : int

Minimum number of bins to return. IF the ftol is met but there are not enough bins, ftol is decreased

ftol : SimArray, float

Function tolerance. Maximum difference between the interpolated, subsampled y(x) and the full-ly resolved version

Returns:

x2 : array or SimArray

A subview of x that provides the required resolution

diskpy.pdmath.dA(redges, thetaedges)[source]

Calculates the area of bins in cylindrical coordinates as

\[dA = r(\Delta r) (\Delta \theta)\]

on a grid of r, theta values

Parameters:

redges, thetaedges : array like

1D arrays of the binedges in r, theta.

Returns:

dA : array

2D array of dA values over r, theta. dA[i,j] corresponds to r[i] theta[j]

diskpy.pdmath.setupbins(x, bins=10, spacing=’linear’)[source]

Sets up bins for data x (similar to numpy.histogram). If bins is an integer, the min/max are set to include all data and bins are spaced according to spacing (‘linear’ or ‘log’)

Parameters:

x : arraylike

Data to be binned or [xmin, xmax] if x has two elements

bins : int or array-like

Number of bins or binedges to use

spacing : str

‘linear’ or ‘log’. The spacing to use

Returns:

binedges : array

Bin edges

diskpy.pdmath.bin2dsum(x, y, z, xbins=10, ybins=10)[source]

Bins x,y using bin2d and sums z in those bins

Parameters:

x, y, z: array-like

x, y, and z values. Bins are in x-y, z values are summed

xbins, ybins: int or array-like

(see bin2d) Number of bins or bin edges

Returns:

zbinned : array-like

2D array of z-values, summed in the bins. zbinned[i,j] gives the value of z summed in xbin[i], ybin[j]

xedges, yedges : array

1D arrays of binedges in x,y

diskpy.pdmath.bin2d(x, y, xbins=10, ybins=10)[source]

2-dimensional binning of x, y Works as a 2-D extension of numpy.digitize but also automatically sets-up binedges

Parameters:

x, y : array-like

x, y values to bin according to

xbins, ybins : int OR list/array like

Either the number of bins or the binedges to use

Returns:

ind : list of arrays

The x,y bin indices each entry belongs to. ind[0][i] gives the x-bin of the ith entry. ind[1][i] gives the y-bin of the ith entry

xedges, yedges: arrays

Bin edges used

diskpy.pdmath.cdf(x, pdf)[source]

Estimates the cumalitive distribution fuction from the pdf. The pdf does not need to be normalized. Sections where the pdf=0 are dropped so that the cdf is monotonically increasing.

Parameters:

x : 1D array-like

positions the pdf is evaluated at

pdf : 1D array

Probability distribution function. Does not need to be normalized

Returns:

x : array-like

Positions, potentially shorter than input x or pdf

cdf : array-like

Cumulative distribution function. Same shape as output x

diskpy.pdmath.weighted_avg_and_std(values, weights)[source]

Return the weighted average and standard deviation.

Parameters:

values, weights : SimArray-like

Numpy ndarrays with the same shape or SimArrays

Returns:

average, std : SimArray-like

Weighted average and standard deviation.

diskpy.pdmath.interp1dunits(x, y, **kwargs)[source]

A thin wrapper around scipy.interpolate.interp1d that respects pynbody units.