python - How to check if a neighbourhood of at least five elements of an array satisfies a criterion? -
i have numpy array, , check local minima lower threshold (mean value - 3 * standard deviation). out of minima want select in neighbourhood of @ least 5 points below threshold value. if neighbourhood contains multiple minima, want know minimum has lowest value. how , make run relatively fast?
code similar 1 suggested b.m. doesn't quite need.
from numpy import * a=random.rand(10) n = ones(7) threshold=0.5 u=convolve(a<t,n,'same')
this produced: x array([ 0.6034448 , 0.16098872, 0.39563129, 0.33611677, 0.90138981, 0.26088853, 0.45720198, 0.100786 , 0.47705187, 0.15514734]) u array([ 3., 3., 4., 5., 6., 6., 6., 5., 5., 4.])
which suggests element @ index 6 part of neighbourhood of 6 points below threshold value. guess counted element index 3, not desirable behaviour, there value > 0.9 @ position 4. , element @ position 9 claims in group of 4 elements, while group of 5.
this current solution problem:
layer = xa while layer > overlap: if d[layer] > d[layer+1] , d[layer] > d[layer-1]: if layer > 300: threshold = threshold_free else: threshold = threshold_pbl if d[layer] <= threshold: upper_limit = layer lower_limit = layer k = 1 kp = 0 while kp < k , layer + kp < xa: kp = k if d[layer+k] <= threshold: upper_limit = layer + k k += k k = 1 kp = 0 while kp < k , layer - kp > overlap: kp = k if d[layer-k] <= threshold: lower_limit = layer - k k += k transition_interval = upper_limit - lower_limit if transition_interval >= 5: print layer, upper_limit, lower_limit, upper_limit - lower_limit layer = lower_limit if valid_time in layers: layers[valid_time].append(layer) else: layers[valid_time] = [layer] layer -= 1
some tricks that:
from numpy import * matplotlib.pyplot import * scipy.signal import convolve2d scipy.ndimage.filters import minimum_filter mini a=random.rand(100,100) neighbours = ones((3,3)) threshold=0.2 u=convolve2d(a<threshold,neighbours,'same') mins=((u>=6)*(a<threshold)) minis=mini(choose(mins,(1,a)),size=(3,3))==a subplot(121);imshow(mins,cmap=cm.gray_r,interpolation='none') subplot(122);imshow(minis,cmap=cm.gray_r,interpolation='none')
this script produce:
on left figure have 5 neighbours, on right min selected. if want indices , values, use inds=mask_indices(100,lambda x,k: minis)
, a[inds]
.
Comments
Post a Comment