Skip to content

Commit 696279e

Browse files
committed
BUG: fix heuristic for detecting uniformly spaced grid axes
1 parent 99eb64e commit 696279e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/gpgi/_data_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def collect_exceptions(cls, data: Grid[FloatT]) -> list[Exception]:
112112
)
113113

114114

115+
def is_uniformly_spaced(arr: np.ndarray[tuple[int], np.dtype[FloatT]]) -> bool:
116+
dbase = np.diff(arr)
117+
return bool(dbase.std() / dbase.max() / dbase.size < 5 * np.finfo(arr.dtype).eps)
118+
119+
115120
@final
116121
class Grid(Generic[FloatT]):
117122
def __init__(
@@ -149,7 +154,7 @@ def __init__(
149154
(3,), -1, dtype=self.coordinates[self.axes[0]].dtype
150155
)
151156
for i, ax in enumerate(self.axes):
152-
if self.size == 1 or np.diff(self.coordinates[ax]).std() < 1e-16:
157+
if self.size == 1 or is_uniformly_spaced(self.coordinates[ax]):
153158
# got a constant step in this direction, store it
154159
self._dx[i] = self.coordinates[ax][1] - self.coordinates[ax][0]
155160

tests/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ def test_inconsistent_grid_data(data, invalid_attr, expected):
404404
geometry="cartesian",
405405
grid={
406406
"cell_edges": {
407-
"x": np.array([0.0, 0.0]),
408-
"y": np.array([0.0, 0.0, 0.0]),
407+
"x": np.array([0.0, 1.0]),
408+
"y": np.array([0.0, 2.0, 4.0]),
409409
},
410410
"fields": {
411411
"density": data,
@@ -419,8 +419,8 @@ def test_validate_empty_fields():
419419
geometry="cartesian",
420420
grid={
421421
"cell_edges": {
422-
"x": np.array([0.0, 0.0]),
423-
"y": np.array([0.0, 0.0, 0.0]),
422+
"x": np.array([0.0, 1.0]),
423+
"y": np.array([0.0, 2.0, 4.0]),
424424
},
425425
"fields": {},
426426
},

0 commit comments

Comments
 (0)