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 empty sclicing dimension in iterate_slices #390

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release Notes
Upcoming Version
----------------

* When creating slices for variables and constraints (important for the `solve` function), the slicing is now fixed in case now dimension to slice is available.
* Added a pandas priority attribute. With this change, the operation with pandas objects is now prioritizing linopy objects over pandas objects. This is useful when the using linopy objects in arithmetic operations with pandas objects, e.g. `a * x` where `a` is a pandas Series/DataFrame and `x` is a linopy variable.
* The method :meth:`model.to_file <linopy.model.Model.to_file>` now includes a progress argument to enable or disable the progress bar while writing.

Expand Down
4 changes: 4 additions & 0 deletions linopy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ def iterate_slices(

# leading dimension (the dimension with the largest size)
sizes = {dim: ds.sizes[dim] for dim in slice_dims}
if not sizes:
yield ds
return

leading_dim = max(sizes, key=sizes.get) # type: ignore
size_of_leading_dim = ds.sizes[leading_dim]

Expand Down
14 changes: 11 additions & 3 deletions test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,22 @@ def test_iterate_slices_slice_size_none():
assert ds.equals(s)


def test_iterate_slices_invalid_slice_dims():
def test_iterate_slices_empty_slice_dims():
ds = xr.Dataset(
{"var": (("x", "y"), np.random.rand(10, 10))}, # noqa: NPY002
coords={"x": np.arange(10), "y": np.arange(10)},
)
with pytest.raises(ValueError):
list(iterate_slices(ds, slice_size=50, slice_dims=[]))
slices = list(iterate_slices(ds, slice_size=50, slice_dims=[]))
assert len(slices) == 1
for s in slices:
assert ds.equals(s)


def test_iterate_slices_invalid_slice_dims():
ds = xr.Dataset(
{"var": (("x", "y"), np.random.rand(10, 10))}, # noqa: NPY002
coords={"x": np.arange(10), "y": np.arange(10)},
)
with pytest.raises(ValueError):
list(iterate_slices(ds, slice_size=50, slice_dims=["z"]))

Expand Down
Loading