In general your method of finding peaks is pretty good but you could do this in a better Python way. The current version of the code I'm checking is from commit @9545ad1 from 2 days ago. Here's your original function:
def NRMethod(Peaks):
Text = open("Data.txt", "r")
Counts = Text.read()
Text.close()
Counts = Counts.splitlines()
Counts.reverse()
Channel = []
for i in range(len(Counts)):
Counts[i] = float(Counts[i])
Channel.append(i+1)
Channel.reverse()
i = 0
Threshold = 50
j = 0
Aux = [[0 for i in range(1)] for j in range (Peaks)] #[ [], [], ..., [] ]
Temp = []
for i in range(len(Counts)):
if Counts[i] > Threshold:
Temp.append(Counts[i])
if Counts[i] <= Threshold:
Temp.append(0)
Counts.clear()
j = 0
i = 0
for i in range(len(Temp)-1):
if j == Peaks:
break
elif Temp[i] == 0 and Temp[i+1] != 0:
Aux[j].append(Temp[i+1])
#Counts.append(Channel[i])
elif Temp[i] != 0 and Temp[i+1] != 0:
Aux[j].append(Temp[i+1])
elif Temp[i] != 0 and Temp[i+1] == 0:
j = j + 1
elif Temp[i] == 0 and Temp[i+1] == 0:
()
print(Aux)
j = 0
Channel.clear()
for j in range(Peaks):
Channel.append(max(Aux[j]))
print(Channel[j])
In general your code:
Reads a text file and saves it.
Goes over the values that are above a certain threshold:
a. If they're above just add that value to a list (aka we started hitting a peak let's save these values)
b. We out of a peak domain, forget these counts (put to 0)
Take the maximum value of those peaks that are interesting.
Here's my code suggestion, it may help you understand some cool things about Python:
def NRMethod(Peaks):
#Use "with open" it will close the file after reading, no need to close it manually
with open("Data.txt", "r") as file:
Counts = [float(line) for line in file]
Threshold = 50
Channel = []
current_peak_counts = 0
current_peak = []
#Just iterate of the values of the list itself. Each "count" is an element of Counts
for count in (Counts):
#Remember, if you hit a peak the condition will enter this part always. So the "current_peak_counts" will just
#add the total sum of the area under the peak (this may even be useful for FWHM calculations).
if count > Threshold:
current_peak_counts += count
current_peak.append(count)
else:
#Just checking that our current_peak list is not empty, if it is then we're out of a peak and this will just skip this if statment
#but if it is not, then we're at the end of a peak (count is now <= Threshold) and we should save the max value of
#current_peak and put everything to zero (aka move to the next peak)
if current_peak:
Channel.append(max(current_peak))
current_peak_counts = 0
current_peak = []
#If the last peak is not added because the data ends with a peak (could be a problem, you should check it)
if current_peak:
Channel.append(max(current_peak))
print(Channel)
In general your method of finding peaks is pretty good but you could do this in a better Python way. The current version of the code I'm checking is from commit @9545ad1 from 2 days ago. Here's your original function:
In general your code:
Here's my code suggestion, it may help you understand some cool things about Python: