-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
ENH: adding spherical Voronoi diagram algorithm to scipy.spatial #5232
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
Conversation
scipy/spatial/spherical_voronoi.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could simplify this to:
circumcenter_array = np.column_stack((array_x0_values, array_y0_values, array_z0_values))
|
Thanks CJ. I've made your specific suggested changes. I added a new task for improving the concision of the variable names to the to-do list above. |
|
Cool. :) I think I can fix the problem with the docstrings (and maybe some PEP8 issues along the way). |
scipy/spatial/spherical_voronoi.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this function be simply replaced by np.linalg.det?
"Compatiblity with Python 2.6" is probably not the reason why it exists; whether np.linalg.det works or not shouldn't depend on that.
|
@tylerjereddy a few comments on open items on the list:
Addition to the reference guide is already achieved by your edit to the docstring of
This isn't necessary, your code will take very little time to import. Everything looks good as is.
Do you think that your code is unacceptably slow (for real-world size problems or compared to another implementation)? I see some for-loops that might be quite slow, but only optimize at the end if you think it's needed. |
scipy/spatial/spherical_voronoi.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this import can be deleted, it's assumed in every single example in scipy and the docs will build correctly without it.
|
The plotting example works for me even though it's not pretty. Mayavi is indeed a too heavy dependency. |
|
@rgommers It seems that some of the (essential and much appreciated) refactoring Nikolai has done has caused a pretty substantial loss in performance based on my informal benchmarks: The Jupyter notebook used to produce the crude benchmarks has been made public (https://github.com/tylerjereddy/bench_spherical_voronoi/blob/master/assess_spherical_Voronoi_algorithm.ipynb). So, I think it would be reasonable to expect the performance of the code in the Why is the scipy fork performing slower? Speculation probably isn't productive without line profiling (as usual). However, the new I'm going to have to read the refactored code carefully. One question--if I can manage to restore the performance is there an accepted way to incorporate a "performance test" to prevent future "performance regressions" of this nature? I imagine unit tests aren't quite the right place for various reasons--execution time could change with computers / other parts of code improving, etc., though I suppose one could decorate the "unit test" with the appropriate flags such that it only runs when a comprehensive suite is desired. |
|
Hi @tylerjereddy, a quick reply about benchmarking (without having time to look into the details here now): we'd love to have proper benchmarks for any new feature that's added. Those live in https://github.com/scipy/scipy/tree/master/benchmarks and are easy to add. |
|
@rgommers It also seems that removing |
|
There is nothing in the |
|
That is a massive difference in performance indeed, looks like it needs fixing. |
Table 1: Spherical Voronoi Algorithms
Beyond that, I guess there's still a few check points in the original list above, although they may be less important now. |
|
Regarding the Example in Docstring: I can confirm that there are visibility issues with the generators and the Voronoi vertices. If you rotate the plot in the interactive view, then sometimes generators or Voronoi vertices become visible/invisible depending on the current perspective on the plot. All in all plotting spherical Voronoi diagrams in this way is certainly not ideal. Using |
|
Regarding the 2n-4 unittest: If one plays around with the interactive version from https://www.jasondavies.com/maps/voronoi/ , one can see that the claim that a spherical Voronoi diagram always has exactly v=2n-4 vertices is wrong in general: This picture shows two diagrams with the same number n of generator points, but the right one has one Voronoi vertex more than the left one. However, the left one is a pretty special case, because the degree of that vertex in the graph is four (would our algorithm actually detect that?). The fact that the better test v=2n-4 seems to be correct in all your test cases is probably because the problem described above is unlikely to occur in practice and the following should be true: Let v be the number of vertices of a spherical Voronoi diagram. Assume that the union over all faces of the diagram covers the whole sphere and that every vertex has degree 3. Then v=2n-4. Consequently: If we test the condition v=2n-4 on diagrams, which do not fall into this very special category, I think this is actually okay. Alternatively, we could test for v <= 2n-2. What do you think? |
|
@niknow Just to clarify, the upper limit should be 2n-4 Voronoi vertices, not the exact number of vertices. Does the exception exceed the upper limit [I can't tell if the diagram with one less vertex is at the 2n-4 limit already, or somewhere below it--so adding one more vertex doesn't seem to rule the upper limit out clearly]? If it exceeds the upper limit, that would be an issue, but if it is <= 2n-4, the unit test still holds as far as I can tell. The textbook also defines the planar version (2n-5) as an upper limit only. If we have a concrete example where 2n-4 is exceeded in a spherical Voronoi diagram then yeah maybe v <= 2n-2 could be used, although maybe not crucial. |
Isn't this what is meant by cooking up a pathological input which is not in (the spherical equivalent of) general position?
|
|
@argriffing I ended up rebasing on the command line (didn't see any 'button' for this based on the already-rebased branch created above?). Had to add one more commit to clean up the testing module after the rebase for some reason (I had to resolve a few conflicts along the way). I suspect the latter commit shouldn't have been necessary with a perfect conflict resolution workflow using After recompiling / installing scipy the full suite of |
Now I see that this PR does not have its own branch -- the changes are directly to |
| for vertex in sv.vertices: | ||
| distances = distance.cdist(sv.points,np.array([vertex])) | ||
| closest = np.array(sorted(distances)[0:3]) | ||
| print(closest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra print
|
afaik, github ui doesn't have helpers for rebasing things, you have to do it manually (as done above) |
ENH: spatial: adding spherical Voronoi diagram algorithm to scipy.spatial
|
Thanks, merged based on above comments. |
|
Glad to hear people are finding a use for this code! I agree there's some room for improvement in some areas--I have a few WIP PRs spattered around & I even tried to write a small grant recently for 3D plotting things, but hasn't taken off yet. I'll try to add a few short answers to queries below, for now. Certainly open to collaborating on improvements.
If you have useful literature references or you'd like to open a PR that you think can improve behavior that is likely to be welcomed. |
Thank you very much for your reply. These answers are helpful for me. In my case, it's not hard to orient the directions of each regions. You are definitely right that deduplicating generators is necessary for it. I will think about to solve the hemisphere problem and if i get a good result or read some good references, i will share it here. Thanks again for your help and your contributions to the spherical voronoi problem! |
|
Hello, i think maybe projecting the generators(in a hemisphere) to a plane from the south(or north) polar, then use the scipy.spatial.Voronoi to compute planar voronoi, after getting the voronoi vertices and regions(faces), projecting them back to the hemisphere in a same way, then to get a spherical voronoi. In my case, it seems work. But be aware that the generators should not be multiple and choose the opposite polar of the hemisphere. The process is just like the following figure in the paper But i don't quite understand if there is relation with this paper. I didn't find if there are some materials about it and if someone has already proved the feasibility. Hope someone may add such findings here. Thank you! |
|
Ah yeah, I saw that paper -- couldn't quite get the algorithm working, but I suppose it may indeed circumvent the hemisphere vulnerability. The algorithms are all loglinear time complexity, but I've not seen anyone successfully implement that one in the wild. |
|
Hello, i'd like to know where can i get the Source code of scipy.spatial.Voronoi? The source code in scipy.org webpage can't be found. I am working with this function and want to get only one vertex when two vor.vertices are near to each other, like they have same value in decimal 2. Hoping someone can help me. A little hurry. Thanks a lot. |
|
@WWmore the source code is in this repo, please use the search function. Also, please don't add questions on unrelated PRs - best to use Stackoverflow or the scipy-user mailing list. |
|
Dear Ralf,
Thank you very much. I got it.
Best regards,
Hui
Ralf Gommers <[email protected]>于2018年11月19日 周一上午5:53写道:
… @WWmore <https://github.com/WWmore> the source code is in this repo,
please use the search function. Voronoi is here:
https://github.com/scipy/scipy/blob/master/scipy/spatial/qhull.pyx#L2399
Also, please don't add questions on unrelated PRs - best to use
Stackoverflow or the scipy-user mailing list.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5232 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AbXa1cyyGQ-H3ywSUXQ7KpFXoXsPxHjGks5uwjlGgaJpZM4F5iu6>
.
|










Background
On July 30th I proposed the idea of adding spherical Voronoi code to scipy (details / motivation described in Issue #5097), which was positively received. As suggested, I posted a similar proposal to the mailing list, which did not receive any responses. While recognizing that an addition this major may be rejected outright, I nonetheless thought I'd move forward with an initial preview of the code, its documentation, and associated unit tests, to give a more concrete idea of how it behaves. The unit tests I wrote all passed after the code migration was complete (all failed initially).
Question / Advice
Before investing more time into refinements I thought it would be useful to receive feedback, if only to confirm that items on my checklist need to be addressed, etc.
My current to-do list for improving the code (if we move forward with this):
2to3diff first)scipy.spatialcodes, for which some effort seems to have been made to establish unification of returned data structures (as evidenced here: http://docs.scipy.org/doc/scipy/reference/spatial.html#simplex-representation)scipy.spatial.tsearchfor certain parts of my code that were written manually (I noticed this function might be useful when working on this PR)SphericalVoronoiclass may have to be adjusted--some may need to be _hidden and / or have import mechanics adjusted so thatimport scipy.spatial.spherical_voronoican be avoided as a supplement for certain utility functions (i.e., generating random array of points on sphere) tofrom scipy.spatial import SphericalVoronoiscipytends to avoid committing potentially useful tests that aren't quite working yet