[Numpy-discussion] numpy.ma.flatnotmasked_contiguous error in slice.stop values?

Geordie McBain gdmcbain at freeshell.org
Sun Aug 8 22:37:17 EDT 2010


The function numpy.ma.flatnotmasked_contiguous returns slices which
miss the last element; for example, based on the example at
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.flatnotmasked_contiguous.html,

import numpy as np
a = np.arange (10)
am = np.ma.array (a, mask = (a < 3) | (a > 8) | (a == 5))
segments = np.ma.flatnotmasked_contiguous (am)
print 'the masked array:', am
print 'flatnotmasked_contiguous: ', segments
print am[segments[0]], am[segments[1]]

prints

the masked array: [-- -- -- 3 4 -- 6 7 8 --]
flatnotmasked_contiguous:  [slice(3, 4, None), slice(6, 8, None)]
[3] [6 7]

which isn't really what we're after; I would have expected [3 4] and
[6 7 8]. Compare the similarly named
matplotlib.mlab.contiguous_regions which does have the expected
behaviour (although its output format is different)

import matplotlib.mlab
segments1 = matplotlib.mlab.contiguous_regions (am)
print 'contiguous_regions:', segments1
print am[slice (*segments1[0])], am[slice (*segments1[1])]

which prints

contiguous_regions: [(3, 5), (6, 9)]
[3 4] [6 7 8]

(I'm running Python 2.6.4 on 64-bit Linux Mint 8 `Helena', with NumPy
from Ubuntu Karmic, which I think means I have NumPy version 1.3.0?)



More information about the NumPy-Discussion mailing list