import sys
import matplotlib.pyplot as plt
sys.path.append("../../src")
from nmrlineshapeanalyser.core import NMRProcessor
#create NMRProcessor object
processor = NMRProcessor()
#Load filepath
filepath = r"..\..\data\six_peaks\4\pdata\1\\"
# Load the data
processor.load_data(filepath)
#Select the region of interest
x_data, y_data = processor.select_region(200, 800)
#Normalize the data and return normalised y_axis and the corresponding x_axis
x_data, y_normalized = processor.normalize_data(x_data, y_data)
#define initial parameters for the fitting
#this example is for a single peak
#format of the parameters is [x0, amplitude, width, eta, offset]
# x0 (position), amplitude, width, eta (mixing parameter), offset
#x0 has to be close to the peak position
initial_params = [
380, 0.12, 40.51, 0.89, -143.115,
425, 0.12, 40.51, 0.89, -143.115,
500, 0.219, 55.43, 0.52, -143.115,
547, 0.12, 40.51, 0.89, -143.115,
586, 0.219, 55.43, 0.52, -143.155,
647, 0.12, 40.51, 0.89, -143.115
]
number_of_peaks = 6
# fixed_x0 controls whether peak positions should be fixed during fitting
# False means position can vary, True means position is fixed
fixed_x0 = [False]*number_of_peaks
#FIt the data
popt, metrics, fitted = processor.fit_peaks(x_data, y_normalized, initial_params, fixed_x0)
#popt is the optimized parameters
#metrics is the metrics of the fitting
#fitted is the fitted curve data
#Plot and examine the results of the fitting
fig, axes, components = processor.plot_results(x_data, y_normalized, fitted, popt)
#Save the figure as an png file and the results as a csv file
processor.save_results(filepath, x_data, y_normalized, fitted, metrics, popt, components)