geogg122
geogg122 copied to clipboard
length of x0 != length of bounds
Hello,
I'm working on a programm which needs to use many times a smoothing operation. I took the advice of using smoothn and included it, but it randomly raises the error length of x0 != length of bounds
. Randomly means at different points of iteration each time I launch the programm, even if I don't call any random function... Sometimes it doesn't raise any error.
I feed the procedure with no more than a single-dimension np.array, with only float values, and I set the value isrobust = True
. Here is the complete error :
File "...\analyseurv11.1.py", line 403, in smoothn
args=(Lambda, aow, DCTy, IsFinite, Wtot, y, nof, noe, smoothOrder))
File "...\anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py", line 199, in fmin_l_bfgs_b
**opts)
File "...\anaconda3\lib\site-packages\scipy\optimize\lbfgsb.py", line 267, in _minimize_lbfgsb
raise ValueError('length of x0 != length of bounds')
ValueError: length of x0 != length of bounds
Do you know where and what the problem could be ? If any further information could help, I would be glad to give it. Thanks !
Can you send eg dataset and cmd and specify which version of snoothn (link) I’m afraid there are multiple versions we need to tidy up.
Lewis
Here is one array which caused smoothn to stop :
A = np.array([0.98995828,0.99198168,0.99152638,1.00859731,1.00141969,0.99200759,1.01817028,1.01285904,0.9928275,0.99749446,0.98429146,0.9958292,0.99312379,1.00706916,1.0143851,1.00023936,1.00226406,1.00235472,0.99281227,0.99561885,1.0112607,0.99840608,0.99177887,1.00734221,0.98707976,0.99835478,0.98988786,1.00832306,1.01032195,0.98680792,1.0101957,1.00222628,1.00633978,1.00296833,1.00319181,1.00176974,1.00194725,0.99991647,1.00709695,1.00205083,1.01166178,0.9943371,1.0075763,0.99350578,1.00996582,0.97408382,1.00390002,0.98337256,1.01344012,0.99751394,1.01640771,0.99282777,1.,0.99591555,0.99473071,0.99854936,1.01252626,0.99796799,1.01854632,1.00573901,1.02625201,0.99771241,0.99272678,1.00027505,1.00436897,0.97615321,0.99466163,1.00185199,0.98767986,0.9978664,0.99393643,1.0199506,0.99416738,0.98259949,1.00177499,0.98120451,1.00121547,0.99307413,1.0093023,0.98914166,1.00938402,0.98634995,1.00256478,1.00754708,0.98062691,0.98167024,1.01094675,0.99513754,1.01617415,1.00658529,1.00829196,1.00357285,0.9959327,0.99778838,1.00154792,1.00698366,0.99100209,0.99026061,1.03146068,1.02630266,1.0038577,0.9904123,0.99963774,1.01733569,1.0016439,1.02144896,0.99250695,1.00387687,0.99913341,0.98995279,0.99941509,1.01174223,1.02927348,0.99841681,1.00385497,0.99582836,1.0089839,0.99480449,1.03001362,1.01651657,0.97844824,0.99056732,0.99205773,0.99859452,0.98486029,1.00998909,0.99323192,1.02482475,0.99414407,1.007263,0.98086587,0.9795539,1.00205789,0.98374921,0.98364449,0.99662116,1.00319638,1.01339668,0.99762841,1.00585772,0.99351885,1.02042228,0.99098382,1.0093932,1.00956314,1.00509008,0.99586688,0.98353405,0.99358174,0.98645831,0.99486538,0.99260983,1.01367608,1.02075753,0.99688676,0.99544651,1.0080879,1.00324265,1.00315116,1.00402432,0.99858136,1.00843228,1.01985105,0.99870034,0.99583121,1.00213633,1.00936204,1.01327235,0.99791084,1.00370299,1.0004356,1.03397202,1.01224443,1.02311162,0.99822839,0.99342336,0.99552825,0.98714092,1.01098118,0.99372479,0.99789137])
With the command B = smoothn(A)[0]
.
But when I tried again the program with this array, everything worked properly... About the version, I don't have any clue, sorry... All I can is giving you the code, or at least the first comment section :
'''
function [z,s,exitflag,Wtot] = smoothn(varargin)
SMOOTHN Robust spline smoothing for 1-D to N-D data.
SMOOTHN provides a fast, automatized and robust discretized smoothing
spline for data of any dimension.
Z = SMOOTHN(Y) automatically smoothes the uniformly-sampled array Y. Y
can be any N-D noisy array (time series, images, 3D data,...). Non
finite data (NaN or Inf) are treated as missing values.
Z = SMOOTHN(Y,S) smoothes the array Y using the smoothing parameter S.
S must be a real positive scalar. The larger S is, the smoother the
output will be. If the smoothing parameter S is omitted (see previous
option) or empty (i.e. S = []), it is automatically determined using
the generalized cross-validation (GCV) method.
Z = SMOOTHN(Y,W) or Z = SMOOTHN(Y,W,S) specifies a weighting array W of
real positive values, that must have the same size as Y. Note that a
nil weight corresponds to a missing value.
Robust smoothing
----------------
Z = SMOOTHN(...,'robust') carries out a robust smoothing that minimizes
the influence of outlying data.
[Z,S] = SMOOTHN(...) also returns the calculated value for S so that
you can fine-tune the smoothing subsequently if needed.
An iteration process is used in the presence of weighted and/or missing
values. Z = SMOOTHN(...,OPTION_NAME,OPTION_VALUE) smoothes with the
termination parameters specified by OPTION_NAME and OPTION_VALUE. They
can contain the following criteria:
-----------------
TolZ: Termination tolerance on Z (default = 1e-3)
TolZ must be in ]0,1[
MaxIter: Maximum number of iterations allowed (default = 100)
Initial: Initial value for the iterative process (default =
original data)
-----------------
Syntax: [Z,...] = SMOOTHN(...,'MaxIter',500,'TolZ',1e-4,'Initial',Z0);
[Z,S,EXITFLAG] = SMOOTHN(...) returns a boolean value EXITFLAG that
describes the exit condition of SMOOTHN:
1 SMOOTHN converged.
0 Maximum number of iterations was reached.
Class Support
-------------
Input array can be numeric or logical. The returned array is of class
double.
Notes
-----
The N-D (inverse) discrete cosine transform functions <a
href="matlab:web('http://www.biomecardio.com/matlab/dctn.html')"
>DCTN</a> and <a
href="matlab:web('http://www.biomecardio.com/matlab/idctn.html')"
>IDCTN</a> are required.
To be made
----------
Estimate the confidence bands (see Wahba 1983, Nychka 1988).
Reference
---------
Garcia D, Robust smoothing of gridded data in one and higher dimensions
with missing values. Computational Statistics & Data Analysis, 2010.
<a
href="matlab:web('http://www.biomecardio.com/pageshtm/publi/csda10.pdf')">PDF download</a>
Examples:
--------
# 1-D example
x = linspace(0,100,2**8);
y = cos(x/10)+(x/50)**2 + randn(size(x))/10;
y[[70, 75, 80]] = [5.5, 5, 6];
z = smoothn(y); # Regular smoothing
zr = smoothn(y,'robust'); # Robust smoothing
subplot(121), plot(x,y,'r.',x,z,'k','LineWidth',2)
axis square, title('Regular smoothing')
subplot(122), plot(x,y,'r.',x,zr,'k','LineWidth',2)
axis square, title('Robust smoothing')
# 2-D example
xp = 0:.02:1;
[x,y] = meshgrid(xp);
f = exp(x+y) + sin((x-2*y)*3);
fn = f + randn(size(f))*0.5;
fs = smoothn(fn);
subplot(121), surf(xp,xp,fn), zlim([0 8]), axis square
subplot(122), surf(xp,xp,fs), zlim([0 8]), axis square
# 2-D example with missing data
n = 256;
y0 = peaks(n);
y = y0 + rand(size(y0))*2;
I = randperm(n^2);
y(I(1:n^2*0.5)) = NaN; # lose 1/2 of data
y(40:90,140:190) = NaN; # create a hole
z = smoothn(y); # smooth data
subplot(2,2,1:2), imagesc(y), axis equal off
title('Noisy corrupt data')
subplot(223), imagesc(z), axis equal off
title('Recovered data ...')
subplot(224), imagesc(y0), axis equal off
title('... compared with original data')
# 3-D example
[x,y,z] = meshgrid(-2:.2:2);
xslice = [-0.8,1]; yslice = 2; zslice = [-2,0];
vn = x.*exp(-x.^2-y.^2-z.^2) + randn(size(x))*0.06;
subplot(121), slice(x,y,z,vn,xslice,yslice,zslice,'cubic')
title('Noisy data')
v = smoothn(vn);
subplot(122), slice(x,y,z,v,xslice,yslice,zslice,'cubic')
title('Smoothed data')
# Cardioid
t = linspace(0,2*pi,1000);
x = 2*cos(t).*(1-cos(t)) + randn(size(t))*0.1;
y = 2*sin(t).*(1-cos(t)) + randn(size(t))*0.1;
z = smoothn(complex(x,y));
plot(x,y,'r.',real(z),imag(z),'k','linewidth',2)
axis equal tight
# Cellular vortical flow
[x,y] = meshgrid(linspace(0,1,24));
Vx = cos(2*pi*x+pi/2).*cos(2*pi*y);
Vy = sin(2*pi*x+pi/2).*sin(2*pi*y);
Vx = Vx + sqrt(0.05)*randn(24,24); # adding Gaussian noise
Vy = Vy + sqrt(0.05)*randn(24,24); # adding Gaussian noise
I = randperm(numel(Vx));
Vx(I(1:30)) = (rand(30,1)-0.5)*5; # adding outliers
Vy(I(1:30)) = (rand(30,1)-0.5)*5; # adding outliers
Vx(I(31:60)) = NaN; # missing values
Vy(I(31:60)) = NaN; # missing values
Vs = smoothn(complex(Vx,Vy),'robust'); # automatic smoothing
subplot(121), quiver(x,y,Vx,Vy,2.5), axis square
title('Noisy velocity field')
subplot(122), quiver(x,y,real(Vs),imag(Vs)), axis square
title('Smoothed velocity field')
See also SMOOTH, SMOOTH3, DCTN, IDCTN.
-- Damien Garcia -- 2009/03, revised 2010/11
Visit my <a
href="matlab:web('http://www.biomecardio.com/matlab/smoothn.html')">website</a> for more details about SMOOTHN
# Check input arguments
error(nargchk(1,12,nargin));
z0=None,W=None,s=None,MaxIter=100,TolZ=1e-3
'''
I know this isn't much information, and I suppose the array will work properly ; though I hope it can help... As the errors aren't twice at the same place in the iterations, could we suppose that there is some memory of the iterations in the program ? A variable which should reset, for example...