Updated documentation

Added more examples
Added more tests
master
Per.Andreas.Brodtkorb 14 years ago
parent e1ed205bd2
commit 83bda6890a

File diff suppressed because it is too large Load Diff

@ -6,9 +6,8 @@ from numpy import pi, sqrt, ones, zeros #@UnresolvedImport
from scipy import integrate as intg from scipy import integrate as intg
import scipy.special.orthogonal as ort import scipy.special.orthogonal as ort
from scipy import special as sp from scipy import special as sp
import matplotlib
import pylab as plb import pylab as plb
matplotlib.interactive(True)
_POINTS_AND_WEIGHTS = {} _POINTS_AND_WEIGHTS = {}
def humps(x=None): def humps(x=None):
@ -344,7 +343,7 @@ def h_roots(n, method='newton'):
>>> import numpy as np >>> import numpy as np
>>> [x,w] = h_roots(10) >>> [x,w] = h_roots(10)
>>> np.sum(x*w) >>> np.sum(x*w)
-5.2516042729766621e-019 -5.2516042729766621e-19
See also See also
-------- --------
@ -453,7 +452,7 @@ def j_roots(n, alpha, beta, method='newton'):
-------- --------
>>> [x,w]= j_roots(10,0,0) >>> [x,w]= j_roots(10,0,0)
>>> sum(x*w) >>> sum(x*w)
2.7755575615628914e-016 2.7755575615628914e-16
See also See also
-------- --------
@ -554,7 +553,7 @@ def la_roots(n, alpha=0, method='newton'):
>>> import numpy as np >>> import numpy as np
>>> [x,w] = h_roots(10) >>> [x,w] = h_roots(10)
>>> np.sum(x*w) >>> np.sum(x*w)
-5.2516042729766621e-019 -5.2516042729766621e-19
See also See also
-------- --------
@ -939,7 +938,7 @@ def gaussq(fun, a, b, reltol=1e-3, abstol=1e-3, alpha=0, beta=0, wfun=1,
integration of x**2 from 0 to 2 and from 1 to 4 integration of x**2 from 0 to 2 and from 1 to 4
>>> from scitools import numpyutils as npt >>> from scitools import numpyutils as npt
scitools.easyviz backend is gnuplot scitools.easyviz backend is matplotlib
>>> A = [0, 1]; B = [2,4] >>> A = [0, 1]; B = [2,4]
>>> fun = npt.wrap2callable('x**2') >>> fun = npt.wrap2callable('x**2')
>>> [val1,err1] = gaussq(fun,A,B) >>> [val1,err1] = gaussq(fun,A,B)
@ -1165,22 +1164,22 @@ def quadgr(fun,a,b,abseps=1e-5):
>>> import numpy as np >>> import numpy as np
>>> Q, err = quadgr(np.log,0,1) >>> Q, err = quadgr(np.log,0,1)
>>> quadgr(np.exp,0,9999*1j*np.pi) >>> quadgr(np.exp,0,9999*1j*np.pi)
(-2.0000000000122617, 2.1933275196062141e-009) (-2.0000000000122617, 2.1933275196062141e-09)
>>> quadgr(lambda x: np.sqrt(4-x**2),0,2,1e-12) >>> quadgr(lambda x: np.sqrt(4-x**2),0,2,1e-12)
(3.1415926535897811, 1.5809575870662229e-013) (3.1415926535897811, 1.5809575870662229e-13)
>>> quadgr(lambda x: x**-0.75,0,1) >>> quadgr(lambda x: x**-0.75,0,1)
(4.0000000000000266, 5.6843418860808015e-014) (4.0000000000000266, 5.6843418860808015e-14)
>>> quadgr(lambda x: 1./np.sqrt(1-x**2),-1,1) >>> quadgr(lambda x: 1./np.sqrt(1-x**2),-1,1)
(3.141596056985029, 6.2146261559092864e-006) (3.141596056985029, 6.2146261559092864e-06)
>>> quadgr(lambda x: np.exp(-x**2),-np.inf,np.inf,1e-9) #% sqrt(pi) >>> quadgr(lambda x: np.exp(-x**2),-np.inf,np.inf,1e-9) #% sqrt(pi)
(1.7724538509055152, 1.9722334876348668e-011) (1.7724538509055152, 1.9722334876348668e-11)
>>> quadgr(lambda x: np.cos(x)*np.exp(-x),0,np.inf,1e-9) >>> quadgr(lambda x: np.cos(x)*np.exp(-x),0,np.inf,1e-9)
(0.50000000000000044, 7.3296813063450372e-011) (0.50000000000000044, 7.3296813063450372e-11)
See also See also
-------- --------
@ -1418,7 +1417,7 @@ def qdemo(f,a,b):
#[x, w]=qrule(n,1) #[x, w]=qrule(n,1)
#x = (b-a)/2*x + (a+b)/2 % Transform base points X. #x = (b-a)/2*x + (a+b)/2 % Transform base points X.
#w = (b-a)/2*w % Adjust weigths. #w = (b-a)/2*w % Adjust weigths.
#q = sum(feval(f,x).*w) #q = sum(feval(f,x)*w)
qg[k] = q qg[k] = q
eg[k] = abs(q - true_val) eg[k] = abs(q - true_val)

@ -694,8 +694,9 @@ class TimeSeries(WafoData):
Examples Examples
-------- --------
>>> import wafo.data >>> import wafo.data
>>> import wafo.objects as wo
>>> x = wafo.data.sea() >>> x = wafo.data.sea()
>>> ts = wafo.objects.mat2timeseries(x) >>> ts = wo.mat2timeseries(x)
>>> rf = ts.tocovdata(lag=150) >>> rf = ts.tocovdata(lag=150)
>>> h = rf.plot() >>> h = rf.plot()
@ -771,8 +772,9 @@ class TimeSeries(WafoData):
Example: Example:
-------- --------
>>> import wafo.data >>> import wafo.data
>>> import wafo.objects as wo
>>> x = wafo.data.sea() >>> x = wafo.data.sea()
>>> ts = mat2timeseries(x) >>> ts = wo.mat2timeseries(x)
>>> acf = ts.tocovdata(150) >>> acf = ts.tocovdata(150)
>>> h = acf.plot() >>> h = acf.plot()
''' '''
@ -814,6 +816,7 @@ class TimeSeries(WafoData):
acf = _wafocov.CovData1D(R[lags], t) acf = _wafocov.CovData1D(R[lags], t)
acf.stdev = sqrt(r_[ 0, 1 , 1 + 2 * cumsum(R[1:] ** 2)] / Ncens) acf.stdev = sqrt(r_[ 0, 1 , 1 + 2 * cumsum(R[1:] ** 2)] / Ncens)
acf.children = [WafoData(-2. * acf.stdev[lags], t), WafoData(2. * acf.stdev[lags], t)] acf.children = [WafoData(-2. * acf.stdev[lags], t), WafoData(2. * acf.stdev[lags], t)]
acf.plot_args_children = ['r:']
acf.norm = norm acf.norm = norm
return acf return acf

@ -8,4 +8,3 @@ Spectrum package in WAFO Toolbox.
from wafo.spectrum.core import SpecData1D from wafo.spectrum.core import SpecData1D
import wafo.spectrum.models import wafo.spectrum.models
import wafo.spectrum.dispersion_relation import wafo.spectrum.dispersion_relation
#from wafo.data_structures import SpecData1D

@ -1,9 +1,9 @@
from __future__ import division from __future__ import division
from scipy.misc.ppimport import ppimport from wafo.misc import meshgrid
from wafo.objects import mat2timeseries, TimeSeries from wafo.objects import mat2timeseries, TimeSeries
import warnings import warnings
import numpy as np import numpy as np
from numpy import (pi, inf, meshgrid, zeros, ones, where, nonzero, #@UnresolvedImport from numpy import (pi, inf, zeros, ones, where, nonzero, #@UnresolvedImport
flatnonzero, ceil, sqrt, exp, log, arctan2, #@UnresolvedImport flatnonzero, ceil, sqrt, exp, log, arctan2, #@UnresolvedImport
tanh, cosh, sinh, random, atleast_1d, maximum, #@UnresolvedImport tanh, cosh, sinh, random, atleast_1d, maximum, #@UnresolvedImport
minimum, diff, isnan, any, r_, conj, mod, #@UnresolvedImport minimum, diff, isnan, any, r_, conj, mod, #@UnresolvedImport
@ -945,7 +945,8 @@ class SpecData1D(WafoData):
... res = fun(x2[:,1::],axis=0) ... res = fun(x2[:,1::],axis=0)
... m = res.mean() ... m = res.mean()
... sa = res.std() ... sa = res.std()
... assert(np.abs(m-trueval)<sa) ... trueval, m, sa
... np.abs(m-trueval)<sa
waveplot(x1,'r',x2,'g',1,1) waveplot(x1,'r',x2,'g',1,1)
@ -1316,8 +1317,6 @@ class SpecData1D(WafoData):
if verbose: if verbose:
print('2nd order frequency Limits = %g,%g' % (f_limit_lo, f_limit_up)) print('2nd order frequency Limits = %g,%g' % (f_limit_lo, f_limit_up))
## if nargout>3, ## if nargout>3,
## %compute the sum and frequency effects separately ## %compute the sum and frequency effects separately
## [svec, dvec] = disufq((amp.'),w,kw,min(h,10^30),g,nmin,nmax) ## [svec, dvec] = disufq((amp.'),w,kw,min(h,10^30),g,nmin,nmax)
@ -1402,7 +1401,10 @@ class SpecData1D(WafoData):
See also See also
--------- ---------
hermitetr, ochitr, lc2tr, dat2tr transform.TrHermite
transform.TrOchi
objects.LevelCrossings.trdata
objects.TimeSeries.trdata
References: References:
----------- -----------
@ -1429,7 +1431,7 @@ class SpecData1D(WafoData):
w = ravel(self.args) w = ravel(self.args)
S = ravel(self.data) S = ravel(self.data)
if self.freqtype in ['f', 'w']: if self.freqtype in ['f', 'w']:
vari = 't' #vari = 't'
if self.freqtype == 'f': if self.freqtype == 'f':
w = 2. * pi * w w = 2. * pi * w
S = S / (2. * pi) S = S / (2. * pi)
@ -1514,7 +1516,7 @@ class SpecData1D(WafoData):
## skew = sum((6*C2+8*E2).*E)/sa^3 % skewness ## skew = sum((6*C2+8*E2).*E)/sa^3 % skewness
## kurt = 3+48*sum((C2+E2).*E2)/sa^4 % kurtosis ## kurt = 3+48*sum((C2+E2).*E2)/sa^4 % kurtosis
return output return output
def testgaussian(self, ns,test0=None, cases=100, method='nonlinear',**opt): def testgaussian(self, ns,test0=None, cases=100, method='nonlinear',verbose=False,**opt):
''' '''
TESTGAUSSIAN Test if a stochastic process is Gaussian. TESTGAUSSIAN Test if a stochastic process is Gaussian.
@ -1607,6 +1609,7 @@ class SpecData1D(WafoData):
#xs = cov2sdat(R,[ns Nstep]); #xs = cov2sdat(R,[ns Nstep]);
#[g, tmp] = dat2tr(xs,method, **opt); #[g, tmp] = dat2tr(xs,method, **opt);
#test1 = [test1; tmp(:)] #test1 = [test1; tmp(:)]
if verbose:
print('finished %d of %d ' % (ix+1,rep) ) print('finished %d of %d ' % (ix+1,rep) )
if rep>1: if rep>1:
@ -1704,6 +1707,14 @@ class SpecData1D(WafoData):
def nyquist_freq(self): def nyquist_freq(self):
""" """
Return Nyquist frequency Return Nyquist frequency
Example
-------
>>> import wafo.spectrum.models as sm
>>> Sj = sm.Jonswap(Hm0=5)
>>> S = Sj.tospecdata() #Make spectrum ob
>>> S.nyquist_freq()
3.0
""" """
return self.args[-1] return self.args[-1]
@ -1722,8 +1733,11 @@ class SpecData1D(WafoData):
Example Example
------- -------
S = jonswap >>> import wafo.spectrum.models as sm
dt = spec2dt(S) >>> Sj = sm.Jonswap(Hm0=5)
>>> S = Sj.tospecdata() #Make spectrum ob
>>> S.sampling_period()
1.0471975511965976
See also See also
''' '''
@ -1880,9 +1894,16 @@ class SpecData1D(WafoData):
Example: Example:
------- -------
S = jonswap >>> import wafo.spectrum.models as sm
[Sn,mn4] = specnorm(S) >>> Sj = sm.Jonswap(Hm0=5)
mts = spec2mom(S,2) % Should be equal to one! >>> S = Sj.tospecdata() #Make spectrum ob
>>> S.moment(2)
([1.5614600345079888, 0.95567089481941048], ['m0', 'm0tt'])
>>> Sn = S.copy(); Sn.normalize()
Now the moments should be one
>>> Sn.moment(2)
([1.0000000000000004, 0.99999999999999967], ['m0', 'm0tt'])
''' '''
mom, unused_mtext = self.moment(nr=4, even=True) mom, unused_mtext = self.moment(nr=4, even=True)
m0 = mom[0] m0 = mom[0]
@ -2018,7 +2039,6 @@ class SpecData1D(WafoData):
Examples: Examples:
--------- ---------
>>> import numpy as np
>>> import wafo.spectrum.models as sm >>> import wafo.spectrum.models as sm
>>> Sj = sm.Jonswap(Hm0=5) >>> Sj = sm.Jonswap(Hm0=5)
>>> S = Sj.tospecdata() #Make spectrum ob >>> S = Sj.tospecdata() #Make spectrum ob

@ -53,8 +53,6 @@ from numpy import (inf, atleast_1d, newaxis, any, minimum, maximum, array, #@Unr
isfinite, mod, expm1, tanh, cosh, finfo, ones, ones_like, isnan, #@UnresolvedImport isfinite, mod, expm1, tanh, cosh, finfo, ones, ones_like, isnan, #@UnresolvedImport
zeros_like, flatnonzero, sinc, hstack, vstack, real, flipud, clip) #@UnresolvedImport zeros_like, flatnonzero, sinc, hstack, vstack, real, flipud, clip) #@UnresolvedImport
from dispersion_relation import w2k from dispersion_relation import w2k
#ppimport.enable()
#_wafospectrum = ppimport.ppimport('wafo.spectrum')
from wafo.spectrum import SpecData1D from wafo.spectrum import SpecData1D
sech = lambda x: 1.0 / cosh(x) sech = lambda x: 1.0 / cosh(x)
@ -638,8 +636,6 @@ class Jonswap(ModelSpectrum):
def phi1(wi, h, g=9.81): def phi1(wi, h, g=9.81):
''' Factor transforming spectra to finite water depth spectra. ''' Factor transforming spectra to finite water depth spectra.
CALL: tr = phi1(w,h)
Input Input
----- -----
w : arraylike w : arraylike
@ -743,7 +739,8 @@ class Tmaspec(Jonswap):
phi1, phi1,
Torsethaugen Torsethaugen
References: References
----------
Buows, E., Gunther, H., Rosenthal, W., and Vincent, C.L. (1985) Buows, E., Gunther, H., Rosenthal, W., and Vincent, C.L. (1985)
'Similarity of the wind wave spectrum in finite depth water: 1 spectral form.' 'Similarity of the wind wave spectrum in finite depth water: 1 spectral form.'
J. Geophys. Res., Vol 90, No. C1, pp 975-986 J. Geophys. Res., Vol 90, No. C1, pp 975-986
@ -1361,8 +1358,8 @@ class Spreading(object):
(Hasselman: spa ~= spb) (cos-2s) [6.97 9.77 4.06 -2.3 0 1.05 3 ] (Hasselman: spa ~= spb) (cos-2s) [6.97 9.77 4.06 -2.3 0 1.05 3 ]
(Banner : spa ~= spb) (sech2) [2.61 2.28 1.3 -1.3 0.56 0.95 1.6] (Banner : spa ~= spb) (sech2) [2.61 2.28 1.3 -1.3 0.56 0.95 1.6]
Examples : Examples
--------
>>> import pylab as plb >>> import pylab as plb
>>> D = Spreading('cos2s',s_a=10.0) >>> D = Spreading('cos2s',s_a=10.0)
@ -1385,9 +1382,12 @@ class Spreading(object):
>>> plb.close('all') >>> plb.close('all')
See also mkdspec, plotspec, spec2spec See also
--------
mkdspec, plotspec, spec2spec
References References
---------
Krogstad, H.E. and Barstow, S.F. (1999) Krogstad, H.E. and Barstow, S.F. (1999)
"Directional Distributions in Ocean Wave Spectra" "Directional Distributions in Ocean Wave Spectra"
Proceedings of the 9th ISOPE Conference, Vol III, pp. 79-86 Proceedings of the 9th ISOPE Conference, Vol III, pp. 79-86

@ -159,7 +159,7 @@ class TrData(WafoData, TrCommon):
>>> g.sigma >>> g.sigma
5 5
>>> g.dat2gauss(1,2,3) >>> g.dat2gauss(1,2,3)
[array([ 0.]), array([ 0.4]), array([ 0.6])]
Check that the departure from a Gaussian model is zero Check that the departure from a Gaussian model is zero
>>> g.dist2gauss() < 1e-16 >>> g.dist2gauss() < 1e-16

@ -3,6 +3,7 @@ Transform Gaussian models
------------------------- -------------------------
TrHermite TrHermite
TrOchi TrOchi
TrLinear
''' '''
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Name: transform.models # Name: transform.models
@ -84,7 +85,10 @@ class TrHermite(TrCommon):
See also See also
-------- --------
spec2skew, ochitr, lc2tr, dat2tr SpecData1d.stats_nl
wafo.transform.TrOchi
wafo.objects.LevelCrossings.trdata
wafo.objects.TimeSeries.trdata
References References
---------- ----------
@ -306,6 +310,11 @@ class TrLinear(TrCommon):
See also See also
-------- --------
TrOchi
TrHermite
SpecData1D.stats_nl
LevelCrossings.trdata
TimeSeries.trdata
spec2skew, ochitr, lc2tr, dat2tr spec2skew, ochitr, lc2tr, dat2tr
""" """

@ -0,0 +1,47 @@
from wafo.transform.models import TrHermite, TrOchi, TrLinear
def test_trhermite():
'''
>>> std = 7./4
>>> g = TrHermite(sigma=std, ysigma=std)
>>> g.dist2gauss()
3.9858776379926808
>>> g.mean
0.0
>>> g.sigma
1.75
>>> g.dat2gauss([0,1,2,3])
array([ 0.04654321, 1.03176393, 1.98871279, 2.91930895])
'''
def test_trochi():
'''
>>> std = 7./4
>>> g = TrOchi(sigma=std, ysigma=std)
>>> g.dist2gauss()
5.9322684525265501
>>> g.mean
0.0
>>> g.sigma
1.75
>>> g.dat2gauss([0,1,2,3])
array([ 6.21927960e-04, 9.90237621e-01, 1.96075606e+00,
2.91254576e+00])
'''
def test_trlinear():
'''
>>> std = 7./4
>>> g = TrLinear(sigma=std, ysigma=std)
>>> g.dist2gauss()
0.0
>>> g.mean
0.0
>>> g.sigma
1.75
>>> g.dat2gauss([0,1,2,3])
array([ 0., 1., 2., 3.])
'''
if __name__=='__main__':
import doctest
doctest.testmod()

@ -0,0 +1,32 @@
from wafo.transform import TrData
def test_trdata():
'''
Construct a linear transformation model
>>> import numpy as np
>>> sigma = 5; mean = 1
>>> u = np.linspace(-5,5); x = sigma*u+mean; y = u
>>> g = TrData(y,x)
>>> g.mean
array([ 1.])
>>> g.sigma
array([ 5.])
>>> g = TrData(y,x,mean=1,sigma=5)
>>> g.mean
1
>>> g.sigma
5
>>> g.dat2gauss(1,2,3)
[array([ 0.]), array([ 0.4]), array([ 0.6])]
>>> g.dat2gauss([0,1,2,3])
array([-0.2, 0. , 0.2, 0.4])
Check that the departure from a Gaussian model is zero
>>> g.dist2gauss() < 1e-16
True
'''
if __name__=='__main__':
import doctest
doctest.testmod()
Loading…
Cancel
Save