python - Function to compute 3D gradient with unevenly spaced sample locations -
i have experimental observations in volume:
import numpy np # observations not uniformly spaced x = np.random.normal(0, 1, 10) y = np.random.normal(5, 2, 10) z = np.random.normal(10, 3, 10) xx, yy, zz = np.meshgrid(x, y, z, indexing='ij') # fake temperatures @ coords tt = xx*2 + yy*2 + zz*2 # sample distances dx = np.diff(x) dy = np.diff(y) dz = np.diff(z) grad = np.gradient(tt, [dx, dy, dz]) # returns error
this gives me error:
valueerror: operands not broadcast shapes (10,10,10) (3,9) (10,10,10)
.
edit: according @jay-kominek in comments below:
np.gradient won't work you, doesn't handle unevenly sampled data.
i've updated question. there function can can computation?
two things note: first, scalars single values, not arrays. second, signature of function numpy.gradient(f, *varargs, **kwargs)
. note * before varargs
. means if varargs
list, pass *varargs
. or can provide elements of varargs
separate arguments.
so, np.gradient
wants single value distance along each dimension, like:
np.gradient(tt, np.diff(x)[0], np.diff(y)[0], np.diff(z)[0])
or:
distances = [np.diff(x)[0], np.diff(y)[0], np.diff(z)[0]] np.gradient(tt, *distances)
Comments
Post a Comment