python - Find sequence in two Numpy arrays -



python - Find sequence in two Numpy arrays -

i have 2 numpy arrays contain indices of maxima , minima array.

for example, 1 output, maxima , minima arrays this:

maxima indices (array([ 4, 10, 14, 37, 43, 51, 59, 67, 81, 89, 95]),) minima indices (array([ 7, 12, 25, 33, 40, 49, 56, 63, 76, 92]),)

these indices come intensity values in image row.

i need find how many times maxima occurs within 4 index positions between 2 minima - in other words:

minima + 4 + maxima + 4 + minima

how can efficiently in python? how can compare index values in both arrays find instances of sequence , count how many instances there in total?

many help.

edit: each maximum has within 4 positions of closest minima on left , right. trying identify dotted line within image based on intensity values.

let try.

import numpy # create vector distances nearest leftmost minimum # img_len length of image row # first create integer vector 1 @ each minimum b = numpy.zeros(img_len, dtype='int') # create integer vector distances d = numpy.zeros(img_len, dtype='int') # mark leftmost distances first minimum largest possible d[:minima[0]] = minima[0] - numpy.arange(minima[0]) # iterate through vector , calculate distances in range(len(minima) - 1): prev = minima[i] next = minima[i+1] # have gap between 2 minima # let's fill triangle 0,1,2,...,2,1,0 k = (next-prev + 1) // 2 d[prev:prev+k+1] = numpy.arange(k+1) d[next-k+1:next] = k -1 - numpy.arange(k-1) # fill in space after lastly minimum: d[minima[-1]:] = numpy.arange(img_len - minima[-1]) # maxima distance less d closest minimum results = [ m m in maxima if d[m] < d ]

unless evident form code, thought create vector d corresponds distance nearest minimum. resulting vector is, e.g., 4,3,2,1,0,1,2,3,2,1,0,1,2,1,0,... zeros correspond minima positions. hard thing triangle-making in loop right. (i hope cleaned off-by-ones...)

of course, can create list of tuples maxima positions:

[ (m, d[m]) m in maxima ]

for info in question returns:

[(4, 3), (10, 2), (14, 2), (37, 3), (43, 3), (51, 2), (59, 3), (67, 4), (81, 5), (89, 3), (95, 3)]

the code works if there more 1 maxima between 2 minima in question. (if there single maximum, code different.)

python arrays algorithm numpy sequence

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -