python 从stoichiometry fitter读入stoichout文件,处理它们并根据数据生成LaTeX表以供发布。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从stoichiometry fitter读入stoichout文件,处理它们并根据数据生成LaTeX表以供发布。相关的知识,希望对你有一定的参考价值。


# coding: utf-8

# In[1]:

# Created 2017, Zack Gainsforth
get_ipython().magic('pylab qt')
import sys, os
import QuickPlot
import matplotlib.pylab as pylab
pylab.rcParams['figure.figsize'] = 8, 6  # that's default image size for this interactive session
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
from ipywidgets.widgets import interactive, fixed, interact
get_ipython().magic("config InlineBackend.figure_format = 'retina'")
from tabulate import tabulate


# In[2]:

import re
from StringIO import StringIO

def GetChunkFromTextFile(FileName, StartStr, StopStr, skip_header=0, skip_footer=0, LastHit=True, DataType='array'):
    # DataType means we can extract the chunk and then turn it into:
    # 1) Numpy table 'numpy'
    # 2) return the raw text 'raw'
    DataType = DataType.lower()

    # Read the file.
    try:
        with open(FileName, 'r') as myfile:
            data = myfile.read()
    except:
        print('Failed to open ' + FileName + '.  Skipping.')
        return

    # This regex looks for the data between the start and top strings.
    reout = re.compile('%s(.*?)%s' % (StartStr, StopStr), re.S)
    try:
        # Extract just the data we want.
        if LastHit == False:
            SectionStr = reout.search(data).group(1)
        else:
            SectionStr = reout.findall(data)[-1]
    except:
        # It is possible that the user asked for something that isn't in the file.  If so, just bail.
        return None

    if DataType == 'raw':
        # Now apply skip_header and skip_footer
        SectionData = SectionStr
        SectionData = ''.join(SectionData.splitlines(True)[skip_header:])
        if skip_footer > 0:
            SectionData = ''.join(SectionData.splitlines(True)[:-skip_footer])

    if DataType == 'float':
        SectionData = np.float(SectionStr)

    if DataType == 'array':
        # Convert it into a numpy array.
        SectionData = np.genfromtxt(StringIO(SectionStr), skip_header=skip_header, skip_footer=skip_footer, dtype=None)

    return SectionData


# In[3]:

def ReadOneStoichout(FileName):
    # Change ppm lines into numbers that can be read in automatically.
    get_ipython().system('sed -r \'s/([0-9]*) ppm\\s*([0-9]*) ppm\\s*([0-9]*) ppm/\\1e-4 \\2e-4 \\3e-4/\' "{FileName}" > "{FileName+\'noppm.txt\'}"')

    # Read in the array with the AtPct, WtPct, OxWtPct.
    Compo = GetChunkFromTextFile(FileName+'noppm.txt', 'Quantification results:', '---', skip_header=2, skip_footer=0, DataType='array')

    # Get the list of elements, and OxWtPcts.
    Elements = [q[0] for q in Compo]
    OxWtPct = [q[3] for q in Compo]

    # Verify the element list:
    assert Elements == ['O', 'Mg', 'Al', 'Si', 'S', 'K', 'Ca', 'Cr', 'Mn', 'Fe', 'Ni']

    # Read in the array with the chondritic norms.
    Chond = GetChunkFromTextFile(FileName+'noppm.txt', r'Abundances ratioed to protosolar and normalized to:\nElement to   Mg       Si       Fe\n-----------------------------------------', '-----------------------------------------', 
                                 skip_header=1, skip_footer=0, DataType='array')
    # Get the list of elements and abundance/Mg/Chondritic.
    Elements = [q[0] for q in Chond]
    ChondMg = [q[1] for q in Chond]
    # Verify the element list:
    assert Elements == ['O', 'Mg', 'Al', 'Si', 'S', 'K', 'Ca', 'Cr', 'Mn', 'Fe', 'Ni']

    return Elements, OxWtPct, ChondMg


# In[4]:

ReadOneStoichout('Object 1 Bruker Quant.txt.trimmed stoichout.txt')


# In[5]:

Objects = [1,2,3,4,5,6,7,8,9,11,12,13,14]
Elements = ['O', 'Mg', 'Al', 'Si', 'S', 'K', 'Ca', 'Cr', 'Mn', 'Fe', 'Ni']
Oxides = ['MgO', 'Al$_2$O$_3$', 'SiO$_2$', 'S', 'K$_2$O', 'CaO', 'Cr$_2$O$_3$', 'MnO', 'FeO', 'NiO']

OxWtTable = np.zeros((len(Oxides)+1,len(Objects)))
ChondMgTable = np.zeros((len(Oxides)+1,len(Objects)))

for n, i in enumerate(Objects):
    FileName='Object '+str(i)+' Bruker Quant.txt.trimmed stoichout.txt'
    print FileName
    Vals = ReadOneStoichout(FileName)
    OxWtTable[:,n] = Vals[1]
    ChondMgTable[:,n] = Vals[2]

print tabulate(OxWtTable[1:,:].T, headers = Oxides)
print tabulate(ChondMgTable[1:,:].T, headers = Oxides)


# In[43]:

tablefmt='latex'
floatfmt='.2f'
print "Oxide Weight Percents: "
print "Object numbers in sequence: " + str(Objects)

ElementOrder = [2, 1,6, 0,8,9,5,7,4, 3]
SortedOxides = list(np.array(Oxides)[ElementOrder])
SortedElements = list(np.array(Elements[1:])[ElementOrder])
SortedElements.append('Mean')
SortedElements.append('\sigma')

get_ipython().magic('pdb')
SortedOxWtTable = OxWtTable[1:,:].T
SortedOxWtTable = SortedOxWtTable[:,ElementOrder]
#Sums = np.sum(SortedOxWtTable, axis=1)
#SortedOxWtTable = np.hstack((SortedOxWtTable, Sums[:,np.newaxis]))
print tabulate(SortedOxWtTable, headers = SortedOxides, tablefmt=tablefmt, floatfmt=floatfmt)

print '\n\nElement Abundances: At% El/Mg/Chond:'

# Rebuild the chondritic normalized to Mg table but with the elements in the same order as the OxWtPct table.
SortedChondMgTable = ChondMgTable[1:,:].T
SortedChondMgTable = SortedChondMgTable[:,ElementOrder]
# Add a mean and stdev column on the right.
Meanrow = np.mean(SortedChondMgTable, axis=1)
Stdevrow = np.round(np.std(SortedChondMgTable, axis=1)/Meanrow*100) # This is as a percent.
SortedChondMgTable = np.hstack((SortedChondMgTable, Meanrow[:,None]))
SortedChondMgTable = np.hstack((SortedChondMgTable, Stdevrow[:,None]))
# And add mean and stdev rows on the bottom (means and stdev for each element across all the particles.)
Meanrow = np.mean(SortedChondMgTable, axis=0)
Stdevrow=np.round(np.std(SortedChondMgTable, axis=0)/Meanrow*100)
SortedChondMgTable = np.vstack((SortedChondMgTable, Meanrow))
SortedChondMgTable = np.vstack((SortedChondMgTable, Stdevrow))
# Clear out that corner in the lower right:
SortedChondMgTable[-2:, -2:] = 0

print tabulate(SortedChondMgTable, headers = SortedElements, tablefmt=tablefmt, floatfmt=floatfmt)


# In[ ]:




# In[ ]:



以上是关于python 从stoichiometry fitter读入stoichout文件,处理它们并根据数据生成LaTeX表以供发布。的主要内容,如果未能解决你的问题,请参考以下文章

如何从curve_fit获得置信区间

python中的rfecv.fit()不接受我的x和y参数

Python初探——sklearn库中数据预处理函数fit_transform()和transform()的区别

将 clf.fit 与 csv 中的 numpy 数组一起使用

NLP - 'SklearnClassifier' 对象没有属性 'fit' / Python

Python SKlearn fit() 监控 TypeError