[Scipy-tickets] [SciPy] #1350: fmin_cobyla does not return useful information along with outputs

SciPy Trac scipy-tickets@scipy....
Mon Jan 3 07:45:16 CST 2011


#1350: fmin_cobyla does not return useful information along with outputs
----------------------------+-----------------------------------------------
 Reporter:  darrylwally     |       Owner:  somebody    
     Type:  enhancement     |      Status:  needs_review
 Priority:  normal          |   Milestone:  0.9.0       
Component:  scipy.optimize  |     Version:  0.8.0       
 Keywords:                  |  
----------------------------+-----------------------------------------------

Comment(by zyv):

 Well, actually, this problem is not specific to cobyla. I have been
 struggling with other optimizers as well printing stuff to stdout which I
 could not capture from Python. I tried to write a nice decorator,
 combining different approaches:

 {{{
 def capture(func):
     """
     This decorator captures stdout / stderr and is mostly useful
     for wrapping NumPy / SciPy functions, which in turn wrap around
     some obscure C/C++ or Fortran codes
     """

     @functools.wraps(func)
     def wrapper(*args, **kwargs):

         stdout = os.dup(1)
         new_stdout = os.tmpfile()
         os.dup2(new_stdout.fileno(), 1)

         stderr = sys.stderr
         sys.stderr = new_stderr = StringIO()

         result = None

         try:
             # Run the decorated function...
             result = func(*args, **kwargs)

         finally:
             # ... but not capture raised exceptions, just restore
 descriptors!

             # stdout
             os.dup2(stdout, 1)
             new_stdout.seek(0)
             c1 = new_stdout.read()
             new_stdout.close()

             # stderr
             sys.stderr = stderr
             c2 = new_stderr.getvalue()

         return result, c1, c2

     return wrapper
 }}}

 but it just wouldn't work and that's it!

 Of course it would be nice to commit this particular patch, but it would
 be even nicer to solve the problem in a general case.

 Does anyone know of a way to capture the output of the optimizers from
 Python into strings which can be printed / logged / parsed?

 Thanks!

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1350#comment:2>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list