tqt_nmr package
Subpackages
Submodules
tqt_nmr.collection module
Dummy pipeline:
>>> specs = NMRSpectraCollection.from_signals(signals, zf_amount=len(times) * 9, time_scale=1)
>>> spec = specs.map(NMRSpectrum.apply_harmonics_mask).brute_windowing(100).fuse('auto', verbose=True)
>>> spec.normalizes_frequencies(fast=False).correct_phase()
>>> spec.kivi_baseline_correction().plot()
- class NMRSpectraCollection(spectra)
Bases:
object
- Parameters:
spectra (list[NMRSpectrum])
-
spectra:
list
[NMRSpectrum
]
- fuse(mode='mean', verbose=False)
Averages the spectra into a single one.
- Parameters:
mode (
Literal
['mean'
,'corr'
,'auto'
]) – Either ‘mean’, ‘corr’, or ‘auto’. -mean
: Averages the spectra element-wise. -corr
: Aligns by correlating the spectra beforehand. -auto
: Tries bothmean
andcorr
and uses the one that produces the most symmetric spectrum.verbose (
bool
) – Whether to log which method is picked ifmode="corr"
.
- Returns:
The fused single spectrum.
- Return type:
- classmethod from_raw(times, voltages, zf_amount=-1)
Converts a list of Signal objects into a collection of NMR spectra.
- Parameters:
times (
ndarray
) – 2D array of shape (samples, time_points).voltages (
ndarray
) – 2D array of shape (samples, voltages).zf_amount (
int
) – Zero-filling parameter for the NMRSpectrum’s construction.
- Return type:
Self
- classmethod from_signals(signals, zf_amount=-1, cut_dead_time=False, **kwargs)
Converts a list of Signal objects into a collection of NMR spectra.
- Parameters:
signals (
list
[Signal
]) – A collection of signals to convert.zf_amount (
int
) – Zero-filling parameter for the NMRSpectrum’s construction.cut_dead_time (
bool
) – Whether to cut the dead time signal.
- Return type:
Self
- map(func, inplace=True, *args, **kwargs)
Applies a function to all of the spectra in the collection.
- Parameters:
func (
Callable
[...
,NMRSpectrum
]) – Function to apply. Takes NMRSpectrum as input, returning a modified spectrum instance.inplace (
bool
) – Whether to modify the current collection or to create a new one. Defaults to True.
- Return type:
Self
- brute_windowing(window_width=150)
Cuts a window of the specified size around the max abs peak of each spectrum. Shifts the frequencies to move the peak position to zero.
- Parameters:
window_width (
float
) – Width of the window in Hz. Defaults to 100.- Return type:
Self
tqt_nmr.config module
tqt_nmr.fft module
- apply_fft(times, values, length, full_fft=False, **kwargs)
- Return type:
tuple
[array
,array
]- Parameters:
times (ndarray)
values (ndarray)
length (int)
full_fft (bool)
tqt_nmr.signal module
- class Signal(time_points, amplitudes)
Bases:
object
- Parameters:
time_points (ndarray)
amplitudes (ndarray)
-
time_points:
ndarray
-
amplitudes:
ndarray
- plot(ax=None, **kwargs)
Plots the signal with matplotlib.
- Parameters:
ax – Axis to plot on. Uses the default one is not provided.
kwargs – Additional arguments to be provided to ax.plot(…).
- Returns:
The axis on which the signal is drawn.
- get_upper_envelope(smoothing_kernel_factor=70)
Creates an upper-envelope of the signal
- Return type:
Self
- cut_dead_time(max_duration_millis=inf, threshold=0.5, extra_cut_millis=0)
Finds the first local maxima of the envelope and cuts the signal by that point.
- Parameters:
max_duration_millis (
float
) – Max possible length of the dead timethreshold (
float
) – Normalized abs signal above this level is considered to be a signal.extra_cut_millis (
float
) – Additional time to cut after the dead time.
- Return type:
Self
- Returns:
Self
- apodize(func)
Applies the apodization function provided.
- Parameters:
func (
Callable
[[ndarray
],ndarray
]) – Apodization function. Will take the range [0;1] as input.output. (The signal is multiplied by the)
- Return type:
Self
- Returns:
Self
- align_to(reference_signal, max_shift=0.02, res=3, fast=False)
Cuts the signal to align with the reference.
- Parameters:
reference_signal (
Self
) – Signal to align to.max_shift (
float
) – Max shift in seconds. Defaults to 0.02.res (
int
) – Resolution of phase alignment, see phase_align(…). Defaults to 3.fast (
bool
) – Whether to use a faster version of the algorithm. Defaults to False.
- Return type:
Self
- Returns:
Self
- normalize()
Scales the signal to [-1 1] range.
- Return type:
Self
- quadrature_detection(F0=None)
- Return type:
Self
- Parameters:
F0 (float | None)
- as_plotly(plot_type='line', **kwargs)
- Parameters:
plot_type (str)
- copy()
- classmethod from_folder(folder, file_extension='.dat')
Reads a collection of signals from a folder containing raw *.dat files.
- Return type:
list
[Self
]- Parameters:
folder (Path | str)
file_extension (str)
- classmethod from_csv(file)
- Return type:
Self
- Parameters:
file (Path | str | IOBase)
- save_bin(file, **kwargs)
- Return type:
None
- Parameters:
file (BytesIO | Path | str)
- classmethod from_bin(file)
- Return type:
Self
- Parameters:
file (BytesIO | Path | str)
tqt_nmr.spectrum module
- class NMRSpectrum(frequencies, amplitudes)
Bases:
object
- Parameters:
frequencies (ndarray)
amplitudes (ndarray)
-
frequencies:
ndarray
-
amplitudes:
ndarray
- classmethod from_signal(signal, zero_fill_amount, time_scale=1e-06, **kwargs)
Shortcut for NMRSpectrum.from_voltages(…)
- Return type:
Self
- Parameters:
signal (Signal)
zero_fill_amount (int)
time_scale (float)
- classmethod from_voltages(times, voltages, zero_fill_amount, time_scale=1e-06, **kwargs)
Constructs a spectrum from a raw signal.
- Parameters:
times (
Union
[ndarray
,Sequence
[float
]]) – Time points of the signal.voltages (
Union
[ndarray
,Sequence
[float
]]) – Signal amplitudes at the corresponding time points.zero_fill_amount (
int
) – How many zeros to add at the end. Rounds to the nearest power of two if <= 0.time_scale (
float
) – Scaling factor of the time points to convert to seconds. Defaults to millis (1e-6).
- Return type:
Self
- Returns:
The spectrum constructed.
- shift_freqs(amount)
- Return type:
Self
- Parameters:
amount (float)
- smooth(sigma=4)
- Return type:
Self
- Parameters:
sigma (float)
- find_peaks(dist_hz=None, min_height=None, prominence=None)
Finds the frequencies of peaks matching the parameters. See
scipy.signal.find_peaks(...)
.- Return type:
list
[float
]
- filter_peaks(peaks, max_hz_diff, max_amp_diff, min_amp, roi=(-20, 20))
Filters the peaks by ROI, symmetricity, amps difference.
- Parameters:
peaks (
ndarray
) – List of peaks to filter (Hz).max_hz_diff (
float
) – Max allowed abs difference between positions of the peaks.max_amp_diff (float) – Max allowed abs difference between amplitudes of the peaks.
min_amp (float) – Minium allowed amplitude of a peak.
roi (
tuple
[float
,float
]) – Frequency range to search in. Defaults to (-20, 20).
- Returns:
Pairs of peaks found. List[float]: List of mean amplitudes of the peaks pairs. List[float]: List of symmetricity scores of the peak pairs (more is better).
- Return type:
List[Tuple[float, float]]
- apply_harmonics_mask(freq=50, k=0.99)
Masks out harmonics by a cosine wave.
- Parameters:
freq (
float
) – Frequency of the harmonics to mask.k (
float
) – Cutoff threshold of the cosine wave (higher = thinner mask). Defaults to 0.99.
- Return type:
Self
- cut_freqs(start=-inf, end=inf)
Cuts the the spectrum to the frequency range provided (inclusive).
- Parameters:
start (
float
) – Left-most side of the frequencies range. Defaults to -inf (no cut).end (
float
) – Right-most side of the frequencies range. Defaults to +inf (no cut).
- Return type:
Self
- Returns:
Modified version of the object.
- normalizes_frequencies(fast=False, peaks_prominence=0.15, return_peaks=False)
Finds the central peak and shifts it to 0Hz.
- Parameters:
fast (
bool
) – Usesargmax(abs(spec))
if True. Tries to find the anchor peaks and align them symmetrically if False. Falls back tofast=True
if unable to find any anchor peaks.peaks_prominence (
float
) – Used for finding anchor peaks whenfast=False
. Seescipy.signal.find_peaks
.return_peaks (
bool
) – Returns the peaks found (after normalization) if True.
- Return type:
Union
[Self
,tuple
[Self
,ndarray
]]
- correct_phase(method='acme', baseline_correct=False, **kwargs)
Uses the nmrglue’s autops method to correct the phase.
Optionally applies the median base-line correction component-wise.
- Parameters:
method – Phase correction method to use. See
nmrglue.proc_autophase.autops
. Defaults to “acme”.baseline_correct (
bool
) – Whether to apply the median base-line correction. Defaults to False.
- Return type:
Self
- Returns:
Modified version of the object.
- kivi_baseline_correction()
Applies the @kivicode’s version of baseline correction.
- Return type:
Self
- remove_central_peak(base_freq=0)
Finds a pair of downward peaks closest to the base frequency and nullifies the values between them.
- Parameters:
base_freq (
float
) – Frequency of the central peak. Defaults to 0.- Return type:
Self
- plot(ax=None, comp_getter=<function real>, **kwargs)
Plots the spectrum with matplotlib.
- Parameters:
ax – Axis to plot on. Uses the default one is not provided.
comp_getter (
Callable
[[ndarray
],ndarray
]) – Callable to convert the complex amplitudes to real numbers. Defaults to np.real.kwargs – Additional arguments to be provided to
ax.plot(...)
.
- Returns:
The axis on which the spectrum is drawn.
- halves_symmetry_score(base_frequency=0)
Calculates the inverse cosine distance between the left and right halves of the spectrum.
- Parameters:
base_frequency (
float
) – Central frequency to split the spectrum with. Defaults to 0.- Returns:
Inverse cosine distance. The lower the score the higher the spectrum’s symmetry rate.
- Return type:
float
- as_plotly(plot_type='line', comp_getter=<function real>, **kwargs)
- Parameters:
comp_getter (Callable[[ndarray], ndarray])
- copy()
- Return type:
Self
- save(save_dir)
Saves the spectrum into frequencies.npy and amplitudes.npy NumPy arrays in the folder provided.
- Return type:
None
- Parameters:
save_dir (Path)
- at(freq)
- Return type:
float
- Parameters:
freq (float)
- classmethod from_file(spectra_dir)
Reconstructs the spectrum object from the files saved by the NMRSpectrum.save(…) method.
- Raises:
OSError – If couldn’t find the save files.
- Return type:
Self
- Parameters:
spectra_dir (Path)
Module contents
- use_experimental(flag=True)
- Return type:
None
- Parameters:
flag (bool)