Skip to content
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

Support basic arithmetic between PredictionEnsemble objects. #248

Closed
bradyrx opened this issue Oct 25, 2019 · 6 comments · Fixed by #377
Closed

Support basic arithmetic between PredictionEnsemble objects. #248

bradyrx opened this issue Oct 25, 2019 · 6 comments · Fixed by #377

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Oct 25, 2019

It might be useful to support basic arithmetic between PredictionEnsemble objects (+, -, *, /).

Code Sample, a copy-pastable example if possible

import climpred

hind = climpred.tutorial.load_dataset('CESM-DP-SST') 
hindcast1 = climpred.HindcastEnsemble(hind)
hindcast2 = climpred.HindcastEnsemble(hind)

hindcast1 + hindcast2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-c0a6615fb41c> in <module>
      5 hindcast2 = climpred.HindcastEnsemble(hind)
      6 
----> 7 hindcast1 + hindcast2

TypeError: unsupported operand type(s) for +: 'HindcastEnsemble' and 'HindcastEnsemble'

Expected Output

This should perform the arithmetic across variables and products in common. Maybe this doesn't make much sense in most cases, but the best example I can think of is if you're aligning observations, etc. with the drift-correction method used for CESM-DPLE.

You set this up by doing

obs = obs - obs.sel(time=slice(1964, 2014)).mean('time')

But you cannot currently do

hindcast = hindcast - hindcast.sel(time=slice(1964, 2014)).mean('time')

which in theory could do this over every dataset that contains the time dimension.

I anticipate the solution would follow something similar to #243

Output of climpred.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.7 | packaged by conda-forge | (default, Jul 2 2019, 02:07:37) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] python-bits: 64 OS: Darwin OS-release: 18.7.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

climpred: 1.1.0.post55
xarray: 0.14.0
pandas: 0.25.2
numpy: 1.17.2
scipy: 1.3.1
cftime: 1.0.4
dask: 2.6.0
distributed: 2.6.0
setuptools: 41.4.0
pip: 19.3.1
conda: None
pytest: 5.2.1
IPython: 7.8.0
sphinx: 2.2.0

@aaronspring
Copy link
Collaborator

Applying these operators to HimdcatsEnsemble then means to apply it to all datasets therein, right? But does this make sense? For skill computation not really

@bradyrx
Copy link
Collaborator Author

bradyrx commented Oct 28, 2019

I guess I'm thinking just of the specific case I mentioned in this PR. But for now I guess it's reasonable to expect the user to do that before hand (e.g. ds - ds.isel(slice(a, b))).

Another use case would be if there is some variable derived from two other variables. But that's actually a different issue I think. Right now I don't have it set up to be able to do:

pm['C'] = pm['A'] + pm['B'] which would be nice at some point.

@ahuang11
Copy link
Member

@bradyrx
Copy link
Collaborator Author

bradyrx commented Oct 30, 2019

Ah nice, of course there's a magic method for that. Cool. I'll keep this in mind for a future PR.

@bradyrx bradyrx self-assigned this Feb 28, 2020
@bradyrx bradyrx added low priority and removed bug labels Feb 28, 2020
@bradyrx
Copy link
Collaborator Author

bradyrx commented Jun 5, 2020

@aaronspring here is the issue if you want to post the use case for this. I think your thought was for integrating, e.g. through PredictionEnsemble.sum(['lat', 'lon']). FYI xarray now has a weighted operators method you could leverage through .apply() for our objects. I haven't tried it yet.

@aaronspring
Copy link
Collaborator

my use case for +-*/:

he=(he*area).sum([‘lon’,’lat'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants