Skip to content

Commit f1c9820

Browse files
committed
Merge branch 'dev' of github.com:simvue-io/python-api into dev
2 parents 7aef301 + a97ecf8 commit f1c9820

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323
args: [--branch, main, --branch, dev]
2424
- id: check-added-large-files
2525
- repo: https://github.com/astral-sh/ruff-pre-commit
26-
rev: v0.13.1
26+
rev: v0.13.2
2727
hooks:
2828
- id: ruff
2929
args: [ --fix, --exit-non-zero-on-fix, "--ignore=C901" ]

simvue/api/objects/grids.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@
3333
__all__ = ["Grid"]
3434

3535

36-
def check_ordered_array(axis_ticks: list[float]) -> bool:
36+
def check_ordered_array(
37+
axis_ticks: list[list[float]] | numpy.ndarray,
38+
) -> list[list[float]]:
3739
"""Returns if array is ordered or reverse ordered."""
38-
if not isinstance(axis_ticks[0], float):
39-
raise ValueError("Ordering can only be checked on a 1D array")
40-
_array = numpy.array(axis_ticks)
41-
return numpy.all(numpy.sort(_array) == _array) or numpy.all(
42-
numpy.reversed(numpy.sort(_array)) == _array
43-
)
40+
if isinstance(axis_ticks, numpy.ndarray):
41+
axis_ticks = axis_ticks.tolist()
42+
for i, _array in enumerate(axis_ticks):
43+
_array = numpy.array(_array)
44+
if not numpy.all(numpy.sort(_array) == _array) or numpy.all(
45+
reversed(numpy.sort(_array)) == _array
46+
):
47+
raise ValueError(f"Axis {i} has unordered values.")
48+
return axis_ticks
4449

4550

4651
class Grid(SimvueObject):
@@ -104,7 +109,13 @@ def new(
104109
cls,
105110
*,
106111
name: str,
107-
grid: list[list[float]],
112+
grid: typing.Annotated[
113+
list[list[float]],
114+
pydantic.conlist(
115+
pydantic.conlist(float, min_length=1), min_length=1, max_length=2
116+
),
117+
pydantic.AfterValidator(check_ordered_array),
118+
],
108119
labels: list[str],
109120
offline: bool = False,
110121
**kwargs,
@@ -116,7 +127,8 @@ def new(
116127
name : str
117128
name for this grid.
118129
grid : list[list[float]]
119-
define a grid as a list of axes containing tick values.
130+
define a grid as a list of axes containing tick values
131+
number of axes must be 1 or 2
120132
labels : list[str]
121133
label each of the axes defined.
122134
offline: bool, optional
@@ -127,22 +139,13 @@ def new(
127139
Metrics
128140
metrics object
129141
"""
130-
if len(grid) < 1:
131-
raise ValueError("Invalid argument for 'grid'")
132-
133-
if len(labels) != len(set(labels)):
134-
raise ValueError("Labels must be unique.")
135142

136143
if len(labels) != len(grid):
137144
raise AssertionError(
138145
"Length of argument 'labels' must match first "
139146
f"grid dimension {len(grid)}."
140147
)
141148

142-
for i, axis in enumerate(grid):
143-
if not check_ordered_array(axis):
144-
raise ValueError(f"Axis {i} has unordered values.")
145-
146149
return Grid(
147150
grid=grid,
148151
labels=labels,

tests/unit/test_grids.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ def test_grid_creation_online() -> None:
2626
_run.commit()
2727
_grid_def=numpy.vstack([
2828
numpy.linspace(0, 10, 10),
29-
numpy.linspace(0, 20, 10),
3029
numpy.linspace(50, 60, 10),
3130
])
3231
_grid_list = _grid_def.tolist()
3332
_grid = Grid.new(
3433
name=f"test_grid_creation_online_{_uuid}",
35-
labels=["x", "y", "z"],
34+
labels=["x", "y"],
3635
grid=_grid_list
3736
)
3837
_grid.commit()
@@ -57,14 +56,13 @@ def test_grid_creation_offline() -> None:
5756
_run.commit()
5857
_grid_def=numpy.vstack([
5958
numpy.linspace(0, 10, 10),
60-
numpy.linspace(0, 20, 10),
6159
numpy.linspace(50, 60, 10),
6260
])
6361
_grid_list = _grid_def.tolist()
6462
_grid = Grid.new(
6563
name=f"test_grid_creation_online_{_uuid}",
6664
grid=_grid_list,
67-
labels=["x", "y", "z"],
65+
labels=["x", "y"],
6866
offline=True
6967
)
7068
_grid.commit()
@@ -105,13 +103,12 @@ def test_grid_metrics_creation_online() -> None:
105103
_run.commit()
106104
_grid_def=numpy.vstack([
107105
numpy.linspace(0, 10, 10),
108-
numpy.linspace(0, 20, 10),
109106
numpy.linspace(50, 60, 10),
110107
])
111108
_grid_list = _grid_def.tolist()
112109
_grid = Grid.new(
113110
name=f"test_grid_creation_online_{_uuid}",
114-
labels=["x", "y", "z"],
111+
labels=["x", "y"],
115112
grid=_grid_list
116113
)
117114
_grid.commit()
@@ -126,7 +123,7 @@ def test_grid_metrics_creation_online() -> None:
126123
),
127124
"time": _time,
128125
"step": _step,
129-
"array": numpy.ones((10, 10, 10)),
126+
"array": numpy.ones((10, 10)),
130127
"grid": _grid.id,
131128
"metric": "A"
132129
}
@@ -156,13 +153,12 @@ def test_grid_metrics_creation_offline() -> None:
156153
_run.commit()
157154
_grid_def=numpy.vstack([
158155
numpy.linspace(0, 10, 10),
159-
numpy.linspace(0, 20, 10),
160156
numpy.linspace(50, 60, 10),
161157
])
162158
_grid_list = _grid_def.tolist()
163159
_grid = Grid.new(
164160
name=f"test_grid_creation_offline_{_uuid}",
165-
labels=["x", "y", "z"],
161+
labels=["x", "y"],
166162
grid=_grid_list,
167163
offline=True
168164
)
@@ -178,7 +174,7 @@ def test_grid_metrics_creation_offline() -> None:
178174
),
179175
"time": _time,
180176
"step": _step,
181-
"array": numpy.ones((10, 10, 10)),
177+
"array": numpy.ones((10, 10)),
182178
"grid": _grid.id,
183179
"metric": "A"
184180
}

0 commit comments

Comments
 (0)