任何人都可以帮我优化我的代码吗?目前运行计算需要15分钟,而且我知道它应该可以在不到10秒的时间内实现,因为这是之前在Mathematica中实现的非常相似的事情。我需要模拟许多光谱,因此我需要更快的性能。
我编写了一个简单的代码来模拟不同分子的一种光发射光谱(拉曼散射)。这些拉曼光谱的特征在于发射波长和信号强度。
我正在处理具有许多谱线(350万)的复杂分子,这可能使下面描述的代码变得非常慢。
将棒谱(即波长和强度对)转换为模拟光谱时会出现问题,并考虑到光谱仪的光谱分辨率。通过在模拟中应用光谱仪分辨率,特定波长的每个离散谱线变为在一定波长范围内的光强度分布。每条线的线形是正态(高斯)分布。计算线形后,计算各波长的每条光谱线的强度之和,以确定整体光谱。
下面给出了从线转换为棒谱的这组方程式:
vlow = 2800.0 #cm-1
vhigh = 3050.0 #cm-1
dv = 1 #cm-1
resolution = 2. #cm-1
wavelength = np.arange(vlow,vhigh,dv)
def gauss(wavelength,v0,resolution):
I = np.exp(-((v0-wavelength)/(2*resolution))**2)
I /= np.sum(I*dv)
return I
I = np.zeros(len(wavelength))
def simulate(linelist):
if simulate_spec == True:
for line in linelist:
wn_line,sig_line = line
a = ((wn_line-vlow) % dv)/dv
b = int((wn_line-vlow) // dv)
if b>=0 and b<=len(I)-2:
I[b ] += sig_line * (1-a)
I[b+1] += sig_line * a
halfwayvelength = 0.5*(wavelength[0]+wavelength[-1])
Iconv = np.convolve(I,gauss(wavelength, halfwayvelength,
resolution)*dv,'same')
return(wavelength,Iconv)
我只是将模拟函数应用于数据列表,然后生成光谱。
示例数据如下:
[2988.051201, 2.1528594547391786],
[2965.874737, 9.832493405930505],
[2803.2158010000003, 0.00041319356649735474],
[2825.751201, 2.695035803386361e-06],
[2999.060832, 4.306173915906166],
[2826.208982, 0.00034324344563385156],
[2849.130431, 4.14575931249117e-06],
[2916.502513, 8.227955023939035e-07],
[3019.955186, 4.1128004082464935],
[2864.1083719999997, 4.7168887436524236e-07],
[2967.5610460000003, 2.3584443718262116],
[3011.59334, 8.484855475132457e-06],
[3013.1311739999996, 0.0017841866237857066],
[2810.959775, 1.6278821649562382e-05],
[2824.372448, 8.1122568270338e-06],
[2997.864286, 2.152180604795494],
[3044.539569, 0.000536291455511021],
[2966.429826, 3.932154056795204],
[3013.105109, 0.000979839907319629],
[2855.130571, 0.001800236014966169],
[2946.900868, 5.792331072221127],
[2992.882515, 0.016941583295928393],
[2830.159341, 4.1885065256659556e-05],
[2850.825762, 5.1388969852984256e-05],
[2916.542136, 5.781541962764192e-06],
[3018.591801, 3.0706663183839553],
[2843.208214, 6.239525546259207e-06],
[2945.257879, 3.3138244296836072],
[2991.205655, 0.0003465527450543083],
[2993.135932, 0.004361954391985438],
[2841.004186, 0.0008298581247320486],
[2849.245868, 0.00018996099882164256],
[2870.293698, 1.4363019628341073e-05],
[2875.746432, 2.100659304108956e-05],
[3039.611398, 1.0057174209518613],
[3041.328008, 5.840788673645993],
[2817.899557, 8.412353568584454e-05]