@@ -76,9 +76,9 @@ def smoothn(
7676 ----------
7777 Estimate the confidence bands (see Wahba 1983, Nychka 1988).
7878 Reference
79- ---------
79+ ---------
8080 Garcia D, Robust smoothing of gridded data in one and higher dimensions
81- with missing values. Computational Statistics & Data Analysis, 2010.
81+ with missing values. Computational Statistics & Data Analysis, 2010.
8282 <a
8383 href="matlab:web('http://www.biomecardio.com/pageshtm/publi/csda10.pdf')">PDF download</a>
8484 Examples:
@@ -150,7 +150,7 @@ def smoothn(
150150 See also SMOOTH, SMOOTH3, DCTN, IDCTN.
151151 -- Damien Garcia -- 2009/03, revised 2010/11
152152 Visit my <a
153- href="matlab:web('http://www.biomecardio.com/matlab/smoothn.html')">website</a> for more details about SMOOTHN
153+ href="matlab:web('http://www.biomecardio.com/matlab/smoothn.html')">website</a> for more details about SMOOTHN
154154 # Check input arguments
155155 error(nargchk(1,12,nargin));
156156 z0=None,W=None,s=None,MaxIter=100,TolZ=1e-3
@@ -171,15 +171,15 @@ def smoothn(
171171 sd = None
172172 y [mask ] = np .nan
173173
174- if sd != None :
174+ if sd is not None :
175175 sd_ = np .array (sd )
176- mask = sd > 0.0
176+ mask = sd_ > 0.0
177177 W = np .zeros_like (sd_ )
178178 W [mask ] = 1.0 / sd_ [mask ] ** 2
179179 sd = None
180180
181- if W != None :
182- W = W / W .max ()
181+ if W is not None :
182+ W = W / np .max (W )
183183
184184 sizy = y .shape
185185
@@ -197,8 +197,8 @@ def smoothn(
197197 # Smoothness parameter and weights
198198 # if s != None:
199199 # s = []
200- if W == None :
201- W = ones (sizy )
200+ if W is None :
201+ W = np . ones (sizy )
202202
203203 # if z0 == None:
204204 # z0 = y.copy()
@@ -209,7 +209,7 @@ def smoothn(
209209 # ---
210210 # Weights. Zero weights are assigned to not finite values (Inf or NaN),
211211 # (Inf/NaN values = missing data).
212- IsFinite = np .array (isfinite (y )).astype (bool )
212+ IsFinite = np .array (np . isfinite (y )).astype (bool )
213213 nof = IsFinite .sum () # number of finite elements
214214 W = W * IsFinite
215215 if any (W < 0 ):
@@ -242,15 +242,15 @@ def smoothn(
242242 # penalized least squares process.
243243 axis = tuple (np .array (axis ).flatten ())
244244 d = y .ndim
245- Lambda = zeros (sizy )
245+ Lambda = np . zeros (sizy )
246246 for i in axis :
247247 # create a 1 x d array (so e.g. [1,1] for a 2D case
248- siz0 = ones ((1 , y .ndim ), dtype = int )[0 ]
248+ siz0 = np . ones ((1 , y .ndim ), dtype = int )[0 ]
249249 siz0 [i ] = sizy [i ]
250250 # cos(pi*(reshape(1:sizy(i),siz0)-1)/sizy(i)))
251251 # (arange(1,sizy[i]+1).reshape(siz0) - 1.)/sizy[i]
252252 Lambda = Lambda + (
253- cos (pi * (arange (1 , sizy [i ] + 1 ) - 1.0 ) / sizy [i ]).reshape (siz0 )
253+ np . cos (np . pi * (np . arange (1 , sizy [i ] + 1 ) - 1.0 ) / sizy [i ]).reshape (siz0 )
254254 )
255255 # else:
256256 # Lambda = Lambda + siz0
@@ -264,7 +264,7 @@ def smoothn(
264264 # and lower bounds for h are given to avoid under- or over-smoothing. See
265265 # equation relating h to the smoothness parameter (Equation #12 in the
266266 # referenced CSDA paper).
267- N = sum (array (sizy ) != 1 )
267+ N = sum (np . array (sizy ) != 1 )
268268 # tensor rank of the y-array
269269 hMin = 1e-6
270270 hMax = 0.99
@@ -274,11 +274,11 @@ def smoothn(
274274 # (a**2 -1)/16
275275 try :
276276 sMinBnd = np .sqrt (
277- (((1 + sqrt (1 + 8 * hMax ** (2.0 / N ))) / 4.0 / hMax ** (2.0 / N )) ** 2 - 1 )
277+ (((1 + np . sqrt (1 + 8 * hMax ** (2.0 / N ))) / 4.0 / hMax ** (2.0 / N )) ** 2 - 1 )
278278 / 16.0
279279 )
280280 sMaxBnd = np .sqrt (
281- (((1 + sqrt (1 + 8 * hMin ** (2.0 / N ))) / 4.0 / hMin ** (2.0 / N )) ** 2 - 1 )
281+ (((1 + np . sqrt (1 + 8 * hMin ** (2.0 / N ))) / 4.0 / hMin ** (2.0 / N )) ** 2 - 1 )
282282 / 16.0
283283 )
284284 except :
@@ -300,7 +300,7 @@ def smoothn(
300300 z = y # InitialGuess(y,IsFinite);
301301 z [~ IsFinite ] = 0.0
302302 else :
303- z = zeros (sizy )
303+ z = np . zeros (sizy )
304304 # ---
305305 z0 = z
306306 y [~ IsFinite ] = 0
@@ -324,7 +324,7 @@ def smoothn(
324324 except :
325325 np .array ([100.0 ])
326326 else :
327- xpost = array ([np .log10 (s )])
327+ xpost = np . array ([np .log10 (s )])
328328 while RobustIterativeProcess :
329329 # --- "amount" of weights (see the function GCVscore)
330330 aow = sum (Wtot ) / noe
@@ -335,7 +335,7 @@ def smoothn(
335335 print ("tol" , tol , "nit" , nit )
336336 nit = nit + 1
337337 DCTy = dctND (Wtot * (y - z ) + z , f = dct )
338- if isauto and not remainder (log2 (nit ), 1 ):
338+ if isauto and not np . remainder (np . log2 (nit ), 1 ):
339339 # ---
340340 # The generalized cross-validation (GCV) method is used.
341341 # We seek the smoothing parameter s that minimizes the GCV
@@ -354,8 +354,8 @@ def smoothn(
354354 # only need to do it once though. nS0 is teh number of samples used
355355 if not s0 :
356356 ss = np .arange (nS0 ) * (1.0 / (nS0 - 1.0 )) * (
357- log10 (sMaxBnd ) - log10 (sMinBnd )
358- ) + log10 (sMinBnd )
357+ np . log10 (sMaxBnd ) - np . log10 (sMinBnd )
358+ ) + np . log10 (sMinBnd )
359359 g = np .zeros_like (ss )
360360 for i , p in enumerate (ss ):
361361 g [i ] = gcv (
@@ -383,7 +383,7 @@ def smoothn(
383383 fprime = None ,
384384 factr = 10.0 ,
385385 approx_grad = True ,
386- bounds = [(log10 (sMinBnd ), log10 (sMaxBnd ))],
386+ bounds = [(np . log10 (sMinBnd ), np . log10 (sMaxBnd ))],
387387 args = (Lambda , aow , DCTy , IsFinite , Wtot , y , nof , noe , smoothOrder ),
388388 )
389389 s = 10 ** xpost [0 ]
@@ -394,16 +394,16 @@ def smoothn(
394394
395395 z = RF * dctND (Gamma * DCTy , f = idct ) + (1 - RF ) * z
396396 # if no weighted/missing data => tol=0 (no iteration)
397- tol = isweighted * norm (z0 - z ) / norm (z )
397+ tol = isweighted * np . linalg . norm (z0 - z ) / np . linalg . norm (z )
398398
399399 z0 = z
400400 # re-initialization
401401 exitflag = nit < MaxIter
402402
403403 if isrobust : # -- Robust Smoothing: iteratively re-weighted process
404404 # --- average leverage
405- h = sqrt (1 + 16.0 * s )
406- h = sqrt (1 + h ) / sqrt (2 ) / h
405+ h = np . sqrt (1 + 16.0 * s )
406+ h = np . sqrt (1 + h ) / np . sqrt (2 ) / h
407407 h = h ** N
408408 # --- take robust weights into account
409409 Wtot = W * RobustWeights (y - z , IsFinite , h , weightstr )
@@ -466,11 +466,11 @@ def gcv(p, Lambda, aow, DCTy, IsFinite, Wtot, y, nof, noe, smoothOrder):
466466 # --- RSS = Residual sum-of-squares
467467 if aow > 0.9 : # aow = 1 means that all of the data are equally weighted
468468 # very much faster: does not require any inverse DCT
469- RSS = norm (DCTy * (Gamma - 1.0 )) ** 2
469+ RSS = np . linalg . norm (DCTy * (Gamma - 1.0 )) ** 2
470470 else :
471471 # take account of the weights to calculate RSS:
472472 yhat = dctND (Gamma * DCTy , f = idct )
473- RSS = norm (sqrt (Wtot [IsFinite ]) * (y [IsFinite ] - yhat [IsFinite ])) ** 2
473+ RSS = np . linalg . norm (np . sqrt (Wtot [IsFinite ]) * (y [IsFinite ] - yhat [IsFinite ])) ** 2
474474 # ---
475475 TrH = sum (Gamma )
476476 GCVscore = RSS / float (nof ) / (1.0 - TrH / float (noe )) ** 2
@@ -481,9 +481,9 @@ def gcv(p, Lambda, aow, DCTy, IsFinite, Wtot, y, nof, noe, smoothOrder):
481481# function W = RobustWeights(r,I,h,wstr)
482482def RobustWeights (r , I , h , wstr ):
483483 # weights for robust smoothing.
484- MAD = median (abs (r [I ] - median (r [I ])))
484+ MAD = np . median (abs (r [I ] - np . median (r [I ])))
485485 # median absolute deviation
486- u = abs (r / (1.4826 * MAD ) / sqrt (1 - h ))
486+ u = abs (r / (1.4826 * MAD ) / np . sqrt (1 - h ))
487487 # studentized residuals
488488 if wstr == "cauchy" :
489489 c = 2.385
@@ -498,7 +498,7 @@ def RobustWeights(r, I, h, wstr):
498498 W = (1 - (u / c ) ** 2 ) ** 2.0 * ((u / c ) < 1 )
499499 # bisquare weights
500500
501- W [isnan (W )] = 0
501+ W [np . isnan (W )] = 0
502502 return W
503503
504504
@@ -812,14 +812,14 @@ def smooth(u, mask):
812812
813813def smooth_masked_array (u ):
814814 """ Use smooth() on the masked array """
815-
815+
816816 if not isinstance (u , np .ma .MaskedArray ):
817817 raise ValueError ("Expected masked array" )
818818
819819 m = u .mask
820820
821821 # run the data through the smoothing filter a few times
822- for i in range (10 ):
822+ for i in range (10 ):
823823 smooth (u , m )
824824
825825 return np .ma .array (u , mask = m ) # put together the mask and the data
0 commit comments