-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Decorators for registering custom accessors in xarray #806
Conversation
👍 very cool |
👍 Very nice. |
Of the two different projects I'm working (sporadically) on that both subclass Dataset, it seems like one (pyGDX) should more properly be a backend, while the other could work as an accessor. This code looks good! Just to be clear— |
============ | ||
|
||
.. autosummary:: | ||
:toctree: generate/ |
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 generated
?
Yes, this looks very good, thanks! @khaeru : I had the same question and just tested it: import xarray as xr
import numpy as np
@xr.register_dataset_accessor('geo')
class GeoAccessor(object):
def __init__(self, xarray_obj):
print('In constructor')
self._obj = xarray_obj
@property
def center(self):
# return the geographic center point of this dataset
lon = self._obj.latitude
lat = self._obj.longitude
return (float(lon.mean()), float(lat.mean()))
def plot(self):
print('plot data on a map') # e.g., using Cartopy
# Nothing is printed
ds = xr.Dataset({'longitude': np.linspace(0, 10),
'latitude': np.linspace(0, 20)})
# out: In constructor
out = ds.geo.center
# out: In constructor
# plot data on a map
ds.geo.plot() So the constructor is called only if the property is accessed, which is good. the drawback is that a new object is instantiated at each access. |
Oh, I realize that this doesn't answer your question though 😳 |
This will happen after Dataset.init/DataArray.init. For cases where you want to do custom initialization, the suggestion (which I should add) is to simply write your own function to use in place of It's probably worth noting in the documentation that this is equivalent to just adding a property, e.g., class Dataset:
...
@property
def geo(self)
return GeoAccessor(self) |
Thanks for the clarification. Would it make sense to make a |
@fmaussion that's still helpful, thanks.
Now that I think of it, it should also be possible to use some other in logic in For instance, if the accessor creates and uses certain variables in a Dataset, it could check for their presence, and skip any initialization code if they already exist. |
👍 nice one |
Fixes GH706 New (experimental) decorators `xarray.register_dataset_accessor` and `xarray.register_dataarray_accessor` for registering custom xarray extensions without subclassing. They are described in the new documentation page on `internals`.
c52efb0
to
3489fd0
Compare
Finally got around to updating this. I added caching of these properties per @fmaussion's suggestion. Caching is a little error prone (I wrote a custom descriptor), but I think test cases should cover it pretty well. |
This looks good to me. Anyone else have any final comments? |
|
||
The intent here is that libraries that extend xarray could add such an accessor | ||
to implement subclass specific functionality rather than using actual subclasses | ||
rather patching in a large number of domain specific methods. |
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.
Isn't something wrong in this sentence?
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.
fixed
Apart for the doc typo I mentioned, this looks very good, thanks! |
Fixes #706
New (experimental) decorators
xarray.register_dataset_accessor
andxarray.register_dataarray_accessor
for registering custom xarray extensions without subclassing. They are described in the new documentation page oninternals
.CC @rafa-guedes @rabernat @fmaussion @khaeru @ajdawson -- as people who might use such an interface, it would be great to get some feedback about how well this would work for you.