-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add block_reduce and block_replicate functions #3453
Conversation
Two quick comments:
|
I think this is more restricted that necessary. It basically offers two function that can be used: sum and mean. At least for downsampling there could be a parameter that defaults to |
block_size).sum(axis=1) | ||
elif data.ndim == 2: | ||
output = data[:size_init[0], :size_init[1]].reshape( | ||
size_new[0], block_size, size_new[1], block_size).sum(axis=(1, 3)) |
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.
Instead of sum
this could be a parameter that the use could set to any jumpy ufunc.
+1 to @hamogu's suggestion |
Thanks for the suggestions. I started to implement them, but then I realized that http://scikit-image.org/docs/dev/api/skimage.measure.html#block-reduce There is no exact equivalent of So, should we not add a rebining/downsampling function to |
I suggest to advertise scikit-image. The original docs for imutils had a sentence that we would only implement what is not available in numpy, scipy, or scikit-image. That being said, as long as we only need one or two functions from scikit-image they could live in extern as long as we have a big warning sign that they are only there temporarily for convenience and will be removed if we see that we need more functionality from scikit-image so that it becomes impractical to maintain a separate copy. |
I also agree that for functionality that is already available there scikit-image should be promoted. |
if scikit-image dosen't have the upscale functionality why not see if they would be interested in adding it there? As more and more people are using packaging systems like anaconda, worrying about peoples access to packages is, imo, becoming less of a concern. |
I think maybe as @hamogu suggested, it can't hurt to include such features provisionally in Astropy if there is a strong need. But they should also be offered to scikit-image as perhaps an eventual permanent home. As long as it's something that doesn't have deeply embedded astronomy-specific functionality, which it sounds like upsample doesn't. |
|
Even though I said it above, I'll answer here again for the record:
Note: |
Let me advocate a contrary view. There are actually a few issues in the background that need clarification (i.e., we need to reconsider the currently adopted principles).
These are at odds with each other, of course, and I think we need to be clearer on how to resolve the conflict between them in general. The two could be satisfied by copying the code from scikit-image into astropy. That has a drawback of imposing a requirement to keep tabs on the original code in the event there are improvements or bug fixes there. Or we can relax our dependencies requirement (yes, we can make it optional, but for such basic functionality, that doesn't really seem like a workable solution). One solution I am not in favor of is simply directing people to scikit-image for that functionality. As developers that are familiar with the scientific python landscape, that doesn't seem like such a big deal. But for people who are new to this, having basic image operations coming from a number of different sources (and also having potentially different API's or conventions) is likely to be a great annoyance. I'd argue that even if we do allow a direct dependence on scikit-image, that it makes a lot of sense to repackage these disparate tools into one package by effectively collecting them into one namespace even when underlying imports come from a number of different areas. It also allows us to present a more unified API for these tools by wrapping them with adapters. Finally, if for some reason we absolutely don't want a scikit-image dependency in this case, then the above code is better than no code at all. This is a perfect illustration of the perfect being the enemy of the good. Just accept it, and if someone wants a more general implementation, add it yourself! Rejecting additional functionality because it isn't as general as we would like doesn't help to encourage contributions. |
Just one note (which I've expanded on in the mailing list) - we do ultimately want our own downsample/upsample wrappers that deal with WCS, even if they have optional dependencies under-the-hood. |
I've advocated in the recent past for gradually relaxing this requirement, since I think the installation issues have improved a lot in recent years. So I thought I'd just put that out there--don't want to derail the discussion though. |
A couple comments, then a concrete (if not particularly imaginative) proposal that can hopefully wrap this up promptly.
Proposal:
I do think there is some value astropy can add here by wrapping and decorating. @larrybradley -- what are your thoughts? |
My current plans for this PR are to:
One other feature that I would like to add to these functions is handling masks, but I will likely add that in a separate PR. I completely agree that these functions should eventually support WCS, but that will likely be easier once gWCS is ready. |
Sounds great to me! |
Also, if you could ping me when it is ready for review that would be helpful. |
ca44c65
to
2f606fe
Compare
This is ready for review. CC: @mwcraig |
""" | ||
|
||
from skimage.measure import block_reduce | ||
|
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.
Do we need to wrap this a try except block in case the dependency is not available?
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.
Looks like no....in coordinates, where scipy is needed for some things, the import is not wrapped in the functions that use it but the tests are wrapped to avoid failures due to imports.
See:
astropy/astropy/coordinates/matching.py
Line 402 in 321a41c
from scipy import spatial |
I like the way this looks (assuming Travis passes). |
@larrybradley -- all of the test failures are because scikit-image isn't installed. Looks like the correct place to add the dependency for travis would be at: https://github.com/astropy/astropy/blob/master/.continuous-integration/travis/setup_dependencies_common.sh#L33 For appveyor please add it at: |
@@ -5,7 +5,8 @@ | |||
from numpy.testing import assert_allclose | |||
|
|||
from ...tests.helper import pytest | |||
from ..utils import extract_array, add_array, subpixel_indices | |||
from ..utils import (extract_array, add_array, subpixel_indices, | |||
block_reduce, block_replicate) | |||
|
|||
test_positions = [(10.52, 3.12), (5.62, 12.97), (31.33, 31.77), |
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 you please add a scikit-image import here in a try/except, and if the import fails, skip the tests that require scipy?
An example of how to do that is at:
try: |
Forgot to comment on the actual code -- that part looks fine! |
Can someone please restart travis test 9179.14? I think that must be a transient failure. |
Done. |
Thanks, @embray. All tests pass now. |
@larrybradley -- looks good! Just read the code over one more time, I think this is ready to merge. |
This looks very good! Thanks @larrybradley - merging |
Add block_reduce and block_replicate functions
The
downsample
andupsample
(Cython
) functions weren't merged as part of the largeimageutils
PR (#3201) because they could be accomplished usingndarray
tricks (which are a bit slower than theCython
-based functions). This PR implementsdownsample
andupsample
using thendarray
tricks, which are likely too obscure for most users. Both functions can handle 1D or 2D data, anddownsample
will trim the input array if necessary.