我有一系列索引,如下所示:
data-tooltip
给出以下数组:
indices=np.where(f!=1)
这个数组基本上是光线曲线的下降。我想在每次下降的两侧选择两个以上的指数。所以基本上这个数组看起来如下:
(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]),)
答案 0 :(得分:1)
首先,将索引作为数组获取:
indices=np.where(f!=1)[0] # or use np.flatnonzero
所以,我们会 -
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])
接下来,我们需要获得每个逢低的开始和停止位置。然后,使用一些broadcasting
获取扩展数字。最后,只需插入那些连接和排序。
因此,实施将是 -
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()])
输出将是 -
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])
答案 1 :(得分:0)
您可能希望完全避免使用where
/ nonzero
:
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
是一个bool
数组,其中True
位于您要保留的位置。如果您真的想要索引,可以致电np.nonzero(dip_adj2)
。
如果您正在使用scipy,则可以将这6行替换为binary_dilation