fork download
  1. import numpy as np
  2. from scipy.ndimage import median
  3.  
  4. dat = np.arange(12).reshape(2, 2, 3)
  5. idx = np.array([[0, 0], [1, 2]])
  6.  
  7. def summarize(dat, idx):
  8. idx = np.unique(idx, return_inverse=True)[1].reshape(idx.shape)
  9. chan = dat.shape[-1]
  10. offset = idx.max() + 1
  11. index = np.stack([idx + i * offset for i in range(chan)], axis=-1)
  12. return median(dat, index, index=range(offset * chan)).reshape(chan, offset).T
  13.  
  14. print(summarize(dat, idx))
Success #stdin #stdout 0.29s 38608KB
stdin
Standard input is empty
stdout
[[  1.5   2.5   3.5]
 [  6.    7.    8. ]
 [  9.   10.   11. ]]