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:
The CSV file must have the following columns:
# 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)