numpy - Optimal multiple return values in scientific python -


i'm using scipy/numpy research code instead of matlab. there 1 flaw, running frequently. found work-around solution, want check best practice , better solution. imagine mathematical optimisation:

def calculation (data, max_it=10000, tol = 1e-5):     k = 0     rmse = np.inf      while k < max_it , rmse > tol:         #calc , modify data - rmse becomes smaller in each iteration         k += 1     return data 

it works fine, embed code, in multiple locations, e.g.:

 import module  d = module.calculation (data) 

but want check further insights , need multiple return values. if append multiple return values, have modify other code , unpack first return value. this 1 of few situations prefer matlab scipy.in matlab first return value evaluated, unless explicitly demand rest.

so work-around matlab-like (= optimal) multiple return values global variables [of module]

def calculation (data, max_it=10000, tol = 1e-5):     global k     global rmse     k = 0     rmse = np.inf      while k < max_it , rmse > tol:         #calc , modify data - rmse becomes smaller in each iteration         k += 1     return data 

my function calls work without modification , if want verify in ipython, iset variables global reload(module) , check insight module.rmse.

but imagine oo-aproach beginning, or use pdb, or use other ipython magic

you specify want more info returned using info=true argument when calling calculation. approach taken np.unique (with return_inverse , return_index parameters) , scipy.optimize.leastsq (with full_output parameter):

def calculation(data, max_it=10000, tol = 1e-5, info=false):     k = 0     rmse = np.inf      while k < max_it , rmse > tol:         #calc , modify data - rmse becomes smaller in each iteration         k += 1     if info:         return data, k, rmse     else:         return data 

or, assign additional attributes on calculation function:

def calculation(data, max_it=10000, tol = 1e-5):     k = 0     rmse = np.inf      while k < max_it , rmse > tol:         #calc , modify data - rmse becomes smaller in each iteration         k += 1     calculation.k = k     calculation.rmse = rmse     return data 

the added info accessible with

import module d = module.calculation(data) rmse = module.calculation.rmse 

note latter approach not work if calculation run concurrently multiple threads...

in cpython (due gil), 1 thread can execute @ given time, there little attraction running calculation in multiple threads. knows? there may situation calls use of threads on small scale, such perhaps in gui. there, accessing calculation.k or calculation.rmse might return incorrect values.

moreover, zen of python says, "explicit better implicit".

so recommend first approach on second.


Comments