Decorator for functions that want to accept NDData#2855
Conversation
|
Took a very quick look, the approach looks good! |
|
Let me know when you want a more thorough review. The decorator in ccdproc that changes function signatures is at https://github.com/astropy/ccdproc/blob/master/ccdproc/log_meta.py It is not as broad as @embray's recent PR, I'm nearly positive. |
|
Yup, looks good so far! I don't know if you want to add the issue of what the decorator returns (ie if you give it nddata-type object if it returns an nddata-type object), or to save that for later discussions. |
|
This looks good! However, it's currently restricted to functions that have only one positional argument ( |
|
@larrybradley - yes, good point. Can we impose the restriction that the data should be the first argument though? (cc @cdeil @bsipocz) |
|
@astrofrog Yes, the restriction that |
|
|
|
@mwcraig @larrybradley @cdeil - the decorator is not more robust, well tested, and documented in the docstring. Could you try it out on some real-life cases to see if it works? The function signature is not yet preserved because I need to wait for #2849 to be merged, then I'll update this code to use @embray, the test here marked as xfail fails on Python 3.4 too - should I write the test differently? (I thought the built-in |
|
Also, a general question - do we need a documentation page for this, or is the docstring enough? |
|
@astrofrog I'm about to push a change to make it work on Python 3.4 as well, and then assuming all is good there will merge it. FYI I also am moving the new |
|
Actually better still, you might just remove that test entirely. The new |
|
@embray - I'll leave the test for now just to make sure that it all works fine, and I can always remove the test after that. |
|
@astrofrog It's working with |
There was a problem hiding this comment.
The following code block is not syntax-highlighted in the html output because having no statement in the function is considered as a syntax error by Sphinx (don't know why ... it's OK for Python if there's a comment).
You could either add a line return data, wcs at the end or add a .. code-block:: python before and the syntax highlighting will work.
There was a problem hiding this comment.
Actually just having a def statement followed by a comment is a syntax error. There has to be at a minimum one Python statement (it could be just a docstring, or a pass statement, etc.)
|
Users that have an @expand_nddata_args
def downsample(data, wcs)it's a bit weird that expanding the NDData on input is implicit, but I have to explicitly reassemble the output into an NDData: nddata = NDData(data, wcs)
data_out, wcs_out = downsample(nddata)
nddata_out = NDData(data_out, wcs_out)Even worse if I have an NDData with some property that the function doesn't support ... I have to write: nddata = NDData(data, wcs, mask)
data_out, wcs_out = downsample(nddata.data, nddata.wcs)
mask_out, _ = downsample(nddata.mask, None)
nddata_out = NDData(data_out, wcs_out, mask_out)What if I write a library function that calls Since I wasn't able to participate in the telcon, let me just say once more that I'd prefer a more explicit API, where downsample for NDData is handled in a separate |
|
@cdeil - before we continue the discussion further, I should just mention that there really are two kinds of functions here:
For the first category of functions I think there really is no reason not to use the decorator proposed in this PR - there is no point in duplicating the API. Do you have any objections to that? For the second, here's a possible solution. The issue with duplicating the API is the repetition of a lot of boilerplate code, for example it would be good to warn the user if the uncertainties can't be used, etc. So we could basically have a slightly different decorator that rather than modifying the original function is used to generate an NDData-friendly wrapper: downsample_nddata = wrap_nddata(downsample, ('data', 'wcs'))where the (Edit: in the second idea, if the functions conform to the numpydoc format, we can even edit the docstring on-the-fly to reflect the fact the input should be |
|
I should admit that I think The only reason it works for So far So my preference would be to get rid of If we keep |
|
I need to think about this a bit more, but if we go back to basics, I think the idea was really to provide a simple data container than was a generalized version of an HDU. At the base level we wanted to have a data container that could be format-agnostic and could be used to represent an n-dimensional array with some meta-data. But of course, once you start to interpret objects as e.g. a spectrum, IFU, image, you want to treat them differently. So maybe the mistake is to try and write functions that can work with any
You may ask what the point of even inheriting from |
|
I just found out about GrokTheDocs, if you click the link you see some info about which docs are read by the users. |
|
Also all the work it's incorporated for handling masked data. Maybe something we might end up doing is making It's no surprise that the docs for |
|
And agreed ... we should do 1.0 deadlines more often to get stuff done! |
|
@cdeil - there is no real case in the core package but the decorator currently preserves the docstring of the decorated function, so we can start using it seamlessly in affiliated packages and then try and come up with something smarter for the docstring? Or come up with a docstring convention then? |
|
+1 to merge this tomorrow ... and when the first real example exists in the core or photutils or ccdproc add a link to it from these docs. |
|
@mhvk - I think indeed the approach you are suggesting could be considered an extension of what's here, so I think we are fine with deferring that for now. I added a note about this being experimental. |
|
This can be merged once Travis and AppVeyor pass |
|
Given that this is in line with APE7 which was approved, and that there have been no objections, I will go ahead and merge this. We can still tweak this during the beta-testing phase if needed. It would be great if affiliated package developers could try this out (@cdeil @mwcraig - worth trying out in photutils and ccdproc?) |
…ns to take NDData as their first argument and automatically expand properties into the keyword arguments of the function.
974d31f to
6f14ca0
Compare
Decorator for functions that want to accept NDData

As discussed during the NDData telecon today, rather than having to provide two interfaces for users - one that accepts separate arguments and one that accepts NDData - it would be nice to be able to write functions with explicit arguments that are needed, such as:
and then have a decorator that could make it so that
datacan be anNDData, andwcsandunitwould automatically be fetched from theNDDataobject, since these are validNDDataproperties. The idea is that the function would only list the properties it needs and that properties that exist on NDData but can't be interpreted by the function would emit a warning so the user knows that e.g. uncertainties are not being used.This pull request implements such a decorator, and here is an example of usage:
The restrictions for now are:
dataargument. I don't see why not have this restrictions since it makes a bit of the coding easier, but I'm happy to be convinced otherwise!The function can also take arguments not specified by NDData, for example
n_iterationsorverboseor anything else. Those arguments are passed through untouched.Remaining todos (if we want to go ahead with this):
cc @mwcraig @cdeil @perrygreenfield @crawfordsm @larrybradley @adamginsburg @Cadair (who did I miss?)
also cc @embray because he likes decorators (#2849) :)