-
-
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
Hooks for custom attribute handling in xarray operations #988
Comments
Let me give concrete examples of what this interface could look like. To implement units: from typing import List, Optional # optional Python 3.5 type annotations
@xarray.register_ufunc_variables_attrs_handler
def propagate_units(results: List[xarray.Variable],
context: xarray.UFuncContext) -> Optional[List[dict]]:
if context.func.__name__ in ['add', 'sub']:
units_set = set(getattr(arg, 'attrs', {}).get('units') for arg in context.args)
if len(units_set) > 1:
raise ValueError('not all input units the same: %r' % units_set)
units, = units_set
return [{'units': units}]
else:
return [] * len(results)
# or equivalently, don't return anything at all Or to (partially) handle @xarray.register_ufunc_variables_attrs_handler
def add_cell_methods(results, context):
if context.func.__name__ in ['mean', 'median', 'sum', 'min', 'max', 'std']):
dims = set(context.args[0].dims) - set(results[0].dims)
cell_methods = ': '.join(dims) + ': ' + context.func.__name__
return [{'cell_methods': cell_methods}) Or to implement @xarray.register_ufunc_variables_attrs_handler
def always_keep_attrs(results, context):
if len(context.args) == 1:
return [context.args[0].attrs] * len(result) Every time xarray does an operation, we would call all of these registered
Similarly, we would have The downside of this approach is that unlike the way NumPy handles things, this doesn't handle conflicting implementations well. If you try to use two different libraries that register their own global attribute handlers instead of using the context manager (e.g., two different units implementations), things will break, even if the unrelated code paths do not touch. So alternatively to using the registration system, we could support/encourage using a context manager, e.g., with xarray.ufunc_variables_attrs_handlers([always_keep_attrs, add_cell_methods]):
# either augment or ignore other attrs handlers, possibly depending
# on the choice of a keyword argument to ufunc_variables_attrs_handlers
result = ds.mean() It's kind of verbose, but certainly useful for libraries that want to be cautious about breaking other code. In general, it's poor behavior for libraries to unilaterally change unrelated code without an explicit opt-in. So perhaps the best approach is to encourage users to always use a context manager, e.g., import contextlib
@contextlib.contextmanager
def my_attrs_context():
with xarray.ufunc_variables_attrs_handlers(
[always_keep_attrs, add_cell_methods, ...]):
yield
with my_attrs_context():
result = ds.mean() - 0.5 * (ds.max() - ds.min()) So maybe a subclass based implementation (with a custom attribute like |
I definitely see the logic with regards to encouraging users to use a context manager, and from the perspective of someone building a third-party library on top of xarray it would be fine. However, I think that from the perspective of an end-user (for example, a scientist) crunching numbers and analyzing data with xarray simply as a convenience library, this produces much too obfuscated code - a standard library import ( I think your earlier proposal of an |
I agree that end users are likely to set this flag unilaterally, especially for interactive use. That's fine. This could even be OK in a higher level library, though I would encourage requiring an explicit opt in application code. One thing to consider is whether to allow multiple attribute handlers to be registered simultaneously or not. I kind of like a set_options interface that requires all handlers to be registered at once (as opposed to adding handlers incrementally ), because that ensures conflicts cannot arise inadvertantly. Either way, I don't think the performance penalty here would be significant in most cases, given how much of Python's dynamic nature xarray already uses. |
I also prefer an approach that doesn't use context managers: I agree with @darothen's comments about the issues with their use. Regardless of the exact implementation, I am strongly in favour of this functionality being added to xarray - I can already think of a number of very useful ways I could use it! |
+1 on requiring attribute handlers to be registered at a single location because this will make for cleaner code long term. |
So I guess I guess we can also start with the attrs only hooks for now and add the others later if/as necessary. |
Some related discussion that may be of interest to participants here is going on over in #1271. |
I would say using the
which if I understand the codebase correctly could be added to |
Apparently |
We currently have the binary arithmetic logic in |
Is it not? The documentation says it's new in numpy 1.11 and we're at 1.12 now. I tried to make a small units-aware subclass of
|
Definitely not, I'm afraid. It's gone back and forth several times on master but hasn't landed yet. |
I wrote a small recipe that appears to contain basic functionality I'm looking for. There's plenty of caveats but it could be a start, if such an approach is deemed desirable at all.
|
@gerritholl Interesting! The difficulty I am seeing with this approach is that the units apply only to the main data array, and not the coordinates. In a scientific application, the coordinates are generally physical quantities with units as well. If we want xarray with units to be really useful for scientific computation, we need to have the coordinate arrays be unitful 'quantities' too, rather than tacking the units on as an attribute of xarray.DataArray. I tinkered with making the 'units' attribute into a dictionary, with units for each coordinate (and for the data) as key-value pairs, but it is very cumbersome and goes against my philosophy (for instance, extracting a coordinate from a DataArray leaves it without units). |
Good point. I didn't think of that; my coordinates happen to be either time or unitless, I think. How common is it though that the full power of a unit library is needed for coordinates? I suppose it arises with indexing, i.e. the ability to write When it's a bit more polished I intend to publish it somewhere, but currently several things are missing ( |
@gerritholl In my line of work we often deal with 2+1 or 3+1 dimensional datasets (space + time). I have been bitten when I expected space in meters, but it was in centimeters, or time in seconds but it was in milliseconds. Also, I would like to improve the plotting functionality so that publication-quality plots can be made directly by automatically including units in the axis labels (and while I'm wishing for a pony, there could be pretty-printing versions of coordinate names (ie, LaTeX symbols or something)). |
We do often deal with those in my line of work as well, I just happen not to right now. But time is the one thing that already carries units, doesn't it? One can convert between various |
There's some chance that In general, this is a pretty tough design problem, which explains why it hasn't been solved yet :). But I was pretty happy with the way our Speaking of PEP 472, if someone has energy to push on that, it would be really awesome to see that happen. |
Hi, I would really appreciate having some simple way to store some persisting attributes in xarray objects. One typical use case in my interactive work is that I have functions that have a DataArray or Dataset object as their only input, they perform some processing on the data and return an updated Dataset. Some additional information needed for this processing, such as an integer identifier of the experiment from which the data come from, are stored as attributes, so that when I have multiple instances of the same kind of objects, I will not mess up data from different experiments together. |
@shoyer I know elsewhere you said you weren't sure about this idea any more, but personally I'd like to push forward on this idea. Do you have problems with this approach we need to resolve? Any chance you have some preliminary code? I think this is the right way to solve the unit issue in XArray, since at it's core unit handling is mostly a metadata operation. |
I think these overloads are a much more maintainable way to add features like unit handling into xarray, as outlined in our development roadmap. It's not a complete system for overloading attribute handling in |
I see your argument, but here's my problem. In this future where things work (assuming that NEP is accepted), and I want distributed computing with dask, units, and xarray, I have: xarray wrapping a pint array wrapping a dask array. I like composition, but that level of wrapping...feels wrong to me for some reason. Is there some elegance I'm missing here? (Other than array-like things playing together.) And then I still need hooks in xarray so that when pint does a calculation, it can update the metadata in xarray; so it feels like we're back here anyway. |
Yep, this is pretty much what I was thinking of.
The virtue of this approach vs setting an global "attribute handler" (as suggested here) is that everything is controlled locally. For example, suppose people want to plug in two separate unit systems into xarray (e.g., pint and unyt). If the unit handling is determined by the specific arrays, then libraries relying on both approaches internally can happily co-exist and even call each other. In principle, this could be done safely with global handlers if you always know exactly when to switch back and forth, but that requires explicitly switching on handlers for even basic arithmetic. I doubt most users are going to bother, which is going to make using multiple tools that make use of this feature really hard. The other big advantage is that you only have to write the bulk of the unit system once, e.g., to define operations on NumPy arrays.
Rather than struggling to keep We could still do some work on the xarray side to make this easy to use. Specifically:
|
Looks like that ship has sailed, PEP-472 has been rejected |
Over in #964, I am working on a rewrite/unification of the guts of xarray's logic for computation with labelled data. The goal is to get all of xarray's internal logic for working with labelled data going through a minimal set of flexible functions which we can also expose as part of the API.
Because we will finally have all (or at least nearly all) xarray operations using the same code path, I think it will also finally become feasible to open up hooks allowing extensions how xarray handles metadata.
Two obvious use cases here are units (#525) and automatic maintenance of metadata (e.g.,
cell_methods
orhistory
fields). Both of these are out of scope for xarray itself, mostly because the specific logic tends to be domain specific. This could also subsume options like the existingkeep_attrs
on many operations.I like the idea of supporting something like NumPy's
__array_wrap__
to allow third-party code to finalize xarray objects in some way before they are returned. However, it's not obvious to me what the right design is.__array_wrap__
(or__numpy_ufunc__
) in NumPy, or should we have a system (e.g., unilaterally or with a context manager andxarray.set_options
) for registering hooks that are then checked on all xarray objects? I am inclined toward the later, even though it's a little slower, just because it will be simpler and easier to get rightattrs
and/orname
?__numpy_ufunc__
, which is a little more ambitious than what I had in mind here.Feedback would be greatly appreciated.
CC @darothen @rabernat @jhamman @pwolfram
The text was updated successfully, but these errors were encountered: