Mar-09-2019, 01:28 PM
I´m trying to convert the following R code to python but theres something wrong with my code results are not the same can you take a look at it? Thank you.
With this input vector or list:
wtr = c(1,1,10,12,11,1) or wtr = [1,1,10,12,11,1]
R code:
# Finds the local peaks in a vector. Checks the optionally supplied threshold
# for minimum height.
python code:
With this input vector or list:
wtr = c(1,1,10,12,11,1) or wtr = [1,1,10,12,11,1]
Output:The correct ouput should be 4 in R and 3 in python.
The output is an index of a value therefore there´s a difference in the output obtained with each code.R code:
# Finds the local peaks in a vector. Checks the optionally supplied threshold
# for minimum height.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
findPeaks < - function(dataIn, thresh = 0 ){ varL = length(dataIn); locs = rep(FALSE, varL); peaks = rep(NaN, varL); for (i in 2 :varL - 1 ){ pkI = which. max (dataIn[(i - 1 ):(i + 1 )]) posPeak = max (dataIn[(i - 1 ):(i + 1 )]); if (pkI = = 2 ) { peaks[i] = posPeak; locs[i] = TRUE; } } inds = 1 :varL; locs = inds[locs]; peaks = peaks[locs]; # remove all below threshold value useI = peaks > thresh; locs = locs[useI]; return (locs) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def findPeaks(dataIn, thresh = 0.0 ): varL = len (dataIn) locs = [ False ] * varL peaks = [ None ] * varL for i in range ( 1 ,varL): pKi = np.argmax(dataIn[(i - 1 ):(i + 1 )]) posPeak = np. max (dataIn[(i - 1 ):(i + 1 )]) if (pKi = = 1 ): peaks[i] = posPeak locs[i] = True locs = [i for i, e in enumerate (locs) if e = = True ] peaks = [dataIn[i] for i in locs] # remove all below threshold value useI = [a for a in peaks if a > thresh] locs = [i for i in range (varL) if dataIn[i] in useI] return locs |