Fiber kabloların standart sapmaları

NUBA, Akdeniz Ãœniversitesi, 2015

Uncorrected sample standard deviation

$s_N = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \overline{x})^2}$

In [27]:
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

find data files and make a list of their name

In [28]:
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)
Total file number found is 28. 
array(['carsamba_USB2E9051_03-27-40-814.txt',
       'carsamba_USB2E9051_03-28-26-394.txt',
       'carsamba_USB2E9051_03-29-26-408.txt',
       'carsamba_USB2E9051_03-30-26-392.txt',
       'carsamba_USB2E9051_03-31-26-408.txt',
       'carsamba_USB2E9051_03-32-26-392.txt',
       'carsamba_USB2E9051_03-33-26-408.txt',
       'carsamba_USB2E9051_03-34-26-391.txt',
       'carsamba_USB2E9051_03-35-26-408.txt',
       'carsamba_USB2E9051_03-36-26-392.txt',
       'carsamba_USB2E9051_03-37-26-406.txt',
       'carsamba_USB2E9051_03-38-26-391.txt',
       'carsamba_USB2E9051_03-39-26-406.txt',
       'carsamba_USB2E9051_03-40-26-391.txt',
       'carsamba_USB2E9051_03-41-26-405.txt',
       'carsamba_USB2E9051_03-42-26-391.txt',
       'carsamba_USB2E9051_10-03-26-248.txt',
       'carsamba_USB2E9051_10-06-26-200.txt',
       'carsamba_USB2E9051_10-07-26-188.txt',
       'carsamba_USB2E9051_10-08-26-170.txt',
       'carsamba_USB2E9051_10-09-26-461.txt',
       'carsamba_USB2E9051_10-10-26-449.txt',
       'carsamba_USB2E9051_10-11-26-461.txt',
       'carsamba_USB2E9051_10-12-26-446.txt',
       'carsamba_USB2E9051_10-13-26-429.txt',
       'carsamba_USB2E9051_10-14-26-414.txt',
       'carsamba_USB2E9051_10-15-26-398.txt',
       'carsamba_USB2E9051_10-16-26-383.txt'], 
      dtype='|S35')

a file sample

%load carsamba_USB2E9051_03-27-40-814.txt

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

a function to read data files

In [29]:
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 and plot a sample

In [30]:
# read all data files
dataBegins = '>>>>>Begin Spectral Data<<<<<'
allData = [readDataFile(xfile, dataBegins) for xfile in data_files]
In [31]:
%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])
Out[31]:
[<matplotlib.lines.Line2D at 0x7f83e27810d0>]

calculate Standard Deviations

In [32]:
intenStdDev = [stdDev([eachData[2][i] for eachData in allData]) for i in range(len(allData[0][1]))]
In [33]:
# 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)
Out[33]:
[<matplotlib.lines.Line2D at 0x7f83e2789490>]
In [34]:
# 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])
Out[34]:
[<matplotlib.lines.Line2D at 0x7f83d9c25890>]

short list of STDs

In [35]:
zip(allData[0][1][:10], intenStdDev[:10])
Out[35]:
[(345.758, 0.46279802863249198),
 (346.12, 1.3437927496759434),
 (346.482, 1.3995109842005384),
 (346.844, 1.1281569650341665),
 (347.206, 1.2452781735788339),
 (347.568, 2.3238653968448908),
 (347.93, 1.0762052451051503),
 (348.292, 1.5283872969871992),
 (348.653, 1.2086395518625601),
 (349.015, 1.3335461299153644)]

write STDs to a file

In [41]:
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)
STDs can be found in carsamba_USB2E9051_03-27-40-814__to__10-16-26-383.txt

In [37]:
# 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)
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
28

In []:
# 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
X, Y =  [array([[  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431],
       [  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431],
       [  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431],
       ..., 
       [  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431],
       [  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431],
       [  345.758,   346.12 ,   346.482, ...,  1001.886,  1002.159,
         1002.431]]), array([[ 0,  0,  0, ...,  0,  0,  0],
       [ 1,  1,  1, ...,  1,  1,  1],
       [ 2,  2,  2, ...,  2,  2,  2],
       ..., 
       [25, 25, 25, ..., 25, 25, 25],
       [26, 26, 26, ..., 26, 26, 26],
       [27, 27, 27, ..., 27, 27, 27]])]
Z =  [[-411.71   11.59   14.89 ...,  106.49  105.49  106.79]
 [-411.31   14.59   17.99 ...,   92.69   91.59   91.89]
 [-411.12   12.58   16.08 ...,  106.78  110.08  104.98]
 ..., 
 [-411.38   11.92   14.52 ...,  107.12  109.52  107.62]
 [-410.49   13.01   15.21 ...,  108.21  107.91  108.01]
 [-410.84   14.06   13.76 ...,  107.46  108.46  108.36]]

In []:
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)
In []:
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)
In []: