spectroscopy 0.20.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spectroscopy/__init__.py +125 -0
- spectroscopy/cli/__init__.py +10 -0
- spectroscopy/cli/ftir_sidechains.py +116 -0
- spectroscopy/collection.py +605 -0
- spectroscopy/data/cd_reference/LICENSE.DichroWebGit +21 -0
- spectroscopy/data/cd_reference/smp180/A.txt +61 -0
- spectroscopy/data/cd_reference/smp180/F.txt +4 -0
- spectroscopy/data/cd_reference/smp180/lbl1.txt +4 -0
- spectroscopy/data/cd_reference/smp180/lbl2.txt +1 -0
- spectroscopy/data/cd_reference/sp175/A.txt +66 -0
- spectroscopy/data/cd_reference/sp175/F.txt +4 -0
- spectroscopy/data/cd_reference/sp175/lbl1.txt +4 -0
- spectroscopy/data/cd_reference/sp175/lbl2.txt +1 -0
- spectroscopy/data/cd_spectra/aqpz_w14a.spy +110 -0
- spectroscopy/data/cd_spectra/aqpz_w14a_native.spy +441 -0
- spectroscopy/data/fluorescence/J_Peri.csv +140 -0
- spectroscopy/data/fluorescence/README.txt +9 -0
- spectroscopy/data/ftir_replicates/CelluloseX.0.dpt +1608 -0
- spectroscopy/data/ftir_replicates/CelluloseX.1.dpt +1608 -0
- spectroscopy/data/ftir_replicates/CelluloseX.2.dpt +1608 -0
- spectroscopy/data/ftir_replicates/Glucose.1.dpt +1608 -0
- spectroscopy/data/ftir_replicates/Glucose.2.dpt +1608 -0
- spectroscopy/data/ftir_replicates/Glucose.3.dpt +1608 -0
- spectroscopy/data/ftir_replicates/H2O.0.dpt +1608 -0
- spectroscopy/data/ftir_replicates/H2O.1.dpt +1608 -0
- spectroscopy/data/ftir_replicates/H2O.2.dpt +1608 -0
- spectroscopy/data/ftir_replicates/README.txt +12 -0
- spectroscopy/data/infrared_spectra/ethanol.jdx +751 -0
- spectroscopy/data/raman_spectra/tannic_acid.jdx +1971 -0
- spectroscopy/data/uvvis_spectra/Spectrum1.csv +701 -0
- spectroscopy/data/uvvis_spectra/Spectrum2.csv +701 -0
- spectroscopy/data/uvvis_spectra/toluene.jdx +370 -0
- spectroscopy/datasets.py +323 -0
- spectroscopy/fitting.py +521 -0
- spectroscopy/history.py +102 -0
- spectroscopy/io/README.md +54 -0
- spectroscopy/io/__init__.py +45 -0
- spectroscopy/io/csv.py +197 -0
- spectroscopy/io/dpt.py +170 -0
- spectroscopy/io/jcamp.py +613 -0
- spectroscopy/io/opus.py +304 -0
- spectroscopy/io/registry.py +344 -0
- spectroscopy/io/spc.py +465 -0
- spectroscopy/io/spy.py +458 -0
- spectroscopy/io/table.py +297 -0
- spectroscopy/library.py +1034 -0
- spectroscopy/lineshapes.py +58 -0
- spectroscopy/main.py +12 -0
- spectroscopy/messages.py +25 -0
- spectroscopy/metadata.py +266 -0
- spectroscopy/peaks.py +190 -0
- spectroscopy/processing/__init__.py +29 -0
- spectroscopy/processing/cd.py +750 -0
- spectroscopy/processing/common.py +563 -0
- spectroscopy/processing/ftir.py +305 -0
- spectroscopy/processing/melting.py +470 -0
- spectroscopy/processing/multivariate.py +497 -0
- spectroscopy/processing/scattering.py +276 -0
- spectroscopy/processing/structure.py +1119 -0
- spectroscopy/processing/unmix.py +407 -0
- spectroscopy/py.typed +0 -0
- spectroscopy/simple_example.py +25 -0
- spectroscopy/spectra.py +1539 -0
- spectroscopy/units.py +307 -0
- spectroscopy/viz.py +427 -0
- spectroscopy-0.20.0rc1.dist-info/METADATA +212 -0
- spectroscopy-0.20.0rc1.dist-info/RECORD +70 -0
- spectroscopy-0.20.0rc1.dist-info/WHEEL +4 -0
- spectroscopy-0.20.0rc1.dist-info/entry_points.txt +2 -0
- spectroscopy-0.20.0rc1.dist-info/licenses/LICENSE +373 -0
spectroscopy/__init__.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
"""
|
|
5
|
+
SpectroscoPy -- a common framework for handling and analysing spectra.
|
|
6
|
+
|
|
7
|
+
⚠️ API unstable pre-1.0: breaking changes are expected between 0.x releases.
|
|
8
|
+
|
|
9
|
+
Layout (see SpectroscoPy_Codebase_Review.md):
|
|
10
|
+
spectroscopy.spectra core data model
|
|
11
|
+
spectroscopy.io format readers/writers
|
|
12
|
+
spectroscopy.processing algorithms
|
|
13
|
+
spectroscopy.lineshapes gaussian / lorentzian / voigt-ish components
|
|
14
|
+
spectroscopy.cli command-line entry points
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from importlib.metadata import PackageNotFoundError as _PackageNotFoundError
|
|
18
|
+
from importlib.metadata import version as _version
|
|
19
|
+
|
|
20
|
+
from . import datasets, fitting, io, library, lineshapes, metadata, processing, units
|
|
21
|
+
from .collection import SpectrumCollection
|
|
22
|
+
from .fitting import FitResult
|
|
23
|
+
from .history import ProcessingStep
|
|
24
|
+
from .peaks import PeakTable
|
|
25
|
+
from .spectra import Spectrum
|
|
26
|
+
|
|
27
|
+
#: The public surface. Everything reachable as ``spectroscopy.<name>`` that is
|
|
28
|
+
#: not in this list is an implementation detail, and 1.0 does not promise to
|
|
29
|
+
#: keep it. Submodules stay importable either way (``import spectroscopy.io``
|
|
30
|
+
#: always works); what this list settles is what ``spc.<name>`` offers and what
|
|
31
|
+
#: the freeze covers. Roadmap section 14.2 has the audit that produced it.
|
|
32
|
+
__all__ = [
|
|
33
|
+
# -- opening a file ------------------------------------------------------
|
|
34
|
+
'read', # spc.read(path) -- the first thing anyone types
|
|
35
|
+
# -- core objects --------------------------------------------------------
|
|
36
|
+
'Spectrum', # one spectrum: x, y, units, metadata, history
|
|
37
|
+
'SpectrumCollection', # many, with grouping and batch operations
|
|
38
|
+
'PeakTable', # what find_peaks() returns
|
|
39
|
+
'FitResult', # what fit_peaks() returns
|
|
40
|
+
'ProcessingStep', # what .history is made of
|
|
41
|
+
# -- submodules ----------------------------------------------------------
|
|
42
|
+
'datasets', # bundled example data, so the docs run anywhere
|
|
43
|
+
'io', # readers, writers, and the format registry
|
|
44
|
+
'library', # reference spectra and extinction coefficients
|
|
45
|
+
'metadata', # the agreed metadata keys and what they mean
|
|
46
|
+
'processing', # common, ftir, multivariate, unmix
|
|
47
|
+
'units', # unit names, conversion, axis quantities
|
|
48
|
+
'lineshapes', # gaussian / lorentzian components
|
|
49
|
+
'viz', # plotting -- imported lazily, see below
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
def read(path, file_type=None, **kwargs):
|
|
53
|
+
"""
|
|
54
|
+
Read one spectrum from a file. ``spc.read("sample.dpt")``.
|
|
55
|
+
|
|
56
|
+
The format is worked out from the file itself -- the two binary formats
|
|
57
|
+
check their own magic value rather than trusting the extension -- so
|
|
58
|
+
``file_type`` is only needed to override that.
|
|
59
|
+
|
|
60
|
+
This is what most people type first, so it is what the package offers
|
|
61
|
+
first: one function, any supported format, no need to know which reader
|
|
62
|
+
handles what.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
path : str or Path
|
|
67
|
+
The file to read.
|
|
68
|
+
file_type : str, optional
|
|
69
|
+
Force a format, e.g. ``'jcamp'``. Otherwise inferred.
|
|
70
|
+
**kwargs
|
|
71
|
+
Passed to the reader, e.g. ``x_col=0`` for a table.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
-------
|
|
75
|
+
Spectrum
|
|
76
|
+
|
|
77
|
+
Raises
|
|
78
|
+
------
|
|
79
|
+
ValueError
|
|
80
|
+
If the file holds several spectra, naming
|
|
81
|
+
``spectroscopy.io.read_spectra``, which returns them all. Handing
|
|
82
|
+
back the first of three silently would be worse than failing.
|
|
83
|
+
|
|
84
|
+
See also
|
|
85
|
+
--------
|
|
86
|
+
spectroscopy.io.read_spectra : every spectrum in one file.
|
|
87
|
+
spectroscopy.SpectrumCollection.from_files : many files at once, with
|
|
88
|
+
globs, sample names and a measured parameter.
|
|
89
|
+
|
|
90
|
+
Notes
|
|
91
|
+
-----
|
|
92
|
+
There is deliberately no ``spc.load``. Two of the agents guessed it, but
|
|
93
|
+
``load`` already means something else here -- ``datasets.load`` fetches
|
|
94
|
+
a bundled example by name -- and one word cannot mean both "open this
|
|
95
|
+
path" and "fetch that example" without becoming a coin toss.
|
|
96
|
+
"""
|
|
97
|
+
return io.read_spectrum(path, file_type, **kwargs)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
#: ``__version__`` is public but deliberately not in __all__: ``from
|
|
101
|
+
#: spectroscopy import *`` would then bind it in the caller's namespace and
|
|
102
|
+
#: shadow their own.
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def __getattr__(name):
|
|
106
|
+
"""Import ``spectroscopy.viz`` on first use rather than at import time.
|
|
107
|
+
|
|
108
|
+
``viz`` pulls in matplotlib, which costs about 350 ms -- most of a second
|
|
109
|
+
added to every ``import spectroscopy``, including the ones that only read a
|
|
110
|
+
file. Deferring it keeps the import cheap for people who never plot, while
|
|
111
|
+
``spc.viz.plot(...)`` still works without a separate import line.
|
|
112
|
+
"""
|
|
113
|
+
if name == 'viz':
|
|
114
|
+
# import_module, not ``from . import viz``: the latter goes through
|
|
115
|
+
# _handle_fromlist, which calls getattr() on this package, which calls
|
|
116
|
+
# this function again -- straight into infinite recursion.
|
|
117
|
+
import importlib # noqa: PLC0415
|
|
118
|
+
return importlib.import_module('.viz', __name__)
|
|
119
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
__version__ = _version("spectroscopy")
|
|
124
|
+
except _PackageNotFoundError: # running from a source tree, not installed
|
|
125
|
+
__version__ = "0.0.0+unknown"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
"""
|
|
5
|
+
Command-line entry points.
|
|
6
|
+
|
|
7
|
+
These are thin wrappers: argument parsing, file reading and plotting only.
|
|
8
|
+
All computation lives in ``spectroscopy.processing``, so that a CLI can do
|
|
9
|
+
nothing a notebook user could not also do through the library API.
|
|
10
|
+
"""
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
"""
|
|
6
|
+
Command-line front end for the protein side-chain IR spectrum calculation.
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
spc-ftir-sidechains [-h] [--wn_range X Y] [--pH PH] [--res RES] [--D2O]
|
|
10
|
+
[--addN] [--addC] [--plot] [--output OUTPUT] filename
|
|
11
|
+
|
|
12
|
+
All the science is in ``spectroscopy.processing.ftir``; this module only
|
|
13
|
+
parses arguments, reads the sequence file, and optionally plots or saves.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import argparse
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
from spectroscopy.processing.ftir import (
|
|
21
|
+
DEF_PH,
|
|
22
|
+
DEF_RES,
|
|
23
|
+
HIGH_FREQ,
|
|
24
|
+
LOW_FREQ,
|
|
25
|
+
calc_resid_spectrum,
|
|
26
|
+
ftir_sidechain,
|
|
27
|
+
get_composition,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _parse_args(argv=None):
|
|
32
|
+
"""Build the argument parser and parse ``argv`` (defaults to sys.argv)."""
|
|
33
|
+
parser = argparse.ArgumentParser(description="Process command-line arguments.")
|
|
34
|
+
|
|
35
|
+
parser.add_argument("filename", type=str,
|
|
36
|
+
help="File name for sequence or '-' for stdin.")
|
|
37
|
+
parser.add_argument("--wn_range", type=int, nargs=2,
|
|
38
|
+
default=(LOW_FREQ, HIGH_FREQ),
|
|
39
|
+
metavar=('X', 'Y'), help="An optional 2-tuple giving the wavenumber range.")
|
|
40
|
+
parser.add_argument("--pH", type=float, help="Optional pH value, default 7.0")
|
|
41
|
+
parser.add_argument("--res", type=float,
|
|
42
|
+
help="Optional spectral resolution, default 1.0")
|
|
43
|
+
parser.add_argument("--D2O", action="store_true",
|
|
44
|
+
help="Optional switch if D2O used, default False")
|
|
45
|
+
parser.add_argument("--addN", action="store_false",
|
|
46
|
+
help="Optional switch to include N-term contribution, default True")
|
|
47
|
+
parser.add_argument("--addC", action="store_false",
|
|
48
|
+
help="Optional switch to include C-term contribution, default True")
|
|
49
|
+
parser.add_argument("--plot", action="store_true",
|
|
50
|
+
help="Optional switch to plot spectrum for sequence and individual components.")
|
|
51
|
+
parser.add_argument("--output", type=str,
|
|
52
|
+
help="An optional filename for saving the spectrum in csv format.")
|
|
53
|
+
return parser.parse_args(argv)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _plot_components(sequence, args, kwargs, sum_spectrum):
|
|
57
|
+
"""Plot the summed side-chain spectrum together with its components."""
|
|
58
|
+
import matplotlib.pyplot as plt # pylint: disable=C0415
|
|
59
|
+
|
|
60
|
+
options = {
|
|
61
|
+
"pH": DEF_PH,
|
|
62
|
+
"D2O": False,
|
|
63
|
+
"addN": True,
|
|
64
|
+
"addC": True,
|
|
65
|
+
"res": DEF_RES,
|
|
66
|
+
}
|
|
67
|
+
options.update({k: v for k, v in kwargs.items() if k in options})
|
|
68
|
+
|
|
69
|
+
if options.get("addN"):
|
|
70
|
+
sequence = sequence + '+'
|
|
71
|
+
if options.get("addC"):
|
|
72
|
+
sequence = sequence + '-'
|
|
73
|
+
|
|
74
|
+
components = []
|
|
75
|
+
for residue in get_composition(sequence):
|
|
76
|
+
resid_spectrum = calc_resid_spectrum(residue[0],
|
|
77
|
+
options.get("pH"), options.get("D2O"),
|
|
78
|
+
(args.wn_range[0], args.wn_range[1], options.get("res"))) * residue[1]
|
|
79
|
+
if np.any(resid_spectrum.y):
|
|
80
|
+
components.append(resid_spectrum)
|
|
81
|
+
|
|
82
|
+
fig, ax = plt.subplots()
|
|
83
|
+
for element in components:
|
|
84
|
+
element.plot(ax)
|
|
85
|
+
|
|
86
|
+
sum_spectrum.plot(ax)
|
|
87
|
+
ax.set_xlabel(sum_spectrum.x_label)
|
|
88
|
+
ax.set_ylabel(sum_spectrum.y_label)
|
|
89
|
+
fig.legend()
|
|
90
|
+
plt.show()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def main(argv=None):
|
|
94
|
+
"""Entry point for the ``spc-ftir-sidechains`` command."""
|
|
95
|
+
args = _parse_args(argv)
|
|
96
|
+
|
|
97
|
+
kwargs = {k: v for k, v in vars(args).items()
|
|
98
|
+
if k not in ["filename", "wn_range"] and v is not None}
|
|
99
|
+
|
|
100
|
+
with open(args.filename, encoding="UTF-8") as handle:
|
|
101
|
+
sequence = handle.read()
|
|
102
|
+
|
|
103
|
+
sum_spectrum = ftir_sidechain(sequence, tuple(args.wn_range), **kwargs)
|
|
104
|
+
|
|
105
|
+
if args.plot:
|
|
106
|
+
_plot_components(sequence, args, kwargs, sum_spectrum)
|
|
107
|
+
|
|
108
|
+
if args.output:
|
|
109
|
+
sum_spectrum.fileinfo['NAME'] = args.output
|
|
110
|
+
sum_spectrum.save()
|
|
111
|
+
|
|
112
|
+
return 0
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == '__main__':
|
|
116
|
+
raise SystemExit(main())
|