Skip to content

Commit d2f7faf

Browse files
FBumannclaude
andcommitted
test: make charge_state assertion order-agnostic for linopy 0.8
linopy 0.8.0 (PR PyPSA/linopy#732, "unify coords-as-truth handling") makes the explicit `coords` argument the single source of truth for variable dimension ordering. Previously `add_variables` inherited the dim order from the `lower`/`upper` bound DataArrays when present. `charge_state` is the only Storage variable created with explicit bound arrays, which are built time-leading via `expand_dims('time')` / `concat(dim='time')`. So on linopy <0.8 it came back as (time, cluster), inconsistent with every other variable (e.g. flow_rate is (cluster, time)). On >=0.8 it follows the coords order -> (cluster, time), resolving that inconsistency. The test hard-coded the old order and indexed positionally. Switch to label-based access (isel(cluster=...)) and assert the dim set rather than its order, so the test passes on both linopy 0.7 and 0.8. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b6d66d8 commit d2f7faf

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

tests/test_math/test_clustering.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,14 @@ def test_storage_cyclic_charge_discharge_pattern(self, optimize):
336336
discharge_fr = fs.solution['Battery(discharge)|flow_rate'].values[:, :4]
337337
assert_allclose(discharge_fr, [[0, 50, 0, 50], [0, 50, 0, 50]], atol=1e-5)
338338

339-
# Charge state: dims=(time, cluster), 5 entries (incl. final)
339+
# Charge state: time + cluster dims, 5 entries (incl. final).
340+
# Access by label so the assertion is robust to dim ordering
341+
# (linopy <0.8 returned (time, cluster); >=0.8 returns (cluster, time)).
340342
# Cyclic: SOC wraps, starting with pre-charge from previous cycle
341343
charge_state = fs.solution['Battery|charge_state']
342-
assert charge_state.dims == ('time', 'cluster')
343-
cs_c0 = charge_state.values[:5, 0]
344-
cs_c1 = charge_state.values[:5, 1]
344+
assert set(charge_state.dims) == {'time', 'cluster'}
345+
cs_c0 = charge_state.isel(cluster=0).values[:5]
346+
cs_c1 = charge_state.isel(cluster=1).values[:5]
345347
assert_allclose(cs_c0, [50, 50, 0, 50, 0], atol=1e-5)
346348
assert_allclose(cs_c1, [100, 100, 50, 100, 50], atol=1e-5)
347349

0 commit comments

Comments
 (0)