python 使用两个Bruker元素定量输出文件,从另一个中减去一个的计数。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用两个Bruker元素定量输出文件,从另一个中减去一个的计数。相关的知识,希望对你有一定的参考价值。

# Created 2015, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
import wx
import pandas as pd
import sys, os

if 'app' not in locals():
    app = wx.App(None)

def FileDialog(DefaultDir='', Title='Select File:', Extension=''):
    dlg = wx.FileDialog (None, Title, DefaultDir, '', Extension, wx.FD_DEFAULT_STYLE | wx.FD_FILE_MUST_EXIST)
    if dlg.ShowModal() == wx.ID_OK:
        FileName = dlg.GetPath()
    else:
        FileName = None
    dlg.Destroy()

    return FileName

SFileName = FileDialog(Title='Select Bruker quant results file for the spectrum with a background:')
BFileName = FileDialog(Title='Select Bruker quant results file for the background spectrum:')
NormElement = raw_input('Choose normalization element (e.g. S):')

#SFileName = '/Volumes/Stardust/Desktop/Current samples/Dante Foils/Foil 2 ACI2,05, 500C/20150422 - TitanX - Dante foil ACI2,05/Sulfide Metal interface 200 keV/EDS/Bruker Spectra/Metal whisker Bruker Results.txt'
#BFileName = '/Volumes/Stardust/Desktop/Current samples/Dante Foils/Foil 2 ACI2,05, 500C/20150422 - TitanX - Dante foil ACI2,05/Sulfide Metal interface 200 keV/EDS/Bruker Spectra/Metal whisker background Bruker Results.txt'
#NormElement='S'

S = np.genfromtxt(SFileName, skip_header=5, skip_footer=2, dtype=None)
B = np.genfromtxt(BFileName, skip_header=5, skip_footer=2, dtype=None)

if S.shape != B.shape:
    print 'Both spectra must have the same element list.'
    
# Drop it into pandas, and do a sort on elemental Z.  Then 
pdS = pd.DataFrame(S).sort('f1')
pdB = pd.DataFrame(B).sort('f1')

if np.any(pdS['f0'].as_matrix() != pdB['f0'].as_matrix()):
    print 'Both spectra must have the same element list.'

# Get the list of elements for reporting.  Columns are 0: Element Name, 3: Counts.
CountsArray = np.vstack((pdS.as_matrix()[:,(0,3)].T, pdB.as_matrix()[:,3]))

# Now we have three rows: Element names, spectrum counts, background counts.  We'll make a new row which is the normalized counts for the background.
# First get the index of the normalization element.
try:
    NormIdx = np.where(CountsArray[0,:]==NormElement)[0][0]
except:
    print "Normalization element wasn't found!"
    sys.exit()
        
print 'Normalizing to ' + NormElement

# We multiply the background row by the appropriate amount so that the counts in the normalization element are the same
# as the counts in the main spectrum.
NormBkg = CountsArray[2, :] * (CountsArray[1,NormIdx]*1. / CountsArray[2,NormIdx])
CountsArray = np.vstack((CountsArray, NormBkg))

# And now we add one more row which is the corrected counts after removing the normalized background.
CountsArray = np.vstack((CountsArray, CountsArray[1,:]-CountsArray[3,:]))

# And back to pandas for the final formatting and output.
CountsTable = pd.DataFrame(CountsArray.T)
CountsTable.columns = ['Element', 'Counts', 'Bkg Counts', 'Norm Bkg Counts', 'Corr Counts']

print CountsTable

print 'Saving xls.'

CountsTable.to_excel(os.path.splitext(SFileName)[0] + ' minus background.xls')

print 'Done.'

以上是关于python 使用两个Bruker元素定量输出文件,从另一个中减去一个的计数。的主要内容,如果未能解决你的问题,请参考以下文章

迭代Python列表中的每两个元素[重复]

python爬虫同时输出两个列表(zip函数)

如何比较两个目录并在python中输出更改的文件

比较python中的两个CSV文件并输出

Python使用difflib模块比较两个文件内容异同,同时输出html易浏览

Leetcode 350.两个数组的交集|| By Python