Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting R code to python
#1
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
Reply
#2
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]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You are right,
Thank you so much,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting python to FileMaker DWolf 6 1,603 Sep-22-2022, 03:40 AM
Last Post: DWolf
  Converting SQL Code To Python Code Query eddywinch82 13 27,856 Feb-15-2020, 06:42 PM
Last Post: buran
  Converting python to c# benahmadi 1 2,607 Oct-22-2019, 09:50 PM
Last Post: micseydel
  converting array to and from string in python 3.7.2 srm 5 6,082 Jul-03-2019, 01:11 PM
Last Post: snippsat
  help with converting C# function to Python korenron 17 11,155 May-19-2019, 10:26 AM
Last Post: Gribouillis
  converting from c# to python peper 1 2,919 Jun-12-2018, 05:19 PM
Last Post: micseydel
  converting python script to another version gptura 5 4,634 Mar-13-2018, 12:16 PM
Last Post: gptura
  Converting handwriting into code that can be recreated kill3rcat456 2 2,584 Mar-04-2018, 02:27 AM
Last Post: kill3rcat456
  Converting Pseudocode to Python ncp511 1 5,096 Jan-16-2018, 04:34 PM
Last Post: Windspar
  Resources for converting Python 2.x code to 3.x Luke_Drillbrain 4 4,950 May-03-2017, 06:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020