GithubHelp home page GithubHelp logo

Comments (7)

matt-long avatar matt-long commented on July 18, 2024

cc @klindsay28

from esmlab.

matt-long avatar matt-long commented on July 18, 2024

@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.

mnlevy1981 avatar mnlevy1981 commented on July 18, 2024

@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.

matt-long avatar matt-long commented on July 18, 2024

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.

mnlevy1981 avatar mnlevy1981 commented on July 18, 2024

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.

matt-long avatar matt-long commented on July 18, 2024

@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.

andersy005 avatar andersy005 commented on July 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.