Skip to content

Instantly share code, notes, and snippets.

@ashishsinghucd
Created November 17, 2020 14:18
Show Gist options
  • Save ashishsinghucd/7ff0f047e489172b6bebc3ac249197ab to your computer and use it in GitHub Desktop.
Save ashishsinghucd/7ff0f047e489172b6bebc3ac249197ab to your computer and use it in GitHub Desktop.
Function to find all the regions in a numpy array greater than a threshold
def get_segments(weights, threshold=80):
marker_list = [True if i >= threshold else False for i in weights]
i = 0
final_pairs = []
while i < len(weights):
if marker_list[i]:
start = i
while i < len(weights) and marker_list[i]:
i = i + 1
end = i - 1
if end-start > 1:
final_pairs.append(start)
final_pairs.append(end)
i = i + 1
return np.array(final_pairs)
"""
weights = np.array([10, 20, 80, 81, 89, 1, 2, 90, 91, 100, 1, 2, 80])
print(get_segments(weights))
Output: [2 4 7 9]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment