$s_N = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \overline{x})^2}$
def stdDev(data):
"""
other examples from other Python libs
to calculate standard deviation
-------------------------------------
import numpy
numpy.std(data)
import statistics
statistics.stdev(data)
http://www.macroption.com/population-sample-variance-standard-deviation/
"""
data_av = sum(data)/len(data)
data_std = (sum([(element - data_av)**2 for element in data])/len(data))**.5
return data_std
import os
from pprint import pprint
path = './'
extension = "txt"
fkeyword1 = "carsamba_USB2E9051"
fkeyword2 = "__to__"
data_files = sort([f for f in os.listdir(path) if f.count(fkeyword1)>0 and f.count(fkeyword2)==0 and f.endswith('.'+extension)])
print "Total file number found is %s. " %len(data_files)
pprint(data_files)
Data from carsamba_USB2E9051_03-27-40-814.txt Node
Date: Wed Jan 14 03:27:40 VET 2015
User: PRO2000
Spectrometer: USB2E905
Autoset integration time: false
Trigger mode: 3
Integration Time (sec): 3.000000E-2
Scans to average: 10
Electric dark correction enabled: true
Nonlinearity correction enabled: false
Boxcar width: 0
Stray light correction enabled: false
XAxis mode: Wavelengths
Stop averaging: false
Number of Pixels in Spectrum: 2048
>>>>>Begin Spectral Data<<<<<
345.758 -411.71
346.12 11.59
346.482 14.89
346.844 19.09
347.206 19.09
347.568 29.39
def readDataFile(filename, dataBegins):
"""
This function reads the data files.
"""
# open the file
f = open(filename, 'r')
# make a list of raw text lines
dataStripped = map(lambda dressedText: dressedText.strip(), f.read().strip().split("\n"))
# read header of the data file
fileheader = dataStripped[:dataStripped.index(dataBegins)+1]
# make a list of data points as strings
dataText = map(lambda merged:merged.split(), dataStripped[dataStripped.index(dataBegins)+1:])
# convert text to float data
dataLambda = map(lambda element: float(element[0]), dataText)
dataIntens = map(lambda element: float(element[1]), dataText)
f.close()
return fileheader, dataLambda, dataIntens
# read all data files
dataBegins = '>>>>>Begin Spectral Data<<<<<'
allData = [readDataFile(xfile, dataBegins) for xfile in data_files]
%matplotlib inline
import matplotlib.pylab as plt
# plot a sample file
title_font = {'size':'14'}
axis_font = {'size':'18'}
plt.title(allData[0][0][0], **title_font)
plt.xlabel('$\lambda$', **axis_font )
plt.ylabel('Intensity', **axis_font )
plt.plot(allData[0][1], allData[0][2])
intenStdDev = [stdDev([eachData[2][i] for eachData in allData]) for i in range(len(allData[0][1]))]
# plot standard deviations vs lambda's
plt.title("Standard Deviations of the Intensities", **title_font)
plt.xlabel('$\lambda$', **axis_font )
plt.ylabel('STDs', **axis_font )
plt.plot(allData[0][1], intenStdDev)
# plot intensities from different data files for same lambda
plt.title("Intensities for $\lambda$ = %g"%allData[0][1][1], **title_font)
plt.xlabel("file sorting number", **axis_font)
plt.ylabel("Intensity", **axis_font)
plt.plot([eachData[2][1] for eachData in allData])
zip(allData[0][1][:10], intenStdDev[:10])
from tabulate import tabulate
resultFname = data_files[0][:-4] +"__to__"+ data_files[-1].replace("carsamba_USB2E9051_","")
print "STDs can be found in", resultFname
f = open(resultFname, 'w')
tabulatedData = tabulate(zip(allData[0][1], intenStdDev), tablefmt="plain")
f.write(tabulatedData)
# check if lambda columns whether they are all equal or not.
trueList = [allData[0][1] == eachdata[1] for eachdata in allData]
print trueList
print len(trueList)
# http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb
import matplotlib.cm as cm
from numpy import *
from mpl_toolkits.mplot3d.axes3d import Axes3D
mesh = meshgrid(allData[0][1], range(len(data_files)))
X, Y = mesh
Z = array([eachData[2] for eachData in allData])
print "X, Y = ", mesh
print "Z = ", Z
title_font = {'size':'14'}
axis_font = {'size':'18'}
fig = plt.figure(figsize=(14,6))
# `ax` is a 3D-aware axis instance because of the projection='3d' keyword argument to add_subplot
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.set_xlabel("$\lambda$", **axis_font)
ax.set_ylabel("file sorting #", **axis_font)
ax.set_zlabel("Intensity", **axis_font)
p = ax.plot_surface(X, Y, Z, rstride=4, cstride=4, linewidth=0)
# surface_plot with color grading and color bar
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.set_xlabel("$\lambda$", **axis_font)
ax.set_ylabel("file sorting #", **axis_font)
ax.set_zlabel("Intensity", **axis_font)
p = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
cb = fig.colorbar(p, shrink=0.5)
fig = plt.figure(figsize=(14,6))
# surface_plot with color grading and color bar
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.set_xlabel("$\lambda$", **axis_font)
ax.set_ylabel("file sorting #", **axis_font)
ax.set_zlabel("Intensity", **axis_font)
ax.view_init(azim=180)
p = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
cb = fig.colorbar(p, shrink=0.5)