python question from matlab user

Hi All,

Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here.

In http://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg

def set_pub():
    rc('font', weight='bold') # bold fonts are easier to see

Then as you are working interactively, you just need to do

set_pub()

"""

Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling

from jmkfigure import *

set_pub()

yields the error: "NameError: global name 'rc' is not defined"

I tried importing matplotlib and rc into jmkfigure.py, but to no avail.

I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.

Thanks for any help,

Cheers, Jody

in your jmkfile.py you should have

from pylab import *

Paul

···

On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,

Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here.

In http://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg

def set_pub():
     rc('font', weight='bold') # bold fonts are easier to see

Then as you are working interactively, you just need to do

set_pub()

"""

Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling

from jmkfigure import *

set_pub()

yields the error: "NameError: global name 'rc' is not defined"

I tried importing matplotlib and rc into jmkfigure.py, but to no avail.

I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.

Thanks for any help,

Cheers, Jody

in your jmkfile.py you should have

from pylab import *

Or to be more pythonic, import only what you actually need in a given module, e.g.,

from matplotlib import rc

Eric

···

On 2012/09/07 7:52 PM, Paul Tremblay wrote:

Paul

On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,

Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here.

Inhttp://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg

def set_pub():
     rc('font', weight='bold') # bold fonts are easier to see

Then as you are working interactively, you just need to do

set_pub()

"""

Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling

from jmkfigure import *

set_pub()

yields the error: "NameError: global name 'rc' is not defined"

I tried importing matplotlib and rc into jmkfigure.py, but to no avail.

I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.

Thanks for any help,

Cheers, Jody

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi all,

Thats what I thought too:

I have: jmkfigure.py:

···

in your jmkfile.py you should have

from pylab import
*
Paul

  On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,
Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here. In it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg
def set_pub():
rc('font', weight='bold') # bold fonts are easier to see
Then as you are working interactively, you just need to do
set_pub()

"""
Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling from jmkfigure import *
set_pub()
yields the error: "NameError: global name 'rc' is not defined" I tried importing matplotlib and rc into jmkfigure.py, but to no avail. I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.
Thanks for any help,
Cheers, Jody

http://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html

Ack, OK, to answer my own question…

Somehow ipython was caching the definition of jmkfigure, so changing the module in the jmkfigure.py file did not actually change the version ipython was using. Running a new version of ipython, it worked fine.

Sorry for the chatter, and thanks for the pointers…

Cheers, Jody

···

in your jmkfile.py you should have

from pylab import
*
Paul

  On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,
Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here. In it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg
def set_pub():
rc('font', weight='bold') # bold fonts are easier to see
Then as you are working interactively, you just need to do
set_pub()

"""
Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling from jmkfigure import *
set_pub()
yields the error: "NameError: global name 'rc' is not defined" I tried importing matplotlib and rc into jmkfigure.py, but to no avail. I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.
Thanks for any help,
Cheers, Jody

http://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html

Ack, OK, to answer my own question...

Somehow ipython was caching the definition of jmkfigure, so changing the
module in the jmkfigure.py file did not actually change the version
ipython was using. Running a new version of ipython, it worked fine.

Jody,

This is one of the big differences between python and matlab: in matlab, if an m-file has changed within a session, the change is immediately effective. The python "import" statement is very different. If a module has been imported, then a subsequent "import" of it does not re-read the file, even if it has changed. There is a "reload" builtin function that will re-read a file, but it is rarely used. It reloads only the specified module, not modules that use it. Ipython has a recursive dreload (for deep reload) function, but I don't use that, either; I just do what you did, start a new instance of ipython.

In ipython, the %run magic is useful for developing and modifying a single module or script at at time, making changes and testing without restarting ipython.

Eric

···

On 2012/09/08 3:50 AM, Jody Klymak wrote:

Sorry for the chatter, and thanks for the pointers..
Cheers, Jody

On Sep 8, 2012, at 6:18 AM, Jody Klymak <jklymak@…4192… > <mailto:jklymak@…4192…>> wrote:

Hi all,

Thats what I thought too:

I have: jmkfigure.py:

===============
from pylab import *

def jmkfigure():
    rc('figure',figsize=(3+3/8,8.5/2),dpi=96)
    rc('font',size=9);

and test.py:

=========
from pylab import *

from jmkfigure import *

jmkfigure()
figure(1)
plot([1,2,3]);

show()

>>> run test.py

yields a traceback ending w/:

===========
Users/jklymak/teaching/Phy411/project/jmkfigure.py in jmkfigure()
      1 from pylab import *
----> 2
      3 def jmkfigure():
      4 rc('figure',figsize=(3+3/8,8.5/2),dpi=96)
      5 rc('font',size=9);

NameError: global name 'rc' is not defined

Same error if I just import "rc" from matplot lib....

Is it some strange set up problem? If I put the same def in test.py
it works fine...

Thanks, Jody

On Sep 7, 2012, at 22:52 PM, Paul Tremblay <paulhtremblay@…287… >> <mailto:paulhtremblay@…287…>> wrote:

in your jmkfile.py you should have

from pylab import *

Paul

On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,

Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here.

Inhttp://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg

def set_pub():
     rc('font', weight='bold') # bold fonts are easier to see

Then as you are working interactively, you just need to do

set_pub()

"""

Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling

from jmkfigure import *

set_pub()

yields the error: "NameError: global name 'rc' is not defined"

I tried importing matplotlib and rc into jmkfigure.py, but to no avail.

I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.

Thanks for any help,

Cheers, Jody

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond.
Discussions
will include endpoint security, mobile security and the latest in
malware
threats.
http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Jody Klymak
http://web.uvic.ca/~jklymak/

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats.
http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Jody Klymak
http://web.uvic.ca/~jklymak/

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

This is one of the big differences between python and matlab: in matlab,
if an m-file has changed within a session, the change is immediately
effective. The python "import" statement is very different.

Gotchya, thanks.

So, while I'm being a bother:

in Matlab, I often organize data in structures as:

adcp.time [1xN]
adcp.z [Mx1]
adcp.u [MxN]

where time is the x-axis, z the z-axis and u an array of values at each depth and time (an example chosen after Eric's heart).

What is the recommended way to represent this in python? I see the info about numpy structured arrays. Is that it? It also seems that Mx1 arrays are hard in python. It also seems you need to preallocate the whole array, which isn't very flexible compared to how you can do it in Matlab. Am I missing something?

Thanks, Jody

···

Sorry for the chatter, and thanks for the pointers..
Cheers, Jody

On Sep 8, 2012, at 6:18 AM, Jody Klymak <jklymak@…4192… >> <mailto:jklymak@…4192…>> wrote:

Hi all,

Thats what I thought too:

I have: jmkfigure.py:

===============
from pylab import *

def jmkfigure():
   rc('figure',figsize=(3+3/8,8.5/2),dpi=96)
   rc('font',size=9);

and test.py:

=========
from pylab import *

from jmkfigure import *

jmkfigure()
figure(1)
plot([1,2,3]);

show()

run test.py

yields a traceback ending w/:

===========
Users/jklymak/teaching/Phy411/project/jmkfigure.py in jmkfigure()
     1 from pylab import *
----> 2
     3 def jmkfigure():
     4 rc('figure',figsize=(3+3/8,8.5/2),dpi=96)
     5 rc('font',size=9);

NameError: global name 'rc' is not defined

Same error if I just import "rc" from matplot lib....

Is it some strange set up problem? If I put the same def in test.py
it works fine...

Thanks, Jody

On Sep 7, 2012, at 22:52 PM, Paul Tremblay <paulhtremblay@…287… >>> <mailto:paulhtremblay@…287…>> wrote:

in your jmkfile.py you should have

from pylab import *

Paul

On 9/8/12 12:45 AM, Jody Klymak wrote:

Hi All,

Sorry to ask a dumb python newbie question, but the problem arose while reading the matplotlib documentation, and an hour or so on the internet didnt' help, so I felt it was fair-ish game to post here.

Inhttp://matplotlib.sourceforge.net/examples/pylab_examples/customize_rc.html it says:
"""
If you like to work interactively, and need to create different sets
of defaults for figures (eg one set of defaults for publication, one
set for interactive exploration), you may want to define some
functions in a custom module that set the defaults, eg

def set_pub():
    rc('font', weight='bold') # bold fonts are easier to see

Then as you are working interactively, you just need to do

set_pub()

"""

Which I thought was great, because I'd like to have some presets for different journals. However, saving the def into a file (jmkfigure.py) and calling

from jmkfigure import *

set_pub()

yields the error: "NameError: global name 'rc' is not defined"

I tried importing matplotlib and rc into jmkfigure.py, but to no avail.

I appreciate this is a scoping issue with python, but I can't figure out how to set rc from within an external module.

Thanks for any help,

Cheers, Jody

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond.
Discussions
will include endpoint security, mobile security and the latest in
malware
threats.
http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Jody Klymak
http://web.uvic.ca/~jklymak/

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats.
http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
<mailto:Matplotlib-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Jody Klymak
http://web.uvic.ca/~jklymak/

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Jody Klymak
http://web.uvic.ca/~jklymak/

This is one of the big differences between python and matlab: in
matlab, if an m-file has changed within a session, the change is
immediately effective. The python "import" statement is very
different.

Gotchya, thanks.

So, while I'm being a bother:

in Matlab, I often organize data in structures as:

adcp.time [1xN] adcp.z [Mx1] adcp.u [MxN]

where time is the x-axis, z the z-axis and u an array of values at
each depth and time (an example chosen after Eric's heart).

What is the recommended way to represent this in python? I see the
info about numpy structured arrays. Is that it? It also seems that
Mx1 arrays are hard in python. It also seems you need to preallocate
the whole array, which isn't very flexible compared to how you can do
it in Matlab. Am I missing something?

Jody,

A structured array is probably overkill; it would require storing everything as MxN, which may not be necessary.

Most of the time, if you have something that is 1-D, you can just keep it in a 1-D array. If you need adcp.time to behave as if it were MxN, you can just use it as-is, because numpy broadcasting will add dimensions to the left as needed. If you need adcp.z to behave as if it were MxN, you can simply index it like this: adcp.z[:, np.newaxis].

Now, for the structure syntax, you can use a class, e.g.

class Data:
     pass

adcp = Data()
adcp.time = time
adcp.z = z
adcp.u = u

Now your adcp instance is just like the matlab structure.

This works, but you might want to use a more flexible container. One variation on the Bunch is here:
http://currents.soest.hawaii.edu/hgstage/pycurrents/file/8bf05a53b326/system/misc.py.
It is fancier than you need for now, but illustrates the sort of thing you can do with python, and it will work fine even when you don't need all its features. You could initialize it like this:

adcp = Bunch(time=time, z=z, u=u)

assuming, as before, that you already have individual numpy arrays called time, z, and u. You can still tack on additional attributes, like

adcp.something_else = whatever

The Bunch allows access using the structure notation, and also using dictionary syntax, so adcp.u is the same as adcp['u']. The dictionary syntax is particularly useful when automating operations, because you can easily iterate over a list of dictionary entries.

Regarding the need to pre-allocate: yes, matlab is slicker in this regard, and every now and then there is discussion about implementing equivalent behavior in numpy, or in an add-on module.

In many cases you can simply accumulate values in a list, and then at the end use an array constructor to make an ndarray from the list.

You can also use the numpy concatenate function, or its derivatives, but this usually makes sense only for gluing together small numbers of arrays.

Eric

···

On 2012/09/08 5:34 PM, Jody Klymak wrote:

Thanks, Jody

Technically, you don´t have to preallocate the memory, but every time you expand your array, the whole of it will be copied to a new contiguous piece of memory. This works, but it can be slow if you do it many times, or you are handling big data.

Using a Python list is not much more flexible, but you could use carrays (for chunked arrays), that are structures NumPyArrays-compatible (AFAIK) that are stored non contiguosly in memory. Adding data only needs to move one of those small pieces, instead of the whole array.

···

On Sunday, September 9, 2012, Eric Firing wrote:

Regarding the need to pre-allocate: yes, matlab is slicker in this

regard, and every now and then there is discussion about implementing

equivalent behavior in numpy, or in an add-on module.