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. 7 >>> a=rand(6,4); b=rebin(a,3,2) 8 >>> a=rand(6); b=rebin(a,2) 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)]
18 return eval(
''.join(evList))
22 Bins an ndarray in all axes based on the target shape, by summing or 25 Number of output dimensions must match number of input dimensions and 26 new axes must divide old ones. 30 >>> m = np.arange(0,100,1).reshape((10,10)) 31 >>> n = bin_ndarray(m, new_shape=(5,5), operation='sum') 38 [342 350 358 366 374]] 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,
48 compression_pairs = [(d, c//d)
for d,c
in zip(new_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))
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
def bin_ndarray(ndarray, new_shape, operation='sum')