Array elements within a function aren't able to change?

Viewed 45
import numpy as np
from scipy.signal import argrelextrema

a = np.array([0,2,4,2,0,2,3,2,3,2,0])

def raycrit(a,thresh):
    min_ind = argrelextrema(a, np.less)
    max_ind = argrelextrema(a, np.greater)
    maxima = a[max_ind]
    minima = a[min_ind]
    if min_ind[0][0] > max_ind[0][0]:
        for i in range(0,len(minima)):
            if maxima[i] > thresh and maxima[i+1] > thresh:
                if minima[i] > maxima[i]/2 and minima[i] > maxima[i+1]/2:
                    minima[i] = thresh 
    a[min_ind] = minima
    return a
        
b = raycrit(a,2.5)

In the above code excerpt, I am trying to find local minima surrounded by local maxima that are more than half the height of the highest local maximum that neighbours it. And while this put into text might be confusing, I think reading the code helps understand what I'm trying to reach for. The problem arises with the fact that minima[i] = thresh stays unchanged even when the condition is triggered. Here the result should be [0,2,4,2,0,2,3,2.5,3,2,0] but remains [0,2,4,2,0,2,3,2,3,2,0] and I am quite confused as to why...

1 Answers

This is a dtype issue. You have the dtype of a set to int32/64 and when you try to update with a float (2.5), it updates it to 2 which is the value it already was at. Do a = np.array(...., dtype=float) to fix this.

Related