Skip to content

Commit

Permalink
Check for too high dimensions in sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
uvchik committed Jan 16, 2025
1 parent 67804b1 commit 006b2a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/oemof/solph/_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def sequence(iterable_or_scalar):
10
"""
if len(np.shape(iterable_or_scalar)) > 1:
d = len(np.shape(iterable_or_scalar))
raise ValueError(
f"Dimension too high ({d} > 1) for {iterable_or_scalar}"
)
if isinstance(iterable_or_scalar, str):
return iterable_or_scalar
elif isinstance(iterable_or_scalar, abc.Iterable):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SPDX-License-Identifier: MIT
"""
import numpy as np
import pandas as pd
import pytest

from oemof.solph._plumbing import _FakeSequence
Expand Down Expand Up @@ -66,6 +67,15 @@ def test_sequence():
assert seq_ab == "ab"


def test_dimension_is_too_high_to_create_a_sequence():
df = pd.DataFrame({"epc": 5}, index=["a"])
with pytest.raises(ValueError, match="Dimension too high"):
sequence(df)
n2 = [[4]]
with pytest.raises(ValueError, match="Dimension too high"):
sequence(n2)


def test_valid_sequence():
np_array = np.array([0, 1, 2, 3, 4])
assert valid_sequence(np_array, 5)
Expand Down

0 comments on commit 006b2a2

Please sign in to comment.