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

Fix keepdims #778

Merged
merged 6 commits into from
Jan 24, 2025
Merged

Fix keepdims #778

merged 6 commits into from
Jan 24, 2025

Conversation

ecomodeller
Copy link
Member

No description provided.

@ecomodeller ecomodeller linked an issue Jan 20, 2025 that may be closed by this pull request
Copy link
Member

@jsmariegaard jsmariegaard left a comment

Choose a reason for hiding this comment

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

Great

See comments and decide if you want to change :-)


dfs = open(filename)
if isinstance(dfs, Mesh):
raise ValueError("mikeio.read() is not supported for Mesh files")
assert not isinstance(dfs, Mesh)
Copy link
Member

Choose a reason for hiding this comment

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

I do not understand this change - isn't the user better supported by a ValueError with a clear error message?

Copy link
Member Author

Choose a reason for hiding this comment

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

True

shape = (nt, self.nz, self.ny, self.nx) # type: ignore

spdims = self.geometry.default_dims
dims = ["time"]
Copy link
Member

Choose a reason for hiding this comment

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

It seems a little confusing to me to first always prepend dims with "time" and then afterwards potentially removing it in line 403 🤔 But it looks like it works and you have probably considered alternative ways.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is room for improvement...


d[d == self.deletevalue] = np.nan

if self._ndim == 2:
d = d.reshape(self.ny, self.nx) # type: ignore

if single_time_selected:
data_list[item] = d
if keepdims:
Copy link
Member

Choose a reason for hiding this comment

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

Why not

data_list[item] = np.atleast_2d(d) if keepdims else d

@@ -574,3 +574,38 @@ def test_concat_dfsu3d_single_timesteps_generic_vs_dataset(tmp_path):
assert ds3.end_time == ds4.end_time
assert ds3.n_items == ds4.n_items
assert ds3.n_timesteps == ds4.n_timesteps


def test_keepdims_removes_singleton_dimension() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Great tests ❤️

And I guess it is not relevant to add one with dfs0 or dfs3? (what happens with the keepdims argument then)

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose dfs3 could be relevant. Let's revisit it when we improve dfs2 functionality.

@ecomodeller ecomodeller merged commit d5546ca into main Jan 24, 2025
7 checks passed
@ecomodeller ecomodeller deleted the keepdims branch January 24, 2025 07:28
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.

keepdims in mikeio.read() not working
2 participants