-
- -
-

Examples

-

You can consult the help of each function/class/method on this website, please click on the rampy package item on the navigation bar on the left side.

-

Below, some quick examples of the use of those functions/classes are shown to highlight some of the possibilities offered by Rampy.

-

For complete examples of use, please directly see the example Jupyter notebooks here

-
-

Library importation

-

This is done with the usual Python way. A useful shortname for rampy is rp:

-
# importing rampy
-import rampy as rp
-# and for numpy we will respect the usual name:
-import numpy as np
-# for matplotlib
-import matplotlib.pyplot as plt
-
-
-
-
-

Data importation

-

This is done using directly Pandas or numpy, except for maps for which rampy provides a function to import the CSV files generated by -the Renishaw or Horiba Raman spectrometers.

-

To import a spectrum saved in a 2 column text file “data.txt” with space separator, we can use the numpy genfromtxt function:

-
import numpy as np
-spectrum = np.genfromtxt("data.txt")
-
-
-

If our file has a 10 line header, we can skip it like

-
spectrum = np.genfromtxt("data.txt", skip_header=10)
-
-
-

See the documentation of numpy for further details, as well as that of Pandas.

-
-
-

Plot a spectrum

-

This can be done with Matplotlib directly. For instance, to plot our spectrum we can do:

-
plt.figure(figsize=(5,5))
-plt.plot(spectrum[:,0],spectrum[:,1],'k.',markersize=1)
-plt.xlabel("Raman shift, cm$^{-1}$", fontsize = 12)
-plt.ylabel("Normalized intensity, a. u.", fontsize = 12)
-plt.title("Fig. 1: the raw data",fontsize = 12,fontweight="bold")
-
-
-

This will give the image:

-_images/spectrum.png -
-
-

Flipping

-

Some spectra come with decreasing X values. Rampy offers a simple function to flip them. This can be necessary to resample them (as interpolation algorithms usually require the X values to increase). Here for our spectrum we can do:

-
spectrum_increasing = rp.flipsp(spectrum)
-
-
-
-
-

Resampling

-

We need sometime to resample a spectrum with a new X axis. rampy.resample() offers such ability. For instance, -we have a spectrum that has a X axis from 400 to 1300 cm-1, with points each 0.9 cm-1. We want the same but with an X axis with a value each cm-1. We can do for our spectrum:

-
x_new = np.arange(400., 1300., 1.0) # we generate the new X values with numpy.arange()
-y_new = rp.resample(spectrum[:,0], spectrum[:,1], x_new)
-
-
-

Sometime, this can fail because we have a new X point at higher or lower values than the original X axis. -This is simply solved by asking rampy.resample() to extrapolate:

-
x_new = np.arange(400., 1300., 1.0) # we generate the new X values with numpy.arange()
-y_new = rp.resample(spectrum[:,0], spectrum[:,1], x_new, fill_value="extrapolate")
-
-
-

rampy.resample uses scipy.interpolate.interp1d. It can takes all the arguments that the latter function takes, so see its doc for details!

-

We can eventually create a new array with the x_new and y_new like:

-
spectrum_resample = np.vstack((x_new,y_new)).T
-
-
-
-
-

Normalisation

-

Rampy provides the rampy.normalisation() function to normalise the Y values of a spectrum to

-
    -
  • the maximum intensity

  • -
  • the trapezoidal area under the curve

  • -
  • to min-max values of intensities

  • -
-

See the notebook on Github for examples

-
-
-

Smoothing

-

Rampy provides the rampy.smooth() function to smooth a spectrum. 10 different algorithms are available, see the notebook on Github for examples of use.

-_images/smooth.png -
-
-

Baseline

-

Rampy allows you to fit polynomial, spline, generalized cross-validated spline, logarithms, exponential, ALS, arPLS, drPLS and rubberband baselines to your spectra, in order to remove the background.

-

See this notebook for an example of use, and the help of rampy.baseline() for details on each algorithm.

-_images/baseline.png -
-
-

Temperature and excitation line effects

-

Raman spectra may need correction from temperature and excitation line effects. See the review of Brooker et al. 1988 for details. rampy offers several way to do so with the rampy.tlexcitation() function. See its documentation for details.

-
-
-

Peak fitting

-

Rampy does not provide functions for peak fitting yet. However, it integrates well with lmfit for instance. See this notebook for an example of use. We will upload soon an example of Bayesian peak fitting with a function integrated to rampy.

-
-
-

Machine learning

-

Rampy offers three classes for performing classification, regression or unsupervised ML exploration of a set of spectra. Those are helpful ways to automatically perform usual scalling, test-train splitting and ML training of popular ML algorithms, and use scikit-learn in the background.

-
-
    -
  • rampy.mlexplorer allows performing PCA and NMF at the moment. This is a work in progress. See the example notebook.

  • -
  • rampy.regressor allows linking variations in spectra to some known variations like the composition of a sample. See the example notebook.

  • -
  • rampy.classificator allows automatic recognition of substances/stuffs from spectra. See the example notebook here.

  • -
-
-

Those functions work (rp.regressor was used in this publication) but still may evolve in the futur. For advanced, ML, I suggest using directly scikit-learn or other ML libraries.

-

Do not hesitate to ask for new features depending on your needs !

-
-
-

Linear mixture

-

If you have two endmember spectra, you can use the rampy.mixing() function. See the mixing example notebook here.

-

If you do not know the endmember spectra, then you may be interested in using directly the PyMCR library, see the documentation here and an example notebook here. We used it in this publication, see the code here.

-
-
- - -
-