cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Discovery is online this week, April 16 and 18. Join us for these exciting interactive sessions.
Choose Language Hide Translation Bar
SDF1
Super User

Is it possible to pause a FOR loop?

Dear JMP Community,

 

  Is it possible to pause a FOR loop and then have it resume again later? I'm thinking of having a popup window with a "pause" button and a "resume" button. The idea would be that in the script when the pause button is pushed, the calculation would finish, the iteration number still being in memory would remain so that when the resume button is pushed, the calculation would pick up at the given iteration.

 

  If it's not possible, please let me know and I'll stop trying to pursue this on my own.

 

Thanks!,

DS

1 ACCEPTED SOLUTION

Accepted Solutions
cwillden
Super User (Alumni)

Re: Is it possible to pause a FOR loop?

Here's an illustration of one approach.  Basically, there is a variable, iter, that tracks the iterations separate from the iteration variable of the For loop, 'i' in this case.  When you pause and restart, the For loop starts at iter+1 and keeps going.  Without the wait() function, this would not work because the script from the stop button would not execute until the end of the loop.  0.5 seconds is a much longer wait than necessary.  In fact, this example works with wait(0).

New Window( "Test Pause Loop",
	V List Box(
		tb1 = Text Box( "Iteration: 0" ),
		H List Box(
			start_btn = Button Box( "Start", On Start ),
			stop_btn = Button Box( "Stop", On Stop )
		)
	)
);

pause_state = 1;
iter = 0;

loop_expr = Expr(
	for(i = iter+1, i<=1000, i++,
		iter = i;
		tb1 << Set Text("Iteration: "||char(iter));
		wait(0.5);
		show(iter, pause_state);
		if(pause_state == 1, break());
	)
);

On Start = Expr(
	pause_state = 0;
	start_btn << Enable(0);
	loop_expr();
);

On Stop = Expr(
	pause_state=1;
	start_btn << Enable(1);
);

 

 

-- Cameron Willden

View solution in original post

2 REPLIES 2
cwillden
Super User (Alumni)

Re: Is it possible to pause a FOR loop?

Here's an illustration of one approach.  Basically, there is a variable, iter, that tracks the iterations separate from the iteration variable of the For loop, 'i' in this case.  When you pause and restart, the For loop starts at iter+1 and keeps going.  Without the wait() function, this would not work because the script from the stop button would not execute until the end of the loop.  0.5 seconds is a much longer wait than necessary.  In fact, this example works with wait(0).

New Window( "Test Pause Loop",
	V List Box(
		tb1 = Text Box( "Iteration: 0" ),
		H List Box(
			start_btn = Button Box( "Start", On Start ),
			stop_btn = Button Box( "Stop", On Stop )
		)
	)
);

pause_state = 1;
iter = 0;

loop_expr = Expr(
	for(i = iter+1, i<=1000, i++,
		iter = i;
		tb1 << Set Text("Iteration: "||char(iter));
		wait(0.5);
		show(iter, pause_state);
		if(pause_state == 1, break());
	)
);

On Start = Expr(
	pause_state = 0;
	start_btn << Enable(0);
	loop_expr();
);

On Stop = Expr(
	pause_state=1;
	start_btn << Enable(1);
);

 

 

-- Cameron Willden
SDF1
Super User

Re: Is it possible to pause a FOR loop?

Hi @cwillden,

 

  Thanks for your input on this, it's a great solution! It works with my test script that shows a progress indicator for the overall modelling progress when I run a large tuning table.

 

  My end goal is to put the call to fit the data into the loop_expr portion so that I can pause the overall modeling process and resume it later. I think this might take a little more finagling. I am testing it with script (Mark Bailey's) from this post here using the SVM platform.

 

  Here's the progress bar/iteration script I have that I'd like to use to "control" the progress by being able to pause and then resume at a later time.

Names Default To Here( 1 );
i = 1;
imax = 100;
dlgStatus = New Window( "Overall Modeling Progress",
	V List Box(
		Text Box( " Overall Modeling Progress ", <<Set Font Size( 12 ), <<Justify Text( "center" ), <<Set width( 200 ) ),
		dlg_gb = Graph Box( FrameSize( 200, 15 ), X Scale( 0, 100 ), Y Scale( 0, 1 ) ),
		tb = Text Box(
			"Current step " || Char( i ) || " of " || Char( imax ),
			<<Set Font Size( 12 ),
			<<Justify Text( "center" ),
			<<Set width( 200 )
		),
		H List Box( bbp = Button Box( "Pause", On Pause ), bbr = Button Box( "Resume", On Resume ) )
	)
);

dlg_gb[Axis Box( 2 )] << Delete;
dlg_gb[Axis Box( 1 )] << Delete;

pause_state = 0;
iter = 0;

Loop_Expr();

Loop_expr = Expr(
	For( i = iter + 1, i <= imax, i++,
		iter = i;
		prog = (i / imax) * 100;
		dlgStatus[FrameBox( 1 )] << Add Graphics Script( {Fill Color( "purple" ), Rect( 0, 1, prog, 0, 1 )} );
		tb << set text( "Current step " || Char( i ) || " of " || Char( imax ) );
		Wait( 0.1 );
		If( pause_state == 1,
			Break()
		);
	)
);

On Resume = Expr(
	pause_state = 0;
	bbr << Enable( 0 );
	Loop_expr();
);

On Pause = Expr(
	pause_state = 1;
	bbr << Enable( 1 );
);

Here's the SVM script that Mark wrote:

Names Default To Here( 1 );

// use a small example
data = Open( "$SAMPLE_DATA/Big Class.jmp" );

// launch SVM platform
svm = data << Support Vector Machines( Y( :age ), X( :height, :weight ) );

// iterate over a range of hyper-parameters
For( cost = 0.1, cost <= 5, cost += 0.05,
	For( gamma = 0.1, gamma <= 1, gamma += 0.1,
		svm << Fit(
			Kernel Function( "Radial Basis Function" ),
			Gamma( gamma ),
			Cost( cost ),
			Validation Method( "None" )
		)
	)
);

// separate parameters and performance in a new table
results = Report( svm )["Model Comparison"][Table Box( 1 )] << Make Into Data Table;

// fit interpolator for profiling
results << Neural(
	Y( :Training Misclassification Rate ),
	X( :Cost, :Gamma ),
	Informative Missing( 0 ),
	Validation Method( "Holdback", 0.3333 ),
	Fit(
		NTanH( 3 ),
		Profiler(
			1,
			Confidence Intervals( 1 ),
			Term Value( Cost( 2.75, Lock( 0 ), Show( 1 ) ), Gamma( 1, Lock( 0 ), Show( 1 ) ) )
		),
		Plot Actual by Predicted( 1 ),
		Plot Residual by Predicted( 1 )
	)
);

Any suggestions on how to do that would be great!

 

Thanks!,

DS