indices, lists and arrays
i have something like this: sizes = ndimage.sum(bl_img, labels=bl_l, index=range(1,bl_n+1)) sizes = array(sizes) bl_obj_indices = where(sizes<21) bl_l[bl_objects[bl_obj_indices]] = 0 sizes was a list, but i converted it to an array in order to use the function where on it where returns an array of arrays, something like (array([14, 17]),) bl_objects is the output of ndimage.find_objects and is a list. this means that the assignment on the last row of the proposed code does not work. is there an elegant solution to solve the problem? list(bl_obj_indices) returns [array([14, 17])] so it does not do the trick. i would need something like [14, 17] any hint? Emanuele
El dc 06 de 06 del 2007 a les 16:13 +0200, en/na Emanuele Zattin va escriure:
i have something like this:
sizes = ndimage.sum(bl_img, labels=bl_l, index=range(1,bl_n+1)) sizes = array(sizes) bl_obj_indices = where(sizes<21) bl_l[bl_objects[bl_obj_indices]] = 0
sizes was a list, but i converted it to an array in order to use the function where on it where returns an array of arrays, something like (array([14, 17]),) bl_objects is the output of ndimage.find_objects and is a list. this means that the assignment on the last row of the proposed code does not work. is there an elegant solution to solve the problem?
In order to use fancy indexing, you always need an array as the base, so, the next should do the trick: bl_l[array(bl_objects)[bl_obj_indices]] = 0 or, for short: bl_l[array(bl_objects)[sizes<21]] = 0 HTH, -- Francesc Altet | Be careful about using the following code -- Carabos Coop. V. | I've only proven that it works, www.carabos.com | I haven't tested it. -- Donald Knuth
On 6/6/07, Francesc Altet <faltet@carabos.com> wrote:
El dc 06 de 06 del 2007 a les 16:13 +0200, en/na Emanuele Zattin va escriure:
i have something like this:
sizes = ndimage.sum(bl_img, labels=bl_l, index=range(1,bl_n+1)) sizes = array(sizes) bl_obj_indices = where(sizes<21) bl_l[bl_objects[bl_obj_indices]] = 0
sizes was a list, but i converted it to an array in order to use the function where on it where returns an array of arrays, something like (array([14, 17]),) bl_objects is the output of ndimage.find_objects and is a list. this means that the assignment on the last row of the proposed code does not work. is there an elegant solution to solve the problem?
In order to use fancy indexing, you always need an array as the base, so, the next should do the trick:
bl_l[array(bl_objects)[bl_obj_indices]] = 0
or, for short:
bl_l[array(bl_objects)[sizes<21]] = 0
Stupid me, i forgot to mention that i actually converted bl_objects to array before that... but still that does not work. it compains that arrays used as indices should be integers... but i can see that where returns a tuple... mmm
El dc 06 de 06 del 2007 a les 17:46 +0200, en/na Emanuele Zattin va escriure:
On 6/6/07, Francesc Altet <faltet@carabos.com> wrote: El dc 06 de 06 del 2007 a les 16:13 +0200, en/na Emanuele Zattin va escriure: > i have something like this: > > sizes = ndimage.sum(bl_img, labels=bl_l, index=range(1,bl_n +1)) > sizes = array(sizes) > bl_obj_indices = where(sizes<21) > bl_l[bl_objects[bl_obj_indices]] = 0 > > sizes was a list, but i converted it to an array in order to use the > function where on it > where returns an array of arrays, something like (array([14, 17]),) > bl_objects is the output of ndimage.find_objects and is a list. this > means that the assignment on the last row of the proposed code does > not work. > is there an elegant solution to solve the problem?
In order to use fancy indexing, you always need an array as the base, so, the next should do the trick:
bl_l[array(bl_objects)[bl_obj_indices]] = 0
or, for short:
bl_l[array(bl_objects)[sizes<21]] = 0
Stupid me, i forgot to mention that i actually converted bl_objects to array before that... but still that does not work. it compains that arrays used as indices should be integers... but i can see that where returns a tuple... mmm
That's strange. Fancy indexing seems to happily accept tuples as well: In [1]:import numpy In [2]:a=numpy.arange(10) In [3]:a[a>3] Out[3]:array([4, 5, 6, 7, 8, 9]) In [4]:a[numpy.where(a>3)] Out[4]:array([4, 5, 6, 7, 8, 9]) In [5]:numpy.where(a>3) Out[5]:(array([4, 5, 6, 7, 8, 9]),) Perhaps your bl_objects is multidimensional? But even in this case, this should work fine: In [19]:a=numpy.arange(10).reshape(2,5) In [20]:a Out[20]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) In [21]:a[a>3] Out[21]:array([4, 5, 6, 7, 8, 9]) In [22]:a[numpy.where(a>3)] Out[22]:array([4, 5, 6, 7, 8, 9]) Which version of NumPy are you using? -- Francesc Altet | Be careful about using the following code -- Carabos Coop. V. | I've only proven that it works, www.carabos.com | I haven't tested it. -- Donald Knuth
participants (2)
-
Emanuele Zattin -
Francesc Altet