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%3ESpline%20f%C3%BCr%20mehrere%20Spalten%20anpassen%20und%20vorhergesagte%20Werte%20speichern%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801459%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EHallo%20zusammen%2C%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3EIch%20habe%20viele%20verschiedene%20Spalten%20aus%20einem%20FTIR-Spektrenexperiment%20und%20um%20die%20Basislinie%20zu%20korrigieren%2C%20muss%20ich%20f%C3%BCr%20jede%20Spalte%20eine%20Spline-Anpassung%20durchf%C3%BChren%20(eine%20Spalte%20%3D%20ein%20Spektrum).%20Ich%20m%C3%B6chte%20dies%20automatisch%20mit%20JSL%20tun%2C%20damit%20ich%20nicht%20Hunderte%20von%20%E2%80%9Evorhergesagten%20Werten%E2%80%9C%20manuell%20speichern%20muss.%3C%2FP%3E%0A%3CP%3EIch%20habe%20versucht%2C%20einen%20Code%20zu%20erstellen%2C%20aber%20irgendwie%20will%20er%20die%20Werte%20nicht%20speichern%20oder%20in%20jede%20Spalte%20eine%200%20einf%C3%BCgen.%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3EKann%20mir%20da%20jemand%20weiterhelfen%3F%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3EUnten%20ist%20mein%20Code%3A%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%3EEdit%202024-09-24%20jthi%3A%20JSL-Formatierung%20hinzugef%C3%BCgt%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%3EAutomatisierung%20und%20Skripting%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%3EBetreff%3A%20Spline%20f%C3%BCr%20mehrere%20Spalten%20anpassen%20und%20vorhergesagte%20Werte%20speichern%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801578%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EDies%20k%C3%B6nnte%20einige%20Ideen%20geben%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%3EIch%20w%C3%BCrde%20auch%20alle%20Fit%20Y%20by%20X%20in%20einem%20einzigen%20Fenster%20sammeln%2C%20damit%20es%20einfacher%20ist%2C%20sie%20zu%20%C3%BCberpr%C3%BCfen%2C%20anstatt%20mehrere%20separate%20Fenster%20zu%20%C3%B6ffnen%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-801715%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EBetreff%3A%20Spline%20f%C3%BCr%20mehrere%20Spalten%20anpassen%20und%20vorhergesagte%20Werte%20speichern%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-801715%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EEs%20hat%20wunderbar%20funktioniert%2C%20vielen%20Dank!%20Ich%20habe%20stundenlang%20mit%20ChatGPT%20gek%C3%A4mpft%2C%20um%20eine%20L%C3%B6sung%20zu%20finden%20%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