Python Forum

Full Version: Converting R code to python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
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.
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)
}
python code:

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
The problem is lines 7 and 8 in the Python program. The '+1's should be '+2's to account for differences in R and Python slices. That is, findPeaks is looking for numbers that are at least as high as the two numbers on either side, so you need to be looking at a three number range.

That code was a bit clunky, so I redid it and removed the np dependency:

def find_peaks(data_in, thresh = 0.0):
    peaks = [thresh - 1] * len(data_in)
    for center in range(1, len(data_in)):
        trip = data_in[(center - 1):(center + 2)]
        if max(trip) == trip[1]:
            peaks[center] = trip[1]
    return [index for index, peak in enumerate(peaks) if peak > thresh]
I might be able to clean up that for loop more, let me poke around in itertools.

Edit: It cleans up the loop at the expense of the conditional.

def find_peaks(data_in, thresh = 0.0):
    peaks = [thresh - 1]
    for trip in zip(*[data_in[n:] for n in range(3)]):
        if max(trip) == trip[1]:
            peaks.append(trip[1])
        else:
            peaks.append(thresh - 1)
    return [index for index, peak in enumerate(peaks) if peak > thresh]
You are right,
Thank you so much,