1
$\begingroup$

I'm creating too much throw away code for interfacing with the scipy optimize package in a user friendly way. (See code below for example of interruptible optimization that keeps last optimization value after a KeyBoard interrupt)

def monitoring_callback(x):
    global callback_dict
    callback_dict['cached_results'] = x
    callback_dict['counter']  = callback_dict['counter']  +1

try:
    fit_results= optimize.minimize(get_mean_squared_error,
                                   my_fit_params,method="CG", 
                                   callback=monitoring_callback)
    my_fit_params = fit_results

except KeyboardInterrupt:
    my_fit_params = callback_dict['cached_results']


Is there an existing package that does this sort of thing? (Also, if it implemented graphics like here, http://louistiao.me/notes/visualizing-and-animating-optimization-algorithms-with-matplotlib/

that would be really useful.)

$\endgroup$
  • 2
    $\begingroup$ Why do you consider this 'too much' 'throw away' code? Which part is 'throw away' code? It looks nice, it is readable and compact. $\endgroup$ – nluigi May 13 at 6:11
  • $\begingroup$ The global vars is ugly. Maybe there's some way to turn this into a class... The ideal solution could be wrapped into a conda package that other people could use. Maybe a good weekend warrior project. I'm open for advice/suggestions. Also, it looks like some call back hooks are in the works. github.com/scipy/scipy/pull/7425 Maybe it will lead to something. $\endgroup$ – mathew gunther May 13 at 12:59
  • 2
    $\begingroup$ It doesn't have to be a global variable, you can also make monitoring_callback a nested function within a function. You should be able to pass lambda expressions as callbacks, so there should be no trouble using objects with this. $\endgroup$ – Kirill May 13 at 14:38
  • $\begingroup$ @Kirill consider writing your suggestion as an answer. It is very useful. $\endgroup$ – Anton Menshov May 16 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.