-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Added initial implementation of Polygon to_mask and as_patch #87
Conversation
@astrofrog - Awesome! I have to run now, but I'll try to review tonight. What's the origin of the code in
I think we should also acknowledge them in the docs or changelog. The ideal situation would be if they agree to contribute the code under our BSD-3 license via a comment here or by email, just so that we don't complicate the license situation of Astropy. |
Just to be clear, when we take MIT code and we put it in Astropy, we are re-licensing it as BSD but just acknowledging the original license. So the code is not currently MIT, we are releasing it in a BSD package, but we're just including the original license for reference. |
I added the URLs where the original code was adapted from |
Another note: I think there are some inefficiencies in the Cython code at the moment that could be sped up, but I'm not too concerned about performance right now. I think maybe once we have a lot of the functionality in place we could do a performance sprint where we try and speed everything up where needed? |
Thanks. The origin / license is now clearer in Still, it doesn't say what you write in that comment. If we're re-licensing as BSD, then I'd suggest the first line in the file to be our usual
and then after that to explain the origin and keep the MIT license. In the end it's just ~ 15 lines of code. We could just do a BSD licensed clean-room implementation before putting it in Astropy core, i.e. one person writes down or links to a description of the algorithm, and a second writes the code from that only. I've always wanted to do that. But not now, I'm OK to merge this and will shut about license now and have a look at the code. :-) |
@@ -35,8 +40,10 @@ def area(self): | |||
raise NotImplementedError | |||
|
|||
def contains(self, pixcoord): | |||
# TODO: needs to be implemented | |||
raise NotImplementedError | |||
return points_in_polygon(pixcoord.x.astype(float), |
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.
There's this error:
https://travis-ci.org/astropy/regions/jobs/180103544#L819
E AttributeError: 'int' object has no attribute 'astype'
Besides fixing the error, may I suggest you change the code to do the type conversions on separate lines before the points_in_polygon
call and put them in temp variables?
The efficiency is the same, and the error message for invalid data like here will improve.
At the moment from the traceback it's not clear which variable / line is the problematic one.
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.
Should be fixed
In your first comment on this PR, you have a plot with a polygon and a mask. Very nice! |
@cdeil - the 'getting started' documentation is growing quite long - what do you think about splitting it up into multiple pages? |
@@ -0,0 +1,5 @@ | |||
cimport numpy as np |
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.
Add standard license line at the top, even if it's a very small file.
Can you add a docstring with a sentence saying why we need a pxd
file at all?
(my Cython is always rusty because I use it so rarely. It's the same for most Python programmers.)
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.
My knowledge of Cython is 'because Cython complains if it's not there' ;)
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.
Done (it's because it's needed for cimport to work)
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.
I've left two more small inline comments.
I didn't review the algorithm / math.
@astrofrog - Should we merge this when travis-ci passes, or do you want more review here?
Maybe we leave it open for a day or two, and if people want to contribute tests or docs, they can make pull requests against this branch?
Or we merge in master now, to increase the chance this gets some usage before the regions 0.2 release?
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
cimport cython |
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.
I think this line can be removed!?
cimport cython
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.
Done
|
||
for i in range(n): | ||
j = (i+n-1) % n | ||
if(((vy[i] > y) != (vy[j] > y)) and \ |
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 if statement spanning over 4 lines is very hard to read.
Can you compute the parts on separate lines and store them in temp variables, and then have a one-line conditional?
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.
The 'and' short-circuits the evaluation of the second part of the if statement, so I don't want to pre-compute the second part - but I'll clean it up.
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.
PyCharm suggests to remove the backslashes, and I agree, I think generally just parens to group multi-line expressions are the preferred style over backslashes. Not sure if that's PEP8 or not.
@cdeil - I'll add an arraydiff test now so there is at least some form of testing. But we can merge before the docs. I think for 0.2 it'd be nice to split up the docs into more pages - at the moment 'getting started' is a bit monolithic - would you have time to work on that? |
I was thinking about this today, too. The other idea I had was that we create an IPython notebook with some nice demos to get people's attention. We could make it a binder, so that they can try it out online. |
What about putting that in the astropy-tutorials repo? It's already on binder that way. |
Very little, but yes, I'll try to put some more time into docs before making the release. |
Yes, probably that would be the best solution. |
I've split out the discussion / work on high-level docs re-structuring into a separate issue: |
@cdeil - I implemented your comments. If the tests pass, I think we can merge this and add documentation after. |
For the polygon code, only the @astrofrog - If you know which algorithm is implemented in the polygon functions, could you mention it by name and link e.g. to wikipedia? I mean you are linking to https://github.com/gregvw/pnpoly/blob/master/pnpoly.pyx and https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html, but from looking at the code and those pages for 1 minute it's not clear to me which algorithm we have. Is it one of the algorithms mentioned here? |
+1 to merge and add more tests / docs in the coming days. |
@cdeil - the algorithm is the even-odd rule |
This implements to_mask for Polygon in line with other shapes.
It does not use code from ginga for example - I did look at that code, but the issue is we need to be able to efficiently compute the sub-pixel overlap without making large arrays of pixels (the ginga code operates with vectorized operations on Numpy arrays).
However, the code I'm adding here for the point/polygon stuff is pretty lightweight. Example usage:
This needs tests of course, but waiting first to see if people are open to this.