简体   繁体   中英

how to manipulate the following numpy.where array

I have an array of indices which looks as follows:

indices=np.where(f!=1)

which gives the following array:

(array([ 249,  250,  251,  252,  253,  254,  804,  805,  806,  807,  808,
        809, 1365, 1366, 1367, 1368, 1369, 1860, 1861, 1862, 1863, 1864,
       2424, 2425, 2426, 2427, 2428, 2948, 2949, 2950, 2951, 2952, 2953,
       3501, 3502, 3503, 3504, 3505, 3506, 4061, 4062, 4063, 4064, 4065,
       4555, 4556, 4557, 4558, 4559, 5111, 5112, 5113, 5114, 5115, 5116,
       6188, 6189, 6190, 6191, 6752, 6753, 6754, 6755, 6756, 7261, 7262,
       7263, 7264, 7265, 7821, 7822, 7823, 7824, 7825, 7826, 8385, 8386,
       8387, 8388, 8389]),)

This array is basically the dips in a light curve. I want to select two more indices on either side of each dip. So essentially the array will look as follows:

(array([ **247, 248**, 249,  250,  251,  252,  253,  254, **255, 256**, **802, 803**, 804,  805,  806,  807,  808, 809, **810, 811** etc....]),)

First off, get the indices as an array with :

indices=np.where(f!=1)[0] # or use np.flatnonzero

So, we would have -

In [804]: indices
Out[804]: 
array([ 249,  250,  251,  252,  253,  254,  804,  805,  806,  807,  808,
        809, 1365, 1366, 1367, 1368, 1369, 1860, 1861, 1862, 1863, 1864,
       2424, 2425, 2426, 2427, 2428, 2948, 2949, 2950, 2951, 2952, 2953,
       3501, 3502, 3503, 3504, 3505, 3506, 4061, 4062, 4063, 4064, 4065,
       4555, 4556, 4557, 4558, 4559, 5111, 5112, 5113, 5114, 5115, 5116,
       6188, 6189, 6190, 6191, 6752, 6753, 6754, 6755, 6756, 7261, 7262,
       7263, 7264, 7265, 7821, 7822, 7823, 7824, 7825, 7826, 8385, 8386,
       8387, 8388, 8389])

Next up, we need to get the start and stop positions of each of those dips. Then, get extended numbers with some broadcasting . Finally, simply insert those with concatenation and sorting.

Hence, the implementation would be -

idx = np.r_[0,np.flatnonzero(np.diff(indices) > 1)+1,len(indices)]
start_pad = indices[idx[:-1]][:,None] + range(-2,0)
stop_pad = indices[idx[1:]-1][:,None] + range(1,3)
out = np.sort(np.r_[start_pad.ravel(), indices, stop_pad.ravel()])

Output would be -

In [832]: out
Out[832]: 
array([ 247,  248,  249,  250,  251,  252,  253,  254,  255,  256,  802,
        803,  804,  805,  806,  807,  808,  809,  810,  811, 1363, 1364,
       1365, 1366, 1367, 1368, 1369, 1370, 1371, 1858, 1859, 1860, 1861,
       1862, 1863, 1864, 1865, 1866, 2422, 2423, 2424, 2425, 2426, 2427,
       2428, 2429, 2430, 2946, 2947, 2948, 2949, 2950, 2951, 2952, 2953,
       2954, 2955, 3499, 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507,
       3508, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4553,
       4554, 4555, 4556, 4557, 4558, 4559, 4560, 4561, 5109, 5110, 5111,
       5112, 5113, 5114, 5115, 5116, 5117, 5118, 6186, 6187, 6188, 6189,
       6190, 6191, 6192, 6193, 6750, 6751, 6752, 6753, 6754, 6755, 6756,
       6757, 6758, 7259, 7260, 7261, 7262, 7263, 7264, 7265, 7266, 7267,
       7819, 7820, 7821, 7822, 7823, 7824, 7825, 7826, 7827, 7828, 8383,
       8384, 8385, 8386, 8387, 8388, 8389, 8390, 8391])

You might want to avoid using where / nonzero entirely:

dip = f != 1

# below is `scipy.ndimage.morphology.binary_dilation(dip, iterations=2)`
dip_adj1 = dip.copy()
dip_adj1[1:] |= dip[:-1]
dip_adj1[:-1] |= dip[1:]

dip_adj2 = dip_adj1.copy()
dip_adj2[1:] |= dip_adj1[:-1]
dip_adj2[:-1] |= dip_adj1[1:]

dip_adj2 is an array of bool , with True in the locations you want to keep. You can call np.nonzero(dip_adj2) if you really want the indices.

If you're using scipy, you can swap those 6 lines for binary_dilation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM