pylab collides with fmin?

I'm getting odd behavior when I try to use fmin and pylab in the same program. The issue is illustrated in the code snippet below. As written, fmin won't work: the "print xopt" simply returns the contents of x0 as assigned in the line before fmin. If the "from pylab import *" line is commented out, however, then fmin runs as expected.

I'm running python 2.7.2 on a MacBook Pro with a recent install & upgrade of scipy and matplotlib via macports. Any suggestions would be appreciated.

···

-------------------------------------

#!/opt/local/bin/python

from scipy import *
from scipy.optimize import fmin
import matplotlib
matplotlib.use('MacOSX')
from pylab import *

def rosen(x): # The Rosenbrock function
  return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]

xopt = fmin(rosen, x0)

print xopt

Because pylab brings the numpy namespace into the current namespace, numpy’s fmin is imported and replaces the previously def’ed fmin from scipy.optimize. Numpy’s fmin function is completely different from scipy’s fmin. Try putting the “from scipy.optimize import fmin” after the pylab import line. Or, do something like “from scipy.optimize import fmin as fminimize” to avoid name collision.

I hope that helps.

Ben Root

···

On Wed, Sep 14, 2011 at 2:17 PM, Raymond Hawkins <rhawkin2@…3776…> wrote:

I’m getting odd behavior when I try to use fmin and pylab in the same program. The issue is illustrated in the code snippet below. As written, fmin won’t work: the “print xopt” simply returns the contents of x0 as assigned in the line before fmin. If the “from pylab import *” line is commented out, however, then fmin runs as expected.

I’m running python 2.7.2 on a MacBook Pro with a recent install & upgrade of scipy and matplotlib via macports. Any suggestions would be appreciated.


#!/opt/local/bin/python

from scipy import *

from scipy.optimize import fmin

import matplotlib

matplotlib.use(‘MacOSX’)

from pylab import *

def rosen(x): # The Rosenbrock function

return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]

xopt = fmin(rosen, x0)

print xopt

I'm getting odd behavior when I try to use fmin and pylab in the same program. The issue is illustrated in the code snippet below. As written, fmin won't work: the "print xopt" simply returns the contents of x0 as assigned in the line before fmin. If the "from pylab import *" line is commented out, however, then fmin runs as expected.

This is a good illustration of why "from package_x import *" is so strongly discouraged; it is throwing away one of the most important features of python--the default separation of packages into their own name spaces.

The only exception with respect to pylab is that for quick and dirty interactive use, particularly within ipython, it is sometimes worthwhile to sacrifice some name space separation for typing speed. But in a script that imports from more than one external package, it is best to always use explicit imports in some form.

The preferred idiom is to avoid importing pylab at all in scripts; instead, do this:

import numpy as np
import matplotlib.pyplot as plt

Eric

···

On 09/14/2011 09:17 AM, Raymond Hawkins wrote:

I'm running python 2.7.2 on a MacBook Pro with a recent install& upgrade of scipy and matplotlib via macports. Any suggestions would be appreciated.

-------------------------------------

#!/opt/local/bin/python

from scipy import *
from scipy.optimize import fmin
import matplotlib
matplotlib.use('MacOSX')
from pylab import *

def rosen(x): # The Rosenbrock function
   return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]

xopt = fmin(rosen, x0)

print xopt
------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs& more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks. Removing the import of pylab and adding your suggested "import matplotlib.pyplot as plt" worked.

···

On Sep 14, 2011, at 12:46 PM, Eric Firing wrote:

On 09/14/2011 09:17 AM, Raymond Hawkins wrote:

I'm getting odd behavior when I try to use fmin and pylab in the same program. The issue is illustrated in the code snippet below. As written, fmin won't work: the "print xopt" simply returns the contents of x0 as assigned in the line before fmin. If the "from pylab import *" line is commented out, however, then fmin runs as expected.

This is a good illustration of why "from package_x import *" is so
strongly discouraged; it is throwing away one of the most important
features of python--the default separation of packages into their own
name spaces.

The only exception with respect to pylab is that for quick and dirty
interactive use, particularly within ipython, it is sometimes worthwhile
to sacrifice some name space separation for typing speed. But in a
script that imports from more than one external package, it is best to
always use explicit imports in some form.

The preferred idiom is to avoid importing pylab at all in scripts;
instead, do this:

import numpy as np
import matplotlib.pyplot as plt

Eric

I'm running python 2.7.2 on a MacBook Pro with a recent install& upgrade of scipy and matplotlib via macports. Any suggestions would be appreciated.

-------------------------------------

#!/opt/local/bin/python

from scipy import *
from scipy.optimize import fmin
import matplotlib
matplotlib.use('MacOSX')
from pylab import *

def rosen(x): # The Rosenbrock function
  return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]

xopt = fmin(rosen, x0)

print xopt
------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs& more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks. With this explanation I was able to get things to work.

···

On Sep 14, 2011, at 12:41 PM, Benjamin Root wrote:

On Wed, Sep 14, 2011 at 2:17 PM, Raymond Hawkins <rhawkin2@…44…> wrote:

I’m getting odd behavior when I try to use fmin and pylab in the same program. The issue is illustrated in the code snippet below. As written, fmin won’t work: the “print xopt” simply returns the contents of x0 as assigned in the line before fmin. If the “from pylab import *” line is commented out, however, then fmin runs as expected.

I’m running python 2.7.2 on a MacBook Pro with a recent install & upgrade of scipy and matplotlib via macports. Any suggestions would be appreciated.


#!/opt/local/bin/python

from scipy import *

from scipy.optimize import fmin

import matplotlib

matplotlib.use(‘MacOSX’)

from pylab import *

def rosen(x): # The Rosenbrock function

return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]

xopt = fmin(rosen, x0)

print xopt

Because pylab brings the numpy namespace into the current namespace, numpy’s fmin is imported and replaces the previously def’ed fmin from scipy.optimize. Numpy’s fmin function is completely different from scipy’s fmin. Try putting the “from scipy.optimize import fmin” after the pylab import line. Or, do something like “from scipy.optimize import fmin as fminimize” to avoid name collision.

I hope that helps.

Ben Root