Search code examples
pythonnumpyscipyndimage

apply a function that takes an argument to an ndimage labeled array


I have an array that I've labeled using scipy.ndimage and I'd like to multiply each element by a factor specific to its corresponding label. I thought I could use ndimage.labeled_comprehension for this, however I can't seem to figure out how to pass an argument to the function. For example:

a = np.random.random(9).reshape(3,3)
lbls = np.repeat(np.arange(3),3).reshape(3,3)
ndx = np.arange(0,lbls.max()+1)
factors = np.random.randint(10,size=3)

>>> lbls
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])
>>> ndx
array([0, 1, 2])
>>> factors
array([5, 4, 8])

def fn(a, x):
    return a*x

>>> b = ndimage.labeled_comprehension(a, labels=lbls, index=ndx, func=fn, out_dtype=float, default=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tgrant/anaconda/envs/python2/lib/python2.7/site-packages/scipy/ndimage/measurements.py", line 416, in labeled_comprehension
    do_map([input], temp)
  File "/Users/tgrant/anaconda/envs/python2/lib/python2.7/site-packages/scipy/ndimage/measurements.py", line 411, in do_map
    output[i] = func(*[inp[l:h] for inp in inputs])
TypeError: fn() takes exactly 2 arguments (1 given)

As expected it gives an error since fn() needs factors fed into it somehow. Is labeled_comprehension able to do this?


Solution

  • Index into factors and then simply multiply with the image array -

    a*factors[lbls]
    

    Sample run -

    In [483]: a    # image/data array
    Out[483]: 
    array([[ 0.10682998,  0.29631501,  0.08501469],
           [ 0.46944505,  0.88346229,  0.75672908],
           [ 0.11381292,  0.24096868,  0.86438641]])
    
    In [484]: factors  # scaling factors
    Out[484]: array([8, 1, 1])
    
    In [485]: lbls  # labels
    Out[485]: 
    array([[0, 0, 0],
           [1, 1, 1],
           [2, 2, 2]])
    
    In [486]: factors[lbls] # factors populated based on the labels
    Out[486]: 
    array([[8, 8, 8],
           [1, 1, 1],
           [1, 1, 1]])
    
    In [487]: a*factors[lbls] # finally scale the image array
    Out[487]: 
    array([[ 0.85463981,  2.37052006,  0.68011752],
           [ 0.46944505,  0.88346229,  0.75672908],
           [ 0.11381292,  0.24096868,  0.86438641]])