Using callbacks

When using basico, usually there is no feedback given when running tasks. That is fine when tasks dont take a long time, but for optimizations or parameter estimation runs, it would be nice to know how long things could go. Setting a callback also allows for interrupting a running task through interrupting by pressing Ctrl+C. Lets see how this works. We start as always by import basico. Additionally we also import the callbacks module (since i consider it an experimental module at the time of writing this, it is not done automatically):

[1]:
from basico import *
from basico.callbacks import create_default_handler

For now i have wrapped the tqdm library, others could easily be added later on let me know if you have a preference! The create_default_handler function sets up a tdqm handler, that will be used for all longer running operations. It takes the following arguments:

  • delay: delay in seconds before showing the first message (defaults to 1)

  • leave: boolean flag, that indicates whether messages should remain on screen after being completed (defaults to False)

  • unit: a string to display as unit for the iterations (by default tqdm will use it/s)

additional key value pairs will be passed on to tqdm. So let us just create a handler, and run some tasks:

[2]:
create_default_handler()

we start with a simple example model that has parameter estimation set up on it already

[3]:
load_example('LM');

it uses a local method and runs for 500 iterations, or until the tolerance of 1e-16 is reached:

[4]:
get_task_settings(T.PARAMETER_ESTIMATION)['method']
[4]:
{'Iteration Limit': 500,
 'Tolerance': 1e-16,
 'Stop after # Stalled Iterations': 0,
 '#LogVerbosity': 0,
 'name': 'Levenberg - Marquardt'}
[5]:
run_parameter_estimation()
[5]:
lower upper sol affected
name
(R1).k2 1e-6 1e6 0.000002 []
(R2).k1 1e-6 1e6 44.661715 []
Values[offset] -0.2 0.4 0.043018 [Experiment_1]
Values[offset] -0.2 0.4 0.054167 [Experiment_3]
Values[offset] -0.2 0.4 -0.050941 [Experiment]
Values[offset] -0.2 0.4 0.045922 [Experiment_4]
Values[offset] -0.2 0.4 0.048025 [Experiment_2]

lets choose just random search, which will take much longer, this will make the callbacks show up, and we will see the #function evaluations, as well as the best value reached. At any point we can interrup the execution of the cell (by presing the stop button on the notebook), this will cause the parameter estimation to stop after the next iteration, with partial results being returned:

[6]:
run_parameter_estimation(method=PE.RANDOM_SEARCH)
[6]:
lower upper sol affected
name
(R1).k2 1e-6 1e6 0.000014 []
(R2).k1 1e-6 1e6 45.274788 []
Values[offset] -0.2 0.4 0.178417 [Experiment_1]
Values[offset] -0.2 0.4 -0.022335 [Experiment_3]
Values[offset] -0.2 0.4 -0.050647 [Experiment]
Values[offset] -0.2 0.4 0.010597 [Experiment_4]
Values[offset] -0.2 0.4 -0.068453 [Experiment_2]

to remove the default handler again, the reset_default_handler function can be used. As it turns out, the output of the handler could not be seen on the html version of the notebook. So here a screenshot of the operation in action.

callback handlder