Fixed more bugs in distributions.py
parent
6b88f2d4cc
commit
0bfe623f5c
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,402 @@
|
|||||||
|
from __future__ import division, print_function, absolute_import
|
||||||
|
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from scipy.lib.six import callable
|
||||||
|
|
||||||
|
|
||||||
|
def binned_statistic(x, values, statistic='mean',
|
||||||
|
bins=10, range=None):
|
||||||
|
"""
|
||||||
|
Compute a binned statistic for a set of data.
|
||||||
|
|
||||||
|
This is a generalization of a histogram function. A histogram divides
|
||||||
|
the space into bins, and returns the count of the number of points in
|
||||||
|
each bin. This function allows the computation of the sum, mean, median,
|
||||||
|
or other statistic of the values within each bin.
|
||||||
|
|
||||||
|
.. versionadded:: 0.11.0
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
x : array_like
|
||||||
|
A sequence of values to be binned.
|
||||||
|
values : array_like
|
||||||
|
The values on which the statistic will be computed. This must be
|
||||||
|
the same shape as `x`.
|
||||||
|
statistic : string or callable, optional
|
||||||
|
The statistic to compute (default is 'mean').
|
||||||
|
The following statistics are available:
|
||||||
|
|
||||||
|
* 'mean' : compute the mean of values for points within each bin.
|
||||||
|
Empty bins will be represented by NaN.
|
||||||
|
* 'median' : compute the median of values for points within each
|
||||||
|
bin. Empty bins will be represented by NaN.
|
||||||
|
* 'count' : compute the count of points within each bin. This is
|
||||||
|
identical to an unweighted histogram. `values` array is not
|
||||||
|
referenced.
|
||||||
|
* 'sum' : compute the sum of values for points within each bin.
|
||||||
|
This is identical to a weighted histogram.
|
||||||
|
* function : a user-defined function which takes a 1D array of
|
||||||
|
values, and outputs a single numerical statistic. This function
|
||||||
|
will be called on the values in each bin. Empty bins will be
|
||||||
|
represented by function([]), or NaN if this returns an error.
|
||||||
|
|
||||||
|
bins : int or sequence of scalars, optional
|
||||||
|
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.
|
||||||
|
range : (float, float) or [(float, float)], optional
|
||||||
|
The lower and upper range of the bins. If not provided, range
|
||||||
|
is simply ``(x.min(), x.max())``. Values outside the range are
|
||||||
|
ignored.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
statistic : array
|
||||||
|
The values of the selected statistic in each bin.
|
||||||
|
bin_edges : array of dtype float
|
||||||
|
Return the bin edges ``(length(statistic)+1)``.
|
||||||
|
binnumber : 1-D ndarray of ints
|
||||||
|
This assigns to each observation an integer that represents the bin
|
||||||
|
in which this observation falls. Array has the same length as values.
|
||||||
|
|
||||||
|
See Also
|
||||||
|
--------
|
||||||
|
numpy.histogram, binned_statistic_2d, binned_statistic_dd
|
||||||
|
|
||||||
|
Notes
|
||||||
|
-----
|
||||||
|
All but the last (righthand-most) bin is half-open. In other words, if
|
||||||
|
`bins` is::
|
||||||
|
|
||||||
|
[1, 2, 3, 4]
|
||||||
|
|
||||||
|
then the first bin is ``[1, 2)`` (including 1, but excluding 2) and the
|
||||||
|
second ``[2, 3)``. The last bin, however, is ``[3, 4]``, which *includes*
|
||||||
|
4.
|
||||||
|
|
||||||
|
Examples
|
||||||
|
--------
|
||||||
|
>>> stats.binned_statistic([1, 2, 1, 2, 4], np.arange(5), statistic='mean',
|
||||||
|
... bins=3)
|
||||||
|
(array([ 1., 2., 4.]), array([ 1., 2., 3., 4.]), array([1, 2, 1, 2, 3]))
|
||||||
|
|
||||||
|
>>> stats.binned_statistic([1, 2, 1, 2, 4], np.arange(5), statistic='mean', bins=3)
|
||||||
|
(array([ 1., 2., 4.]), array([ 1., 2., 3., 4.]), array([1, 2, 1, 2, 3]))
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
N = len(bins)
|
||||||
|
except TypeError:
|
||||||
|
N = 1
|
||||||
|
|
||||||
|
if N != 1:
|
||||||
|
bins = [np.asarray(bins, float)]
|
||||||
|
|
||||||
|
if range is not None:
|
||||||
|
if len(range) == 2:
|
||||||
|
range = [range]
|
||||||
|
|
||||||
|
medians, edges, xy = binned_statistic_dd([x], values, statistic,
|
||||||
|
bins, range)
|
||||||
|
|
||||||
|
return medians, edges[0], xy
|
||||||
|
|
||||||
|
|
||||||
|
def binned_statistic_2d(x, y, values, statistic='mean',
|
||||||
|
bins=10, range=None):
|
||||||
|
"""
|
||||||
|
Compute a bidimensional binned statistic for a set of data.
|
||||||
|
|
||||||
|
This is a generalization of a histogram2d function. A histogram divides
|
||||||
|
the space into bins, and returns the count of the number of points in
|
||||||
|
each bin. This function allows the computation of the sum, mean, median,
|
||||||
|
or other statistic of the values within each bin.
|
||||||
|
|
||||||
|
.. versionadded:: 0.11.0
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
x : (N,) array_like
|
||||||
|
A sequence of values to be binned along the first dimension.
|
||||||
|
y : (M,) array_like
|
||||||
|
A sequence of values to be binned along the second dimension.
|
||||||
|
values : (N,) array_like
|
||||||
|
The values on which the statistic will be computed. This must be
|
||||||
|
the same shape as `x`.
|
||||||
|
statistic : string or callable, optional
|
||||||
|
The statistic to compute (default is 'mean').
|
||||||
|
The following statistics are available:
|
||||||
|
|
||||||
|
* 'mean' : compute the mean of values for points within each bin.
|
||||||
|
Empty bins will be represented by NaN.
|
||||||
|
* 'median' : compute the median of values for points within each
|
||||||
|
bin. Empty bins will be represented by NaN.
|
||||||
|
* 'count' : compute the count of points within each bin. This is
|
||||||
|
identical to an unweighted histogram. `values` array is not
|
||||||
|
referenced.
|
||||||
|
* 'sum' : compute the sum of values for points within each bin.
|
||||||
|
This is identical to a weighted histogram.
|
||||||
|
* function : a user-defined function which takes a 1D array of
|
||||||
|
values, and outputs a single numerical statistic. This function
|
||||||
|
will be called on the values in each bin. Empty bins will be
|
||||||
|
represented by function([]), or NaN if this returns an error.
|
||||||
|
|
||||||
|
bins : int or [int, int] or array-like or [array, array], optional
|
||||||
|
The bin specification:
|
||||||
|
|
||||||
|
* the number of bins for the two dimensions (nx=ny=bins),
|
||||||
|
* the number of bins in each dimension (nx, ny = bins),
|
||||||
|
* the bin edges for the two dimensions (x_edges = y_edges = bins),
|
||||||
|
* the bin edges in each dimension (x_edges, y_edges = bins).
|
||||||
|
|
||||||
|
range : (2,2) array_like, optional
|
||||||
|
The leftmost and rightmost edges of the bins along each dimension
|
||||||
|
(if not specified explicitly in the `bins` parameters):
|
||||||
|
[[xmin, xmax], [ymin, ymax]]. All values outside of this range will be
|
||||||
|
considered outliers and not tallied in the histogram.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
statistic : (nx, ny) ndarray
|
||||||
|
The values of the selected statistic in each two-dimensional bin
|
||||||
|
xedges : (nx + 1) ndarray
|
||||||
|
The bin edges along the first dimension.
|
||||||
|
yedges : (ny + 1) ndarray
|
||||||
|
The bin edges along the second dimension.
|
||||||
|
binnumber : 1-D ndarray of ints
|
||||||
|
This assigns to each observation an integer that represents the bin
|
||||||
|
in which this observation falls. Array has the same length as `values`.
|
||||||
|
|
||||||
|
See Also
|
||||||
|
--------
|
||||||
|
numpy.histogram2d, binned_statistic, binned_statistic_dd
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# This code is based on np.histogram2d
|
||||||
|
try:
|
||||||
|
N = len(bins)
|
||||||
|
except TypeError:
|
||||||
|
N = 1
|
||||||
|
|
||||||
|
if N != 1 and N != 2:
|
||||||
|
xedges = yedges = np.asarray(bins, float)
|
||||||
|
bins = [xedges, yedges]
|
||||||
|
|
||||||
|
medians, edges, xy = binned_statistic_dd([x, y], values, statistic,
|
||||||
|
bins, range)
|
||||||
|
|
||||||
|
return medians, edges[0], edges[1], xy
|
||||||
|
|
||||||
|
|
||||||
|
def binned_statistic_dd(sample, values, statistic='mean',
|
||||||
|
bins=10, range=None):
|
||||||
|
"""
|
||||||
|
Compute a multidimensional binned statistic for a set of data.
|
||||||
|
|
||||||
|
This is a generalization of a histogramdd function. A histogram divides
|
||||||
|
the space into bins, and returns the count of the number of points in
|
||||||
|
each bin. This function allows the computation of the sum, mean, median,
|
||||||
|
or other statistic of the values within each bin.
|
||||||
|
|
||||||
|
.. versionadded:: 0.11.0
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
sample : array_like
|
||||||
|
Data to histogram passed as a sequence of D arrays of length N, or
|
||||||
|
as an (N,D) array.
|
||||||
|
values : array_like
|
||||||
|
The values on which the statistic will be computed. This must be
|
||||||
|
the same shape as x.
|
||||||
|
statistic : string or callable, optional
|
||||||
|
The statistic to compute (default is 'mean').
|
||||||
|
The following statistics are available:
|
||||||
|
|
||||||
|
* 'mean' : compute the mean of values for points within each bin.
|
||||||
|
Empty bins will be represented by NaN.
|
||||||
|
* 'median' : compute the median of values for points within each
|
||||||
|
bin. Empty bins will be represented by NaN.
|
||||||
|
* 'count' : compute the count of points within each bin. This is
|
||||||
|
identical to an unweighted histogram. `values` array is not
|
||||||
|
referenced.
|
||||||
|
* 'sum' : compute the sum of values for points within each bin.
|
||||||
|
This is identical to a weighted histogram.
|
||||||
|
* function : a user-defined function which takes a 1D array of
|
||||||
|
values, and outputs a single numerical statistic. This function
|
||||||
|
will be called on the values in each bin. Empty bins will be
|
||||||
|
represented by function([]), or NaN if this returns an error.
|
||||||
|
|
||||||
|
bins : sequence or int, optional
|
||||||
|
The bin specification:
|
||||||
|
|
||||||
|
* A sequence of arrays describing the bin edges along each dimension.
|
||||||
|
* The number of bins for each dimension (nx, ny, ... =bins)
|
||||||
|
* The number of bins for all dimensions (nx=ny=...=bins).
|
||||||
|
|
||||||
|
range : sequence, optional
|
||||||
|
A sequence of lower and upper bin edges to be used if the edges are
|
||||||
|
not given explicitely in `bins`. Defaults to the minimum and maximum
|
||||||
|
values along each dimension.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
statistic : ndarray, shape(nx1, nx2, nx3,...)
|
||||||
|
The values of the selected statistic in each two-dimensional bin
|
||||||
|
edges : list of ndarrays
|
||||||
|
A list of D arrays describing the (nxi + 1) bin edges for each
|
||||||
|
dimension
|
||||||
|
binnumber : 1-D ndarray of ints
|
||||||
|
This assigns to each observation an integer that represents the bin
|
||||||
|
in which this observation falls. Array has the same length as values.
|
||||||
|
|
||||||
|
See Also
|
||||||
|
--------
|
||||||
|
np.histogramdd, binned_statistic, binned_statistic_2d
|
||||||
|
|
||||||
|
"""
|
||||||
|
if type(statistic) == str:
|
||||||
|
if statistic not in ['mean', 'median', 'count', 'sum', 'std']:
|
||||||
|
raise ValueError('unrecognized statistic "%s"' % statistic)
|
||||||
|
elif callable(statistic):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise ValueError("statistic not understood")
|
||||||
|
|
||||||
|
# This code is based on np.histogramdd
|
||||||
|
try:
|
||||||
|
# Sample is an ND-array.
|
||||||
|
N, D = sample.shape
|
||||||
|
except (AttributeError, ValueError):
|
||||||
|
# Sample is a sequence of 1D arrays.
|
||||||
|
sample = np.atleast_2d(sample).T
|
||||||
|
N, D = sample.shape
|
||||||
|
|
||||||
|
nbin = np.empty(D, int)
|
||||||
|
edges = D * [None]
|
||||||
|
dedges = D * [None]
|
||||||
|
|
||||||
|
try:
|
||||||
|
M = len(bins)
|
||||||
|
if M != D:
|
||||||
|
raise AttributeError('The dimension of bins must be equal '
|
||||||
|
'to the dimension of the sample x.')
|
||||||
|
except TypeError:
|
||||||
|
bins = D * [bins]
|
||||||
|
|
||||||
|
# Select range for each dimension
|
||||||
|
# Used only if number of bins is given.
|
||||||
|
if range is None:
|
||||||
|
smin = np.atleast_1d(np.array(sample.min(0), float))
|
||||||
|
smax = np.atleast_1d(np.array(sample.max(0), float))
|
||||||
|
else:
|
||||||
|
smin = np.zeros(D)
|
||||||
|
smax = np.zeros(D)
|
||||||
|
for i in np.arange(D):
|
||||||
|
smin[i], smax[i] = range[i]
|
||||||
|
|
||||||
|
# Make sure the bins have a finite width.
|
||||||
|
for i in np.arange(len(smin)):
|
||||||
|
if smin[i] == smax[i]:
|
||||||
|
smin[i] = smin[i] - .5
|
||||||
|
smax[i] = smax[i] + .5
|
||||||
|
|
||||||
|
# Create edge arrays
|
||||||
|
for i in np.arange(D):
|
||||||
|
if np.isscalar(bins[i]):
|
||||||
|
nbin[i] = bins[i] + 2 # +2 for outlier bins
|
||||||
|
edges[i] = np.linspace(smin[i], smax[i], nbin[i] - 1)
|
||||||
|
else:
|
||||||
|
edges[i] = np.asarray(bins[i], float)
|
||||||
|
nbin[i] = len(edges[i]) + 1 # +1 for outlier bins
|
||||||
|
dedges[i] = np.diff(edges[i])
|
||||||
|
|
||||||
|
nbin = np.asarray(nbin)
|
||||||
|
|
||||||
|
# Compute the bin number each sample falls into.
|
||||||
|
Ncount = {}
|
||||||
|
for i in np.arange(D):
|
||||||
|
Ncount[i] = np.digitize(sample[:, i], edges[i])
|
||||||
|
|
||||||
|
# Using digitize, values that fall on an edge are put in the right bin.
|
||||||
|
# For the rightmost bin, we want values equal to the right
|
||||||
|
# edge to be counted in the last bin, and not as an outlier.
|
||||||
|
for i in np.arange(D):
|
||||||
|
# Rounding precision
|
||||||
|
decimal = int(-np.log10(dedges[i].min())) + 6
|
||||||
|
# Find which points are on the rightmost edge.
|
||||||
|
on_edge = np.where(np.around(sample[:, i], decimal)
|
||||||
|
== np.around(edges[i][-1], decimal))[0]
|
||||||
|
# Shift these points one bin to the left.
|
||||||
|
Ncount[i][on_edge] -= 1
|
||||||
|
|
||||||
|
# Compute the sample indices in the flattened statistic matrix.
|
||||||
|
ni = nbin.argsort()
|
||||||
|
xy = np.zeros(N, int)
|
||||||
|
for i in np.arange(0, D - 1):
|
||||||
|
xy += Ncount[ni[i]] * nbin[ni[i + 1:]].prod()
|
||||||
|
xy += Ncount[ni[-1]]
|
||||||
|
|
||||||
|
result = np.empty(nbin.prod(), float)
|
||||||
|
|
||||||
|
if statistic == 'mean':
|
||||||
|
result.fill(np.nan)
|
||||||
|
flatcount = np.bincount(xy, None)
|
||||||
|
flatsum = np.bincount(xy, values)
|
||||||
|
a = flatcount.nonzero()
|
||||||
|
result[a] = flatsum[a] / flatcount[a]
|
||||||
|
elif statistic == 'std':
|
||||||
|
result.fill(0)
|
||||||
|
flatcount = np.bincount(xy, None)
|
||||||
|
flatsum = np.bincount(xy, values)
|
||||||
|
flatsum2 = np.bincount(xy, values ** 2)
|
||||||
|
a = flatcount.nonzero()
|
||||||
|
result[a] = np.sqrt(flatsum2[a] / flatcount[a]
|
||||||
|
- (flatsum[a] / flatcount[a]) ** 2)
|
||||||
|
elif statistic == 'count':
|
||||||
|
result.fill(0)
|
||||||
|
flatcount = np.bincount(xy, None)
|
||||||
|
a = np.arange(len(flatcount))
|
||||||
|
result[a] = flatcount
|
||||||
|
elif statistic == 'sum':
|
||||||
|
result.fill(0)
|
||||||
|
flatsum = np.bincount(xy, values)
|
||||||
|
a = np.arange(len(flatsum))
|
||||||
|
result[a] = flatsum
|
||||||
|
elif statistic == 'median':
|
||||||
|
result.fill(np.nan)
|
||||||
|
for i in np.unique(xy):
|
||||||
|
result[i] = np.median(values[xy == i])
|
||||||
|
elif callable(statistic):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
# Numpy generates a warnings for mean/std/... with empty list
|
||||||
|
warnings.filterwarnings('ignore', category=RuntimeWarning)
|
||||||
|
old = np.seterr(invalid='ignore')
|
||||||
|
try:
|
||||||
|
null = statistic([])
|
||||||
|
except:
|
||||||
|
null = np.nan
|
||||||
|
np.seterr(**old)
|
||||||
|
result.fill(null)
|
||||||
|
for i in np.unique(xy):
|
||||||
|
result[i] = statistic(values[xy == i])
|
||||||
|
|
||||||
|
# Shape into a proper matrix
|
||||||
|
result = result.reshape(np.sort(nbin))
|
||||||
|
for i in np.arange(nbin.size):
|
||||||
|
j = ni.argsort()[i]
|
||||||
|
result = result.swapaxes(i, j)
|
||||||
|
ni[i], ni[j] = ni[j], ni[i]
|
||||||
|
|
||||||
|
# Remove outliers (indices 0 and -1 for each dimension).
|
||||||
|
core = D * [slice(1, -1)]
|
||||||
|
result = result[core]
|
||||||
|
|
||||||
|
if (result.shape != nbin - 2).any():
|
||||||
|
raise RuntimeError('Internal Shape Error')
|
||||||
|
|
||||||
|
return result, edges, xy
|
@ -0,0 +1,24 @@
|
|||||||
|
"""
|
||||||
|
Statistics-related constants.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import division, print_function, absolute_import
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
# The smallest representable positive number such that 1.0 + _EPS != 1.0.
|
||||||
|
_EPS = np.finfo(float).eps
|
||||||
|
|
||||||
|
# The largest [in magnitude] usable floating value.
|
||||||
|
_XMAX = np.finfo(float).machar.xmax
|
||||||
|
|
||||||
|
# The smallest [in magnitude] usable floating value.
|
||||||
|
_XMIN = np.finfo(float).machar.xmin
|
||||||
|
|
||||||
|
# -special.psi(1)
|
||||||
|
_EULER = 0.577215664901532860606512090082402431042
|
||||||
|
|
||||||
|
# special.zeta(3, 1) Apery's constant
|
||||||
|
_ZETA3 = 1.202056903159594285399738161511449990765
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,15 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Created on Tue Dec 06 16:02:47 2011
|
||||||
|
|
||||||
|
@author: pab
|
||||||
|
"""
|
||||||
|
import numpy as np
|
||||||
|
import wafo.kdetools as wk
|
||||||
|
n = 100
|
||||||
|
x = np.sort(5*np.random.rand(1,n)-2.5, axis=-1).ravel()
|
||||||
|
y = (np.cos(x)>2*np.random.rand(n, 1)-1).ravel()
|
||||||
|
|
||||||
|
kreg = wk.KRegression(x,y)
|
||||||
|
f = kreg(output='plotobj', title='Kernel regression', plotflag=1)
|
||||||
|
f.plot()
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,154 +1,156 @@
|
|||||||
from __future__ import division, print_function, absolute_import
|
from __future__ import division, print_function, absolute_import
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import numpy.testing as npt
|
import numpy.testing as npt
|
||||||
|
|
||||||
#from scipy.lib._version import NumpyVersion
|
#from scipy.lib._version import NumpyVersion
|
||||||
from scipy import stats
|
from scipy import stats
|
||||||
|
|
||||||
|
|
||||||
#NUMPY_BELOW_1_7 = NumpyVersion(np.__version__) < '1.7.0'
|
#NUMPY_BELOW_1_7 = NumpyVersion(np.__version__) < '1.7.0'
|
||||||
NUMPY_BELOW_1_7 =np.__version__ < '1.7.0'
|
NUMPY_BELOW_1_7 = np.__version__ < '1.7.0'
|
||||||
|
|
||||||
|
|
||||||
def check_normalization(distfn, args, distname):
|
def check_normalization(distfn, args, distname):
|
||||||
norm_moment = distfn.moment(0, *args)
|
norm_moment = distfn.moment(0, *args)
|
||||||
npt.assert_allclose(norm_moment, 1.0)
|
npt.assert_allclose(norm_moment, 1.0)
|
||||||
|
|
||||||
# this is a temporary plug: either ncf or expect is problematic;
|
# this is a temporary plug: either ncf or expect is problematic;
|
||||||
# best be marked as a knownfail, but I've no clue how to do it.
|
# best be marked as a knownfail, but I've no clue how to do it.
|
||||||
if distname == "ncf":
|
if distname == "ncf":
|
||||||
atol, rtol = 1e-5, 0
|
atol, rtol = 1e-5, 0
|
||||||
else:
|
else:
|
||||||
atol, rtol = 1e-7, 1e-7
|
atol, rtol = 1e-7, 1e-7
|
||||||
|
|
||||||
normalization_expect = distfn.expect(lambda x: 1, args=args)
|
normalization_expect = distfn.expect(lambda x: 1, args=args)
|
||||||
npt.assert_allclose(normalization_expect, 1.0, atol=atol, rtol=rtol,
|
npt.assert_allclose(normalization_expect, 1.0, atol=atol, rtol=rtol,
|
||||||
err_msg=distname, verbose=True)
|
err_msg=distname, verbose=True)
|
||||||
|
|
||||||
normalization_cdf = distfn.cdf(distfn.b, *args)
|
normalization_cdf = distfn.cdf(distfn.b, *args)
|
||||||
npt.assert_allclose(normalization_cdf, 1.0)
|
npt.assert_allclose(normalization_cdf, 1.0)
|
||||||
|
|
||||||
|
|
||||||
def check_moment(distfn, arg, m, v, msg):
|
def check_moment(distfn, arg, m, v, msg):
|
||||||
m1 = distfn.moment(1, *arg)
|
m1 = distfn.moment(1, *arg)
|
||||||
m2 = distfn.moment(2, *arg)
|
m2 = distfn.moment(2, *arg)
|
||||||
if not np.isinf(m):
|
if not np.isinf(m):
|
||||||
npt.assert_almost_equal(m1, m, decimal=10, err_msg=msg +
|
npt.assert_almost_equal(m1, m, decimal=10, err_msg=msg +
|
||||||
' - 1st moment')
|
' - 1st moment')
|
||||||
else: # or np.isnan(m1),
|
else: # or np.isnan(m1),
|
||||||
npt.assert_(np.isinf(m1),
|
npt.assert_(np.isinf(m1),
|
||||||
msg + ' - 1st moment -infinite, m1=%s' % str(m1))
|
msg + ' - 1st moment -infinite, m1=%s' % str(m1))
|
||||||
|
|
||||||
if not np.isinf(v):
|
if not np.isinf(v):
|
||||||
npt.assert_almost_equal(m2 - m1 * m1, v, decimal=10, err_msg=msg +
|
npt.assert_almost_equal(m2 - m1 * m1, v, decimal=10, err_msg=msg +
|
||||||
' - 2ndt moment')
|
' - 2ndt moment')
|
||||||
else: # or np.isnan(m2),
|
else: # or np.isnan(m2),
|
||||||
npt.assert_(np.isinf(m2),
|
npt.assert_(np.isinf(m2),
|
||||||
msg + ' - 2nd moment -infinite, m2=%s' % str(m2))
|
msg + ' - 2nd moment -infinite, m2=%s' % str(m2))
|
||||||
|
|
||||||
|
|
||||||
def check_mean_expect(distfn, arg, m, msg):
|
def check_mean_expect(distfn, arg, m, msg):
|
||||||
if np.isfinite(m):
|
if np.isfinite(m):
|
||||||
m1 = distfn.expect(lambda x: x, arg)
|
m1 = distfn.expect(lambda x: x, arg)
|
||||||
npt.assert_almost_equal(m1, m, decimal=5, err_msg=msg +
|
npt.assert_almost_equal(m1, m, decimal=5, err_msg=msg +
|
||||||
' - 1st moment (expect)')
|
' - 1st moment (expect)')
|
||||||
|
|
||||||
|
|
||||||
def check_var_expect(distfn, arg, m, v, msg):
|
def check_var_expect(distfn, arg, m, v, msg):
|
||||||
if np.isfinite(v):
|
if np.isfinite(v):
|
||||||
m2 = distfn.expect(lambda x: x*x, arg)
|
m2 = distfn.expect(lambda x: x * x, arg)
|
||||||
npt.assert_almost_equal(m2, v + m*m, decimal=5, err_msg=msg +
|
npt.assert_almost_equal(m2, v + m * m, decimal=5, err_msg=msg +
|
||||||
' - 2st moment (expect)')
|
' - 2st moment (expect)')
|
||||||
|
|
||||||
|
|
||||||
def check_skew_expect(distfn, arg, m, v, s, msg):
|
def check_skew_expect(distfn, arg, m, v, s, msg):
|
||||||
if np.isfinite(s):
|
if np.isfinite(s):
|
||||||
m3e = distfn.expect(lambda x: np.power(x-m, 3), arg)
|
m3e = distfn.expect(lambda x: np.power(x - m, 3), arg)
|
||||||
npt.assert_almost_equal(m3e, s * np.power(v, 1.5),
|
npt.assert_almost_equal(m3e, s * np.power(v, 1.5),
|
||||||
decimal=5, err_msg=msg + ' - skew')
|
decimal=5, err_msg=msg + ' - skew')
|
||||||
else:
|
else:
|
||||||
npt.assert_(np.isnan(s))
|
npt.assert_(np.isnan(s))
|
||||||
|
|
||||||
|
|
||||||
def check_kurt_expect(distfn, arg, m, v, k, msg):
|
def check_kurt_expect(distfn, arg, m, v, k, msg):
|
||||||
if np.isfinite(k):
|
if np.isfinite(k):
|
||||||
m4e = distfn.expect(lambda x: np.power(x-m, 4), arg)
|
m4e = distfn.expect(lambda x: np.power(x - m, 4), arg)
|
||||||
npt.assert_allclose(m4e, (k + 3.) * np.power(v, 2), atol=1e-5, rtol=1e-5,
|
npt.assert_allclose(
|
||||||
err_msg=msg + ' - kurtosis')
|
m4e, (k + 3.) * np.power(v, 2), atol=1e-5, rtol=1e-5,
|
||||||
else:
|
err_msg=msg + ' - kurtosis')
|
||||||
npt.assert_(np.isnan(k))
|
else:
|
||||||
|
npt.assert_(np.isnan(k))
|
||||||
|
|
||||||
def check_entropy(distfn, arg, msg):
|
|
||||||
ent = distfn.entropy(*arg)
|
def check_entropy(distfn, arg, msg):
|
||||||
npt.assert_(not np.isnan(ent), msg + 'test Entropy is nan')
|
ent = distfn.entropy(*arg)
|
||||||
|
npt.assert_(not np.isnan(ent), msg + 'test Entropy is nan')
|
||||||
|
|
||||||
def check_private_entropy(distfn, args, superclass):
|
|
||||||
# compare a generic _entropy with the distribution-specific implementation
|
def check_private_entropy(distfn, args, superclass):
|
||||||
npt.assert_allclose(distfn._entropy(*args),
|
# compare a generic _entropy with the distribution-specific implementation
|
||||||
superclass._entropy(distfn, *args))
|
npt.assert_allclose(distfn._entropy(*args),
|
||||||
|
superclass._entropy(distfn, *args))
|
||||||
|
|
||||||
def check_edge_support(distfn, args):
|
|
||||||
# Make sure the x=self.a and self.b are handled correctly.
|
def check_edge_support(distfn, args):
|
||||||
x = [distfn.a, distfn.b]
|
# Make sure the x=self.a and self.b are handled correctly.
|
||||||
if isinstance(distfn, stats.rv_continuous):
|
x = [distfn.a, distfn.b]
|
||||||
npt.assert_equal(distfn.cdf(x, *args), [0.0, 1.0])
|
if isinstance(distfn, stats.rv_continuous):
|
||||||
npt.assert_equal(distfn.logcdf(x, *args), [-np.inf, 0.0])
|
npt.assert_equal(distfn.cdf(x, *args), [0.0, 1.0])
|
||||||
|
npt.assert_equal(distfn.logcdf(x, *args), [-np.inf, 0.0])
|
||||||
npt.assert_equal(distfn.sf(x, *args), [1.0, 0.0])
|
|
||||||
npt.assert_equal(distfn.logsf(x, *args), [0.0, -np.inf])
|
npt.assert_equal(distfn.sf(x, *args), [1.0, 0.0])
|
||||||
|
npt.assert_equal(distfn.logsf(x, *args), [0.0, -np.inf])
|
||||||
if isinstance(distfn, stats.rv_discrete):
|
|
||||||
x = [distfn.a - 1, distfn.b]
|
if isinstance(distfn, stats.rv_discrete):
|
||||||
npt.assert_equal(distfn.ppf([0.0, 1.0], *args), x)
|
x = [distfn.a - 1, distfn.b]
|
||||||
npt.assert_equal(distfn.isf([0.0, 1.0], *args), x[::-1])
|
npt.assert_equal(distfn.ppf([0.0, 1.0], *args), x)
|
||||||
|
npt.assert_equal(distfn.isf([0.0, 1.0], *args), x[::-1])
|
||||||
# out-of-bounds for isf & ppf
|
|
||||||
npt.assert_(np.isnan(distfn.isf([-1, 2], *args)).all())
|
# out-of-bounds for isf & ppf
|
||||||
npt.assert_(np.isnan(distfn.ppf([-1, 2], *args)).all())
|
npt.assert_(np.isnan(distfn.isf([-1, 2], *args)).all())
|
||||||
|
npt.assert_(np.isnan(distfn.ppf([-1, 2], *args)).all())
|
||||||
|
|
||||||
def check_named_args(distfn, x, shape_args, defaults, meths):
|
|
||||||
## Check calling w/ named arguments.
|
def check_named_args(distfn, x, shape_args, defaults, meths):
|
||||||
|
# Check calling w/ named arguments.
|
||||||
# check consistency of shapes, numargs and _parse signature
|
|
||||||
signature = inspect.getargspec(distfn._parse_args)
|
# check consistency of shapes, numargs and _parse signature
|
||||||
npt.assert_(signature.varargs is None)
|
signature = inspect.getargspec(distfn._parse_args)
|
||||||
npt.assert_(signature.keywords is None)
|
npt.assert_(signature.varargs is None)
|
||||||
npt.assert_(signature.defaults == defaults)
|
npt.assert_(signature.keywords is None)
|
||||||
|
npt.assert_(signature.defaults == defaults)
|
||||||
shape_argnames = signature.args[1:-len(defaults)] # self, a, b, loc=0, scale=1
|
|
||||||
if distfn.shapes:
|
# self, a, b, loc=0, scale=1
|
||||||
shapes_ = distfn.shapes.replace(',', ' ').split()
|
shape_argnames = signature.args[1:-len(defaults)]
|
||||||
else:
|
if distfn.shapes:
|
||||||
shapes_ = ''
|
shapes_ = distfn.shapes.replace(',', ' ').split()
|
||||||
npt.assert_(len(shapes_) == distfn.numargs)
|
else:
|
||||||
npt.assert_(len(shapes_) == len(shape_argnames))
|
shapes_ = ''
|
||||||
|
npt.assert_(len(shapes_) == distfn.numargs)
|
||||||
# check calling w/ named arguments
|
npt.assert_(len(shapes_) == len(shape_argnames))
|
||||||
shape_args = list(shape_args)
|
|
||||||
|
# check calling w/ named arguments
|
||||||
vals = [meth(x, *shape_args) for meth in meths]
|
shape_args = list(shape_args)
|
||||||
npt.assert_(np.all(np.isfinite(vals)))
|
|
||||||
|
vals = [meth(x, *shape_args) for meth in meths]
|
||||||
names, a, k = shape_argnames[:], shape_args[:], {}
|
npt.assert_(np.all(np.isfinite(vals)))
|
||||||
while names:
|
|
||||||
k.update({names.pop(): a.pop()})
|
names, a, k = shape_argnames[:], shape_args[:], {}
|
||||||
v = [meth(x, *a, **k) for meth in meths]
|
while names:
|
||||||
npt.assert_array_equal(vals, v)
|
k.update({names.pop(): a.pop()})
|
||||||
if not 'n' in k.keys():
|
v = [meth(x, *a, **k) for meth in meths]
|
||||||
# `n` is first parameter of moment(), so can't be used as named arg
|
npt.assert_array_equal(vals, v)
|
||||||
with warnings.catch_warnings():
|
if not 'n' in k.keys():
|
||||||
warnings.simplefilter("ignore", UserWarning)
|
# `n` is first parameter of moment(), so can't be used as named arg
|
||||||
npt.assert_equal(distfn.moment(1, *a, **k),
|
with warnings.catch_warnings():
|
||||||
distfn.moment(1, *shape_args))
|
warnings.simplefilter("ignore", UserWarning)
|
||||||
|
npt.assert_equal(distfn.moment(1, *a, **k),
|
||||||
# unknown arguments should not go through:
|
distfn.moment(1, *shape_args))
|
||||||
k.update({'kaboom': 42})
|
|
||||||
npt.assert_raises(TypeError, distfn.cdf, x, **k)
|
# unknown arguments should not go through:
|
||||||
|
k.update({'kaboom': 42})
|
||||||
|
npt.assert_raises(TypeError, distfn.cdf, x, **k)
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,523 +1,470 @@
|
|||||||
import numpy as np # @UnusedImport
|
|
||||||
#@UnusedImport
|
from numpy.testing import (run_module_suite, assert_equal, assert_almost_equal,
|
||||||
from numpy import cos, exp, linspace, pi, sin, diff, arange, ones
|
assert_array_equal, assert_array_almost_equal)
|
||||||
from numpy.random import randn # @UnusedImport
|
|
||||||
from wafo.data import sea # @UnusedImport
|
import numpy as np
|
||||||
from wafo.misc import (JITImport, Bunch, detrendma, DotDict, findcross, ecross, findextrema, # @UnusedImport
|
from numpy import array, cos, exp, linspace, pi, sin, diff, arange, ones
|
||||||
#@UnusedImport
|
from wafo.data import sea
|
||||||
findrfc, rfcfilter, findtp, findtc, findoutliers,
|
from wafo.misc import (JITImport, Bunch, detrendma, DotDict, findcross, ecross,
|
||||||
common_shape, argsreduce, stirlerr, getshipchar, betaloge,
|
findextrema, findrfc, rfcfilter, findtp, findtc,
|
||||||
#@UnusedImport
|
findoutliers, common_shape, argsreduce, stirlerr,
|
||||||
#@UnusedImport
|
getshipchar, betaloge, hygfz,
|
||||||
gravity, nextpow2, discretize, polar2cart,
|
gravity, nextpow2, discretize, polar2cart,
|
||||||
cart2polar, meshgrid, tranproc) # @UnusedImport
|
cart2polar, tranproc)
|
||||||
|
|
||||||
|
|
||||||
def test_JITImport():
|
def test_JITImport():
|
||||||
'''
|
np = JITImport('numpy')
|
||||||
>>> np = JITImport('numpy')
|
assert_equal(1.0, np.exp(0))
|
||||||
>>> np.exp(0)==1.0
|
|
||||||
True
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_bunch():
|
def test_bunch():
|
||||||
'''
|
d = Bunch(test1=1, test2=3)
|
||||||
>>> d = Bunch(test1=1,test2=3)
|
assert_equal(1, d.test1)
|
||||||
>>> d.test1; d.test2
|
assert_equal(3, d.test2)
|
||||||
1
|
|
||||||
3
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_dotdict():
|
def test_dotdict():
|
||||||
'''
|
d = DotDict(test1=1, test2=3)
|
||||||
>>> d = DotDict(test1=1,test2=3)
|
assert_equal(1, d.test1)
|
||||||
>>> d.test1; d.test2
|
assert_equal(3, d.test2)
|
||||||
1
|
|
||||||
3
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_detrendma():
|
def test_detrendma():
|
||||||
'''
|
x = linspace(0, 1, 200)
|
||||||
>>> x = linspace(0,1,200)
|
y = exp(x) + 0.1 * cos(20 * 2 * pi * x)
|
||||||
>>> y = exp(x)+0.1*cos(20*2*pi*x)
|
y0 = detrendma(y, 20)
|
||||||
>>> y0 = detrendma(y,20); tr = y-y0
|
tr = y - y0
|
||||||
>>> y0,tr
|
assert_array_almost_equal(
|
||||||
(array([ -1.05815186e-02, -2.48280355e-02, -7.01800760e-02,
|
y0,
|
||||||
-1.27193089e-01, -1.71915213e-01, -1.85125121e-01,
|
array(
|
||||||
-1.59745361e-01, -1.03571981e-01, -3.62676515e-02,
|
[-1.05815186e-02, -2.48280355e-02, -7.01800760e-02,
|
||||||
1.82219951e-02, 4.09039083e-02, 2.50630186e-02,
|
-1.27193089e-01, -1.71915213e-01, -1.85125121e-01,
|
||||||
-2.11478040e-02, -7.78521440e-02, -1.21116040e-01,
|
-1.59745361e-01, -1.03571981e-01, -3.62676515e-02,
|
||||||
-1.32178923e-01, -1.04689244e-01, -4.71541301e-02,
|
1.82219951e-02, 4.09039083e-02, 2.50630186e-02,
|
||||||
2.03417510e-02, 7.38826137e-02, 8.95349902e-02,
|
-2.11478040e-02, -7.78521440e-02, -1.21116040e-01,
|
||||||
6.68738432e-02, 1.46828486e-02, -4.68648556e-02,
|
-1.32178923e-01, -1.04689244e-01, -4.71541301e-02,
|
||||||
-9.39871606e-02, -1.08465407e-01, -8.46710629e-02,
|
2.03417510e-02, 7.38826137e-02, 8.95349902e-02,
|
||||||
-3.17365657e-02, 2.99669288e-02, 7.66864134e-02,
|
6.68738432e-02, 1.46828486e-02, -4.68648556e-02,
|
||||||
9.04482283e-02, 6.59902473e-02, 1.27914062e-02,
|
-9.39871606e-02, -1.08465407e-01, -8.46710629e-02,
|
||||||
-4.85841870e-02, -9.44185349e-02, -1.06987444e-01,
|
-3.17365657e-02, 2.99669288e-02, 7.66864134e-02,
|
||||||
-8.13964951e-02, -2.74687460e-02, 3.40438793e-02,
|
9.04482283e-02, 6.59902473e-02, 1.27914062e-02,
|
||||||
7.94643163e-02, 9.13222681e-02, 6.50922520e-02,
|
-4.85841870e-02, -9.44185349e-02, -1.06987444e-01,
|
||||||
1.09390148e-02, -5.02028639e-02, -9.47031411e-02,
|
-8.13964951e-02, -2.74687460e-02, 3.40438793e-02,
|
||||||
-1.05349757e-01, -7.79872833e-02, -2.31196073e-02,
|
7.94643163e-02, 9.13222681e-02, 6.50922520e-02,
|
||||||
3.81412653e-02, 8.22178144e-02, 9.21605209e-02,
|
1.09390148e-02, -5.02028639e-02, -9.47031411e-02,
|
||||||
6.41850565e-02, 9.13184690e-03, -5.17149253e-02,
|
-1.05349757e-01, -7.79872833e-02, -2.31196073e-02,
|
||||||
-9.48363260e-02, -1.03549587e-01, -7.44424124e-02,
|
3.81412653e-02, 8.22178144e-02, 9.21605209e-02,
|
||||||
-1.86890490e-02, 4.22594607e-02, 8.49486437e-02,
|
6.41850565e-02, 9.13184690e-03, -5.17149253e-02,
|
||||||
9.29666543e-02, 6.32740911e-02, 7.37625254e-03,
|
-9.48363260e-02, -1.03549587e-01, -7.44424124e-02,
|
||||||
-5.31142920e-02, -9.48133620e-02, -1.01584110e-01,
|
-1.86890490e-02, 4.22594607e-02, 8.49486437e-02,
|
||||||
-7.07607748e-02, -1.41768231e-02, 4.63990484e-02,
|
9.29666543e-02, 6.32740911e-02, 7.37625254e-03,
|
||||||
8.76587937e-02, 9.37446001e-02, 6.23650231e-02,
|
-5.31142920e-02, -9.48133620e-02, -1.01584110e-01,
|
||||||
5.67876495e-03, -5.43947621e-02, -9.46294406e-02,
|
-7.07607748e-02, -1.41768231e-02, 4.63990484e-02,
|
||||||
-9.94504301e-02, -6.69411601e-02, -9.58252265e-03,
|
8.76587937e-02, 9.37446001e-02, 6.23650231e-02,
|
||||||
5.05608316e-02, 9.03505172e-02, 9.44985623e-02,
|
5.67876495e-03, -5.43947621e-02, -9.46294406e-02,
|
||||||
6.14637631e-02, 4.04610591e-03, -5.55500040e-02,
|
-9.94504301e-02, -6.69411601e-02, -9.58252265e-03,
|
||||||
-9.42796647e-02, -9.71455674e-02, -6.29822440e-02,
|
5.05608316e-02, 9.03505172e-02, 9.44985623e-02,
|
||||||
-4.90556961e-03, 5.47458452e-02, 9.30263409e-02,
|
6.14637631e-02, 4.04610591e-03, -5.55500040e-02,
|
||||||
9.52330253e-02, 6.05764719e-02, 2.48519180e-03,
|
-9.42796647e-02, -9.71455674e-02, -6.29822440e-02,
|
||||||
-5.65735506e-02, -9.37590405e-02, -9.46664506e-02,
|
-4.90556961e-03, 5.47458452e-02, 9.30263409e-02,
|
||||||
-5.88825766e-02, -1.45202622e-04, 5.89553685e-02,
|
9.52330253e-02, 6.05764719e-02, 2.48519180e-03,
|
||||||
9.56890756e-02, 9.59527629e-02, 5.97095676e-02,
|
-5.65735506e-02, -9.37590405e-02, -9.46664506e-02,
|
||||||
1.00314001e-03, -5.74587921e-02, -9.30624694e-02,
|
-5.88825766e-02, -1.45202622e-04, 5.89553685e-02,
|
||||||
-9.20099048e-02, -5.46405701e-02, 4.69953603e-03,
|
9.56890756e-02, 9.59527629e-02, 5.97095676e-02,
|
||||||
6.31909369e-02, 9.83418277e-02, 9.66628470e-02,
|
1.00314001e-03, -5.74587921e-02, -9.30624694e-02,
|
||||||
5.88697331e-02, -3.92724035e-04, -5.81989687e-02,
|
-9.20099048e-02, -5.46405701e-02, 4.69953603e-03,
|
||||||
-9.21847386e-02, -8.91726414e-02, -5.02544862e-02,
|
6.31909369e-02, 9.83418277e-02, 9.66628470e-02,
|
||||||
9.62981387e-03, 6.74543554e-02, 1.00988010e-01,
|
5.88697331e-02, -3.92724035e-04, -5.81989687e-02,
|
||||||
9.73686580e-02, 5.80639242e-02, -1.69485946e-03,
|
-9.21847386e-02, -8.91726414e-02, -5.02544862e-02,
|
||||||
-5.87871620e-02, -9.11205115e-02, -8.61512458e-02,
|
9.62981387e-03, 6.74543554e-02, 1.00988010e-01,
|
||||||
-4.57224228e-02, 1.46470222e-02, 7.17477118e-02,
|
9.73686580e-02, 5.80639242e-02, -1.69485946e-03,
|
||||||
1.03631355e-01, 9.80758938e-02, 5.72993780e-02,
|
-5.87871620e-02, -9.11205115e-02, -8.61512458e-02,
|
||||||
-2.89550192e-03, -5.92162868e-02, -8.98643173e-02,
|
-4.57224228e-02, 1.46470222e-02, 7.17477118e-02,
|
||||||
-8.29421650e-02, -4.10422999e-02, 1.97527907e-02,
|
1.03631355e-01, 9.80758938e-02, 5.72993780e-02,
|
||||||
7.60733908e-02, 1.06275925e-01, 9.87905812e-02,
|
-2.89550192e-03, -5.92162868e-02, -8.98643173e-02,
|
||||||
5.65836223e-02, -3.98665495e-03, -5.94790815e-02,
|
-8.29421650e-02, -4.10422999e-02, 1.97527907e-02,
|
||||||
-8.84105398e-02, -7.95416952e-02, -3.62118451e-02,
|
7.60733908e-02, 1.06275925e-01, 9.87905812e-02,
|
||||||
2.49490024e-02, 8.04340881e-02, 1.08926128e-01,
|
5.65836223e-02, -3.98665495e-03, -5.94790815e-02,
|
||||||
9.95190863e-02, 5.59244846e-02, -4.96008086e-03,
|
-8.84105398e-02, -7.95416952e-02, -3.62118451e-02,
|
||||||
-5.95680980e-02, -8.67534061e-02, -7.59459673e-02,
|
2.49490024e-02, 8.04340881e-02, 1.08926128e-01,
|
||||||
-3.12285782e-02, 3.02378095e-02, 8.48328258e-02,
|
9.95190863e-02, 5.59244846e-02, -4.96008086e-03,
|
||||||
1.11586726e-01, 1.00268126e-01, 5.53301029e-02,
|
-5.95680980e-02, -8.67534061e-02, -7.59459673e-02,
|
||||||
-5.80729079e-03, -5.94756912e-02, -8.48869734e-02,
|
-3.12285782e-02, 3.02378095e-02, 8.48328258e-02,
|
||||||
-7.21509330e-02, -2.60897955e-02, 3.56216499e-02,
|
1.11586726e-01, 1.00268126e-01, 5.53301029e-02,
|
||||||
8.92729678e-02, 1.14262857e-01, 1.01044781e-01,
|
-5.80729079e-03, -5.94756912e-02, -8.48869734e-02,
|
||||||
5.48089359e-02, -6.51953427e-03, -5.91940075e-02,
|
-7.21509330e-02, -2.60897955e-02, 3.56216499e-02,
|
||||||
-8.28051165e-02, -6.81523491e-02, -2.07925530e-02,
|
8.92729678e-02, 1.14262857e-01, 1.01044781e-01,
|
||||||
4.11032641e-02, 9.37582360e-02, 1.16960041e-01,
|
5.48089359e-02, -6.51953427e-03, -5.91940075e-02,
|
||||||
1.13824241e-01, 7.82451609e-02, 2.87461256e-02,
|
-8.28051165e-02, -6.81523491e-02, -2.07925530e-02,
|
||||||
-1.07566250e-02, -2.01779675e-02, 8.98967999e-03,
|
4.11032641e-02, 9.37582360e-02, 1.16960041e-01,
|
||||||
7.03952281e-02, 1.45278564e-01, 2.09706186e-01,
|
1.13824241e-01, 7.82451609e-02, 2.87461256e-02,
|
||||||
2.43802139e-01, 2.39414013e-01, 2.03257341e-01,
|
-1.07566250e-02, -2.01779675e-02, 8.98967999e-03,
|
||||||
1.54325635e-01, 1.16564992e-01, 1.09638547e-01,
|
7.03952281e-02, 1.45278564e-01, 2.09706186e-01,
|
||||||
1.41342814e-01, 2.04600808e-01, 2.80191671e-01,
|
2.43802139e-01, 2.39414013e-01, 2.03257341e-01,
|
||||||
3.44164010e-01, 3.77073744e-01]), array([
|
1.54325635e-01, 1.16564992e-01, 1.09638547e-01,
|
||||||
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
1.41342814e-01, 2.04600808e-01, 2.80191671e-01,
|
||||||
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
3.44164010e-01, 3.77073744e-01
|
||||||
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
]))
|
||||||
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
assert_array_almost_equal(tr, array([
|
||||||
1.11599212, 1.12125245, 1.12643866, 1.13166607, 1.13704477,
|
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
||||||
1.14263723, 1.14843422, 1.15435845, 1.16029443, 1.16613308,
|
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
||||||
1.17181383, 1.17734804, 1.18281471, 1.18833001, 1.19400259,
|
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
||||||
1.19989168, 1.20598434, 1.21220048, 1.21842384, 1.22454684,
|
1.11058152, 1.11058152, 1.11058152, 1.11058152, 1.11058152,
|
||||||
1.23051218, 1.23633498, 1.24209697, 1.24791509, 1.25389641,
|
1.11599212, 1.12125245, 1.12643866, 1.13166607, 1.13704477,
|
||||||
1.26009689, 1.26649987, 1.27302256, 1.27954802, 1.28597031,
|
1.14263723, 1.14843422, 1.15435845, 1.16029443, 1.16613308,
|
||||||
1.29223546, 1.29836228, 1.30443522, 1.31057183, 1.31687751,
|
1.17181383, 1.17734804, 1.18281471, 1.18833001, 1.19400259,
|
||||||
1.32340488, 1.3301336 , 1.33697825, 1.34382132, 1.35055864,
|
1.19989168, 1.20598434, 1.21220048, 1.21842384, 1.22454684,
|
||||||
1.35713958, 1.36358668, 1.36998697, 1.37645853, 1.38310497,
|
1.23051218, 1.23633498, 1.24209697, 1.24791509, 1.25389641,
|
||||||
1.38997553, 1.39704621, 1.40422902, 1.41140604, 1.41847493,
|
1.26009689, 1.26649987, 1.27302256, 1.27954802, 1.28597031,
|
||||||
1.4253885 , 1.43217295, 1.43891784, 1.44574164, 1.45274607,
|
1.29223546, 1.29836228, 1.30443522, 1.31057183, 1.31687751,
|
||||||
1.45997696, 1.46740665, 1.47494469, 1.48247285, 1.48989073,
|
1.32340488, 1.3301336, 1.33697825, 1.34382132, 1.35055864,
|
||||||
1.49715462, 1.50429437, 1.51140198, 1.51859618, 1.52597672,
|
1.35713958, 1.36358668, 1.36998697, 1.37645853, 1.38310497,
|
||||||
1.53358594, 1.54139257, 1.5493038 , 1.55720119, 1.56498641,
|
1.38997553, 1.39704621, 1.40422902, 1.41140604, 1.41847493,
|
||||||
1.57261924, 1.58013316, 1.58762252, 1.5952062 , 1.60298187,
|
1.4253885, 1.43217295, 1.43891784, 1.44574164, 1.45274607,
|
||||||
1.61098836, 1.6191908 , 1.62749412, 1.63577979, 1.64395163,
|
1.45997696, 1.46740665, 1.47494469, 1.48247285, 1.48989073,
|
||||||
1.65197298, 1.65988092, 1.66777202, 1.67576523, 1.68395602,
|
1.49715462, 1.50429437, 1.51140198, 1.51859618, 1.52597672,
|
||||||
1.69237968, 1.70099778, 1.70971307, 1.71840707, 1.72698583,
|
1.53358594, 1.54139257, 1.5493038, 1.55720119, 1.56498641,
|
||||||
1.73541631, 1.74373911, 1.75205298, 1.76047677, 1.76910369,
|
1.57261924, 1.58013316, 1.58762252, 1.5952062, 1.60298187,
|
||||||
1.77796544, 1.78702008, 1.79616827, 1.80529169, 1.81429875,
|
1.61098836, 1.6191908, 1.62749412, 1.63577979, 1.64395163,
|
||||||
1.82316 , 1.83191959, 1.84067831, 1.84955481, 1.85863994,
|
1.65197298, 1.65988092, 1.66777202, 1.67576523, 1.68395602,
|
||||||
1.86796178, 1.87747491, 1.88707803, 1.89665308, 1.9061109 ,
|
1.69237968, 1.70099778, 1.70971307, 1.71840707, 1.72698583,
|
||||||
1.91542572, 1.92464514, 1.9338719 , 1.94322436, 1.9527909 ,
|
1.73541631, 1.74373911, 1.75205298, 1.76047677, 1.76910369,
|
||||||
1.96259596, 1.97259069, 1.9826719 , 1.99272195, 2.00265419,
|
1.77796544, 1.78702008, 1.79616827, 1.80529169, 1.81429875,
|
||||||
2.01244653, 2.02215 , 2.0318692 , 2.04172204, 2.05179437,
|
1.82316, 1.83191959, 1.84067831, 1.84955481, 1.85863994,
|
||||||
2.06210696, 2.07260759, 2.08319129, 2.09374092, 2.10417247,
|
1.86796178, 1.87747491, 1.88707803, 1.89665308, 1.9061109,
|
||||||
2.11446752, 2.12468051, 2.13491776, 2.14529665, 2.1559004 ,
|
1.91542572, 1.92464514, 1.9338719, 1.94322436, 1.9527909,
|
||||||
2.16674609, 2.17777817, 2.18889002, 2.19996511, 2.21092214,
|
1.96259596, 1.97259069, 1.9826719, 1.99272195, 2.00265419,
|
||||||
2.22174641, 2.23249567, 2.24327791, 2.25420982, 2.26537192,
|
2.01244653, 2.02215, 2.0318692, 2.04172204, 2.05179437,
|
||||||
2.2767776 , 2.28836802, 2.30003501, 2.3116628 , 2.32317284,
|
2.06210696, 2.07260759, 2.08319129, 2.09374092, 2.10417247,
|
||||||
2.33455419, 2.34586786, 2.35722337, 2.36873665, 2.38048542,
|
2.11446752, 2.12468051, 2.13491776, 2.14529665, 2.1559004,
|
||||||
2.39247934, 2.4046564 , 2.41690694, 2.42911606, 2.44120808,
|
2.16674609, 2.17777817, 2.18889002, 2.19996511, 2.21092214,
|
||||||
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
2.22174641, 2.23249567, 2.24327791, 2.25420982, 2.26537192,
|
||||||
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
2.2767776, 2.28836802, 2.30003501, 2.3116628, 2.32317284,
|
||||||
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
2.33455419, 2.34586786, 2.35722337, 2.36873665, 2.38048542,
|
||||||
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808]))
|
2.39247934, 2.4046564, 2.41690694, 2.42911606, 2.44120808,
|
||||||
'''
|
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
||||||
|
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
||||||
|
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808,
|
||||||
|
2.44120808, 2.44120808, 2.44120808, 2.44120808, 2.44120808]))
|
||||||
|
|
||||||
|
|
||||||
def test_findcross_and_ecross():
|
def test_findcross_and_ecross():
|
||||||
'''
|
assert_array_equal(findcross([0, 0, 1, -1, 1], 0), np.array([1, 2, 3]))
|
||||||
>>> findcross([0, 0, 1, -1, 1],0)
|
assert_array_equal(findcross([0, 1, -1, 1], 0), np.array([0, 1, 2]))
|
||||||
array([1, 2, 3])
|
|
||||||
>>> findcross([0, 1, -1, 1],0)
|
t = linspace(0, 7 * pi, 250)
|
||||||
array([0, 1, 2])
|
x = sin(t)
|
||||||
|
ind = findcross(x, 0.75)
|
||||||
>>> t = linspace(0,7*pi,250)
|
assert_array_equal(ind, np.array([9, 25, 80, 97, 151, 168, 223, 239]))
|
||||||
>>> x = sin(t)
|
t0 = ecross(t, x, ind, 0.75)
|
||||||
>>> ind = findcross(x,0.75)
|
assert_array_almost_equal(t0, np.array([0.84910514, 2.2933879, 7.13205663,
|
||||||
>>> ind
|
8.57630119, 13.41484739, 14.85909194,
|
||||||
array([ 9, 25, 80, 97, 151, 168, 223, 239])
|
19.69776067, 21.14204343]))
|
||||||
>>> t0 = ecross(t,x,ind,0.75)
|
|
||||||
>>> t0
|
|
||||||
array([ 0.84910514, 2.2933879 , 7.13205663, 8.57630119,
|
|
||||||
13.41484739, 14.85909194, 19.69776067, 21.14204343])
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_findextrema():
|
def test_findextrema():
|
||||||
'''
|
t = linspace(0, 7 * pi, 250)
|
||||||
>>> t = linspace(0,7*pi,250)
|
x = sin(t)
|
||||||
>>> x = sin(t)
|
ind = findextrema(x)
|
||||||
>>> ind = findextrema(x)
|
assert_array_almost_equal(ind, np.array([18, 53, 89, 125, 160, 196, 231]))
|
||||||
>>> ind
|
|
||||||
array([ 18, 53, 89, 125, 160, 196, 231])
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_findrfc():
|
def test_findrfc():
|
||||||
'''
|
t = linspace(0, 7 * pi, 250)
|
||||||
>>> t = linspace(0,7*pi,250)
|
x = sin(t) + 0.1 * sin(50 * t)
|
||||||
>>> x = sin(t)+0.1*sin(50*t)
|
ind = findextrema(x)
|
||||||
>>> ind = findextrema(x)
|
assert_array_almost_equal(
|
||||||
>>> ind
|
ind,
|
||||||
array([ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21,
|
np.array(
|
||||||
23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43,
|
[1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21,
|
||||||
45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65,
|
23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43,
|
||||||
67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87,
|
45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65,
|
||||||
88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109,
|
67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87,
|
||||||
110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131,
|
88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109,
|
||||||
132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152,
|
110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131,
|
||||||
154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174,
|
132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152,
|
||||||
176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196,
|
154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174,
|
||||||
198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218,
|
176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196,
|
||||||
219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240,
|
198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218,
|
||||||
241, 243, 245, 247, 248])
|
219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240,
|
||||||
>>> ti, tp = t[ind], x[ind]
|
241, 243, 245, 247, 248]))
|
||||||
>>> ind1 = findrfc(tp,0.3)
|
_ti, tp = t[ind], x[ind]
|
||||||
>>> ind1
|
ind1 = findrfc(tp, 0.3)
|
||||||
array([ 0, 9, 32, 53, 74, 95, 116, 137])
|
assert_array_almost_equal(
|
||||||
>>> tp[ind1]
|
ind1,
|
||||||
array([-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458,
|
np.array([0, 9, 32, 53, 74, 95, 116, 137]))
|
||||||
1.07849396, -1.0995006 , 1.08094452])
|
assert_array_almost_equal(
|
||||||
'''
|
tp[ind1],
|
||||||
|
np.array(
|
||||||
|
[-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458,
|
||||||
|
1.07849396, -1.0995006, 1.08094452]))
|
||||||
|
|
||||||
|
|
||||||
def test_rfcfilter():
|
def test_rfcfilter():
|
||||||
'''
|
# 1. Filtered signal y is the turning points of x.
|
||||||
# 1. Filtered signal y is the turning points of x.
|
x = sea()
|
||||||
>>> x = sea()
|
y = rfcfilter(x[:, 1], h=0, method=1)
|
||||||
>>> y = rfcfilter(x[:,1], h=0, method=1)
|
assert_array_almost_equal(
|
||||||
>>> y[0:5]
|
y[0:5],
|
||||||
array([-1.2004945 , 0.83950546, -0.09049454, -0.02049454, -0.09049454])
|
np.array([-1.2004945, 0.83950546, -0.09049454,
|
||||||
|
-0.02049454, -0.09049454]))
|
||||||
|
|
||||||
# 2. This removes all rainflow cycles with range less than 0.5.
|
# 2. This removes all rainflow cycles with range less than 0.5.
|
||||||
>>> y1 = rfcfilter(x[:,1], h=0.5)
|
y1 = rfcfilter(x[:, 1], h=0.5)
|
||||||
>>> y1[0:5]
|
assert_array_almost_equal(
|
||||||
array([-1.2004945 , 0.83950546, -0.43049454, 0.34950546, -0.51049454])
|
y1[0:5],
|
||||||
|
np.array([-1.2004945, 0.83950546, -0.43049454,
|
||||||
>>> t = linspace(0,7*pi,250)
|
0.34950546, -0.51049454]))
|
||||||
>>> x = sin(t)+0.1*sin(50*t)
|
|
||||||
>>> ind = findextrema(x)
|
t = linspace(0, 7 * pi, 250)
|
||||||
>>> ind
|
x = sin(t) + 0.1 * sin(50 * t)
|
||||||
array([ 1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21,
|
ind = findextrema(x)
|
||||||
23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43,
|
assert_array_almost_equal(
|
||||||
45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65,
|
ind,
|
||||||
67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87,
|
np.array(
|
||||||
88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109,
|
[1, 3, 4, 6, 7, 9, 11, 13, 14, 16, 18, 19, 21,
|
||||||
110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131,
|
23, 25, 26, 28, 29, 31, 33, 35, 36, 38, 39, 41, 43,
|
||||||
132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152,
|
45, 46, 48, 50, 51, 53, 55, 56, 58, 60, 61, 63, 65,
|
||||||
154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174,
|
67, 68, 70, 71, 73, 75, 77, 78, 80, 81, 83, 85, 87,
|
||||||
176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196,
|
88, 90, 92, 93, 95, 97, 99, 100, 102, 103, 105, 107, 109,
|
||||||
198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218,
|
110, 112, 113, 115, 117, 119, 120, 122, 124, 125, 127, 129, 131,
|
||||||
219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240,
|
132, 134, 135, 137, 139, 141, 142, 144, 145, 147, 149, 151, 152,
|
||||||
241, 243, 245, 247, 248])
|
154, 156, 157, 159, 161, 162, 164, 166, 167, 169, 171, 173, 174,
|
||||||
>>> ti, tp = t[ind], x[ind]
|
176, 177, 179, 181, 183, 184, 186, 187, 189, 191, 193, 194, 196,
|
||||||
>>> tp03 = rfcfilter(tp,0.3)
|
198, 199, 201, 203, 205, 206, 208, 209, 211, 213, 215, 216, 218,
|
||||||
>>> tp03
|
219, 221, 223, 225, 226, 228, 230, 231, 233, 235, 237, 238, 240,
|
||||||
array([-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458,
|
241, 243, 245, 247, 248]))
|
||||||
1.07849396, -1.0995006 , 1.08094452, 0.11983423])
|
_ti, tp = t[ind], x[ind]
|
||||||
'''
|
tp03 = rfcfilter(tp, 0.3)
|
||||||
|
assert_array_almost_equal(
|
||||||
|
tp03,
|
||||||
|
np.array(
|
||||||
|
[-0.00743352, 1.08753972, -1.07206545, 1.09550837, -1.07940458,
|
||||||
|
1.07849396, -1.0995006, 1.08094452, 0.11983423]))
|
||||||
|
|
||||||
|
|
||||||
def test_findtp():
|
def test_findtp():
|
||||||
'''
|
x = sea()
|
||||||
>>> import numpy as np
|
x1 = x[0:200, :]
|
||||||
>>> x = sea()
|
itp = findtp(x1[:, 1], 0, 'Mw')
|
||||||
>>> x1 = x[0:200,:]
|
itph = findtp(x1[:, 1], 0.3, 'Mw')
|
||||||
>>> itp = findtp(x1[:,1],0,'Mw')
|
assert_array_almost_equal(
|
||||||
>>> itph = findtp(x1[:,1],0.3,'Mw')
|
itp,
|
||||||
>>> itp
|
np.array(
|
||||||
array([ 11, 21, 22, 24, 26, 28, 31, 39, 43, 45, 47, 51, 56,
|
[11, 21, 22, 24, 26, 28, 31, 39, 43, 45, 47, 51, 56,
|
||||||
64, 70, 78, 82, 84, 89, 94, 101, 108, 119, 131, 141, 148,
|
64, 70, 78, 82, 84, 89, 94, 101, 108, 119, 131, 141, 148,
|
||||||
149, 150, 159, 173, 184, 190, 199])
|
149, 150, 159, 173, 184, 190, 199]))
|
||||||
>>> itph
|
assert_array_almost_equal(
|
||||||
array([ 11, 28, 31, 39, 47, 51, 56, 64, 70, 78, 89, 94, 101,
|
itph,
|
||||||
108, 119, 131, 141, 148, 159, 173, 184, 190, 199])
|
np.array(
|
||||||
'''
|
[11, 28, 31, 39, 47, 51, 56, 64, 70, 78, 89, 94, 101,
|
||||||
|
108, 119, 131, 141, 148, 159, 173, 184, 190, 199]))
|
||||||
|
|
||||||
|
|
||||||
def test_findtc():
|
def test_findtc():
|
||||||
'''
|
x = sea()
|
||||||
>>> x = sea()
|
x1 = x[0:200, :]
|
||||||
>>> x1 = x[0:200,:]
|
itc, iv = findtc(x1[:, 1], 0, 'dw')
|
||||||
>>> itc, iv = findtc(x1[:,1],0,'dw')
|
assert_array_almost_equal(
|
||||||
>>> itc
|
itc,
|
||||||
array([ 28, 31, 39, 56, 64, 69, 78, 82, 83, 89, 94, 101, 108,
|
np.array(
|
||||||
119, 131, 140, 148, 159, 173, 184])
|
[28, 31, 39, 56, 64, 69, 78, 82, 83, 89, 94, 101, 108,
|
||||||
>>> iv
|
119, 131, 140, 148, 159, 173, 184]))
|
||||||
array([ 19, 29, 34, 53, 60, 67, 76, 81, 82, 84, 90, 99, 103,
|
assert_array_almost_equal(
|
||||||
112, 127, 137, 143, 154, 166, 180, 185])
|
iv,
|
||||||
'''
|
np.array(
|
||||||
|
[19, 29, 34, 53, 60, 67, 76, 81, 82, 84, 90, 99, 103,
|
||||||
|
112, 127, 137, 143, 154, 166, 180, 185]))
|
||||||
|
|
||||||
|
|
||||||
def test_findoutliers():
|
def test_findoutliers():
|
||||||
'''
|
xx = sea()
|
||||||
>>> xx = sea()
|
dt = diff(xx[:2, 0])
|
||||||
>>> dt = diff(xx[:2,0])
|
dcrit = 5 * dt
|
||||||
>>> dcrit = 5*dt
|
ddcrit = 9.81 / 2 * dt * dt
|
||||||
>>> ddcrit = 9.81/2*dt*dt
|
zcrit = 0
|
||||||
>>> zcrit = 0
|
[inds, indg] = findoutliers(xx[:, 1], zcrit, dcrit, ddcrit, verbose=False)
|
||||||
>>> [inds, indg] = findoutliers(xx[:,1],zcrit,dcrit,ddcrit,verbose=True)
|
assert_array_almost_equal(inds[np.r_[0, 1, 2, -3, -2, -1]],
|
||||||
Found 0 spurious positive jumps of Dx
|
np.array([6, 7, 8, 9509, 9510, 9511]))
|
||||||
Found 0 spurious negative jumps of Dx
|
assert_array_almost_equal(indg[np.r_[0, 1, 2, -3, -2, -1]],
|
||||||
Found 37 spurious positive jumps of D^2x
|
np.array([0, 1, 2, 9521, 9522, 9523]))
|
||||||
Found 200 spurious negative jumps of D^2x
|
|
||||||
Found 244 consecutive equal values
|
|
||||||
Found the total of 1152 spurious points
|
def test_hygfz():
|
||||||
>>> inds
|
#y = hyp2f1_taylor(-1, -4, 1, .9)
|
||||||
array([ 6, 7, 8, ..., 9509, 9510, 9511])
|
assert_equal(4.6, hygfz(-1, -4, 1, .9))
|
||||||
>>> indg
|
assert_almost_equal(1.0464328112173522, hygfz(0.1, 0.2, 0.3, 0.5))
|
||||||
array([ 0, 1, 2, ..., 9521, 9522, 9523])
|
assert_almost_equal(1.2027034401166194, hygfz(0.1, 0.2, 0.3, 0.95))
|
||||||
'''
|
#assert_equal(1.661006238211309e-07, hygfz(5, -300, 10, 0.5))
|
||||||
|
assert_equal(0.118311386286, hygfz(0.5, -99.0, 1.5, 0.5625))
|
||||||
|
assert_equal(0.0965606007742, hygfz(0.5, -149.0, 1.5, 0.5625))
|
||||||
|
assert_equal(0.49234384000963544+0.60513406166123973j, hygfz(1, 1, 4, 3+4j))
|
||||||
|
|
||||||
def test_common_shape():
|
def test_common_shape():
|
||||||
'''
|
A = np.ones((4, 1))
|
||||||
>>> import numpy as np
|
B = 2
|
||||||
>>> A = np.ones((4,1))
|
C = np.ones((1, 5)) * 5
|
||||||
>>> B = 2
|
assert_array_equal(common_shape(A, B, C), (4, 5))
|
||||||
>>> C = np.ones((1,5))*5
|
assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
|
||||||
>>> common_shape(A,B,C)
|
A = np.ones((4, 1))
|
||||||
(4, 5)
|
B = 2
|
||||||
>>> common_shape(A,B,C,shape=(3,4,1))
|
C = np.ones((1, 5)) * 5
|
||||||
(3, 4, 5)
|
assert_array_equal(common_shape(A, B, C), (4, 5))
|
||||||
>>> A = np.ones((4,1))
|
assert_array_equal(common_shape(A, B, C, shape=(3, 4, 1)), (3, 4, 5))
|
||||||
>>> B = 2
|
|
||||||
>>> C = np.ones((1,5))*5
|
|
||||||
>>> common_shape(A,B,C)
|
|
||||||
(4, 5)
|
|
||||||
>>> common_shape(A,B,C,shape=(3,4,1))
|
|
||||||
(3, 4, 5)
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_argsreduce():
|
def test_argsreduce():
|
||||||
'''
|
A = linspace(0, 19, 20).reshape((4, 5))
|
||||||
>>> import numpy as np
|
B = 2
|
||||||
>>> rand = np.random.random_sample
|
C = range(5)
|
||||||
>>> A = linspace(0,19,20).reshape((4,5))
|
cond = np.ones(A.shape)
|
||||||
>>> B = 2
|
[_A1, B1, _C1] = argsreduce(cond, A, B, C)
|
||||||
>>> C = range(5)
|
assert_equal(B1.shape, (20,))
|
||||||
>>> cond = np.ones(A.shape)
|
cond[2, :] = 0
|
||||||
>>> [A1,B1,C1] = argsreduce(cond,A,B,C)
|
[A2, B2, C2] = argsreduce(cond, A, B, C)
|
||||||
>>> B1.shape
|
assert_equal(B2.shape, (15,))
|
||||||
(20,)
|
assert_array_equal(A2,
|
||||||
>>> cond[2,:] = 0
|
np.array([0., 1., 2., 3., 4., 5., 6., 7.,
|
||||||
>>> [A2,B2,C2] = argsreduce(cond,A,B,C)
|
8., 9., 15., 16., 17., 18., 19.]))
|
||||||
>>> B2.shape
|
assert_array_equal(
|
||||||
(15,)
|
B2, np.array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]))
|
||||||
>>> A2;B2;C2
|
assert_array_equal(
|
||||||
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 15.,
|
C2, np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]))
|
||||||
16., 17., 18., 19.])
|
|
||||||
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
|
|
||||||
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_stirlerr():
|
def test_stirlerr():
|
||||||
'''
|
assert_array_almost_equal(stirlerr(range(5)),
|
||||||
>>> stirlerr(range(5))
|
np.array([np.inf, 0.08106147, 0.0413407, 0.02767793,
|
||||||
array([ inf, 0.08106147, 0.0413407 , 0.02767793, 0.02079067])
|
0.02079067]))
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_getshipchar():
|
def test_getshipchar():
|
||||||
'''
|
sc = getshipchar(10, 'service_speed')
|
||||||
>>> sc = getshipchar(10,'service_speed')
|
true_sc = dict(beam=29,
|
||||||
>>> names = ['beam', 'beamSTD', 'draught',
|
beamSTD=2.9,
|
||||||
... 'draughtSTD', 'length', 'lengthSTD',
|
draught=9.6,
|
||||||
... 'max_deadweight', 'max_deadweightSTD', 'propeller_diameter',
|
draughtSTD=2.112,
|
||||||
... 'propeller_diameterSTD', 'service_speed', 'service_speedSTD']
|
length=216,
|
||||||
>>> for name in names: print( '%s : %g' % (name, sc[name]))
|
lengthSTD=2.011309883194276,
|
||||||
beam : 29
|
max_deadweight=30969,
|
||||||
beamSTD : 2.9
|
max_deadweightSTD=3096.9,
|
||||||
draught : 9.6
|
propeller_diameter=6.761165385916601,
|
||||||
draughtSTD : 2.112
|
propeller_diameterSTD=0.20267047566705432,
|
||||||
length : 216
|
service_speed=10,
|
||||||
lengthSTD : 2.01131
|
service_speedSTD=0)
|
||||||
max_deadweight : 30969
|
|
||||||
max_deadweightSTD : 3096.9
|
for name, val in true_sc.iteritems():
|
||||||
propeller_diameter : 6.76117
|
assert_almost_equal(val, sc[name])
|
||||||
propeller_diameterSTD : 0.20267
|
|
||||||
service_speed : 10
|
|
||||||
service_speedSTD : 0
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_betaloge():
|
def test_betaloge():
|
||||||
'''
|
assert_array_almost_equal(betaloge(3, arange(4)),
|
||||||
>>> betaloge(3, arange(4))
|
np.array([np.inf, -1.09861229, -2.48490665, -3.40119738]))
|
||||||
array([ inf, -1.09861229, -2.48490665, -3.40119738])
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_gravity():
|
def test_gravity():
|
||||||
'''
|
phi = linspace(0, 45, 5)
|
||||||
>>> phi = linspace(0,45,5)
|
assert_array_almost_equal(gravity(phi),
|
||||||
>>> gravity(phi)
|
np.array([9.78049, 9.78245014, 9.78803583,
|
||||||
array([ 9.78049 , 9.78245014, 9.78803583, 9.79640552, 9.80629387])
|
9.79640552, 9.80629387]))
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_nextpow2():
|
def test_nextpow2():
|
||||||
'''
|
assert_equal(nextpow2(10), 4)
|
||||||
>>> nextpow2(10)
|
assert_equal(nextpow2(np.arange(5)), 3)
|
||||||
4
|
|
||||||
>>> nextpow2(np.arange(5))
|
|
||||||
3
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_discretize():
|
def test_discretize():
|
||||||
'''
|
x, y = discretize(np.cos, 0, np.pi)
|
||||||
>>> x, y = discretize(np.cos,0,np.pi)
|
assert_array_almost_equal(
|
||||||
>>> x; y
|
x,
|
||||||
array([ 0. , 0.19634954, 0.39269908, 0.58904862, 0.78539816,
|
np.array(
|
||||||
0.9817477 , 1.17809725, 1.37444679, 1.57079633, 1.76714587,
|
[0., 0.19634954, 0.39269908, 0.58904862, 0.78539816,
|
||||||
1.96349541, 2.15984495, 2.35619449, 2.55254403, 2.74889357,
|
0.9817477, 1.17809725, 1.37444679, 1.57079633, 1.76714587,
|
||||||
2.94524311, 3.14159265])
|
1.96349541, 2.15984495, 2.35619449, 2.55254403, 2.74889357,
|
||||||
array([ 1.00000000e+00, 9.80785280e-01, 9.23879533e-01,
|
2.94524311, 3.14159265]))
|
||||||
8.31469612e-01, 7.07106781e-01, 5.55570233e-01,
|
assert_array_almost_equal(
|
||||||
3.82683432e-01, 1.95090322e-01, 6.12323400e-17,
|
y, np.array([1.00000000e+00, 9.80785280e-01,
|
||||||
-1.95090322e-01, -3.82683432e-01, -5.55570233e-01,
|
9.23879533e-01,
|
||||||
-7.07106781e-01, -8.31469612e-01, -9.23879533e-01,
|
8.31469612e-01, 7.07106781e-01, 5.55570233e-01,
|
||||||
-9.80785280e-01, -1.00000000e+00])
|
3.82683432e-01, 1.95090322e-01, 6.12323400e-17,
|
||||||
'''
|
-1.95090322e-01, -3.82683432e-01, -5.55570233e-01,
|
||||||
|
-7.07106781e-01, -8.31469612e-01, -9.23879533e-01,
|
||||||
|
-9.80785280e-01, -1.00000000e+00]))
|
||||||
|
|
||||||
|
|
||||||
def test_discretize_adaptive():
|
def test_discretize_adaptive():
|
||||||
'''
|
x, y = discretize(np.cos, 0, np.pi, method='adaptive')
|
||||||
>>> x, y = discretize(np.cos,0,np.pi, method='adaptive')
|
assert_array_almost_equal(
|
||||||
>>> x; y
|
x,
|
||||||
array([ 0. , 0.19634954, 0.39269908, 0.58904862, 0.78539816,
|
np.array(
|
||||||
0.9817477 , 1.17809725, 1.37444679, 1.57079633, 1.76714587,
|
[0., 0.19634954, 0.39269908, 0.58904862, 0.78539816,
|
||||||
1.96349541, 2.15984495, 2.35619449, 2.55254403, 2.74889357,
|
0.9817477, 1.17809725, 1.37444679, 1.57079633, 1.76714587,
|
||||||
2.94524311, 3.14159265])
|
1.96349541, 2.15984495, 2.35619449, 2.55254403, 2.74889357,
|
||||||
array([ 1.00000000e+00, 9.80785280e-01, 9.23879533e-01,
|
2.94524311, 3.14159265]))
|
||||||
8.31469612e-01, 7.07106781e-01, 5.55570233e-01,
|
assert_array_almost_equal(
|
||||||
3.82683432e-01, 1.95090322e-01, 6.12323400e-17,
|
y,
|
||||||
-1.95090322e-01, -3.82683432e-01, -5.55570233e-01,
|
np.array(
|
||||||
-7.07106781e-01, -8.31469612e-01, -9.23879533e-01,
|
[1.00000000e+00, 9.80785280e-01, 9.23879533e-01,
|
||||||
-9.80785280e-01, -1.00000000e+00])
|
8.31469612e-01, 7.07106781e-01, 5.55570233e-01,
|
||||||
'''
|
3.82683432e-01, 1.95090322e-01, 6.12323400e-17,
|
||||||
|
-1.95090322e-01, -3.82683432e-01, -5.55570233e-01,
|
||||||
|
-7.07106781e-01, -8.31469612e-01, -9.23879533e-01,
|
||||||
def test_pol2cart_n_cart2pol():
|
-9.80785280e-01, -1.00000000e+00]))
|
||||||
'''
|
|
||||||
>>> r = 5
|
|
||||||
>>> t = linspace(0,pi,20)
|
def test_polar2cart_n_cart2polar():
|
||||||
>>> x, y = polar2cart(t,r)
|
r = 5
|
||||||
>>> x; y
|
t = linspace(0, pi, 20)
|
||||||
array([ 5. , 4.93180652, 4.72908621, 4.39736876, 3.94570255,
|
x, y = polar2cart(t, r)
|
||||||
3.38640786, 2.73474079, 2.00847712, 1.22742744, 0.41289673,
|
assert_array_almost_equal(
|
||||||
-0.41289673, -1.22742744, -2.00847712, -2.73474079, -3.38640786,
|
x,
|
||||||
-3.94570255, -4.39736876, -4.72908621, -4.93180652, -5. ])
|
np.array(
|
||||||
array([ 0.00000000e+00, 8.22972951e-01, 1.62349735e+00,
|
[5., 4.93180652, 4.72908621, 4.39736876, 3.94570255,
|
||||||
2.37973697e+00, 3.07106356e+00, 3.67861955e+00,
|
3.38640786, 2.73474079, 2.00847712, 1.22742744, 0.41289673,
|
||||||
4.18583239e+00, 4.57886663e+00, 4.84700133e+00,
|
-0.41289673, -1.22742744, -2.00847712, -2.73474079, -3.38640786,
|
||||||
4.98292247e+00, 4.98292247e+00, 4.84700133e+00,
|
-3.94570255, -4.39736876, -4.72908621, -4.93180652, -5.]))
|
||||||
4.57886663e+00, 4.18583239e+00, 3.67861955e+00,
|
assert_array_almost_equal(
|
||||||
3.07106356e+00, 2.37973697e+00, 1.62349735e+00,
|
y,
|
||||||
8.22972951e-01, 6.12323400e-16])
|
np.array(
|
||||||
>>> ti, ri = cart2polar(x,y)
|
[0.00000000e+00, 8.22972951e-01, 1.62349735e+00,
|
||||||
>>> ti;ri
|
2.37973697e+00, 3.07106356e+00, 3.67861955e+00,
|
||||||
array([ 0. , 0.16534698, 0.33069396, 0.49604095, 0.66138793,
|
4.18583239e+00, 4.57886663e+00, 4.84700133e+00,
|
||||||
0.82673491, 0.99208189, 1.15742887, 1.32277585, 1.48812284,
|
4.98292247e+00, 4.98292247e+00, 4.84700133e+00,
|
||||||
1.65346982, 1.8188168 , 1.98416378, 2.14951076, 2.31485774,
|
4.57886663e+00, 4.18583239e+00, 3.67861955e+00,
|
||||||
2.48020473, 2.64555171, 2.81089869, 2.97624567, 3.14159265])
|
3.07106356e+00, 2.37973697e+00, 1.62349735e+00,
|
||||||
array([ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.,
|
8.22972951e-01, 6.12323400e-16]))
|
||||||
5., 5., 5., 5., 5., 5., 5.])
|
ti, ri = cart2polar(x, y)
|
||||||
'''
|
assert_array_almost_equal(
|
||||||
|
ti,
|
||||||
|
np.array(
|
||||||
def test_meshgrid():
|
[0., 0.16534698, 0.33069396, 0.49604095, 0.66138793,
|
||||||
'''
|
0.82673491, 0.99208189, 1.15742887, 1.32277585, 1.48812284,
|
||||||
>>> x = np.linspace(0,1,3) # coordinates along x axis
|
1.65346982, 1.8188168, 1.98416378, 2.14951076, 2.31485774,
|
||||||
>>> y = np.linspace(0,1,2) # coordinates along y axis
|
2.48020473, 2.64555171, 2.81089869, 2.97624567, 3.14159265]))
|
||||||
>>> xv, yv = meshgrid(x,y) # extend x and y for a 2D xy grid
|
assert_array_almost_equal(
|
||||||
>>> xv
|
ri,
|
||||||
array([[ 0. , 0.5, 1. ],
|
np.array(
|
||||||
[ 0. , 0.5, 1. ]])
|
[5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.,
|
||||||
>>> yv
|
5., 5., 5., 5., 5., 5., 5.]))
|
||||||
array([[ 0., 0., 0.],
|
|
||||||
[ 1., 1., 1.]])
|
|
||||||
>>> xv, yv = meshgrid(x,y, sparse=True) # make sparse output arrays
|
|
||||||
>>> xv
|
|
||||||
array([[ 0. , 0.5, 1. ]])
|
|
||||||
>>> yv
|
|
||||||
array([[ 0.],
|
|
||||||
[ 1.]])
|
|
||||||
|
|
||||||
>>> meshgrid(x,y,sparse=True,indexing='ij') # change to matrix indexing
|
|
||||||
[array([[ 0. ],
|
|
||||||
[ 0.5],
|
|
||||||
[ 1. ]]), array([[ 0., 1.]])]
|
|
||||||
>>> meshgrid(x,y,indexing='ij')
|
|
||||||
[array([[ 0. , 0. ],
|
|
||||||
[ 0.5, 0.5],
|
|
||||||
[ 1. , 1. ]]), array([[ 0., 1.],
|
|
||||||
[ 0., 1.],
|
|
||||||
[ 0., 1.]])]
|
|
||||||
|
|
||||||
>>> meshgrid(0,1,5) # just a 3D point
|
|
||||||
[array([[[0]]]), array([[[1]]]), array([[[5]]])]
|
|
||||||
>>> map(np.squeeze,meshgrid(0,1,5)) # just a 3D point
|
|
||||||
[array(0), array(1), array(5)]
|
|
||||||
>>> meshgrid(3)
|
|
||||||
array([3])
|
|
||||||
>>> meshgrid(y) # 1D grid y is just returned
|
|
||||||
array([ 0., 1.])
|
|
||||||
|
|
||||||
`meshgrid` is very useful to evaluate functions on a grid.
|
|
||||||
|
|
||||||
>>> x = np.arange(-5, 5, 0.1)
|
|
||||||
>>> y = np.arange(-5, 5, 0.1)
|
|
||||||
>>> xx, yy = meshgrid(x, y, sparse=True)
|
|
||||||
>>> z = np.sin(xx**2+yy**2)/(xx**2+yy**2)
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
def test_tranproc():
|
def test_tranproc():
|
||||||
'''
|
import wafo.transform.models as wtm
|
||||||
>>> import wafo.transform.models as wtm
|
tr = wtm.TrHermite()
|
||||||
>>> tr = wtm.TrHermite()
|
x = linspace(-5, 5, 501)
|
||||||
>>> x = linspace(-5,5,501)
|
g = tr(x)
|
||||||
>>> g = tr(x)
|
y0, y1 = tranproc(x, g, range(5), ones(5))
|
||||||
>>> y0, y1 = tranproc(x, g, range(5), ones(5))
|
assert_array_almost_equal(
|
||||||
>>> y0;y1
|
y0,
|
||||||
array([ 0.02659612, 1.00115284, 1.92872532, 2.81453257, 3.66292878])
|
np.array([0.02659612, 1.00115284, 1.92872532,
|
||||||
array([ 1.00005295, 0.9501118 , 0.90589954, 0.86643821, 0.83096482])
|
2.81453257, 3.66292878]))
|
||||||
'''
|
assert_array_almost_equal(
|
||||||
|
y1,
|
||||||
|
np.array([1.00005295, 0.9501118, 0.90589954,
|
||||||
|
0.86643821, 0.83096482]))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
run_module_suite()
|
||||||
doctest.testmod()
|
|
||||||
|
Loading…
Reference in New Issue