Hi all,
I have a lot of different columns from an FTIR spectra experiment, and in order to correct the baseline, I need to perform a Fit Spline on each column (one column = one spectra). I would like to do this automatically using JSL, so I don't have to save hundreds of "Predicted values" by hand.
I tried to create a code but somehow it doesn't want to save the values or put 0 in each columns.
Could anyone help for that?
Below is my code:
dt = Current Data Table();
// Define the columns for the fit (assuming multiple Y columns and one X column)
x_col = Column("Wavenumber"); // Replace with the name of your X column
y_cols = {};
For(i = 1, i <= N Col(dt), i++,
col_name = Column(i) << Get Name;
// Check if the column name contains "Absorbance at t="
If(Contains(col_name, "Absorbance at t="),
Insert Into(y_cols, col_name); // Add to the list
);
);
// Create a list to store fit reports
fit_reports = {};
// Perform Fit Y by X for each Y column separately and display the fit
For(i = 1, i <= N Items(y_cols), i++,
y_col = y_cols[i];
fit_report = dt << Fit Y by X(
Y(Column(y_col)),
X(x_col),
Fit Spline(1000) // Adjust the lambda (smoothing parameter) as needed
);
Insert Into(fit_reports, fit_report);
);
Show(
"All fits have been displayed. Please review them and adjust the lambda value in the script if needed."
);
// Wait for user to review fits
Wait(0);
// Use a Yes/No dialog to prompt the user to continue or stop
result = New Window("Save Predicted Values?",
Text Box("Are you satisfied with the fits? Click Yes to save predicted values, No to stop."),
Button Box("Yes",
// Save predicted values as columns in the dataset
For(i = 1, i <= N Items(y_cols), i++,
y_col = y_cols[i];
fit_report = fit_reports[i];
predicted_col_name = "Spline Predictor for " || y_col;
dt << New Column(predicted_col_name, Numeric);
Column(predicted_col_name) << Set Values(fit_report["Prediction Formula"]);
);
Show("Predicted values saved successfully.");
),
Button Box("No",
Show("Predicted values were not saved.");
)
);
// Close all fit reports
For(i = 1, i <= N Items(fit_reports), i++,
Close(fit_reports[i], No Save);
);
Close(result); // Close the dialog window
Edit 2024-09-24 jthi: added jsl formatting