Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a 'list of polygons' capability #207

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

SDiot
Copy link

@SDiot SDiot commented Jul 11, 2014

Hi,

I'm using the PolygonPlot function of chaco for a personal project. Unfortunately it becomes really slow when the number of polygons starts to increase and eventually becomes unusable. While googling I found that with matplotlib there is a simple way to plot a list of polygons in one single call (see http://exnumerus.blogspot.com/2011/02/how-to-quickly-plot-polygons-in.html) by simple sending a list of coordinates separated by None to identify the different polygons.

So, I implemented the same idea in chaco to speed-up the plot of a list of polygons. I used numpy.nan instead of None because it needed 'float'. The modification of polygon_plot is trivial, I just receive the list of polygons and cut it in a list of polygons according to the numpy.nan before doing the plot of each polygon within a loop. (I tried several ways of creating the list, from the most trivial to the one currently implemented. The latter is 1.5 times faster than the trivial way of doing it, so I kept it.)

I also modified the hittest to work with the list of polygons. Of course a list of polygons is seen as only one object and it has been coded such that if you send only one polygon the way it is originally done, it also works as expected.

The only restriction for now, is that this allows to send a list of polygons that must have the same properties: face_color, edge_color, line width... but this is nonetheless very useful I think.

I wrote 3 basic scripts to test that:

  1. https://github.com/SDiot/chaco_troubles/blob/master/FastPolygonPlot.py : creates a list of 1000 random polygons and plot it with the original technique and with the new one and measure the computational times. On my machine the original method takes 3.8s while the new one 0.007s for 1000 polygons. Moreover for 2000 polygons, the original takes 14.8s and the new one 0.008s... thus being almost independent of the number of polygons!

  2. https://github.com/SDiot/chaco_troubles/blob/master/polygon_move.py : a simplified version of "Polygon plot with drag-move" where 4 polygons are plotted and can be dragged using the left click. This is only to show that the behavior didn't change with the modification of hittest.

  3. https://github.com/SDiot/chaco_troubles/blob/master/polygon_move_list.py : a simplified version of "Polygon plot with drag-move" where 4 lists of polygons are plotted and can be dragged using the left click. This intends to show that the modification of hittest works as expected, i.e. each list of polygons is seen as an object and any click in one of the polygons of the list allows to drag the whole list.

This simple modification allowed me to use PolygonPlot for my application, I hope it will be interested for some of you!

S.

PS: I'm currently thinking about an extension where a list of face_colors/edge_colors/line_width/... could be send at the same time as the list of polygons to make it even more general.

SDiot added 8 commits May 21, 2014 08:56
in order to get Travis CI to work
This mod allows to send a list of polygons separated by bumpy.nan to
dramatically speed-up the plots when all the polygons have the same
properties (colors, line width).
Add a 'list of polygons' capability. A PR will come soon.
back to original file so that only polygon_plot.py is modified compared to chaco/Master
@enbuilder
Copy link

Can one of the admins verify this patch?

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) when pulling 8905c0e on SDiot:feature/list_of_polygons into 032f21a on enthought:master.

# bnds is True where polygons are separated + ensures last is True
# indx contains the list of indices that separates the polygons
# lines contains a list of lines that represents all polygons
bnds = [np.isnan(point[0]) for point in points]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely to be slow: see https://github.com/enthought/chaco/blob/master/chaco/lineplot.py#L266 for comparison to get an idea of how the line plot finds the runs of non-nan values.

Also this code, and the equivalent code below in hittest should probably go into the gather_points function so that it can be cached and we can avoid re-computation of the polygons on every draw.

@corranwebster
Copy link
Contributor

Sorry for the delay in looking at this. This seems like a worthwhile addition to the polygon plot.

There's one major comment above about the way to compute the runs of non-nan values, and a general style comment.

In addition, it would be great to have a unit test which at least carries out a minimal "smoke-test" of this code (see chaco/tests/scatterplot_renderers_test_case.py for something you might be able to adapt), and an example in the examples/demo directory somewhere (although that's probably less important in this case, unless you've got something that looks really cool).

Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants