python - Performing spline on 3D array -
i have been using python porting of internal company tools matlab python.
i able succesfully port of stuck in particular point. on appreciated.
in matlab, there cubic interpolation taking place reads following: z =spline(a,b,c) a= 1x100 array, b= 2x2x100 array , c =1x200 array.
so, trying interpolate existing 2x2 data of 100 points ( contained in b) 200 points ( in c)
matlab able smoothly perform operation resultant z being 2x2x200 array.
how achieve same in python since im unable find equivalent spline function deals 3d arrays ( found 1d array based cubic interpolation these not useful usecase)
any appreciated since im not knowledgable on interpolation techniques able derieve custom method in python myself.
edited on nov 15:
thank comments. since company internal code, im unable reproduce same here. however, putting out sample of im trying achieve below:
matlab code:
data1=zeros(2,2,5) set1=zeros(1,5) set2=zeros(1,10) data1(1,1,1)=10 data1(1,2,1)=11 data1(2,1,1)=12 data1(2,2,1)=13 % data1(1,1,2)=10 data1(1,2,2)=11 data1(2,1,2)=12 data1(2,2,2)=13 % data1(1,1,3)=10 data1(1,2,3)=11 data1(2,1,3)=12 data1(2,2,3)=13 % data1(1,1,4)=10 data1(1,2,4)=11 data1(2,1,4)=12 data1(2,2,4)=13 data1(1,1,5)=10 data1(1,2,5)=11 data1(2,1,5)=12 data1(2,2,5)=13 set1(1,:)=[2,4,6,8,10] set2(1,:)=[1,2,3,4,5,6,7,8,9,10] out=spline(set1,data1,set2) this displays result expected data1 2x2x5 array set1 having 1x5 values has interpolated set2 having 1x10 values. resultant interpolated data, out, 2x2x10 array.
>> size(data1) ans = 2 2 5 >> size(out) ans = 2 2 10 however, im unable replicate same behaviour python 3d array ( 1d arrays fine univariate spline functions or similar ones).
the python equivalent code above is:
#!/user/bin/python import numpy np import scipy scipy.interpolate import griddata data=np.zeros((2,2,5)) set1=[2,4,6,8,10] set2=[1,2,3,4,5,6,7,8,9,10] in range(0,5): data[0,0,i]=10 data[0,1,i]=11 data[1,0,i]=12 data[1,1,i]=13 print data.shape print data[:,:,1] print set1 print set2 out1=np.interp(set1,data,set2) out2=griddata(set1,data,set2,method="cubic") however, errors trying run np.interp or griddata are:
traceback (most recent call last): line 22, in <module> out=griddata(set1,data,set2,method="cubic") file "/user/python-2.6.6/lib/python2.6/site-packages/scipy/interpolate/ndgriddata.py", line 176, in griddata fill_value=fill_value) file "/user/python-2.6.6/lib/python2.6/site-packages/scipy/interpolate/interpolate.py", line 274, in __init__ self._spline = splmake(x,oriented_y,order=order) file "/user/python-2.6.6/lib/python2.6/site-packages/scipy/interpolate/interpolate.py", line 771, in splmake coefs = func(xk, yk, order, conds, b) file "/user/python-2.6.6/lib/python2.6/site-packages/scipy/interpolate/interpolate.py", line 506, in _find_smoothest return _dot0(tmp, yk) file "/user/python-2.6.6/lib/python2.6/site-packages/scipy/interpolate/interpolate.py", line 480, in _dot0 return dot(a, b.transpose(axes)) valueerror: objects not aligned and
file "/user/python_scripts/ecps_functions_definitions.py", line 958, in trise_check out2=np.interp(set1,data,set2) file "/user/python-2.6.6/lib/python2.6/site-packages/numpy/lib/function_base.py", line 1053, in interp return compiled_interp(x, xp, fp, left, right) valueerror: object deep desired array any function able replicate spline of matlab 3d array helpful me move ahead code ( since interpolation not main functionality of code , large code ahead held due function not working)
Comments
Post a Comment