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 <jpsc...@gmail.com>
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 cmap
                 if cmap is None and shade:
                     i1, i2, i3 = 0, int(len(ps2)/3), int(2*len(ps2)/3)
-                    v1 = np.array(ps2[i1]) - np.array(ps2[i2])
-                    v2 = np.array(ps2[i2]) - np.array(ps2[i3])
-                    normals.append(np.cross(v1, v2))
-
+                    i1, i2, i3 = 0, int(len(ps2)/3), int(2*len(ps2)/3)
+                    v1[which] = np.array(ps2[i1]) - np.array(ps2[i2])
+                    v2[which] = np.array(ps2[i2]) - np.array(ps2[i3])
+                    which += 1
+        if cmap is None and shade:
+            normals = np.cross(v1, v2)
         polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
 
         if fcolors is not None:
@@ -802,18 +807,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,11 +829,10 @@
             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) \
-                            for c, v in zip(color, shade)]
+		colors = np.array(colorConverter.to_rgba_array(color) * \
+                          (0.5 + 0.5 * norm(shade)[:,None]))
         else:
             colors = color.copy()
 
------------------------------------------------------------------------------
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

Reply via email to