In [1]:
%matplotlib ipympl
import numpy as np
import matplotlib.pyplot as plt
import itertools
import spectrochempy as scp
import pandas as pd
  SpectroChemPy's API - v.0.5.1
© Copyright 2014-2023 - A.Travert & C.Fernandez @ LCS
Warning: Cannot change to a different GUI toolkit: widget. Using ipympl instead.
In [143]:
def cmtoum(x):
    return 10000/x
def umtocm(x):
    return 10000/x

def CreateDualAxisIRPlot(FontSize=12):
    # Plot with two axes.
    fig, axcm = plt.subplots()
    axcm.set_xlabel('cm$^{-1}$', fontsize=FontSize)
    axum = axcm.secondary_xaxis('top', functions=(cmtoum, umtocm))
    axum.set_xlabel('$\mu$m', fontsize=FontSize)
    axcm.set_ylabel('a.u.', fontsize=FontSize)
    axcm.tick_params('x', labelsize=FontSize)
    axcm.tick_params('y', labelsize=FontSize)
    axum.tick_params('x', labelsize=FontSize)
    axum.xaxis.set_ticks(range(6,12))
    axcm.xaxis.set_ticks(range(900,1900,100))
    return fig, axcm

def FinishDualAxisIRPlot(fig, axcm, Title='', FontSize=12):
    axcm.legend()
    axcm.invert_xaxis()
    plt.title(Title, fontsize=FontSize*1.5)
    plt.tight_layout()

def plotone(ax, SpecName, Spectradf, offset=0, Norm=False, **kwargs):
    S = Spectradf[SpecName].to_numpy()
    if Norm==True:
        S -= np.min(S)
        S /= np.max(S)
    axcm = Spectradf['X value [cm-1]'].to_numpy()
    ax.plot(axcm, S+offset, label=SpecName, **kwargs)

def EncapsulateSpectrum(df, csvlabel):
    S = scp.NDDataset(df[csvlabel].to_numpy(), units='a.u.', title='absorption')
    S.data -= np.min(S.data)
    S.data /= np.max(S.data)
    S.x = scp.NDDataset(df['X value [cm-1]'].to_numpy(), units='cm^-1', title='wavenumbers')
    return S
In [144]:
# Read in the raw csv files.
df016 = pd.read_csv('016 - Point Spectra.csv')
df016 = df016[~df016['X value [cm-1]'].isin([1505, 1537, 1557, 1621, 1633, 1645, 1681])]
df018 = pd.read_csv('018 spectra.csv')
df018 = df018[~df018['X value [cm-1]'].isin([1419, 1507, 1521, 1539, 1557, 1558, 1559, 1616, 1623, 1635, 1645, 1652, 1683, 1684, 1696, 1701, 1704, 1706, 1717, 1718, 1733, 1754, 1780, 1784, 1790, 1792, 1799])]

# Convert the csv files into a dictionary of spectrum objects.
Spectra = {}
SmoothLength = 19
Spectra['Green'] = EncapsulateSpectrum(df018, 'Spectrum 5 Green phase').smooth(window_length=SmoothLength)
Spectra['CarbonateBottom'] = EncapsulateSpectrum(df018, 'Spectrum 11 Carbonate bottom').smooth(window_length=SmoothLength)
Spectra['CarbonateTop'] = EncapsulateSpectrum(df018, 'Spectrum 3 Carbonate top').smooth(window_length=SmoothLength)
Spectra['White'] = EncapsulateSpectrum(df018, 'Spectrum 2 White').smooth(window_length=SmoothLength)
Spectra['TopRed'] = EncapsulateSpectrum(df018, 'Spectrum 9 Top red').smooth(window_length=SmoothLength)
Spectra['Purple'] = EncapsulateSpectrum(df016, 'Spectrum 3 Purple').smooth(window_length=1)
Spectra['Red'] = EncapsulateSpectrum(df016, 'Spectrum 1 Red').smooth(window_length=1)
Spectra['Blue'] = EncapsulateSpectrum(df016, 'Spectrum 2 Blue').smooth(window_length=1)
In [145]:
fig,ax = CreateDualAxisIRPlot(FontSize=12)
S = Spectra['TopRed']
ax.plot(S.x.data, S.data+1.8, label='a', linewidth=2, color='xkcd:dark pink')
S = Spectra['CarbonateTop']
ax.plot(S.x.data, S.data+1, label='b', linewidth=2, color='xkcd:yellowy brown')
# ax.plot(S.x.data, S.smooth(window_length=19).data+2, label='Carbonate Top', linewidth=2, color='gray')
S = Spectra['CarbonateBottom']
ax.plot(S.x.data, S.data+0.5, label='c', linewidth=2, color='orange')
S = Spectra['Green']
ax.plot(S.x.data, S.data, label='d', linewidth=2, color='forestgreen')
S = Spectra['White']
ax.plot(S.x.data, S.data-0.5, label='e', linewidth=2, color='xkcd:cement')
# S = Spectra['Purple']
# ax.plot(S.x.data, S.data-1.0, label='Purple', linewidth=2, color='Purple')
FinishDualAxisIRPlot(fig, ax, Title='', FontSize=12)
fig.set_size_inches(6,8)
plt.savefig('FIBSpectra.png', dpi=300)
Figure
In [147]:
fig,ax = CreateDualAxisIRPlot(FontSize=12)
S = Spectra['TopRed']
ax.plot(S.x.data, S.data+0.1, label='a', linewidth=2, color='xkcd:dark pink')
S = Spectra['Green']
ax.plot(S.x.data, S.data, label='d', linewidth=2, color='forestgreen')
S = Spectra['White']
ax.plot(S.x.data, S.data-0.1, label='e', linewidth=2, color='xkcd:cement')
FinishDualAxisIRPlot(fig, ax, Title='', FontSize=12)
fig.set_size_inches(6,8)
plt.ylim([-0.1,0.3])
plt.xlim([1800,1100])
plt.savefig('PhylloOrganics.png', dpi=300)
Figure
In [130]:
!jupyter nbconvert --to html 'PlotSpectra.ipynb'
This application is used to convert notebook files (*.ipynb)
        to various other formats.

        WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.

Options
=======
The options below are convenience aliases to configurable class-options,
as listed in the "Equivalent to" description-line of the aliases.
To see all configurable class-options for some <cmd>, use:
    <cmd> --help-all

--debug
    set log level to logging.DEBUG (maximize logging output)
    Equivalent to: [--Application.log_level=10]
--show-config
    Show the application's configuration (human-readable format)
    Equivalent to: [--Application.show_config=True]
--show-config-json
    Show the application's configuration (json format)
    Equivalent to: [--Application.show_config_json=True]
--generate-config
    generate default config file
    Equivalent to: [--JupyterApp.generate_config=True]
-y
    Answer yes to any questions instead of prompting.
    Equivalent to: [--JupyterApp.answer_yes=True]
--execute
    Execute the notebook prior to export.
    Equivalent to: [--ExecutePreprocessor.enabled=True]
--allow-errors
    Continue notebook execution even if one of the cells throws an error and include the error message in the cell output (the default behaviour is to abort conversion). This flag is only relevant if '--execute' was specified, too.
    Equivalent to: [--ExecutePreprocessor.allow_errors=True]
--stdin
    read a single notebook file from stdin. Write the resulting notebook with default basename 'notebook.*'
    Equivalent to: [--NbConvertApp.from_stdin=True]
--stdout
    Write notebook output to stdout instead of files.
    Equivalent to: [--NbConvertApp.writer_class=StdoutWriter]
--inplace
    Run nbconvert in place, overwriting the existing notebook (only 
            relevant when converting to notebook format)
    Equivalent to: [--NbConvertApp.use_output_suffix=False --NbConvertApp.export_format=notebook --FilesWriter.build_directory=]
--clear-output
    Clear output of current file and save in place, 
            overwriting the existing notebook.
    Equivalent to: [--NbConvertApp.use_output_suffix=False --NbConvertApp.export_format=notebook --FilesWriter.build_directory= --ClearOutputPreprocessor.enabled=True]
--no-prompt
    Exclude input and output prompts from converted document.
    Equivalent to: [--TemplateExporter.exclude_input_prompt=True --TemplateExporter.exclude_output_prompt=True]
--no-input
    Exclude input cells and output prompts from converted document. 
            This mode is ideal for generating code-free reports.
    Equivalent to: [--TemplateExporter.exclude_output_prompt=True --TemplateExporter.exclude_input=True --TemplateExporter.exclude_input_prompt=True]
--allow-chromium-download
    Whether to allow downloading chromium if no suitable version is found on the system.
    Equivalent to: [--WebPDFExporter.allow_chromium_download=True]
--disable-chromium-sandbox
    Disable chromium security sandbox when converting to PDF..
    Equivalent to: [--WebPDFExporter.disable_sandbox=True]
--show-input
    Shows code input. This flag is only useful for dejavu users.
    Equivalent to: [--TemplateExporter.exclude_input=False]
--embed-images
    Embed the images as base64 dataurls in the output. This flag is only useful for the HTML/WebPDF/Slides exports.
    Equivalent to: [--HTMLExporter.embed_images=True]
--log-level=<Enum>
    Set the log level by value or name.
    Choices: any of [0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL']
    Default: 30
    Equivalent to: [--Application.log_level]
--config=<Unicode>
    Full path of a config file.
    Default: ''
    Equivalent to: [--JupyterApp.config_file]
--to=<Unicode>
    The export format to be used, either one of the built-in formats
            ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides', 'webpdf']
            or a dotted object name that represents the import path for an
            ``Exporter`` class
    Default: ''
    Equivalent to: [--NbConvertApp.export_format]
--template=<Unicode>
    Name of the template to use
    Default: ''
    Equivalent to: [--TemplateExporter.template_name]
--template-file=<Unicode>
    Name of the template file to use
    Default: None
    Equivalent to: [--TemplateExporter.template_file]
--theme=<Unicode>
    Template specific theme(e.g. the name of a JupyterLab CSS theme distributed
    as prebuilt extension for the lab template)
    Default: 'light'
    Equivalent to: [--HTMLExporter.theme]
--writer=<DottedObjectName>
    Writer class used to write the 
                                        results of the conversion
    Default: 'FilesWriter'
    Equivalent to: [--NbConvertApp.writer_class]
--post=<DottedOrNone>
    PostProcessor class used to write the
                                        results of the conversion
    Default: ''
    Equivalent to: [--NbConvertApp.postprocessor_class]
--output=<Unicode>
    overwrite base name use for output files.
                can only be used when converting one notebook at a time.
    Default: ''
    Equivalent to: [--NbConvertApp.output_base]
--output-dir=<Unicode>
    Directory to write output(s) to. Defaults
                                  to output to the directory of each notebook. To recover
                                  previous default behaviour (outputting to the current 
                                  working directory) use . as the flag value.
    Default: ''
    Equivalent to: [--FilesWriter.build_directory]
--reveal-prefix=<Unicode>
    The URL prefix for reveal.js (version 3.x).
            This defaults to the reveal CDN, but can be any url pointing to a copy 
            of reveal.js. 
            For speaker notes to work, this must be a relative path to a local 
            copy of reveal.js: e.g., "reveal.js".
            If a relative path is given, it must be a subdirectory of the
            current directory (from which the server is run).
            See the usage documentation
            (https://nbconvert.readthedocs.io/en/latest/usage.html#reveal-js-html-slideshow)
            for more details.
    Default: ''
    Equivalent to: [--SlidesExporter.reveal_url_prefix]
--nbformat=<Enum>
    The nbformat version to write.
            Use this to downgrade notebooks.
    Choices: any of [1, 2, 3, 4]
    Default: 4
    Equivalent to: [--NotebookExporter.nbformat_version]

Examples
--------

    The simplest way to use nbconvert is

            > jupyter nbconvert mynotebook.ipynb --to html

            Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides', 'webpdf'].

            > jupyter nbconvert --to latex mynotebook.ipynb

            Both HTML and LaTeX support multiple output templates. LaTeX includes
            'base', 'article' and 'report'.  HTML includes 'basic', 'lab' and 
            'classic'. You can specify the flavor of the format used.

            > jupyter nbconvert --to html --template lab mynotebook.ipynb

            You can also pipe the output to stdout, rather than a file

            > jupyter nbconvert mynotebook.ipynb --stdout

            PDF is generated via latex

            > jupyter nbconvert mynotebook.ipynb --to pdf

            You can get (and serve) a Reveal.js-powered slideshow

            > jupyter nbconvert myslides.ipynb --to slides --post serve

            Multiple notebooks can be given at the command line in a couple of 
            different ways:

            > jupyter nbconvert notebook*.ipynb
            > jupyter nbconvert notebook1.ipynb notebook2.ipynb

            or you can specify the notebooks list in a config file, containing::

                c.NbConvertApp.notebooks = ["my_notebook.ipynb"]

            > jupyter nbconvert --config mycfg.py

To see all available configurables, use `--help-all`.

[NbConvertApp] WARNING | pattern "'PlotSpectra.ipynb'" matched no files