-
Notifications
You must be signed in to change notification settings - Fork 58
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 multiplication and division of datasets. #792
base: main
Are you sure you want to change the base?
Conversation
tests/test_dataset.py
Outdated
def test_multiply_datasets_must_match(): | ||
dsa = mikeio.Dataset({"Foo": mikeio.DataArray([1, 2, 3])}) | ||
dsb = mikeio.Dataset({"Bar": mikeio.DataArray([4, 5, 6])}) | ||
with pytest.raises(ValueError): |
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.
Why do the name/item need to match on multiplication/division?
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.
Let's try with minimal requirements. If two datasets have the same number of items, and the shapes of the data is identical, we can multiply them.
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.
Agree
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.
Additional thought: could this feature also be used for scaling several items in a file with the same spatially/temporally varying field?
a) Should we allow that shapes conform but items in "other" is either the same as "this" or 1 (meaning same scaling applied to all variables).
b) Even further should we allow that as long as broadcasting rules allow multiplication then shape of "other" is okay? (so I could e.g. scale a dfsu with a dfs0)
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.
If we decide to go down this rabbit hole, I think we should consider restructuring MIKE IO to rely on xarray instead of numpy and to make use of the broadcasting in xarray. https://docs.xarray.dev/en/stable/user-guide/computation.html#broadcasting-by-dimension-name
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.
Let's wait until someone has the need
mikeio/dataset/_dataset.py
Outdated
data = [self[x].to_numpy() / value for x in self.items] | ||
except TypeError: | ||
raise TypeError(f"{value} could not be divided to Dataset") | ||
items = deepcopy(self.items) |
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.
I guess the unit is often not the same after division 🤔
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.
How to handle units in a pragmatic way is what I am struggling with, and I suppose one of the reasons that this is not already supported.
tests/test_dataset.py
Outdated
def test_divide_dataset_must_match(): | ||
dsa = mikeio.Dataset({"Foo": mikeio.DataArray([1, 2, 3])}) | ||
dsb = mikeio.Dataset({"Bar": mikeio.DataArray([4, 5, 6])}) | ||
with pytest.raises(ValueError): |
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.
As comment above
No description provided.