Fixed bug

master
Per A Brodtkorb 7 years ago
parent 19e4af8f3e
commit 7fcc4d18bd

@ -989,8 +989,8 @@ def findextrema(x):
findcross findcross
crossdef crossdef
''' '''
x1 = np.atleast_1d(x) dx = np.atleast_1d(diff(x))
return findcross(diff(x1), 0.0) + 1 return findcross(dx, 0.0) + 1
def findpeaks(data, n=2, min_h=None, min_p=0.0): def findpeaks(data, n=2, min_h=None, min_p=0.0):

@ -396,8 +396,8 @@ class LevelCrossings(PlotData):
>>> alpha2 = Se.characteristic('alpha')[0] >>> alpha2 = Se.characteristic('alpha')[0]
>>> np.round(alpha2*10) >>> np.round(alpha2*10)
array([ 7.]) array([ 7.])
>>> np.abs(alpha-alpha2)<0.03 >>> np.allclose(alpha, alpha2, atol=0.03)
array([ True], dtype=bool) True
>>> lc2 = ts2.turning_points().cycle_pairs().level_crossings() >>> lc2 = ts2.turning_points().cycle_pairs().level_crossings()
@ -814,108 +814,108 @@ class CyclePairs(PlotData):
return LevelCrossings(dcount, levels, mean=self.mean, sigma=self.sigma, return LevelCrossings(dcount, levels, mean=self.mean, sigma=self.sigma,
ylab=ylab, intensity=intensity) ylab=ylab, intensity=intensity)
def _smoothcmat(F, method=1, h=None, NOsubzero=0, alpha=0.5): # def _smoothcmat(self, F, method=1, h=None, NOsubzero=0, alpha=0.5):
""" # """
SMOOTHCMAT Smooth a cycle matrix using (adaptive) kernel smoothing # SMOOTHCMAT Smooth a cycle matrix using (adaptive) kernel smoothing
#
CALL: Fsmooth = smoothcmat(F,method); # CALL: Fsmooth = smoothcmat(F,method);
Fsmooth = smoothcmat(F,method,[],NOsubzero); # Fsmooth = smoothcmat(F,method,[],NOsubzero);
Fsmooth = smoothcmat(F,2,h,NOsubzero,alpha); # Fsmooth = smoothcmat(F,2,h,NOsubzero,alpha);
#
Input: # Input:
F = Cycle matrix. [nxn] # F = Cycle matrix. [nxn]
method = 1: Kernel estimator (constant bandwidth). (Default) # method = 1: Kernel estimator (constant bandwidth). (Default)
2: Adaptiv kernel estimator (local bandwidth). # 2: Adaptiv kernel estimator (local bandwidth).
h = Bandwidth (Optional, Default='automatic choice') # h = Bandwidth (Optional, Default='automatic choice')
NOsubzero = Number of subdiagonals that are zero # NOsubzero = Number of subdiagonals that are zero
(Optional, Default = 0, only the diagonal is zero) # (Optional, Default = 0, only the diagonal is zero)
alpha = Parameter for method (2) (Optional, Default=0.5). # alpha = Parameter for method (2) (Optional, Default=0.5).
A number between 0 and 1. # A number between 0 and 1.
alpha=0 implies constant bandwidth (method 1). # alpha=0 implies constant bandwidth (method 1).
alpha=1 implies most varying bandwidth. # alpha=1 implies most varying bandwidth.
#
Output: # Output:
F = Smoothed cycle matrix. [nxn] # F = Smoothed cycle matrix. [nxn]
h = Selected bandwidth. # h = Selected bandwidth.
#
See also # See also
cc2cmat, tp2rfc, tp2mm, dat2tp # cc2cmat, tp2rfc, tp2mm, dat2tp
""" # """
aut_h = h is None # aut_h = h is None
if method not in [1, 2]: # if method not in [1, 2]:
raise ValueError('Input argument "method" should be 1 or 2') # raise ValueError('Input argument "method" should be 1 or 2')
#
n = len(F) # Size of matrix # n = len(F) # Size of matrix
N = np.sum(F) # Total number of cycles # N = np.sum(F) # Total number of cycles
#
Fsmooth = np.zeros((n, n)) # Fsmooth = np.zeros((n, n))
#
if method == 1 or method == 2: # Kernel estimator # if method == 1 or method == 2: # Kernel estimator
#
d = 2 # 2-dim # d = 2 # 2-dim
x = np.arange(n) # x = np.arange(n)
I, J = np.meshgrid(x, x) # I, J = np.meshgrid(x, x)
#
# Choosing bandwidth # # Choosing bandwidth
# This choice is optimal if the sample is from a normal distr. # # This choice is optimal if the sample is from a normal distr.
# The normal bandwidth usualy oversmooths, # # The normal bandwidth usualy oversmooths,
# therefore we choose a slightly smaller bandwidth # # therefore we choose a slightly smaller bandwidth
#
if aut_h == 1: # if aut_h == 1:
h_norm = smoothcmat_hnorm(F, NOsubzero) # h_norm = smoothcmat_norm(F, NOsubzero)
h = 0.7 * h_norm # Don't oversmooth # h = 0.7 * h_norm # Don't oversmooth
#
# h0 = N^(-1/(d+4)); # # h0 = N^(-1/(d+4));
# FF = F+F'; # # FF = F+F';
# mean_F = sum(sum(FF).*(1:n))/N/2; # # mean_F = sum(sum(FF).*(1:n))/N/2;
# s2 = sum(sum(FF).*((1:n)-mean_F).^2)/N/2; # # s2 = sum(sum(FF).*((1:n)-mean_F).^2)/N/2;
# s = sqrt(s2); % Mean of std in each direction # # s = sqrt(s2); % Mean of std in each direction
# h_norm = s*h0; % Optimal for Normal distr. # # h_norm = s*h0; % Optimal for Normal distr.
# h = h_norm; % Test # # h = h_norm; % Test
# endif # # endif
#
# Calculating kernel estimate # # Calculating kernel estimate
# Kernel: 2-dim normal density function # # Kernel: 2-dim normal density function
#
for i in range(n - 1): # for i in range(n - 1):
for j in range(i + 1, n): # for j in range(i + 1, n):
if F[i, j] != 0: # if F[i, j] != 0:
F1 = exp(-1 / (2 * h**2) * ((I - i)**2 + (J - j)**2)) # Gaussian kernel # F1 = exp(-1 / (2 * h**2) * ((I - i)**2 + (J - j)**2)) # Gaussian kernel
F1 = F1 + F1.T # Mirror kernel in diagonal # F1 = F1 + F1.T # Mirror kernel in diagonal
F1 = np.triu(F1, 1 + NOsubzero) # Set to zero below and on diagonal # F1 = np.triu(F1, 1 + NOsubzero) # Set to zero below and on diagonal
F1 = F[i, j] * F1 / np.sum(F1) # Normalize # F1 = F[i, j] * F1 / np.sum(F1) # Normalize
Fsmooth = Fsmooth + F1 # Fsmooth = Fsmooth + F1
# endif # # endif
# endfor # # endfor
# endfor # # endfor
# endif method 1 or 2 # # endif method 1 or 2
#
if method == 2: # if method == 2:
Fpilot = Fsmooth / N # Fpilot = Fsmooth / N
Fsmooth = np.zeros(n, n) # Fsmooth = np.zeros(n, n)
[I1, I2] = find(F > 0) # [I1, I2] = find(F > 0)
logg = 0 # logg = 0
for i in range(len(I1)): # =1:length(I1): # for i in range(len(I1)): # =1:length(I1):
logg = logg + F(I1[i], I2[i]) * log(Fpilot(I1[i], I2[i])) # logg = logg + F(I1[i], I2[i]) * log(Fpilot(I1[i], I2[i]))
# endfor # # endfor
g = np.exp(logg / N) # g = np.exp(logg / N)
_lamda = (Fpilot / g)**(-alpha) # _lamda = (Fpilot / g)**(-alpha)
#
for i in range(n - 1): # = 1:n-1 # for i in range(n - 1): # = 1:n-1
for j in range(i + 1, n): # = i+1:n # for j in range(i + 1, n): # = i+1:n
if F[i, j] != 0: # if F[i, j] != 0:
hi = h * _lamda[i, j] # hi = h * _lamda[i, j]
F1 = np.exp(-1 / (2 * hi**2) * ((I - i)**2 + (J - j)**2)) # Gaussian kernel # F1 = np.exp(-1 / (2 * hi**2) * ((I - i)**2 + (J - j)**2)) # Gaussian kernel
F1 = F1 + F1.T # Mirror kernel in diagonal # F1 = F1 + F1.T # Mirror kernel in diagonal
F1 = np.triu(F1, 1 + NOsubzero) # Set to zero below and on diagonal # F1 = np.triu(F1, 1 + NOsubzero) # Set to zero below and on diagonal
F1 = F[i, j] * F1 / np.sum(F1) # Normalize # F1 = F[i, j] * F1 / np.sum(F1) # Normalize
Fsmooth = Fsmooth + F1 # Fsmooth = Fsmooth + F1
# endif # # endif
# endfor # # endfor
# endfor # # endfor
#
# endif method 2 # # endif method 2
return Fsmooth, h # return Fsmooth, h
def cycle_matrix(self, param=(), ddef=1, method=0, h=None, NOsubzero=0, alpha=0.5): def cycle_matrix(self, param=(), ddef=1, method=0, h=None, NOsubzero=0, alpha=0.5):
"""CC2CMAT Calculates the cycle count matrix from a cycle count. """CC2CMAT Calculates the cycle count matrix from a cycle count.

Loading…
Cancel
Save