Comments (7)
cc @klindsay28
from esmlab.
@andersy005, I am reopening this issue. I am concerned that the implementation we chose in #54 sets xarray
options globally, so that on import of esmlab
, a user will get different behavior from xarray
than its defaults. While I think the xarray
settings are not the best choice for defaults, I don't think we should be mucking around with these settings on the user's behalf, but rather confine our application of the non-default to within esmlab
.
How can we do this without wrapping every computation in a context manager?
One option is to define a function decorator:
from contextlib import ContextDecorator
class xarray_setoptions(ContextDecorator):
def __init__(self, **kwargs):
self.old = xr.set_options(**kwargs).old
def __enter__(self):
return self
def __exit__(self, *exc):
xr.set_options(**self.old)
return
@xarray_setoptions(arithmetic_join="exact")
def compute_mon_climatology(dset, time_coord_name=None):
# computation
This seems like a nice way to implement this feature at the function level, but it's not Python 2 compatible. We could put
xr_settings_old = xr.set_options(arithmetic_join="exact").old
at the top of every function, and
xr.set_options(**self.old)
at the bottom.
@mnlevy1981, we should discuss plans for marbl-diags
; at present the esmlab
dependencies are modest, so we might consider dropping py2 support here sooner rather than later.
from esmlab.
@matt-long I think marbl-diags
will need to use an updated esmlab
if that's where we decide to put the zonal-averaging code... otherwise I'm happy to stick with an older release until the post-processing scripts are py3-compatible. What happens with the code snippet above in python 2? Does it throw an error, or does it just ignore the function decorator?
from esmlab.
I don't think ContextDecorator
is defined in python 2, so we could protect the class definition from a python 2 invocation. Not sure what it would do with the decorator...but we would not get the desired
settings.
from esmlab.
If we could protect the class definition from python 2, I'd be okay with having marbl-diags
define xarray
settings before calling esmlab
(I think that would work without altering xarray
behavior from other calls)
from esmlab.
@andersy005, I think Mike's suggestion is a good one, if the decorator is ignored. There would have to be a dummy object in py2 to import into the computational modules so as to keep these clean of version checks.
from esmlab.
@mnlevy1981, there's a contextlib
backport module for python2 (https://github.com/jazzband/contextlib2). So I used the approach in #46 (comment) and this should work for both Python2 and Python3
from esmlab.
Related Issues (20)
- Climatology is broken HOT 1
- more general resample utility HOT 1
- Remove the old benchmarks notebooks
- Least squares polynomial fit with Dask HOT 4
- Potential GroupBy() bottleneck
- Failing tests in tests/test_core.py
- Esmlab Design Document HOT 8
- sel_time with optional methods HOT 1
- cannot compute annual mean with xarray 0.14.0 HOT 4
- add upstream master test env HOT 2
- Fix Tests
- indexes related error from xarray v0.14.0 when calling esmlab.resample(ds, freq='ann') HOT 7
- Move general functionality upstream HOT 9
- esmlab operations clobber existing ds.attrs['history'] values
- esmlab.climatology does not propagate all coordinate variables
- xarray error after moving from 3897727 to f3a548d HOT 5
- esmlab.anomaly generating error, unrealistically large time values HOT 5
- esmlab.resample reverses order of dimensions in time bounds variable
- Move weighted reductions to xarray
- esmlab.resample() error with xarray 0.15.1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esmlab.