cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

Find and extract an expression argument with a given expression head

MathStatChem
Level VI

I want to write an efficient script that will take as an input a JSL expression and:

  1. Find an argument in that expression that has a specific expression head
  2. Substitute that argument with a modified version of that argument

 

Example:

 

I have this expression

 

my_expr=Expr(Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
		Parameter Estimates( 1 ), Scaled Estimates( 0 ),
		Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
		Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
		Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
		{:Drug << {LSMeans Contrast( [1 0 -1] )}}}
	)
));

I want to find  the "LSMeans Contrast(...)" argument that is buried in the expression and extract that expression in to a new expression, let's call that extracted_expression.  

 

 

So in this case I want 

 

extracted_expression = Expr(LS Means Contrast([1 0 -1]))

 

 

 

I then want to get the argument to that expression, modify it, and then modify the extracted_expression, and substitute the modified argument expression for the original extracted expression. . For example:

 

new_expression=Substitute(Name Expr(extracted_expression), arg(extracted_expression,1), [-1 0 .5]);

Substitute(Name Expr(my_expr), Name Expr(extracted_expression), Name Expr(new_expression));

 

The resulting expression would be

Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
		Parameter Estimates( 1 ), Scaled Estimates( 0 ),
		Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
		Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
		Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
		{:Drug << {LS Means Contrast( [-1 0 0.5] )}}}
	)
)

 

Where I am getting stuck is step 1, how to traverse the arguments of the original expression to find the argument that has an an expression with the specified header (in this example, the head of the argument is LS Means Contrast).

 

Any suggestions/ideas on how to do this?  

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User


Re: Find and extract an expression argument with a given expression head

Extract Expr maybe what you are looking for?

Names Default To Here(1);


my_expr=Expr(Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
		Parameter Estimates( 1 ), Scaled Estimates( 0 ),
		Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
		Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
		Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
		{:Drug << {LSMeans Contrast( [1 0 -1] )}}}
	)
));

myhead = Extract Expr(my_expr, LSMeans Contrast(Wild()));
myarg = Arg(part, 1);

//show(myhead, myarg);

new_expr = Substitute(Name Expr(myhead), myarg, [1 0 1]);
my_expr2 = Substitute(Name Expr(my_expr), Name Expr(myhead), Name Expr(new_expr));

Show(my_expr, my_expr2);

 

 

-Jarmo

View solution in original post

6 REPLIES 6
MathStatChem
Level VI


Re: Find and extract an expression argument with a given expression head

Note, this isn't the real problem I'm working on, the expression provided above is just an example that has an expression argument buried deep in an expression.  

hogi
Level XII


Re: Find and extract an expression argument with a given expression head

Extract Expr()does the magic  : )

hogi_0-1738860892387.png

 

jthi
Super User


Re: Find and extract an expression argument with a given expression head

Extract Expr maybe what you are looking for?

Names Default To Here(1);


my_expr=Expr(Fit Model(
	Y( :y ),
	Effects( :Drug, :x ),
	Personality( "Standard Least Squares" ),
	Emphasis( "Effect Leverage" ),
	Run(
		:y << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
		Parameter Estimates( 1 ), Scaled Estimates( 0 ),
		Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
		Plot Studentized Residuals( 0 ), Plot Effect Leverage( 1 ),
		Plot Residual by Normal Quantiles( 0 ), Box Cox Y Transformation( 0 ),
		{:Drug << {LSMeans Contrast( [1 0 -1] )}}}
	)
));

myhead = Extract Expr(my_expr, LSMeans Contrast(Wild()));
myarg = Arg(part, 1);

//show(myhead, myarg);

new_expr = Substitute(Name Expr(myhead), myarg, [1 0 1]);
my_expr2 = Substitute(Name Expr(my_expr), Name Expr(myhead), Name Expr(new_expr));

Show(my_expr, my_expr2);

 

 

-Jarmo
hogi
Level XII


Re: Find and extract an expression argument with a given expression head

Here are some details /pitfalls of extract expr():

Extract Expr 

 

hogi
Level XII


Re: Find and extract an expression argument with a given expression head

important to know:
extract expr() is not greedy , it will stop after finding the first match.
On the other hand, Substitute() IS greedy. 

 

The 2-step Substitute in @jthi 's code is essential!

my_expr=Expr(Fit Model(
		{:Drug << {LSMeans Contrast( [1 0 -1] )}}
	);
	[1 0 -1] // another time the same Arg, lives in danger
	
);

part= Extract Expr(my_expr, LSMeans Contrast(Wild()));
myarg = Arg(part, 1); //part instead of myhead

new_expr = Substitute(Name Expr(myhead), myarg, [1 0 1]);
my_expr2 = Substitute(Name Expr(my_expr), Name Expr(myhead), Name Expr(new_expr));// won't work// my_expr3 = Substitute(Name Expr(my_expr), Name Expr(myarg), [1 0 1]);

Here, the issue is obvious - but there are other cases where it is more difficult to control which expression gets substituted.

 

related wishes:
Substitute First Occurrence 

🙏 Expression Indexing: read and write access 

subst() - substitute() 2.0 

 

 

MathStatChem
Level VI


Re: Find and extract an expression argument with a given expression head

Thanks @jthi and @hogi .  I wasn't familiar with Extract Expr() and the Wild() expression.  Works nicely for what I am actually working on.  Much appreciated.