Re: [matplotlib-devel] bbox of eps backend : test needed

2010-11-16 Thread Benjamin Root
On Sun, Nov 14, 2010 at 7:25 PM, Jae-Joon Lee  wrote:

> Hi,
>
> I just committed a simple change that disables pstoeps in ps backend
> when "xpdf" distiller is used. Without this, output bbox was incorrect
> with "xpdf" distiller and usetex=False.
>
> Getting the correct bbox with the ps backend is very tricky, and it'll
> be appreciated if others help me test this.
>
> Here is a simple test script (this needs to be run with most recent
> svn version of matplotlib, and you need to able to use ustex=True!).
> It will produce 6 eps files. Please check if any of the eps files have
> incorrect bbox. And report which file has wrong bbox and your setup :
> your OS, gs version number (output from "gs -v"), pdftops version
> number (output from "pdftops -v"). It will be also good if you report
> your setup even when everything is okay.
>
> Regards,
>
> -JJ
>
> import matplotlib.pyplot as plt
>
> plt.plot([1,2])
>
> for distiller in ["False", "xpdf", "ghostscript"]:
>for usetex in ["True", "False"]:
>plt.rcParams["ps.usedistiller"]=distiller
>plt.rcParams["text.usetex"]=usetex
>
>plt.savefig("test_bbox_%s_%s.eps" % (distiller, usetex))
>
>
Evince shows a few of the eps files weirdly:

test_bbox_ghostscript_False.eps comes out as 8.5x11 inch while the other
come out as 8x6 inches.
test_bbox_xpdf_False.eps is the correct size, but the graph was placed as if
it was centered on a 8.5x11 page, thereby cutting off the bottom half of the
graph.

The system:

Fedora 13
pdftops 0.12.4
ghostscript 8.71
evince 2.30.3 (using libspectre 0.2.4)

I will try out my Ubuntu system tonight.

Ben Root
--
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


[matplotlib-devel] plot_surface patch

2010-11-16 Thread J P
Hi all, here's my first patch for matplotlib. Someone noticed at Stack
Overflow that the plot_surface function in mplot3d wasn't especially fast
for a lot of points (and small rstrides/cstrides) and using shading and a
single color. I found some parts of the code that weren't vectorized. These
are my changes so far.

Summary of changes:
1. Changed from double looping over aranges to using xrange
2. Made the normalization of the normals and their dot product with the
vector [-1,-1,0.5] to find the shading a vectorized operation.
3. Changed a list comprehension which calculated the colors using an
iterative approach to using the already built-in vectorization of the
Normalization class and using the np.outer function. The result is a numpy
array rather than a list which actually speeds up things down the line.
4. removed the corners array from plot_surface which wasn't ever used or
returned. It didn't really slow things down, but I'm thinking that it is
cruft.

For change number two, I made a separate function that generates the shades,
but feel free to move that around if you prefer.. or maybe it should be a
function that begins with a _ because it shouldn't be used externally. These
changes give varying levels of speed improvement depending on the number of
points and the rstrides/cstrides arguments. With larger numbers of points
and small rstrides/cstrides, these changes can more than halve the running
time. I have found no difference in output after my changes.

I know there is more work to be done within the plot_surface function and
I'll submit more changes soon.

Justin
Index: lib/mpl_toolkits/mplot3d/axes3d.py
===
--- lib/mpl_toolkits/mplot3d/axes3d.py	(revision 8802)
+++ lib/mpl_toolkits/mplot3d/axes3d.py	(working copy)
@@ -722,10 +722,9 @@
 normals = []
 #colset contains the data for coloring: either average z or the facecolor
 colset = []
-for rs in np.arange(0, rows-1, rstride):
-for cs in np.arange(0, cols-1, cstride):
+for rs in xrange(0, rows-1, rstride):
+for cs in xrange(0, cols-1, cstride):  
 ps = []
-corners = []
 for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
 ztop = a[rs][cs:min(cols, cs+cstride+1)]
 zleft = ta[min(cols-1, cs+cstride)][rs:min(rows, rs+rstride+1)]
@@ -733,7 +732,6 @@
 zbase = zbase[::-1]
 zright = ta[cs][rs:min(rows, rs+rstride+1):]
 zright = zright[::-1]
-corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
 z = np.concatenate((ztop, zleft, zbase, zright))
 ps.append(z)
 
@@ -802,18 +800,21 @@
 normals.append(np.cross(v1, v2))
 return normals
 
+def getshades(self, normals):
+'''
+Find the normalized vectors of the normals and dot them with
+the vector [-1,-1,0.5] to get the proper shadings.
+'''
+return np.dot(normals/np.sqrt((normals**2).sum(axis=1))[:,np.newaxis],
+   np.array([[-1],[-1],[0.5]])).squeeze()
+
 def _shade_colors(self, color, normals):
 '''
 Shade *color* using normal vectors given by *normals*.
 *color* can also be an array of the same length as *normals*.
 '''
 
-shade = []
-for n in normals:
-n = n / proj3d.mod(n)
-shade.append(np.dot(n, [-1, -1, 0.5]))
-
-shade = np.array(shade)
+shade = self.getshades(np.array(normals))
 mask = ~np.isnan(shade)
 
 if len(shade[mask]) > 0:
@@ -821,7 +822,7 @@
 if art3d.iscolor(color):
 color = color.copy()
 color[3] = 1
-colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
+colors = np.outer(0.5 + norm(shade) * 0.5, color)
 else:
 colors = [np.array(colorConverter.to_rgba(c)) * \
 (0.5 + norm(v) * 0.5) \
--
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


[matplotlib-devel] plot_surface final patch (for now)

2010-11-16 Thread Justin Peel
Hi again,

I'll try to keep my messages in plain text from now on. I kept working
on some more speed improvements for plot_surface functions and have
included the diff that has those extra changes.

Here are the main extra changes:
1. vectorized the code in _shade_colors when inputting a grid of
colors by moving to an already vectorized to_rgba_array() function,
using the vectorized Normalize class, and using numpy to multiply the
two. Also, making the result be a numpy array stops set_face_color()
function from redundantly calling to_rgba on each individual item
again. This improvement speeds up the mplot3d example surface3d_demo3
by about 3x on my machine.
2. I preallocated the space for the two vectors that will be crossed
for each area of points to make the normals and do a vectorized
np.cross() afterwards instead. Shaves off a bit more time.
3. Made the functions that select points not include multiples of the
corners of the rectangle of points (if that makes any sense). Not a
huge savings, but still worthwhile I feel.

I think that I'm done with working on these functions now. I hope that
these changes are amenable. Let me know if there are concerns.

Justin Peel
-- Forwarded message --
From: J P 
Date: Tue, Nov 16, 2010 at 4:20 PM
Subject: plot_surface patch
To: Matplotlib-devel@lists.sourceforge.net


Hi all, here's my first patch for matplotlib. Someone noticed at Stack
Overflow that the plot_surface function in mplot3d wasn't especially
fast for a lot of points (and small rstrides/cstrides) and using
shading and a single color. I found some parts of the code that
weren't vectorized. These are my changes so far.

Summary of changes:
1. Changed from double looping over aranges to using xrange
2. Made the normalization of the normals and their dot product with
the vector [-1,-1,0.5] to find the shading a vectorized operation.
3. Changed a list comprehension which calculated the colors using an
iterative approach to using the already built-in vectorization of the
Normalization class and using the np.outer function. The result is a
numpy array rather than a list which actually speeds up things down
the line.
4. removed the corners array from plot_surface which wasn't ever used
or returned. It didn't really slow things down, but I'm thinking that
it is cruft.

For change number two, I made a separate function that generates the
shades, but feel free to move that around if you prefer.. or maybe it
should be a function that begins with a _ because it shouldn't be used
externally. These changes give varying levels of speed improvement
depending on the number of points and the rstrides/cstrides arguments.
With larger numbers of points and small rstrides/cstrides, these
changes can more than halve the running time. I have found no
difference in output after my changes.

I know there is more work to be done within the plot_surface function
and I'll submit more changes soon.

Justin
Index: lib/mpl_toolkits/mplot3d/axes3d.py
===
--- lib/mpl_toolkits/mplot3d/axes3d.py	(revision 8802)
+++ lib/mpl_toolkits/mplot3d/axes3d.py	(working copy)
@@ -719,21 +719,24 @@
 fcolors = self._shade_colors_lightsource(Z, cmap, lightsource)
 
 polys = []
-normals = []
+if cmap is None and shade:
+totpts = int(np.ceil(float(rows - 1) / rstride) * \
+ np.ceil(float(cols - 1) / cstride))
+v1 = np.empty((totpts,3))
+v2 = np.empty((totpts,3))
+which = 0
 #colset contains the data for coloring: either average z or the facecolor
 colset = []
-for rs in np.arange(0, rows-1, rstride):
-for cs in np.arange(0, cols-1, cstride):
+for rs in xrange(0, rows-1, rstride):
+for cs in xrange(0, cols-1, cstride):  
 ps = []
-corners = []
 for a, ta in [(X, tX), (Y, tY), (Z, tZ)]:
-ztop = a[rs][cs:min(cols, cs+cstride+1)]
-zleft = ta[min(cols-1, cs+cstride)][rs:min(rows, rs+rstride+1)]
-zbase = a[min(rows-1, rs+rstride)][cs:min(cols, cs+cstride+1):]
-zbase = zbase[::-1]
-zright = ta[cs][rs:min(rows, rs+rstride+1):]
-zright = zright[::-1]
-corners.append([ztop[0], ztop[-1], zbase[0], zbase[-1]])
+ztop = a[rs,cs:min(cols, cs+cstride+1)]
+zleft = a[rs+1:min(rows, rs+rstride+1),
+  min(cols-1, cs+cstride)]
+zbase = a[min(rows-1, rs+rstride),
+  cs:min(cols-1, cs+cstride):][::-1]
+zright = a[rs:min(rows-1, rs+rstride):,cs][::-1]
 z = np.concatenate((ztop, zleft, zbase, zright))
 ps.append(z)
 
@@ -758,10 +761,12 @@
 # Only need vectors to shade if no c