From 507d205bdc9227f0f35b2db7ef4a672ae280c9b5 Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 5 Dec 2024 10:05:21 +0100 Subject: [PATCH] add release notes --- doc/release_notes.rst | 1 + linopy/common.py | 4 ++++ test/test_common.py | 14 +++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index cc34de26..a31644d3 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -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 ` now includes a progress argument to enable or disable the progress bar while writing. diff --git a/linopy/common.py b/linopy/common.py index 27bc76a1..676dbb5a 100644 --- a/linopy/common.py +++ b/linopy/common.py @@ -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] diff --git a/test/test_common.py b/test/test_common.py index 730713c6..4b52d9e9 100644 --- a/test/test_common.py +++ b/test/test_common.py @@ -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"]))