Skip to content

Work with spectrum data saved in a csv file

The load_csv function lets you work directly with NMR data saved in a csv file.filter

Important thing to know is that this function, unlike the load_data function that works directly with Bruker files, you would have to declare the following parameters alongside the file path:

    - nucleus: the nucleus of the spectrum i.e. H, C, F, P, etc.
    - atomic number: the atomic number of the nucleus
    - larmor frequency: the frequency of the nucleus with respect to the magnetic field B0 in MHz
import sys
import glob
import os
import matplotlib.pyplot as plt
sys.path.append("../../src")
from nmrlineshapeanalyser.core import NMRProcessor

import pandas as pd
nmrglue: 0.11
numpy: 2.4.6
scipy: 1.17.1
matplotlib: 3.11.1
pandas: 3.0.5

# Initialize processor
processor = NMRProcessor()

from pathlib import Path

filepath = Path("../../data/data_saved_in_csv")
csv_path = filepath / "spectrum_peak.csv"

The CSV file must have the following columns:

df = pd.read_csv(csv_path)

print(df.head())

df = df[['ppm', 'Intensity']]

df.head(5)
       ppm  Intensity
0  10547.8      -15.0
1  10546.6      -15.0
2  10545.4      -15.0
3  10544.1      -15.0
4  10542.9      -15.0

ppm Intensity
0 10547.8 -15.0
1 10546.6 -15.0
2 10545.4 -15.0
3 10544.1 -15.0
4 10542.9 -15.0
# Load data
processor.load_csv(filepath, '17', 'O', 67.8)   

# Select region and normalize
x_region, y_region = processor.select_region(160, 850)
x_data, y_normalized, y_amp, y_ground = processor.normalize_data(x_region, y_region)

# Define initial parameters for peaks
# Each peak is defined by 5 parameters in order:
# x0 (position), amplitude, width, eta (mixing parameter), offset
# offset is shared across all peaks and must be in the normalized 0-1 scale
initial_params = [
    348, 0.16, 81, 0.89, 0.0, 

    435.5, 0.29, 51, 0.89, 0.0,

    560, 0.52, 100, 0.52, 0.0,

    600, 0.61, 82, 0.52, 0.0,

]

# Fit peaks
# fixed_x0 controls whether peak positions should be fixed during fitting
# False means position can vary, True means position is fixed
fixed_x0 = [False, False, False, True]
fixed_amp = [False, False, False, False]
fixed_width = [False, False, False, False]
# fixed_eta controls whether each peak's Gaussian/Lorentzian mixing parameter is fixed
# False means eta is fitted freely (default), True fixes it at its initial_params value
fixed_eta = [False, False, False, False]
 # Allow all peak positions to vary
# popt: optimized parameters
# metrics: fitting metrics for each peak
# fitted: fitted curve data
popt, metrics, fitted = processor.fit_peaks(x_data, y_normalized, initial_params, fixed_x0, fixed_amp, fixed_width, fixed_eta)

# Plot and save results
fig, axes, components = processor.plot_results(x_data, y_normalized, fitted, popt)

# Save all results to a separate directory so outputs don't get mixed in with the source CSV
output_dir = filepath / "results"
output_dir.mkdir(exist_ok=True)
# save_results builds filenames via string concatenation (filepath + 'name.ext'),
# so it needs a string ending in a separator rather than a Path object
processor.save_results(str(output_dir) + os.sep, x_data, y_normalized, fitted, metrics, 
                        popt, components)

Peak Fitting Results:
===================

Peak 1 (Position: 343.19 ± 6.06):
  Amplitude: 0.119 ± 0.030
  Width (FWHM): 84.70 ± 11.53 ppm
  Width (FWHM): 5742.38 ± 781.67 Hz
  Carrier Frequency: 67.8 MHz
  Eta: 0.19 ± 0.26
  Offset: 0.0097 ± 0.0038
  Gaussian Area: 8.61 ± 3.69
  Lorentzian Area: 3.06 ± 4.12
  Total Area: 11.67 ± 5.53

Peak 2 (Position: 432.96 ± 0.63):
  Amplitude: 0.338 ± 0.029
  Width (FWHM): 68.40 ± 9.54 ppm
  Width (FWHM): 4637.77 ± 646.71 Hz
  Carrier Frequency: 67.8 MHz
  Eta: 1.00 ± 0.47
  Offset: 0.0097 ± 0.0038
  Gaussian Area: 0.00 ± 11.54
  Lorentzian Area: 36.32 ± 18.04
  Total Area: 36.32 ± 21.41

Peak 3 (Position: 551.51 ± 4.71):
  Amplitude: 0.612 ± 0.066
  Width (FWHM): 110.37 ± 11.19 ppm
  Width (FWHM): 7482.98 ± 758.73 Hz
  Carrier Frequency: 67.8 MHz
  Eta: 0.05 ± 0.11
  Offset: 0.0097 ± 0.0038
  Gaussian Area: 68.28 ± 13.00
  Lorentzian Area: 5.32 ± 12.04
  Total Area: 73.60 ± 17.72

Peak 4 (Position: 600.00 ± 0.00):
  Amplitude: 0.508 ± 0.105
  Width (FWHM): 92.42 ± 1.92 ppm
  Width (FWHM): 6266.06 ± 130.04 Hz
  Carrier Frequency: 67.8 MHz
  Eta: 0.00 ± 0.07
  Offset: 0.0097 ± 0.0038
  Gaussian Area: 50.01 ± 10.94
  Lorentzian Area: 0.00 ± 5.08
  Total Area: 50.01 ± 12.06
Peak 1 Percentage: 6.80% ± 3.44%
Peak 2 Percentage: 21.16% ± 13.04%
Peak 3 Percentage: 42.89% ± 12.88%
Peak 4 Percentage: 29.14% ± 8.76%
Total: 100.00%

No description has been provided for this image