cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-801459%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%E9%81%A9%E5%90%88%E5%A4%9A%E5%88%97%E7%9A%84%E6%A8%A3%E6%A2%9D%E6%9B%B2%E7%B7%9A%E4%B8%A6%E5%84%B2%E5%AD%98%E9%A0%90%E6%B8%AC%E5%80%BC%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801459%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3E%E5%A4%A7%E5%AE%B6%E5%A5%BD%EF%BC%8C%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3E%E6%88%91%E6%9C%89%E5%BE%88%E5%A4%9A%E4%BE%86%E8%87%AA%20FTIR%20%E5%85%89%E8%AD%9C%E5%AF%A6%E9%A9%97%E7%9A%84%E4%B8%8D%E5%90%8C%E5%88%97%EF%BC%8C%E7%82%BA%E4%BA%86%E6%A0%A1%E6%AD%A3%E5%9F%BA%E7%B7%9A%EF%BC%8C%E6%88%91%E9%9C%80%E8%A6%81%E5%B0%8D%E6%AF%8F%E5%88%97%E5%9F%B7%E8%A1%8C%E6%93%AC%E5%90%88%E6%A8%A3%E6%A2%9D%EF%BC%88%E4%B8%80%E5%88%97%20%3D%20%E4%B8%80%E5%80%8B%E5%85%89%E8%AD%9C%EF%BC%89%E3%80%82%E6%88%91%E6%83%B3%E4%BD%BF%E7%94%A8%20JSL%20%E8%87%AA%E5%8B%95%E5%9F%B7%E8%A1%8C%E6%AD%A4%E6%93%8D%E4%BD%9C%EF%BC%8C%E9%80%99%E6%A8%A3%E6%88%91%E5%B0%B1%E4%B8%8D%E5%BF%85%E6%89%8B%E5%8B%95%E4%BF%9D%E5%AD%98%E6%95%B8%E7%99%BE%E5%80%8B%E3%80%8C%E9%A0%90%E6%B8%AC%E5%80%BC%E3%80%8D%E3%80%82%3C%2FP%3E%0A%3CP%3E%E6%88%91%E5%98%97%E8%A9%A6%E5%89%B5%E5%BB%BA%E4%B8%80%E5%80%8B%E4%BB%A3%E7%A2%BC%EF%BC%8C%E4%BD%86%E4%B8%8D%E7%9F%A5%E4%BD%95%E6%95%85%E5%AE%83%E4%B8%8D%E6%83%B3%E4%BF%9D%E5%AD%98%E5%80%BC%E6%88%96%E5%9C%A8%E6%AF%8F%E5%88%97%E4%B8%AD%E6%94%BE%E5%85%A5%200%E3%80%82%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3E%E6%9C%89%E4%BA%BA%E5%8F%AF%E4%BB%A5%E5%B9%AB%E5%BF%99%E5%97%8E%EF%BC%9F%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3E%E4%B8%8B%E9%9D%A2%E6%98%AF%E6%88%91%E7%9A%84%E7%A8%8B%E5%BC%8F%E7%A2%BC%EF%BC%9A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Current%20Data%20Table()%3B%0A%0A%2F%2F%20Define%20the%20columns%20for%20the%20fit%20(assuming%20multiple%20Y%20columns%20and%20one%20X%20column)%0Ax_col%20%3D%20Column(%22Wavenumber%22)%3B%20%2F%2F%20Replace%20with%20the%20name%20of%20your%20X%20column%0A%0Ay_cols%20%3D%20%7B%7D%3B%0AFor(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Col(dt)%2C%20i%2B%2B%2C%20%0A%09col_name%20%3D%20Column(i)%20%26lt%3B%26lt%3B%20Get%20Name%3B%0A%20%20%20%20%2F%2F%20Check%20if%20the%20column%20name%20contains%20%22Absorbance%20at%20t%3D%22%0A%09If(Contains(col_name%2C%20%22Absorbance%20at%20t%3D%22)%2C%20%0A%09%09Insert%20Into(y_cols%2C%20col_name)%3B%20%20%2F%2F%20Add%20to%20the%20list%0A%09)%3B%0A)%3B%0A%0A%2F%2F%20Create%20a%20list%20to%20store%20fit%20reports%0Afit_reports%20%3D%20%7B%7D%3B%0A%0A%2F%2F%20Perform%20Fit%20Y%20by%20X%20for%20each%20Y%20column%20separately%20and%20display%20the%20fit%0AFor(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(y_cols)%2C%20i%2B%2B%2C%20%0A%09y_col%20%3D%20y_cols%5Bi%5D%3B%0A%09fit_report%20%3D%20dt%20%26lt%3B%26lt%3B%20Fit%20Y%20by%20X(%0A%09%09Y(Column(y_col))%2C%20%0A%09%09X(x_col)%2C%20%0A%09%09Fit%20Spline(1000)%20%2F%2F%20Adjust%20the%20lambda%20(smoothing%20parameter)%20as%20needed%0A%09)%3B%0A%09Insert%20Into(fit_reports%2C%20fit_report)%3B%0A)%3B%0A%0AShow(%0A%09%22All%20fits%20have%20been%20displayed.%20Please%20review%20them%20and%20adjust%20the%20lambda%20value%20in%20the%20script%20if%20needed.%22%0A)%3B%0A%0A%2F%2F%20Wait%20for%20user%20to%20review%20fits%0AWait(0)%3B%0A%0A%2F%2F%20Use%20a%20Yes%2FNo%20dialog%20to%20prompt%20the%20user%20to%20continue%20or%20stop%0Aresult%20%3D%20New%20Window(%22Save%20Predicted%20Values%3F%22%2C%20%0A%09Text%20Box(%22Are%20you%20satisfied%20with%20the%20fits%3F%20Click%20Yes%20to%20save%20predicted%20values%2C%20No%20to%20stop.%22)%2C%20%0A%09Button%20Box(%22Yes%22%2C%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Save%20predicted%20values%20as%20columns%20in%20the%20dataset%0A%09%09For(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(y_cols)%2C%20i%2B%2B%2C%20%0A%09%09%09y_col%20%3D%20y_cols%5Bi%5D%3B%0A%09%09%09fit_report%20%3D%20fit_reports%5Bi%5D%3B%0A%09%09%09predicted_col_name%20%3D%20%22Spline%20Predictor%20for%20%22%20%7C%7C%20y_col%3B%0A%09%09%09dt%20%26lt%3B%26lt%3B%20New%20Column(predicted_col_name%2C%20Numeric)%3B%0A%09%09%09Column(predicted_col_name)%20%26lt%3B%26lt%3B%20Set%20Values(fit_report%5B%22Prediction%20Formula%22%5D)%3B%0A%09%09)%3B%0A%09%09Show(%22Predicted%20values%20saved%20successfully.%22)%3B%0A%09)%2C%20%0A%09Button%20Box(%22No%22%2C%20%0A%09%09Show(%22Predicted%20values%20were%20not%20saved.%22)%3B%0A%09)%0A)%3B%0A%0A%2F%2F%20Close%20all%20fit%20reports%0AFor(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(fit_reports)%2C%20i%2B%2B%2C%20%0A%09Close(fit_reports%5Bi%5D%2C%20No%20Save)%3B%0A)%3B%0A%20%0A%0AClose(result)%3B%20%2F%2F%20Close%20the%20dialog%20window%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%E7%B7%A8%E8%BC%AF%202024-09-24%20jthi%EF%BC%9A%E6%96%B0%E5%A2%9E%E4%BA%86%20jsl%20%E6%A0%BC%E5%BC%8F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-801459%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CLINGO-LABEL%3E%E8%87%AA%E5%8B%95%E5%8C%96%E5%92%8C%E8%85%B3%E6%9C%AC%E7%B7%A8%E5%AF%AB%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-801578%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E7%82%BA%E5%A4%9A%E5%88%97%E6%93%AC%E5%90%88%E6%A8%A3%E6%A2%9D%E7%B7%9A%E4%B8%A6%E5%84%B2%E5%AD%98%E9%A0%90%E6%B8%AC%E5%80%BC%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801578%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E9%80%99%E5%8F%AF%E8%83%BD%E6%9C%83%E7%B5%A6%E5%87%BA%E4%B8%80%E4%BA%9B%E6%83%B3%E6%B3%95%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0Adt%20%3D%20Open(%22%24SAMPLE_DATA%2FSemiconductor%20Capability.jmp%22)%3B%0A%0Axcol%20%3D%20%22NPN1%22%3B%0Aycols%20%3D%20%7B%22PNP1%22%2C%20%22NPN2%22%7D%3B%0A%0Afits%20%3D%20%7B%7D%3B%0AFor%20Each(%7Bycol%7D%2C%20ycols%2C%0A%09Insert%20Into(fits%2C%0A%09%09dt%20%26lt%3B%26lt%3B%20Fit%20Y%20by%20X(%0A%09%09%09Y(Eval(ycol))%2C%0A%09%09%09X(Eval(xcol))%2C%0A%09%09%09Fit%20Spline(1000)%09%0A%09%09)%3B%0A%09)%3B%0A)%3B%0A%0Aclose_expr%20%3D%20Expr(%0A%09For%20Each(%7Bfit%7D%2C%20fits%2C%0A%09%09fit%20%26lt%3B%26lt%3B%20close%20window%3B%0A%09)%3B%0A%09nw%20%26lt%3B%26lt%3B%20Close%20Window%3B%0A)%3B%0A%0Anw%20%3D%20New%20Window(%22Save%20Predicted%20Values%3F%22%2C%20%0A%09Text%20Box(%22Are%20you%20satisfied%20with%20the%20fits%3F%20Click%20Yes%20to%20save%20predicted%20values%2C%20No%20to%20stop.%22)%2C%20%0A%09Button%20Box(%22Yes%22%2C%20%0A%09%09For%20Each(%7Bfit%7D%2C%20fits%2C%0A%09%09%09fit%20%26lt%3B%26lt%3B%20(Curve%5B%22Smoothing%20Spline%20Fit%2C%20lambda%3D1000%22%5D%20%26lt%3B%26lt%3B%20Save%20Predicteds)%3B%0A%09%09)%3B%0A%09%09wait(0)%3B%0A%09%09close_expr%3B%0A%09%09Show(%22Predicted%20values%20saved%20successfully.%22)%3B%0A%09)%2C%20%0A%09Button%20Box(%22No%22%2C%20%0A%09%09Show(%22Predicted%20values%20were%20not%20saved.%22)%3B%0A%09%09close_expr%3B%0A%09)%0A)%3B%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%E6%88%91%E9%82%84%E6%9C%83%E6%94%B6%E9%9B%86%E6%89%80%E6%9C%89%E2%80%9CFit%20Y%20by%20X%E2%80%9D%E5%88%B0%E5%96%AE%E5%80%8B%E7%AA%97%E5%8F%A3%EF%BC%8C%E9%80%99%E6%A8%A3%E6%9B%B4%E5%AE%B9%E6%98%93%E6%AA%A2%E6%9F%A5%E5%AE%83%E5%80%91%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E6%89%93%E9%96%8B%E5%A4%9A%E5%80%8B%E5%96%AE%E7%8D%A8%E7%9A%84%E7%AA%97%E5%8F%A3%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-801715%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E7%82%BA%E5%A4%9A%E5%88%97%E6%93%AC%E5%90%88%E6%A8%A3%E6%A2%9D%E6%9B%B2%E7%B7%9A%E4%B8%A6%E5%84%B2%E5%AD%98%E9%A0%90%E6%B8%AC%E5%80%BC%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801715%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E6%95%88%E6%9E%9C%E9%9D%9E%E5%B8%B8%E5%A5%BD%EF%BC%8C%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%AC%9D%EF%BC%81%E6%88%91%E5%B7%B2%E7%B6%93%E5%9C%A8%20ChatGPT%20%E4%B8%8A%E8%8B%A6%E8%8B%A6%E5%B0%8B%E6%89%BE%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88%E5%B9%BE%E5%80%8B%E5%B0%8F%E6%99%82%E4%BA%86%3CLI-EMOJI%20id%3D%22lia_grinning-face-with-smiling-eyes%22%20title%3D%22%3Acara_que_r%C3%ADe_con_la_boca_abierta_y_los_ojos_sonrientes%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
LGaspard_98
Level I

Fit Spline for multiple columns and saving predicted values

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

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Fit Spline for multiple columns and saving predicted values

This might give some ideas

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");

xcol = "NPN1";
ycols = {"PNP1", "NPN2"};

fits = {};
For Each({ycol}, ycols,
	Insert Into(fits,
		dt << Fit Y by X(
			Y(Eval(ycol)),
			X(Eval(xcol)),
			Fit Spline(1000)	
		);
	);
);

close_expr = Expr(
	For Each({fit}, fits,
		fit << close window;
	);
	nw << Close Window;
);

nw = 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", 
		For Each({fit}, fits,
			fit << (Curve["Smoothing Spline Fit, lambda=1000"] << Save Predicteds);
		);
		wait(0);
		close_expr;
		Show("Predicted values saved successfully.");
	), 
	Button Box("No", 
		Show("Predicted values were not saved.");
		close_expr;
	)
);

I would also collect all the Fit Y by X to single window so it is easier to check them instead of opening multiple separate windows

-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: Fit Spline for multiple columns and saving predicted values

This might give some ideas

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");

xcol = "NPN1";
ycols = {"PNP1", "NPN2"};

fits = {};
For Each({ycol}, ycols,
	Insert Into(fits,
		dt << Fit Y by X(
			Y(Eval(ycol)),
			X(Eval(xcol)),
			Fit Spline(1000)	
		);
	);
);

close_expr = Expr(
	For Each({fit}, fits,
		fit << close window;
	);
	nw << Close Window;
);

nw = 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", 
		For Each({fit}, fits,
			fit << (Curve["Smoothing Spline Fit, lambda=1000"] << Save Predicteds);
		);
		wait(0);
		close_expr;
		Show("Predicted values saved successfully.");
	), 
	Button Box("No", 
		Show("Predicted values were not saved.");
		close_expr;
	)
);

I would also collect all the Fit Y by X to single window so it is easier to check them instead of opening multiple separate windows

-Jarmo
LGaspard_98
Level I

Re: Fit Spline for multiple columns and saving predicted values

It worked amazingly, thank you so much! I've been struggling with ChatGPT to find a solution for hours