Comparing pdf output in tests

Andrew Straw <strawman@...36...> writes:

Jouni - I don't think this would be hard to add, but I'm swamped at
work. If this is an itch you'd like to scratch, feel free to hack away
on the image_comparison() function in
lib/matplotlib/testing/decorators.py -- it's a pretty straightforward
piece of code.

Changing that is probably easy enough, but how should the overall code
path look?

I was planning to switch backends in matplotlib.test after it runs the
Agg tests, so that the same test cases could be used to produce pdf
files (that's why they save files without extensions, right?) but this
seems to be impossible. The matplotlib.use function is a no-op if
matplotlib.backends has been imported, regardless of the warn argument.
For example, the following code produces two png files:

    #!/usr/bin/env python
    import matplotlib

    matplotlib.use('agg')
    from matplotlib import pyplot
    pyplot.plot([3,1,4,1])
    pyplot.savefig('foo1')

    pyplot.switch_backend('pdf')
    pyplot.plot([5,9,2,6])
    pyplot.savefig('foo2')

If you interchange the 'agg' and 'pdf' strings, you get two pdf files.
It looks like the following change to matplotlib/__init__.py would fix
this, but I'm a little doubtful since maybe there was a good reason to
make it like it is:

--- __init__.py (revision 7815)
+++ __init__.py (working copy)
@@ -822,8 +822,8 @@
     make the backend switch work (in some cases, eg pure image
     backends) so one can set warn=False to supporess the warnings
     """
- if 'matplotlib.backends' in sys.modules:
- if warn: warnings.warn(_use_error_msg)
+ if 'matplotlib.backends' in sys.modules and warn:
+ warnings.warn(_use_error_msg)
         return
     arg = arg.lower()
     if arg.startswith('module://'):

···

--
Jouni K. Sepp�nen

Take a look at the pyplot "switch_backends" function.

Alternatively, agg knows how to save pdf if given the extension, so we
could wire up the testing to use a module level extension set
somewhere which could be updated for each backend. This is probably
safer and cleaner than switch_backends

JDH

···

On Wed, Sep 23, 2009 at 10:48 AM, Jouni K. Seppänen <jks@...278...> wrote:

Andrew Straw <strawman@...36...> writes:

Jouni - I don't think this would be hard to add, but I'm swamped at
work. If this is an itch you'd like to scratch, feel free to hack away
on the image_comparison() function in
lib/matplotlib/testing/decorators.py -- it's a pretty straightforward
piece of code.

Changing that is probably easy enough, but how should the overall code
path look?

I was planning to switch backends in matplotlib.test after it runs the
Agg tests, so that the same test cases could be used to produce pdf
files (that's why they save files without extensions, right?) but this
seems to be impossible. The matplotlib.use function is a no-op if
matplotlib.backends has been imported, regardless of the warn argument.
For example, the following code produces two png files:

#!/usr/bin/env python
import matplotlib

matplotlib.use('agg')
from matplotlib import pyplot
pyplot.plot([3,1,4,1])
pyplot.savefig('foo1')