# Laser Induced Breakdown Spectroscopy Analysis¶

## Tutorial 4: LIBS Analysis using Python¶

Having looked at the options using tools of increasing complexity we come to using my favourite tool for LIBS Analysis - Python. I like Python because it is reasonably easy to start, and it encourages experimentation - not everything has to be right before you can start getting something out of it.

I use WinPython as it offers (at least on Windows) a version that works out of the box.

The source files for these tutorials are Jupyter notebooks which enable me to combine the code and the explanation together. The files are available from the Elementia Consulting Github if you want to try/adapt these for your own use.

The beauty of Jupyter notebooks is that they allow the combining together of Python script and an explanation of what is happening. If you are viewing this on the libs-info website, then the file won't be interactive - you will just the output. If you download the notebook files from the Github, then they can be run live and the results seen.

If you are only interested in the techniques used, then you can skip the Python code lines and their explanation. The pure code commentary will be marked with italics

In the next could of lines, we initialise the Python code that we need to run the calculations. We will be using the Bokeh for the charts

In [1]:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import Range1d
from IPython.display import display, Markdown, HTML

from elementia.tutorial_fns import calculate_cross_validation, calculate_intensity_cv, calculate_sum_cv

In [2]:
output_notebook()


### Importing the data¶

For this work we will use the following samples, and analyse for Sodium (Na)

Sample Na. Conc (ppm)
OREAS45e 594
OREAS501b 20848
OREAS601 14543
OREAS921 6068
OREAS603 4283
OREAS933 1515
OREAS903 301

To do this, we use the Python Data Analysis Library (PANDAS). This package is included in most Python implementations.

PANDAS has a read_csv function that handles the import of the file, placing into a PANDAS dataframe, which has a range of usefule features which we will take advantage of - the first of which is that we can use the column heading to label the data

In [3]:
conc_data = pd.read_csv('.\\files\\Na_concs.csv')
print(conc_data)

   Sample  Na Conc (ppm)
0  501b_6          20848
1   601_7          14543
2   921_7           6068
3   603_7           4283
4   933_5           1515
5   45e_7            594
6   903_5            301


In this case we will import the same source CSV file as we have used previously and plot all the spectra

If you are using notebook file, then the tools to the left of the chart allow zooming, panning and saving the image to file

In [4]:
# load the data from file
# and display the first 5 lines

#use the Bokeh library, start a chart (or figure)
p1 = figure()

#on our new figure, display the intensities we have loaded from the files.  One of the many beauties of the PANDAS library we used to do
# the import is that it allows us to use the column labels from the file to represent the columns of data.
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['501b_6'], legend='OREAS 501b', color='red')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['601_7'], legend='OREAS 601', color='orange')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['921_7'], legend='OREAS 921', color='yellow')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['603_7'], legend='OREAS 603', color='green')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['933_5'], legend='OREAS 933', color='blue')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['45e_7'], legend='OREAS 45e', color='indigo')
p1.line(x=spectra_data['Wavelength (nm)'], y=spectra_data['903_5'], legend='OREAS 903', color='violet')

show(p1)

   Wavelength (nm)  501b_6  601_7  921_7  603_7  933_5  45e_7  903_5
0         186.4672   206.1   73.9  278.1   57.7  395.6   88.9  116.5
1         186.5354   204.8   85.3  283.4   75.2  392.7   90.3  117.3
2         186.6037   211.0   79.8  285.5   70.5  402.3   92.7  132.2
3         186.6720   218.9   94.6  293.1   72.7  408.3   94.5  137.6
4         186.7402   229.7   94.4  300.6   88.2  415.1  105.0  142.8