Related
I am trying to plot a 2D heat map of the function/surface but it is throwing me the type error which I am unable to resolve..
from numbers import Real
from numpy.lib.type_check import real
x = np.linspace(-2,2, num=40, endpoint=True, retstep=False, dtype=None, axis=0)
y = np.linspace(-2,2, num=40, endpoint=True, retstep=False, dtype=None, axis=0)
`
def goldstein_func(x,y):
z = (1+(x+y+1)**2*(19-14*x+3*x**2-14*y+6*x*y+3*y**2))*(30+(2*x-3*y)**2*(18-32*x+12*x**2+48*y-36*x*y+27*y**2))
m = np.array(x,y)
plt.imshow(goldstein_func(x,y), 4)
plt.show()
**The above code throw in an error below:
TypeError Traceback (most recent call last)
in
----> 2 plt.imshow(goldstein_func(x,y), 4)
3 plt.show()
in goldstein_func(x, y)
10 z = (1+(x+y+1)**2*(19-14*x+3*x**2-14*y+6*x*y+3*y**2))*(30+(2*x-3*y)**2*(18-32*x+12*x**2+48*y-36*x*y+27*y**2))
---> 11 m = np.array(x,y)
TypeError: Cannot construct a dtype from an array**
I tried to fix the type-error
It appears that an issue is happening when attempting to construct a NumPy array with np.array(x, y) inside the goldstein_func() function. The np.array() function needs one argument that symbolizes the components of the array or several arguments that symbolizes multiple arrays to be combined. Nevertheless, in your code, you're supplying two independent arguments, x and y, which is producing the TypeError.
To fix the mistake, you can use the np.meshgrid function to create 2-D arrays from the 1-D arrays x and y, and then pass these arrays into the goldstein_func to calculate the matching z values. Here's the adjusted code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, num=40, endpoint=True)
y = np.linspace(-2, 2, num=40, endpoint=True)
X, Y = np.meshgrid(x, y)
def goldstein_func(x, y):
z = (1+(x+y+1)**2*(19-14*x+3*x**2-14*y+6*x*y+3*y**2))*(30+(2*x-3*y)**2*(18-32*x+12*x**2+48*y-36*x*y+27*y**2))
return z
Z = goldstein_func(X, Y)
plt.imshow(Z, cmap='hot', extent=(-2, 2, -2, 2))
plt.show()
I hope this helps you out.
Here's the code:
importing stuff
import numpy as np
import sympy as sp
from sympy import symbols, diff
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
defining function and it's derivative
t = symbols('t')
B = sp.exp(t)
f = sp.diff(B, t)
print(f)
plot settings
fig1 = plt.figure(1)
ax1 = fig1.gca(projection='3d')
x, t, z = np.meshgrid(np.linspace(-4, 4, 15),
np.linspace(0, 4, 10),
np.linspace(-4, 4, 15))
u = 0
v = 0
ax1.quiver(x, t, z, u, v, f, length = 0.07)
plt.show()
I keep getting the "can't convert to float" error, the program does print the derivative though and plots too when used without the derivative...
Here's the traceback:
In [8]: ax1.quiver(x, t, z, u, v, f, length = 0.07)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-66e3c3d2c6bf> in <module>
----> 1 ax1.quiver(x, t, z, u, v, f, length = 0.07)
/usr/local/lib/python3.6/dist-packages/mpl_toolkits/mplot3d/axes3d.py in quiver(self, length, arrow_length_ratio, pivot, normalize, *args, **kwargs)
2620
2621 XYZ = np.column_stack(input_args[:3])
-> 2622 UVW = np.column_stack(input_args[3:argi]).astype(float)
2623
2624 # Normalize rows of UVW
/usr/local/lib/python3.6/dist-packages/sympy/core/expr.py in __float__(self)
278 if result.is_number and result.as_real_imag()[1]:
279 raise TypeError("can't convert complex to float")
--> 280 raise TypeError("can't convert expression to float")
281
282 def __complex__(self):
TypeError: can't convert expression to float
Apparently it's trying to turn the u,v,f arguments into a numeric array.
In [9]: np.column_stack([u,v,f])
Out[9]: array([[0, 0, exp(t)]], dtype=object)
float(exp(t)) doesn't work. f is a sympy expression; you can't turn it into a number.
The underlying problem is that you are trying to use a numeric plotting function to plot a symbolic expression. sympy and numpy don't work together seamlessly. Either use sympy's plotting tools, or convert your expression to a numeric one - an array of numbers evaluated at some points.
===
ax1.quiver(x, t, z, u, v, np.exp(t), length = 0.07)
does run. np.exp(t) is an array, the same size as t.
===
Sympy plotting:
In [12]: from sympy.plotting import plot
In [13]: plot(f)
Out[13]: <sympy.plotting.plot.Plot at 0x7f02b0f99400>
https://docs.sympy.org/latest/modules/plotting.html
===
https://docs.sympy.org/latest/tutorial/basic_operations.html#lambdify
lambdify is a sympy tool for bridging the gap between sympy and numpy. It generates a numpy (as default) function from the sympy expression:
In [27]: fn=lambdify(symbols('t'),f)
In [28]: fn
Out[28]: <function _lambdifygenerated(t)>
In [29]: fn(t).shape # evaluate a the 3d array `t`
Out[29]: (10, 15, 15)
Now we have numeric array that works in quiver:
In [36]: ax1.quiver(x, t, z, u, v, fn(t), length = 0.07)
Out[36]: <mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x7f02b0f24e80>
I am trying to fit a smoothing B-spline to some data and I found this very helpful post on here. However, I not only need the spline, but also its derivatives, so I tried to add the following code to the example:
tck_der = interpolate.splder(tck, n=1)
x_der, y_der, z_der = interpolate.splev(u_fine, tck_der)
For some reason this does not seem to work due to some data type issues. I get the following traceback:
Traceback (most recent call last):
File "interpolate_point_trace.py", line 31, in spline_example
tck_der = interpolate.splder(tck, n=1)
File "/home/user/anaconda3/lib/python3.7/site-packages/scipy/interpolate/fitpack.py", line 657, in splder
return _impl.splder(tck, n)
File "/home/user/anaconda3/lib/python3.7/site-packages/scipy/interpolate/_fitpack_impl.py", line 1206, in splder
sh = (slice(None),) + ((None,)*len(c.shape[1:]))
AttributeError: 'list' object has no attribute 'shape'
The reason for this seems to be that the second argument of the tck tuple contains a list of numpy arrays. I thought turning the input data to be a numpy array as well would help, but it does not change the data types of tck.
Does this behavior reflect an error in scipy, or is the input malformed?
I tried manually turning the list into an array:
tck[1] = np.array(tck[1])
but this (which didn't surprise me) also gave an error:
ValueError: operands could not be broadcast together with shapes (0,8) (7,1)
Any ideas of what the problem could be? I have used scipy before and on 1D splines the splder function works just fine, so I assume it has something to do with the spline being a line in 3D.
------- edit --------
Here is a minimum working example:
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from mpl_toolkits.mplot3d import Axes3D
total_rad = 10
z_factor = 3
noise = 0.1
num_true_pts = 200
s_true = np.linspace(0, total_rad, num_true_pts)
x_true = np.cos(s_true)
y_true = np.sin(s_true)
z_true = s_true / z_factor
num_sample_pts = 80
s_sample = np.linspace(0, total_rad, num_sample_pts)
x_sample = np.cos(s_sample) + noise * np.random.randn(num_sample_pts)
y_sample = np.sin(s_sample) + noise * np.random.randn(num_sample_pts)
z_sample = s_sample / z_factor + noise * np.random.randn(num_sample_pts)
tck, u = interpolate.splprep([x_sample, y_sample, z_sample], s=2)
x_knots, y_knots, z_knots = interpolate.splev(tck[0], tck)
u_fine = np.linspace(0, 1, num_true_pts)
x_fine, y_fine, z_fine = interpolate.splev(u_fine, tck)
# this is the part of the code I inserted: the line under this causes the crash
tck_der = interpolate.splder(tck, n=1)
x_der, y_der, z_der = interpolate.splev(u_fine, tck_der)
# end of the inserted code
fig2 = plt.figure(2)
ax3d = fig2.add_subplot(111, projection='3d')
ax3d.plot(x_true, y_true, z_true, 'b')
ax3d.plot(x_sample, y_sample, z_sample, 'r*')
ax3d.plot(x_knots, y_knots, z_knots, 'go')
ax3d.plot(x_fine, y_fine, z_fine, 'g')
fig2.show()
plt.show()
Stumbled into the same problem...
I circumvented the error by using interpolate.splder(tck, n=1) and instead used interpolate.splev(spline_ev, tck, der=1) which returns the derivatives at the points spline_ev (see Scipy Doku).
If you need the spline I think you can then use interpolate.splprep() again.
In total something like:
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
points = np.random.rand(10,2) * 10
(tck, u), fp, ier, msg = interpolate.splprep(points.T, s=0, k=3, full_output=True)
spline_ev = np.linspace(0.0, 1.0, 100, endpoint=True)
spline_points = interpolate.splev(spline_ev, tck)
# Calculate derivative
spline_der_points = interpolate.splev(spline_ev, tck, der=1)
spline_der = interpolate.splprep(spline_der_points.T, s=0, k=3, full_output=True)
# Plot the data and derivative
fig = plt.figure()
plt.plot(points[:,0], points[:,1], '.-', label="points")
plt.plot(spline_points[0], spline_points[1], '.-', label="tck")
plt.plot(spline_der_points[0], spline_der_points[1], '.-', label="tck_der")
# Show tangent
plt.arrow(spline_points[0][23]-spline_der_points[0][23], spline_points[1][23]-spline_der_points[1][23], 2.0*spline_der_points[0][23], 2.0*spline_der_points[1][23])
plt.legend()
plt.show()
EDIT:
I also opened an Issue on Github and according to ev-br the usage of interpolate.splprep is depreciated and one should use make_interp_spline / BSpline instead.
As noted in other answers, splprep output is incompatible with splder, but is compatible with splev. And the latter can evaluate the derivatives.
However, for interpolation, there is an alternative approach, which avoids splprep altogether. I'm basically copying a reply on the SciPy issue tracker (https://github.com/scipy/scipy/issues/10389):
Here's an example of replicating the splprep outputs. First let's make sense out of the splprep output:
# start with the OP example
import numpy as np
from scipy import interpolate
points = np.random.rand(10,2) * 10
(tck, u), fp, ier, msg = interpolate.splprep(points.T, s=0, k=3, full_output=True)
# check the meaning of the `u` array: evaluation of the spline at `u`
# gives back the original points (up to a list/transpose)
xy = interpolate.splev(u, tck)
xy = np.asarray(xy)
np.allclose(xy.T, points)
Next, let's replicate it without splprep. First, build the u array: the curve is represented parametrically, and u is essentially an approximation for the arc length. Other parametrizations are possible, but here let's stick to what splprep does. Translating the pseudocode from the doc page, https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splprep.html
vv = np.sum((points[1:, :] - points[:-1, :])**2, axis=1)
vv = np.sqrt(vv).cumsum()
vv/= vv[-1]
vv = np.r_[0, vv]
# check:
np.allclose(u, vv)
Now, interpolate along the parametric curve: points vs vv:
spl = interpolate.make_interp_spline(vv, points)
# check spl.t vs knots from splPrep
spl.t - tck[0]
The result, spl, is a BSpline object which you can evaluate, differentiate etc in a usual way:
np.allclose(points, spl(vv))
# differentiate
spl_derivative = spl.derivative(vv)
I am trying to replicate some of the functionality of Matlab's interp2. I know somewhat similar questions have been asked before, but none apply to my specific case.
I have a distance map (available at this Google drive location):
https://drive.google.com/open?id=0B6acq_amk5e3X0Q5UG1ya1VhSlE&authuser=0
Values are normalized in the range 0-1. Size is 200 rows by 300 columns.
I can load it up with this code snippet:
import numpy as np
dstnc1=np.load('dstnc.npy')
Coordinates are defined by the next snippet:
xmin = 0.
xmax = 9000.
ymin = 0.
ymax = 6000.
r1,c1 = dstnc1.shape
x = np.linspace(xmin,xmax,c1)
y = np.linspace(ymin, ymax,r1)
I have three map points defined by vectors xnew1, ynew1 with this snippet:
xnew1=[3700.540199,3845.940199,3983.240199]
ynew1=[1782.8611,1769.862,1694.862]
I check their location with respect to the distance map with this:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20, 16))
ax = fig.add_subplot(1, 1, 1)
plt.imshow(dstnc1, cmap=my_cmap_r,vmin=0,vmax=0.3,
extent=[0, 9000, 0, 6000], origin='upper')
plt.scatter(xnew1, ynew1, s=50, linewidths=0.15)
plt.show()
They plot in the correct location. Now I would like to extract the
distance value at those three points. I tried first interp2d.
from scipy.interpolate import interp2d
x1 = np.linspace(xmin,xmax,c1)
y1 = np.linspace(ymin,ymax,r1)
f = interp2d(x1, y1, dstnc1, kind='cubic')
but when I try to evaluate with:
test=f(xnew1,ynew1)
I get this error:
--------------------
ValueError Traceback (most recent call last)
<ipython-input-299-d0f42e609b23> in <module>()
----> 1 test=f(xnew1,ynew1)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\interpolate.pyc
in __call__(self, x, y, dx, dy)
270 (self.y_min, self.y_max)))
271
--> 272 z = fitpack.bisplev(x, y, self.tck, dx, dy)
273 z = atleast_2d(z)
274 z = transpose(z)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack.pyc
in bisplev(x, y, tck, dx, dy)
1027 z,ier = _fitpack._bispev(tx,ty,c,kx,ky,x,y,dx,dy)
1028 if ier == 10:
-> 1029 raise ValueError("Invalid input data")
1030 if ier:
1031 raise TypeError("An error occurred")
ValueError: Invalid input data
If I try RectBivariateSpline:
from scipy.interpolate import RectBivariateSpline
x2 = np.linspace(xmin,xmax,r1)
y2 = np.linspace(ymin,ymax,c1)
f = RectBivariateSpline(x2, y2, dstnc1)
I get this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-302-d0f42e609b23> in <module>()
----> 1 test=f(xnew1,ynew1)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack2.pyc
in __call__(self, x, y, mth, dx, dy, grid)
643 z,ier = dfitpack.bispev(tx,ty,c,kx,ky,x,y)
644 if not ier == 0:
--> 645 raise ValueError("Error code returned by
bispev: %s" % ier)
646 else:
647 # standard Numpy broadcasting
ValueError: Error code returned by bispev: 10
Any suggestion as to whether I am using the wrong functions or the right
function with wrong syntax, and how I may fix it is appreciated. Thank you.
UPDATE
I am running Python 2.7.9 and Scipy 0.14.0 (on Continuum Anaconda)
As posted on the Scipy mailing list here the documentation seems confusing, being a mix of Scipy 0.14.0, and the next version. Can anybody suggest a workaround or the correct syntax for version 0.14.0.
UPDATE 2
tried
xnew1=np.array([3700.540199,3845.940199,3983.240199])
ynew1=np.array([1782.8611,1769.862,1694.862])
as suggested inj a comment but the error remains.
This syntax worked with RectBivariateSpline
x2 = np.linspace(xmin,xmax,c1)
y2 = np.linspace(ymin,ymax,r1)
f2 = sp.interpolate.RectBivariateSpline(x2, y2, dstnc1.T,kx=1, ky=1)
I can then evaluate at new points with this:
out2 = f2.ev(xnew1,ynew1)
For interp2d I am stuck as I am not able to bypass firewall at my office to update Anaconda (Windows). I may be able at home on a Mac installation, in which case, if I get the syntax right, I will add to thsi answer.
Basically, is it possible to get scipy.ndimage.map_coordinates to return a multi-valued structure, instead of just a scalar? I'd like to be able to interpolate once to retrieve 5 values at a point, rather than having to interpolate 5 times.
Here's my try at a MWE to demonstrate the problem. I'll start with a 3D interpolation of a scalar. I won't go between points for now because that's not the point.
import numpy as np
from scipy import ndimage
coords = np.array([[1.,1.,1.]])
a = np.arange(3*3*3).reshape(3,3,3)
ndimage.map_coordinates(a,coords.T) # array([13.])
Now, suppose I want a to have pairs of values, not just one. My instinct is
a = np.arange(3*3*3*2).reshape(3,3,3,2)
a[1,1,1] # array([26.,27.])
ndimage.map_coordinates(a[:,:,:],coords.T) # I'd like array([26.,27.])
Instead of the desired output, I get the following:
RuntimeError Traceback (most recent call last)
(...)/<ipython-input-84-77334fb7469f> in <module>()
----> 1 ndimage.map_coordinates(a[:,:,:],np.array([[1.,1.,1.]]).T)
/usr/lib/python2.7/dist-packages/scipy/ndimage/interpolation.pyc in map_coordinates(input, coordinates, output, order, mode, cval, prefilter)
287 raise RuntimeError('input and output rank must be > 0')
288 if coordinates.shape[0] != input.ndim:
--> 289 raise RuntimeError('invalid shape for coordinate array')
290 mode = _extend_mode_to_code(mode)
291 if prefilter and order > 1:
RuntimeError: invalid shape for coordinate array
I can't find a permutation of the shapes of any of the structures (a, coords, etc.) that gives me the answer I'm looking for. Also, if there's a better way to do this than using map_coordinates, go ahead. I thought scipy.interpolate.interp1d might be the way to go but I can't find any documentation or an inkling of what it might do...
That's not possible, I think.
But tensor product interpolation is not difficult:
import numpy as np
from scipy.interpolate import interp1d
def interpn(*args, **kw):
"""Interpolation on N-D.
ai = interpn(x, y, z, ..., a, xi, yi, zi, ...)
where the arrays x, y, z, ... define a rectangular grid
and a.shape == (len(x), len(y), len(z), ...)
"""
method = kw.pop('method', 'cubic')
if kw:
raise ValueError("Unknown arguments: " % kw.keys())
nd = (len(args)-1)//2
if len(args) != 2*nd+1:
raise ValueError("Wrong number of arguments")
q = args[:nd]
qi = args[nd+1:]
a = args[nd]
for j in range(nd):
a = interp1d(q[j], a, axis=j, kind=method)(qi[j])
return a
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 6)
y = np.linspace(0, 1, 7)
k = np.array([0, 1])
z = np.cos(2*x[:,None,None] + k[None,None,:]) * np.sin(3*y[None,:,None])
xi = np.linspace(0, 1, 60)
yi = np.linspace(0, 1, 70)
zi = interpn(x, y, z, xi, yi, method='linear')
plt.subplot(221)
plt.imshow(z[:,:,0].T, interpolation='nearest')
plt.subplot(222)
plt.imshow(zi[:,:,0].T, interpolation='nearest')
plt.subplot(223)
plt.imshow(z[:,:,1].T, interpolation='nearest')
plt.subplot(224)
plt.imshow(zi[:,:,1].T, interpolation='nearest')
plt.show()