如何在Python中将多个高斯曲线拟合到质谱数据? [英] How can I fit multiple Gaussian curved to mass spectrometry data in Python?

查看:150
本文介绍了如何在Python中将多个高斯曲线拟合到质谱数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中将多条高斯曲线拟合到质谱数据.现在,我一次将数据拟合为一个高斯-实际上一次将一个范围拟合.

I would like to fit multiple Gaussian curves to Mass spectrometry data in Python. Right now I'm fitting the data one Gaussian at a time -- literally one range at a time.

是否有更简化的方法?有没有一种方法可以通过循环运行数据以在每个峰值处绘制高斯曲线?我猜想有一种更好的方法,但是我已经通过互联网进行了梳理.

Is there a more streamlined way to do this? Is there a way I can run the data through a loop to plot a Gaussian at each peak? I'm guessing there's gotta be a better way, but I've combed through the internet.

我的两个高斯曲线如下所示.

My graph for two Gaussians is shown below.

我的示例数据可以在以下位置找到: http://txt.do/dooxv

My example data can be found at: http://txt.do/dooxv

这是我当前的代码:

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt

from scipy.interpolate import interp1d

RGAdata = np.loadtxt("/Users/ilenemitchell/Desktop/RGAscan.txt", skiprows=14)
RGAdata=RGAdata.transpose()

x=RGAdata[0]
y=RGAdata[1]

# graph labels
plt.ylabel('ion current')
plt.xlabel('mass/charge ratio')
plt.xticks(np.arange(min(RGAdata[0]), max(RGAdata[0])+2, 2.0))
plt.ylim([10**-12.5, 10**-9])
plt.title('RGA Data Jul 25, 2017')

plt.semilogy(x, y,'b')

#fitting a guassian to a peak

def gauss(x, a, mu, sig):
return a*np.exp(-(x-mu)**2/(2*sig**2))


fitx=x[(x>40)*(x<43)]
fity=y[(x>40)*(x<43)]
mu=np.sum(fitx*fity)/np.sum(fity)
sig=np.sqrt(np.sum(fity*(fitx-mu)**2)/np.sum(fity))

print (mu, sig, max(fity))

popt, pcov = opt.curve_fit(gauss, fitx, fity, p0=[max(fity),mu, sig])
plt.semilogy(x, gauss(x, popt[0],popt[1],popt[2]), 'r-', label='fit')

#second guassian

fitx2=x[(x>26)*(x<31)]
fity2=y[(x>26)*(x<31)]
mu=np.sum(fitx2*fity2)/np.sum(fity2)
sig=np.sqrt(np.sum(fity2*(fitx2-mu)**2)/np.sum(fity2))

print (mu, sig, max(fity2))

popt2, pcov2 = opt.curve_fit(gauss, fitx2, fity2, p0=[max(fity2),mu, sig])
plt.semilogy(x, gauss(x, popt2[0],popt2[1],popt2[2]), 'm', label='fit2')

plt.show()

推荐答案

您可能会找到lmfit模块( https://lmfit.github.io/lmfit-py/)很有帮助.这提供了一个预先构建的GaussianModel类,用于将峰拟合到单个Gaussian,并支持将多个Model(不一定是Gaussians,而且还包括其他峰模型和其他可能对背景有用的函数)添加到可以合并的复合模型中.立刻适应.

You may find the lmfit module (https://lmfit.github.io/lmfit-py/) helpful. This provides a pre-built GaussianModel class for fitting a peak to a single Gaussian and supports adding multiple Models (not necessarily Gaussians, but also other peak models and other functions that might be useful for backgrounds and so for) into a composite model that can be fit at once.

Lmfit支持固定或给某些参数指定范围,因此您可以将模型建立为具有固定位置的高斯之和,从而将质心的值限制为在一定范围内变化(这样就不会混淆峰).此外,您可以对参数值施加简单的数学约束,以便可能要求所有峰宽均具有相同的大小(或以某种简单形式相关).

Lmfit supports fixing or giving a range to some Parameters, so that you could build a model as a sum of Gaussians with fixed positions, limiting the value for the centroid to vary with some range (so the peaks cannot get confused). In addition, you can impose simple mathematical constraints on parameter values, so that you might require that all peak widths are the same size (or related in some simple form).

尤其是,您可能会想到 https://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes 的示例,使用2高斯和背景函数.

In particular, you might look to https://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes for an example a fit using 2 Gaussians and a background function.

对于峰发现,我发现scipy.signal.find_peaks_cwt很好.

For peak finding, I've found scipy.signal.find_peaks_cwt to be pretty good.

这篇关于如何在Python中将多个高斯曲线拟合到质谱数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆