example data in example code

In some examples, I have been moving example functions and data into a
module, so that they can be run from anywhere. Many other examples
still rely on a relative path in the examples dir. Eg, I go to the
gallery and download the source for the axes grid toolkit example
simple_rgb.py, and try to run it from my desktop, I get the error "no
module names demo_image". While I know how to get the data, a naive
user will not. So in some examples I have been adopting the approach,
eg in examples/pylab_examples/scatter_demo2.py

  import matplotlib
  datafile = matplotlib.get_example_data('goog.npy')

These examples will run anywhere mpl is installed. Another approach
would to write a version of get_example_data that checks locally for a
datafile, and if it is not where you expect to be, attempt a
urlretrieve as a temp file.

The gallery is becoming the goto place for most users of the website,
and I would like as many examples as possible to run after a simple
download to the desktop . I am sensitive to packagers who may not
want to ship large amounts of data w/ the main library, so we may want
to minimize the amount we ship in mpl-data which
matplotlib.get_example_data uses, but it may be a good idea to setup a
new svn directory at the top level (mpl_data) and write a urllib
enabled matplotlib.get_example_data that fetches it from the repo if
it can't find it locally.

JDH

OK, I committed a first pass at this to HEAD. I created a new svn
directory called mpl_data

  svn co matplotlib download | SourceForge.net

and a cbook.get_mpl_data function, as used in this example::

    import matplotlib.cbook as cbook
    import matplotlib.pyplot as plt
    fname = cbook.get_mpl_data('lena.png', asfileobj=False)

    print 'fname', fname
    im = plt.imread(fname)
    plt.imshow(im)
    plt.show()

The function will check ~/.matplotlib/mpl_data and fetch it using
urllib from svn HEAD if it is not there, caching in the process. It
would be nice to support an svn revision (w/o relying on svn) as I
note in this comment in get_mpl_data:

    # TODO: how to handle stale data in the cache that has been
    # updated from svn -- is there a clean http way to get the current
    # revision number that will not leave us at the mercy of html
    # changes at sf?

If others agree w/ the basic concept, we should port as many data
requiring examples over, removing data from examples/data and
lib/matplotlib/mpl-data/example as we go. This will result in smaller
tarballs and binaries, and make the examples more portable.

JDH

···

On Fri, Jul 31, 2009 at 1:10 PM, John Hunter<jdh2358@...149...> wrote:

The gallery is becoming the goto place for most users of the website,
and I would like as many examples as possible to run after a simple
download to the desktop . I am sensitive to packagers who may not
want to ship large amounts of data w/ the main library, so we may want
to minimize the amount we ship in mpl-data which
matplotlib.get_example_data uses, but it may be a good idea to setup a
new svn directory at the top level (mpl_data) and write a urllib
enabled matplotlib.get_example_data that fetches it from the repo if
it can't find it locally.

So, I just downloaded 0.99 rc1 and wanted to play with axesgrid examples and
got the results you reported below in your example. I am in fact naive, and
its not clear to me how to get around this problem of the demo_image module
not being found. What is the solution?

Thanks,

Josh

John Hunter-4 wrote:

···

In some examples, I have been moving example functions and data into a
module, so that they can be run from anywhere. Many other examples
still rely on a relative path in the examples dir. Eg, I go to the
gallery and download the source for the axes grid toolkit example
simple_rgb.py, and try to run it from my desktop, I get the error "no
module names demo_image". While I know how to get the data, a naive
user will not. So in some examples I have been adopting the approach,
eg in examples/pylab_examples/scatter_demo2.py

  import matplotlib
  datafile = matplotlib.get_example_data('goog.npy')

These examples will run anywhere mpl is installed. Another approach
would to write a version of get_example_data that checks locally for a
datafile, and if it is not where you expect to be, attempt a
urlretrieve as a temp file.

The gallery is becoming the goto place for most users of the website,
and I would like as many examples as possible to run after a simple
download to the desktop . I am sensitive to packagers who may not
want to ship large amounts of data w/ the main library, so we may want
to minimize the amount we ship in mpl-data which
matplotlib.get_example_data uses, but it may be a good idea to setup a
new svn directory at the top level (mpl_data) and write a urllib
enabled matplotlib.get_example_data that fetches it from the repo if
it can't find it locally.

JDH

-----
Josh Hemann
Statistical Advisor
http://www.vni.com/ Visual Numerics
jhemann@...735... | P 720.407.4214 | F 720.407.4199

--
View this message in context: http://www.nabble.com/example-data-in-example-code-tp24760754p24811726.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.

The solution is to get the examples directory and run it from there,
where it will have the example data. Although I added support for
having auto-fetched data in svn, we haven't ported the examples over
to use it yet. If you have svn you can grab the examples dir with

svn co matplotlib download | SourceForge.net
mpl_examples

and then run the examples in their directory, eg examples/axes_grid.
Then they should be able to see their data.

JDH

···

On Tue, Aug 4, 2009 at 11:17 AM, Josh Hemann<jhemann@...735...> wrote:

So, I just downloaded 0.99 rc1 and wanted to play with axesgrid examples and
got the results you reported below in your example. I am in fact naive, and
its not clear to me how to get around this problem of the demo_image module
not being found. What is the solution?