我有一堆代码,可将质谱峰与质谱图分离,并将峰的值放入两个列表中。 Gaussian_x和gaussian_x。现在,我需要拟合此曲线,理想情况下,使用Levenberg-Marquardt或类似算法进行拟合,以便在拟合后即可计算出峰面积。
对于双高斯峰(峰的前半部分和后半部分的sigma值不同的峰)并没有太多帮助,因此我努力寻找一种方法来使其正常工作
.as-console-wrapper { max-height: 100% !important; top: 0; color: blue; background: #fff}
当我绘制它时,图上只有一条直线,y = 0,然后是两个列表的图。 我不知道为什么yvalue不会在best_fit和init_fit中注册。
这是完整的代码:
yvals = np.asarray(gaussian_y)
model = SkewedGaussianModel()
# set initial parameter values
params = model.make_params(amplitude=a, center=b, sigma=c, gamma=d)
# adjust parameters to best fit data.
result = model.fit(yvals, params, x=xvals)
print(result.fit_report())
plt.plot(xvals, yvals)
plt.plot(xvals, result.init_fit)
plt.plot(xvals, result.best_fit)
plt.show()
健身报告说:
import pymzml
import numpy as np
import matplotlib.pyplot as plt
from lmfit.models import SkewedGaussianModel
import filepath
TARGET_MASS = 152
MASS_WIDTH = 1
PEAK_CENTER = 0.9
PEAK_WIDTH = 0.1
gaussian_x = []
gaussian_y = []
a = 1
b = 400
c = 100
d = 280
def main(in_path):
x_array = []
y_array = []
run = pymzml.run.Reader(in_path)
for spectrum in run:
if spectrum.ms_level == 1:
mass_array = []
intensity_array = []
target_array = []
# change tuple into array.
my_tuple = spectrum.peaks("raw")
my_array = np.asarray(my_tuple)
# append the first element of each index to mass array and second element to intensity array.
for i in my_array:
mass_array.append(i[0])
intensity_array.append(i[1])
# for index and value in mass_array, absolute value of mass - target mass taken.
# append the abs_mass values to a separate array.
for i, mass in enumerate(mass_array):
abs_mass = abs(mass - TARGET_MASS)
target_array.append(abs_mass)
# if the absolute mass values are less than selected mass_width, append the value in the intensity_array
# at that same index same index
if abs_mass < MASS_WIDTH:
y_array.append(intensity_array[i])
x_array.append(spectrum.scan_time_in_minutes())
new_x = []
new_y = []
# loop through x values
for i, x1 in enumerate(x_array):
# temporary store for where there are multiple y values for the same x value
y_temp = []
# ensure we only run through a search for each UNIQUE x value once
if x1 in new_x:
# moves onto the next for loop interation without executing the below
continue
# add unique x value to new_x
new_x.append(x1)
# loop through the x values again, looking for where there are duplicates
for j, x2 in enumerate(x_array):
if x1 == x2:
# where we find a duplicate entry, add the value of y to a temporary array
y_temp.append(y_array[j])
# after accumulating all values of y for the same x value,
# add it to the new_y array
new_y.append(max(y_temp))
lower_bound = PEAK_CENTER - (PEAK_WIDTH / 2)
upper_bound = PEAK_CENTER + (PEAK_WIDTH / 2)
# for each index and value in retention time array
for i, x in enumerate(new_x):
# if x greater than the lower bound and smaller than the upper bound then append x and y value
# to new lists
if lower_bound < x < upper_bound:
gaussian_x.append(x)
gaussian_y.append(new_y[i])
# if x greater than upper bound, stop function
if x > upper_bound:
break
xvals = np.asarray(gaussian_x)
yvals = np.asarray(gaussian_y)
model = SkewedGaussianModel()
# set initial parameter values
params = model.make_params(amplitude=a, center=b, sigma=c, gamma=d)
# adjust parameters to best fit data.
result = model.fit(yvals, params, x=xvals)
print(result.fit_report())
plt.plot(xvals, yvals)
plt.plot(xvals, result.init_fit)
plt.plot(xvals, result.best_fit)
plt.show()
if __name__ == "__main__":
main(in_path)