In numpy, what is the best way to sample an n-dimensional data volume with an m-dimensional probe (m<n)? For instance, a 3D array with a tilted 2D plane? This is working example code to create sampling points on a plane spanned by two vectors v1,v2, which can then be fed into ndimage.map_coordinates, but I am sure there's a better way:
o = [0, 0, 0] # origin
v1 = [1, 0, 0]; l1=1 # First vector and length
v2 = [0, 1, 0]; l2=1 # Second vector and length
ds1,ds2 = 10,10 # samples per direction
l1x,l1y,l1z = np.linspace(o[0],v1[0]*l1,ds1),np.linspace(o[1],v1[1]*l1,ds1),np.linspace(o[2],v1[2]*l1,ds1)
l2x,l2y,l2z = np.linspace(o[0],v2[0]*l2,ds2),np.linspace(o[1],v2[1]*l2,ds2),np.linspace(o[2],v1[2]*l2,ds2)
llx, lly, llz = l1x, l1y, l1z
# shift points in direction of 2nd vector and append
for i in range(1,ds2):
llx = np.hstack((llx,l1x+l2x[i]))
lly = np.hstack((lly,l1y+l2y[i]))
llz = np.hstack((llz,l1z+l2z[i]))
ptcoords = np.vstack((llx,lly,llz)).T
EDIT:
I have found a neater way to formulate this: With l
a list of pxn arrays (e.g. p=3,n=10, an array of 10 3D points, or a linear probe) where m is fixed throughout the list but n can vary by element, this code will build up the whole probing array:
base = l[0] # if l[0] is the only one
for ll in l[1:]:
n = ll.shape[0]
nb = base.shape[0]
base = np.repeat(ll,nb,0) + np.vstack(np.hsplit(np.tile(base,n),n))
Still, a loop I think may be avoidable and a lot of array manipulation! I have tested it on some 3D configurations, here's a screenshot. Let me know if you can elegantulate or find a bug.