def get_minima(array):
sdiff = np.diff(np.sign(np.diff(array)))
rising_1 = (sdiff == 2)
rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1)
rising_all = rising_1
rising_all[1:] = rising_all[1:] | rising_2
min_ind = np.where(rising_all)[0] + 1
minima = list(zip(min_ind, array[min_ind]))
return sorted(minima, key=lambda x: x[1])
by running this code with the array of data i have, it yields:
[(59, 7.958373616052042e-10),
(69, 6.5364637051479655e-09),
(105, 1.0748381102806489e-08),
(88, 2.953895857338913e-07),
(27, 9.083111768048306e-07)]
which is great - its all the minimums in my dataset. But i need to store only the minimum - which is the (59, 7.958373616052042e-10) point in this particular example. I cant figure out how to do this. I Tried some stuff with using np.amin and doing a boolean comparison but i got quite confused with the notation and syntax, since now its an array of list and i havent ever really worked with that before.
Appreciate any help!
Instead of sorting all the minimums, you can just get the lowest pair:
def get_minima(array):
sdiff = np.diff(np.sign(np.diff(array)))
rising_1 = (sdiff == 2)
rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1)
rising_all = rising_1
rising_all[1:] = rising_all[1:] | rising_2
min_ind = np.where(rising_all)[0] + 1
minima = list(zip(min_ind, array[min_ind]))
return min(minima, key=lambda pair: pair[1])
For example:
minima = [(59, 7.958373616052042e-10),
(69, 6.5364637051479655e-09),
(105, 1.0748381102806489e-08),
(88, 2.953895857338913e-07),
(27, 9.083111768048306e-07)]
minimum = min(minima, key=lambda pair: pair[1])
print(minimum)
>>> (59, 7.958373616052042e-10)