reshape with xyz ordering

Nobody nobody at nowhere.invalid
Tue Jul 26 18:55:09 EDT 2016


On Tue, 26 Jul 2016 07:10:18 -0700, Heli wrote:

> I sort a file with 4 columns (x,y,z, somevalue) and I sort it using
> numpy.lexsort.
> 
> ind=np.lexsort((val,z,y,x))
> 
> myval=val[ind]
> 
> myval is a 1d numpy array sorted by x,then y, then z and finally val.
> 
> how can I reshape correctly myval so that I get a 3d numpy array
> maintaining the xyz ordering of the data?

Is it guaranteed that the data actually *is* a 3-D array that's been
converted to a list of x,y,z,val tuples?

In other words, does every possible combination of x,y,z for 0<=x<=max(x),
0<=y<=max(y), 0<=z<=max(z) occur exactly once?

If so, then see Peter's answer. If not, then how do you wish to handle
a) (x,y,z) tuples which never occur (missing values), and
b) (x,y,z) tuples which occur more than once?

If the data "should" to be a 3-D array but you first wish to ensure that
it actually is, you can use e.g.

	nx,ny,nz = max(x)+1,max(y)+1,max(z)+1
	if val.shape != (nx*ny*nz,):
	    raise ValueError
	i = (x*ny+y)*nz+z
	found = np.zeros(val.shape, dtype=bool)
	found[i] = True
	if not np.all(found):
	    raise ValueError
	ind = np.lexsort((val,z,y,x))
	myval = val[ind].reshape((nx,ny,nz))




More information about the Python-list mailing list