This repository contains the code for the analysis of chemical reaction kinetics using nuclear magnetic resonance (NMR) spectra and the Magnetstein algorithm.
Magnetstein is available here. The function dedicated for the analysis of chemical reactions is estimate_proportions_in_time.
The method should be used when:
- peaks move throughout the reaction,
- peaks from different reagents overlap,
- lineshapes are distorted,
- there are contaminations and/or noise.
In such circumstances, the Magnetstein algorithm gains an advantage over the traditional integration methods due to the special properties of the Wasserstein metric that is the core concept of the method. The metric makes the algorithm robust to changes in peaks' locations and shapes, and to ambiguity in assigning signal to particular reagents due to overlap. The additional refinements make it possible for the algorithm to remove noise from the data.
The input should consist of the two crucial parts:
- mixture, i.e. a series of NMR spectra of reaction mixture measured in the consecutive moments of time,
- library, i.e. a set of spectra of individual reagents expected to be present in the mixture (no need for a series here, just a single spectrum for each reagent).
The user needs to define the values of two parameters:
If you are unsure about how to set the parameters, we recommend using values
The main output of the algorithm is the series of vectors indexed by time:
where
In order to make the results reproducible, one needs to provide: 1) input data, i.e. series of spectra of the reaction mixture indexed by time, and the library; 2) information about the parameters settings, i.e. chosen values of
To be able to run the code from this repository, set your environment as in requirements.txt file. We also strongly recommend installing Gurobi and using it as a solver in the analysis.
Due to the large size of the input data, they are not stored in this repository. The data are available here. The data folder should be stored as magnetstein_x_chemical_reactions/data to ensure that all the paths are correct.
The code used for preprocessing, analysis and visualisations is available in analysis/. To reproduce the analysis (without warm-start), use analysis/estimation.py. To run the analysis with warm-start, use analysis/estimation_with_warm_start.py. Note that the results obtained by running these scripts may be slightly different from those saved in results/, for example due to numerical errors or updates/randomness in Gurobi solver.
The numerical results as well as figures are available in results/. Additional figures showing the comparison of results for different settings of parameters are available in kappa_panels/.
To compare the results from Magnetstein with other tools (Mnova and manual integration in Python), we use the data stored in mnova_integrals/ and python_integrals/.
If you use Magnetstein for analysis of series of NMR spectra, please cite one of the following:
Domżał, B., Grochowska-Tatarczak, M., Malinowski, P., Miasojedow, B., Kazimierczuk, K., & Gambin, A. (2025). NMR Reaction Monitoring Robust to Spectral Distortions. Analytical Chemistry. DOI: 10.1021/acs.analchem.5c00800.
Domżał, B., Nawrocka, E.K., Gołowicz, D., Ciach, M.A., Miasojedow, B., Kazimierczuk, K., & Gambin, A. (2023). Magnetstein: An Open-Source Tool for Quantitative NMR Mixture Analysis Robust to Low Resolution, Distorted Lineshapes, and Peak Shifts. Analytical Chemistry. DOI: 10.1021/acs.analchem.3c03594.


