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 multiplication and division of datasets. #792

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

ecomodeller
Copy link
Member

No description provided.

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):
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Member

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)

Copy link
Member Author

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

Copy link
Member

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

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)
Copy link
Member

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 🤔

Copy link
Member Author

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.

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As comment above

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

Successfully merging this pull request may close these issues.

2 participants