Re: reshape with xyz ordering

2016-07-27 Thread Heli
Thanks for your replies. Let me explain my problem a little bit more. I have 
the following data which i read from a file using numpy.loadtxt and then i sort 
it using np.lexsort:

x=f[:,0] # XColumn
y=f[:,1] # YColumn
z=f[:,2] # ZColumn
val=f[:,3] # Val Column
xcoord=np.sort(np.unique(f[:,0])) # XCoordinates
ycoord=np.sort(np.unique(f[:,1])) # YCoordinates
zcoord=np.sort(np.unique(f[:,2])) # ZCoordinates

ind = np.lexsort((val,z,y,x)) 
val_sorted=np.array(val[ind])

I know that the val column has data sorted first by x, then by y, then by z 
which means that column x changes slowest and column z changes fastest.

x,y,z, val 
0,0,0,val1 
0,0,1,val2 
0,0,2,val3 

0,0,zn,valn
...
xn,yn,zn,valfin

I want to reshape val_sorted in to a 3d numpy array of (nx,ny,nz). 

which of the following is the correct way and why?

#1
val_sorted_reshaped=val_sorted.reshape((xcoord.size,ycoord.size,zcoord.size))

#2
#val_sorted_reshaped=val_sorted.reshape((xcoord.size,ycoord.size,zcoord.size)).transpose()

#3
#val_sorted_reshaped=val_sorted.reshape((zcoord.size,ycoord.size,xcoord.size)) 

#4
#val_sorted_reshaped=val_sorted.reshape((zcoord.size,ycoord.size,xcoord.size)).transpose()
 

Thanks, 


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: reshape with xyz ordering

2016-07-26 Thread Nobody
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))

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: reshape with xyz ordering

2016-07-26 Thread Peter Otten
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?
> 
> 
> my val looks like the following:
> 
> x,y,z, val
> 0,0,0,val1
> 0,0,1,val2
> 0,0,2,val3
> ...
> 
> Thanks a lot for your help,

I'm not sure I understand the question. Does

shape = [max(t) + 1 for t in [x, y, z]]
cube = myval.reshape(shape)

give what you want?

-- 
https://mail.python.org/mailman/listinfo/python-list


reshape with xyz ordering

2016-07-26 Thread Heli
Hi, 

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?


my val looks like the following:

x,y,z, val
0,0,0,val1
0,0,1,val2
0,0,2,val3
...

Thanks a lot for your help, 
-- 
https://mail.python.org/mailman/listinfo/python-list