"Don't know a PDF representation for" alpha in graphics?

220 views
Skip to first unread message

john_perry_usm

unread,
Nov 15, 2012, 12:12:07 PM11/15/12
to sage-s...@googlegroups.com
Hello

I have created a nice graphic with some alpha. I try to save it. As a Sage object, I have no problem. The EPS representation doesn't preserve the alpha. The PNG does, but I'd prefer vector graphics.

The PDF representation chokes on it. I get the error,

TypeError: Don't know a PDF representation for <type 'sage.rings.real_mpfr.RealLiteral'> objects.

I'll use the PNG for now, but has anyone else encountered this, and/or found a workaround? I am using 5.4 on Fedora Verne, whichever number that is.

regards
john perry

Volker Braun

unread,
Nov 15, 2012, 12:44:36 PM11/15/12
to sage-s...@googlegroups.com
I think I saw that before. Maybe converting coordinates to RDF works? Please open a ticket if you can isolate a test case.

Jason Grout

unread,
Nov 15, 2012, 1:00:46 PM11/15/12
to sage-s...@googlegroups.com
Can you get a small test case for us to play with?

Thanks,

Jason



john_perry_usm

unread,
Nov 15, 2012, 1:03:27 PM11/15/12
to sage-s...@googlegroups.com
I will try that, thanks. I'll also open a ticket in coming days.

john

Michael Orlitzky

unread,
Nov 15, 2012, 2:21:21 PM11/15/12
to sage-s...@googlegroups.com
If this is a one-time thing, you can open the PNG in Inkscape and use
its Path->Trace Bitmap feature. You should be able to save the result as
SVG, EPS, PDF, etc.

john_perry_usm

unread,
Nov 20, 2012, 2:57:19 PM11/20/12
to sage-s...@googlegroups.com
Hello

Finally got round to it.


On Thursday, November 15, 2012 11:44:36 AM UTC-6, Volker Braun wrote:
I think I saw that before. Maybe converting coordinates to RDF works? Please open a ticket if you can isolate a test case.

RDF doesn't work, either. float does.

I'm trying to write up a patch now. The alpha option should in fact be float, and not RFE or RDF, should it? I was thinking of making the change there, when the option is set, though I could also convert RDF etc. to float in the backend.

john perry

john_perry_usm

unread,
Nov 20, 2012, 2:59:09 PM11/20/12
to sage-s...@googlegroups.com
Jason


On Thursday, November 15, 2012 12:01:02 PM UTC-6, Jason Grout wrote:
Can you get a small test case for us to play with?

sage: p = disk((0,0),5,(0,pi/4),color='red')
sage: p += disk((0,0),5,(pi/4,pi/2),color='red',alpha=0.5)
sage: p.save("test.pdf")

This fails for me. If I change the second line to end with ...alpha=float(0.5)) then it works.

regards
john perry

john_perry_usm

unread,
Nov 20, 2012, 4:59:33 PM11/20/12
to sage-s...@googlegroups.com
Okay, does anyone know how I change files in the library in such a way that mercurial actually notices?

I can modify the matplotlib backend for PDF's so that this works, but those files don't show up in the source directory, so hg doesn't pick up on the changes. How do I supply a patch for this circumstance?

Dima Pasechnik

unread,
Nov 20, 2012, 9:59:14 PM11/20/12
to sage-s...@googlegroups.com
On 2012-11-20, john_perry_usm <john....@usm.edu> wrote:
> ------=_Part_212_31060160.1353448773382
> Content-Type: text/plain; charset=ISO-8859-1
add a patch to the matplotlib spkg, and create a new version of it.

HTH,
Dmitrii
>

Jason Grout

unread,
Nov 20, 2012, 10:40:11 PM11/20/12
to sage-s...@googlegroups.com
Or even better, in my opinion, post the patch to the matplotlib mailing
list, or make the change in the matplotlib github source and submit a
pull request to them.

Or post the patch here and I can forward it to the matplotlib mailing list.

Jason



john_perry_usm

unread,
Nov 26, 2012, 9:07:34 AM11/26/12
to sage-s...@googlegroups.com
Dima + Jason

The "easy" fix I have in mind is the following. In the file local/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py, add after line 16

from sage.rings.real_mpfr import RealLiteral

and then, in the function pdfRepr, change the line

    elif isinstance(obj, float):

to

    elif isinstance(obj, float) or isinstance(obj, RealLiteral):

With this change, I can sage PDF with alpha, and it probably fixes a lot of other things, as well.

Is it a good idea to patch the matplotlib package with a test of whether an object is a sage object? If not, the solution is probably to patch sage's commands that create graphics, to change RealLiteral's to float's.

Volker Braun

unread,
Nov 26, 2012, 9:28:48 AM11/26/12
to sage-s...@googlegroups.com
On Monday, November 26, 2012 2:07:34 PM UTC, john_perry_usm wrote:
Is it a good idea to patch the matplotlib package with a test of whether an object is a sage object?

Definitely not!

If possible, matplotlib should rely on duck-typing:

try:
    alpha = float(obj)
except ValueError:
    <here we know that obj is not a float>
 
If not, the solution is probably to patch sage's commands that create graphics, to change RealLiteral's to float's.

That should be fixed as well. Sage shouldn't feed its own floating point classes to 3rd-party libraries, they certainly aren't doctested with extended precision floating point numbers.

john_perry_usm

unread,
Feb 9, 2013, 5:41:53 PM2/9/13
to sage-s...@googlegroups.com
I have uploaded a patch to the trac, ticket 13732, which fixes this for all the plot commands that seem to accept alpha. (To find them, I performed a grep alpha sage/plot/*py and looked at those files.) The doctests in sage/plot pass for me, though I had to make some changes to older doctests because float gives a different output; I haven't yet tried doctesting anything outside of that directory.

I couldn't find anything that made the save command choke like this except the alpha option, though I didn't try very hard. I hope someone reviews it; it should be easy, unless I did something stupid.

john perry

kcrisman

unread,
Feb 9, 2013, 9:07:07 PM2/9/13
to sage-s...@googlegroups.com

With this change, I can sage PDF with alpha, and it probably fixes a lot of other thing

Huh.  See http://trac.sagemath.org/sage_trac/ticket/14074 for an example where alpha works fine with saving as a pdf.  Of course, you could always put the alpha to float(.5) or something...
 
s, as well.
 
Reply all
Reply to author
Forward
0 new messages