Skip to main content

Fix the misalignments of spectra from R2D3 sequence due to varying thermal conditions.

Project description

R2D3-Shift

R2D3-Shift is a Python package designed to correct spectral shifts induced by the R2D3 sequence in 1.5-dimensional spectra, where one dimension is direct and the other is indirect with varying relaxation times. The R2D3 sequence can cause inhomogeneous thermal elevation across successive spectra (the tradeoff for varying relaxation times), resulting in non-linear spectral shifts for different compounds of the sample.

Shift Correction Methodology

The core methodology involves detecting peaks in each spectrum, matching them across successive spectra, and extrapolating a shift map to correct the observed distortions. Peak detection combines standard algorithms with noise-based filtering to ensure robustness. Peaks are matched using a filtered Gale-Shapley algorithm, producing an irregular grid of shifts. This grid is then extrapolated to a full regular grid using linear radial basis function (RBF) interpolation. The resulting shift map is inverted and applied to realign the peaks and correct the spectra.

Setup

From source

To install the minimal dependencies for using the package in Python:

  1. Create the conda environment:
    conda env create -f environment.yml
    
  2. Activate the environment:
    conda activate R2D3-shift
    
  3. Install the required dependencies:
    poetry install
    

For CLI usage, install the additional dependencies:

  1. Install optional CLI dependencies:
    poetry install --extras cli
    

From PyPI

To install the package directly from PyPI, use pip:

pip install r2d3shift

For CLI usage, ensure you have the optional dependencies installed:

pip install r2d3shift[cli]

Usage Example

Before running any code, ensure the conda environment is activated.

Python Package

To process and correct a single spectrum loaded as a NumPy array, and store the corrected spectrum in a new array:

# Assume `data` is a 2D numpy array and `freqs` is a 1D numpy array of frequencies
from r2d3shift import Spectrum, Spectra, SparsePeaksShiftCorrector
sp = Spectra(freqs, [Spectrum(row) for row in data])
corrector = SparsePeaksShiftCorrector()
csp = corrector.correct(sp)
cdata = np.array([s.data for s in csp.spectra])

To process and correct all spectra in an archive and save the results to a new archive:

from r2d3shift import SpectraArchiveIOHandler2D, SparsePeaksShiftCorrector
handler = SpectraArchiveIOHandler2D()
data = handler.load('path/to/archive.zip', '.csv')
corrector = SparsePeaksShiftCorrector()
corrected = {name: corrector.correct(sp) for name, sp in data.items()}
handler.write('path/to/corrected_archive.zip', corrected)

To simulate a dataset with spectral shifts:

from r2d3shift import GeneralExponentialShiftModel, ShiftedSpectraSimulator
isochromats = [(2,0.1,0.025), (1,1.2,-0.35), (5,0.5,1)]
shift_model = GeneralExponentialShiftModel(
    beta=0.5, beta_std=0.3, max_shift=0.001,max_shift_std=0.01)
ssim = ShiftedSpectraSimulator(shift_model)
spectra = ssim(isochromats=isochromats, nb_indirect=10, acq_time=30, bw=1e3, 
               pSNR=50, TR_min=0.2, TR_max=2, magnet_freq=400*1e6)

Command Line Interface (CLI)

Do not forget to install the optional CLI dependencies.

To process a complete zip archive:

python -m r2d3shift process /path/to/archive.zip save /path/to/corrected.zip

To display correction results and validation metrics:

python -m r2d3shift process /path/to/archive.zip show

To simulate a dataset with spectral shifts:

python -m r2d3shift simulate /path/to/simulated.zip T1_1,T2_1,freq_1 T1_2,T2_2,freq_2 ...

For a full list of parameters, commands and help on CLI usage:

python -m r2d3shift [command] --help

License

This package is licensed under the CeCILL-2.1 license. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

r2d3shift-1.617978.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

r2d3shift-1.617978-py3-none-any.whl (46.2 kB view details)

Uploaded Python 3

File details

Details for the file r2d3shift-1.617978.tar.gz.

File metadata

  • Download URL: r2d3shift-1.617978.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.14.0-33-generic

File hashes

Hashes for r2d3shift-1.617978.tar.gz
Algorithm Hash digest
SHA256 8fa3563844f0232aa9f66ec4bc2a54e71a2ccc95e1f0c41ca8e791aa70d842cc
MD5 d56f38bace6341b964991a4a664f5535
BLAKE2b-256 93f5896c37e79fc58c100b7bc38f54d1e8e4546be70c1cb6a3be23b91b177fd0

See more details on using hashes here.

File details

Details for the file r2d3shift-1.617978-py3-none-any.whl.

File metadata

  • Download URL: r2d3shift-1.617978-py3-none-any.whl
  • Upload date:
  • Size: 46.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Linux/6.14.0-33-generic

File hashes

Hashes for r2d3shift-1.617978-py3-none-any.whl
Algorithm Hash digest
SHA256 6246821fcf50948aac20af1eb1205466cf89262b339e6602aefa4845a8ec573d
MD5 365e4aa3995678a5fae1209a28d74e98
BLAKE2b-256 37f2c6d52694d2d57ca477f99b2a35011798d2d2b06a71c210394e7ecef90e65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page