Comparing pdf output in tests

John Hunter <jdh2358@...149...> writes:

� �pyplot.savefig('foo1')

Take a look at the pyplot "switch_backends" function.

Yes, that function was on the next line after the part you quoted. :slight_smile:
It calls matplotlib.use with warn=False, but that function ends up doing
nothing.

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

That sounds complicated. How about having the test cases call savefig
with all the relevant file formats? That doesn't look so nice if the
test cases end up with a big block of savefig calls, but it has the
advantage that there is no magic involved and it is very obvious what is
going on.

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

Jouni K. Seppänen wrote:

John Hunter <jdh2358@...149...> writes:

   pyplot.savefig('foo1')
      

Take a look at the pyplot "switch_backends" function.
    
Yes, that function was on the next line after the part you quoted. :slight_smile:
It calls matplotlib.use with warn=False, but that function ends up doing
nothing.

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
    
That sounds complicated. How about having the test cases call savefig
with all the relevant file formats? That doesn't look so nice if the
test cases end up with a big block of savefig calls, but it has the
advantage that there is no magic involved and it is very obvious what is
going on.
  

Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.

-Andrew

test-pdf-idea.patch (4.71 KB)

Why not have the decorator pass the extension in to the test funcs --
agg can print to pdf, ps, svg and png

···

On Wed, Sep 23, 2009 at 12:42 PM, Andrew Straw <strawman@...36...> wrote:

Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.

John Hunter wrote:

Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.
    
Why not have the decorator pass the extension in to the test funcs --
agg can print to pdf, ps, svg and png
  

I'm not sure what you're suggesting. Presumably if we're driving agg OK
to draw .png, it will also draw .pdf OK (or does it have a pdf vector
backend independent of the MPL pdf backend that we want to test separately?)

I was just thinking it would be easiest to have test functions that look
like:

@image_comparison('my_figure')
def my_figure_test():
    plt.plot([1,2,3],[4,5,6])
    plt.savefig('my_figure')

This could automatically test all backends we have the infrastructure
and the baseline images for. It doesn't force the test writer to worry
about that stuff.

-Andrew

···

On Wed, Sep 23, 2009 at 12:42 PM, Andrew Straw <strawman@...36...> wrote:

No, it doesn't have a separate backend, but the backend_agg figure
canvas savefig method knows how to create FigureCanvasPDF etc to use
that backend to write the file w/o having to switch the default
backend with all the attendant hassles. So if you are using *Agg, and
do

  savefig(somefile.pdf)

agg will load the native pdf backend and use it. So I was envisioning

def test_something(ext):
   make_plot
   fig.savefig('myfile.%s'%ext)

and having the decorator pass in the extensions it wants one-by-one

JDH

···

On Wed, Sep 23, 2009 at 12:56 PM, Andrew Straw <strawman@...36...> wrote:

John Hunter wrote:

On Wed, Sep 23, 2009 at 12:42 PM, Andrew Straw <strawman@...36...> wrote:

Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.

Why not have the decorator pass the extension in to the test funcs --
agg can print to pdf, ps, svg and png

I'm not sure what you're suggesting. Presumably if we're driving agg OK
to draw .png, it will also draw .pdf OK (or does it have a pdf vector
backend independent of the MPL pdf backend that we want to test separately?)

John Hunter wrote:

···

On Wed, Sep 23, 2009 at 12:56 PM, Andrew Straw <strawman@...36...> wrote:
  

John Hunter wrote:
    

On Wed, Sep 23, 2009 at 12:42 PM, Andrew Straw <strawman@...36...> wrote:

Sorry, I should have been more clear. I was thinking that the
image_compare() decorator would call the test function multiple times,
having switched the backend between invocations. Thus, the call to
savefig() would continue not to explicitly set the extension. I've
quickly modified the source to reflect my idea, but I haven't had a
chance to flesh it out or test it. It should show the idea, though. See
attached.

Why not have the decorator pass the extension in to the test funcs --
agg can print to pdf, ps, svg and png

I'm not sure what you're suggesting. Presumably if we're driving agg OK
to draw .png, it will also draw .pdf OK (or does it have a pdf vector
backend independent of the MPL pdf backend that we want to test separately?)
    
No, it doesn't have a separate backend, but the backend_agg figure
canvas savefig method knows how to create FigureCanvasPDF etc to use
that backend to write the file w/o having to switch the default
backend with all the attendant hassles. So if you are using *Agg, and
do

  savefig(somefile.pdf)

agg will load the native pdf backend and use it. So I was envisioning

def test_something(ext):
   make_plot
   fig.savefig('myfile.%s'%ext)

and having the decorator pass in the extensions it wants one-by-one
  

I see. Is there something like
backend_agg.set_default_savefig_extension()? That would achieve both of
our goals. So maybe if it doesn't exist it would be easy to add in?

-Andrew