Functions
wirecell.validate.arrays Namespace Reference

Functions

def rebin (a, args)
 
def bin_ndarray (ndarray, new_shape, operation='sum')
 

Function Documentation

def wirecell.validate.arrays.bin_ndarray (   ndarray,
  new_shape,
  operation = 'sum' 
)
Bins an ndarray in all axes based on the target shape, by summing or
    averaging.

Number of output dimensions must match number of input dimensions and 
    new axes must divide old ones.

Example
-------
>>> m = np.arange(0,100,1).reshape((10,10))
>>> n = bin_ndarray(m, new_shape=(5,5), operation='sum')
>>> print(n)

[[ 22  30  38  46  54]
 [102 110 118 126 134]
 [182 190 198 206 214]
 [262 270 278 286 294]
 [342 350 358 366 374]]

Definition at line 20 of file arrays.py.

20 def bin_ndarray(ndarray, new_shape, operation='sum'):
21  """
22  Bins an ndarray in all axes based on the target shape, by summing or
23  averaging.
24 
25  Number of output dimensions must match number of input dimensions and
26  new axes must divide old ones.
27 
28  Example
29  -------
30  >>> m = np.arange(0,100,1).reshape((10,10))
31  >>> n = bin_ndarray(m, new_shape=(5,5), operation='sum')
32  >>> print(n)
33 
34  [[ 22 30 38 46 54]
35  [102 110 118 126 134]
36  [182 190 198 206 214]
37  [262 270 278 286 294]
38  [342 350 358 366 374]]
39 
40  """
41  print "%s -> %s" % (ndarray.shape, new_shape)
42  operation = operation.lower()
43  if not operation in ['sum', 'mean']:
44  raise ValueError("Operation not supported.")
45  if ndarray.ndim != len(new_shape):
46  raise ValueError("Shape mismatch: {} -> {}".format(ndarray.shape,
47  new_shape))
48  compression_pairs = [(d, c//d) for d,c in zip(new_shape,
49  ndarray.shape)]
50  flattened = [l for p in compression_pairs for l in p]
51  ndarray = ndarray.reshape(flattened)
52  for i in range(len(new_shape)):
53  op = getattr(ndarray, operation)
54  ndarray = op(-1*(i+1))
55  return ndarray
56 
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
def bin_ndarray(ndarray, new_shape, operation='sum')
Definition: arrays.py:20
def wirecell.validate.arrays.rebin (   a,
  args 
)
rebin ndarray data into a smaller ndarray of the same rank whose dimensions
are factors of the original dimensions. eg. An array with 6 columns and 4 rows
can be reduced to have 6,3,2 or 1 columns and 4,2 or 1 rows.
example usages:
>>> a=rand(6,4); b=rebin(a,3,2)
>>> a=rand(6); b=rebin(a,2)

Definition at line 2 of file arrays.py.

2 def rebin(a, *args):
3  '''rebin ndarray data into a smaller ndarray of the same rank whose dimensions
4  are factors of the original dimensions. eg. An array with 6 columns and 4 rows
5  can be reduced to have 6,3,2 or 1 columns and 4,2 or 1 rows.
6  example usages:
7  >>> a=rand(6,4); b=rebin(a,3,2)
8  >>> a=rand(6); b=rebin(a,2)
9  '''
10  shape = a.shape
11  lenShape = len(shape)
12  factor = numpy.asarray(shape)/numpy.asarray(args)
13  evList = ['a.reshape('] + \
14  ['args[%d],factor[%d],'%(i,i) for i in range(lenShape)] + \
15  [')'] + ['.sum(%d)'%(i+1) for i in range(lenShape)] + \
16  ['/factor[%d]'%i for i in range(lenShape)]
17  #print ''.join(evList)
18  return eval(''.join(evList))
19 
def rebin(a, args)
Definition: arrays.py:2