pwtools.num.DataND

class pwtools.num.DataND(a2=None, an=None, axes=None)[source]

Bases: object

Transform 2d array a2 to nd array an. The 2d array’s last column are values on a grid represented by the nd array. The 2d array is the “flattened” version of the nd array. Works only for ordered axes where a2 was generated by a nested loop over ordered 1d sequences, i.e.

>>> nx,ny,nz = len(x),len(y),len(z)
>>> for ii in range(nx):
...     for jj in range(ny):
...         for kk in range(nz):
...             idx = ii*ny*nz + jj*nz + kk
...             a2[idx,0] = x[ii]
...             a2[idx,1] = y[jj]
...             a2[idx,2] = z[kk]
...             a2[idx,3] = <some value>
>>> axes = [x,y,z]

The axes are also extracted by numpy.unique() from a2’s columns, therefore only ordered axes work.

The reverse operation an -> a2 is not implemented ATM.

Examples

>>> from pwtools import num
>>> # something to create grid values
>>> a=iter(arange(1,100))
>>> # Nested loop
>>> a2=array([[x,y,z,a.next()] for x in [0,1,2] for y in [0,1] for z in [0,1,2,3]])
>>> nd=num.DataND(a2=a2)
>>> nd.an.shape
(3,2,4)
>>> # nd array an[ii,jj,kk]
>>> nd.an
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],
       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]],
       [[17, 18, 19, 20],
        [21, 22, 23, 24]]])
>>> nd.axes
[array([0, 1, 2]), array([0, 1]), array([0, 1, 2, 3])]
__init__(a2=None, an=None, axes=None)[source]
Parameters:

arr (2d array (nrows, ncols))

nd
Type:

nd arry

axes

The axes of the grid from np.unique()’ed ncols-1 columns.

Type:

list of 1d arrays

Methods

a2_to_an()