Package ivs :: Package aux :: Module numpy_ext
[hide private]
[frames] | no frames]

Module numpy_ext

source code

Operations on numpy arrays not present in the standard package.

Functions [hide private]
    Normal arrays
numpy array(, numpy array)
unique_arr(a, axis=0, return_index=False)
Distil unique rows/cols from an array
source code
numpy array
sort_order(a, order)
Sort an array along several axes
source code
(index,index)
argmax2D(a)
Calculate the argmax of a 2D array.
source code
(index,index)
argmin2D(a)
Calculate the argmin of a 2D array.
source code
 
match_arrays(a, b)
Return closest-match indices from b in a.
source code
float
stdw(data, weights=None)
Calculates the weighted (sample) standard deviation of a list of numbers.
source code
 
deriv(x, y)
3 point Lagrangian differentiation.
source code
 
random_rectangular_grid(gridpoints, size)
Generate random points in a non-convex continuous rectangular grid.
source code
    Record arrays
numpy record array
recarr(x, mydtype)
Convert an array to a record array.
source code
numpy record array
recarr_addrows(x, rows)
Add rows to a record array
source code
numpy record array
recarr_addcols(x, cols, dtypes_ext)
Add columns to a record array
source code
 
recarr_join(arr1, arr2)
Join to record arrays column wise.
source code
    Line intersections
 
find_intersections(A, B)
Find intersection of two 2D lines.
source code
Function Details [hide private]

unique_arr(a, axis=0, return_index=False)

source code 

Distil unique rows/cols from an array

axis=0: unique rows axis=1: unique cols

Example with rows: >>> c = np.sort(np.random.normal(size=(3,5)),axis=0) >>> d = np.r_[c,c,c] >>> du = np.sort(unique_arr(d,axis=0),axis=0) >>> np.all(du==c) True

Example with columns: >>> c = np.sort(np.random.normal(size=(3,5)),axis=1) >>> d = np.hstack([c,c]) >>> du = np.sort(unique_arr(d,axis=1),axis=1) >>> np.all(du==c) True

Parameters:
  • a (numpy array) - array to remove duplicate entries from
  • axis (integer) - keep unique elements in rows (axis=0) or columns (axis=1)
  • return_index (bool) - return index array with unique elements
Returns: numpy array(, numpy array)
unique array(,indices)

sort_order(a, order)

source code 

Sort an array along several axes

>>> a = np.array([[ 0.,  1.],                   [ 1.,  1.],                   [ 1.,  0.],                   [ 0.,  0.],                   [ 0.,  3.],                   [ 1.,  0.],                   [ 1.,  3.],                   [ 1.,  2.],                   [ 1.,  3.],                   [ 0.,  4.]])
>>> sort_order(a,[0,1])
array([[ 0.,  0.],
       [ 0.,  1.],
       [ 0.,  3.],
       [ 0.,  4.],
       [ 1.,  0.],
       [ 1.,  0.],
       [ 1.,  1.],
       [ 1.,  2.],
       [ 1.,  3.],
       [ 1.,  3.]])
Parameters:
  • a (numpy array) - numpy array to sort
  • order (list of integers (indices)) - order of the columns to sort
Returns: numpy array
sorted array

argmax2D(a)

source code 

Calculate the argmax of a 2D array.

Example usage:

>>> output = np.zeros(100)
>>> for i in xrange(100):
...     a = np.random.normal(size=(5,6))
...     x,y = argmax2D(a)
...     output[i] = (a[x,y] == a.max())
>>> sum(output)
100.0
Parameters:
  • a (numpy 2D array) - array to calculate the position of the maximum of
Returns: (index,index)
x,y coordinates

argmin2D(a)

source code 

Calculate the argmin of a 2D array.

Example usage:

>>> output = np.zeros(100)
>>> for i in xrange(100):
...     a = np.random.normal(size=(5,6))
...     x,y = argmin2D(a)
...     output[i] = (a[x,y] == a.min())
>>> sum(output)
100.0
Parameters:
  • a (numpy 2D array) - array to calculate the position of the minimum of
Returns: (index,index)
x,y coordinates

match_arrays(a, b)

source code 

Return closest-match indices from b in a.

Example usage:

>>> a = np.random.uniform(size=10)
>>> b = a[np.array(np.random.uniform(size=3)*10,int)]
>>> ind = match_arrays(a,b)
>>> all(a[ind] == b)
True

stdw(data, weights=None)

source code 

Calculates the weighted (sample) standard deviation of a list of numbers.

Parameters:
  • data (list) - input data, must be a two dimensional list in format [value, weight]
Returns: float
weighted standard deviation

deriv(x, y)

source code 

3 point Lagrangian differentiation.

Returns z = dy/dx

Example usage:

>>> X = np.array([ 0.1, 0.3, 0.4, 0.7, 0.9])
>>> Y = np.array([ 1.2, 2.3, 3.2, 4.4, 6.6])
>>> deriv(X,Y)
array([  3.16666667,   7.83333333,   7.75      ,   8.2       ,  13.8       ])

random_rectangular_grid(gridpoints, size)

source code 

Generate random points in a non-convex continuous rectangular grid.

This routine will subdivide the grid in smaller cubicles and then populate those with a number of points proportional to the relative size of the cubicle. Because of this, size will never be the true size of the sample returned, but will always be a little bit higher or lower.

Warning: This function requiresevery square to be populated by at least one point. If this is not the case, it will be forced that every square has a point.

>>> xs = np.array([1,2,0,1,2,3,0,1,2,3,1,2.])
>>> ys = np.array([4,4,3,3,3,3,2,2,2,2,1,1.])
>>> gridpoints = np.column_stack([xs,ys])
>>> sample = random_rectangular_grid(gridpoints,10000)
>>> p = pl.figure()
>>> p = pl.plot(xs,ys,'ro')
>>> p = pl.plot(sample[:,0],sample[:,1],'ko',ms=2)

]include figure]]ivs_aux_numpy_ext_grid.png]

Gridpoints should be Ngridpoints x Ncols

recarr(x, mydtype)

source code 

Convert an array to a record array. dtype = [('name1',int),('name2',float)]

>>> x = np.array([[1,1],[2,2]])
>>> y = recarr(x,[('ones',int),('twos',int)])
>>> print y['ones']
[1 1]
Parameters:
  • x (numpy array) - array to convert
  • mydtype (list of tuples ('column name',type)) - dtype of record array
Returns: numpy record array
convert record array

recarr_addrows(x, rows)

source code 

Add rows to a record array

>>> x = np.array([[1,1],[2,2]])
>>> y = recarr(x,[('ones',int),('twos',int)])
>>> z = recarr_addrows(y,[[1,2]])
>>> print z['ones']
[1 1 1]
Parameters:
  • x (numpy record array) - original record array
  • rows (list or ndarray) - list of lists/tuples or 2D-numpy array
Returns: numpy record array
extended record array

recarr_addcols(x, cols, dtypes_ext)

source code 

Add columns to a record array

>>> x = np.array([[1,1],[2,2]])
>>> y = recarr(x,[('ones',int),('twos',int)])
>>> z = recarr_addcols(y,[[3,3],[4,4]],[('threes',int),('fours',int)])
>>> print z['fours']
[4 4]
Parameters:
  • x (numpy record array) - original record array
  • cols (list or ndarray) - list of lists/tuples or 2D-numpy array
  • dtypes_ext (list of tuples ('column name',type)) - dtype of extra columns
Returns: numpy record array
extended record array

find_intersections(A, B)

source code 

Find intersection of two 2D lines.

Example usage:

>>> pcoeff1 = [1,2,-2.]
>>> pcoeff2 = [-2.12,2.45,3.7321]
>>> x = np.linspace(-3,3,7)
>>> A = np.column_stack([x,np.polyval(pcoeff1,x)])
>>> B = np.column_stack([x,np.polyval(pcoeff2,x)])

We find two intersections:

>>> xs,ys = find_intersections(A,B)
>>> print xs,ys
[-1.22039755  1.34367003] [-2.77960245  2.71835017]
>>> p = pl.figure()
>>> p = pl.plot(x,A[:,1],'bo-')
>>> p = pl.plot(x,B[:,1],'go-')
>>> p = pl.plot(xs,ys,'rs',ms=5)

Returns empty arrays when there are no intersections.

]include figure]]ivs_aux_numpy_ext_intersect.png]