Skip to content

Commit f9661b7

Browse files
committed
Fix FutureWarning on Dataset.dims.__getvalue__
Fixes FutureWarning introduced by pydata/xarray#8500
1 parent 9bbcb32 commit f9661b7

19 files changed

+63
-58
lines changed

docs/releases/development.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
Next release (in development)
33
=============================
44

5-
* ...
5+
* Fix a ``FutureWarning`` on accessing :attr:`xarray.Dataset.dims`
6+
with xarray >= 2023.12.0
7+
(:pr:`124`, :pr:`pydata/xarray#8500`).

src/emsarray/conventions/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def grid_shape(self) -> Dict[GridKind, Sequence[int]]:
17671767
"""
17681768
return {
17691769
grid_kind: tuple(
1770-
self.dataset.dims[dim]
1770+
self.dataset.sizes[dim]
17711771
for dim in self.grid_dimensions[grid_kind]
17721772
)
17731773
for grid_kind in self.grid_kinds
@@ -1877,7 +1877,7 @@ def wind(
18771877
grid_kind = self.default_grid_kind
18781878

18791879
dimensions = self.grid_dimensions[grid_kind]
1880-
sizes = [self.dataset.dims[dim] for dim in dimensions]
1880+
sizes = [self.dataset.sizes[dim] for dim in dimensions]
18811881

18821882
return utils.wind_dimension(
18831883
data_array,

src/emsarray/conventions/arakawa_c.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def i_dimension(self) -> Hashable:
6969
@cached_property
7070
def shape(self) -> Tuple[int, int]:
7171
"""The shape of this grid, as a tuple of ``(j, i)``."""
72-
return (self.dataset.dims[self.j_dimension], self.dataset.dims[self.i_dimension])
72+
return (
73+
self.dataset.sizes[self.j_dimension],
74+
self.dataset.sizes[self.i_dimension],
75+
)
7376

7477
@cached_property
7578
def size(self) -> int:

src/emsarray/conventions/grid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def x_dimension(self) -> Hashable:
173173
@cached_property
174174
def shape(self) -> Tuple[int, int]:
175175
"""The shape of this grid, as a tuple of ``(y, x)``."""
176-
dims = self.dataset.dims
177-
return (dims[self.y_dimension], dims[self.x_dimension])
176+
sizes = self.dataset.sizes
177+
return (sizes[self.y_dimension], sizes[self.x_dimension])
178178

179179
@cached_property
180180
def size(self) -> int:
@@ -346,7 +346,7 @@ def _get_or_make_bounds(self, coordinate: xarray.DataArray) -> xarray.DataArray:
346346
if (
347347
len(bounds.dims) == 2
348348
and bounds.dims[0] == coordinate.dims[0]
349-
and self.dataset.dims[bounds.dims[1]] == 2
349+
and self.dataset.sizes[bounds.dims[1]] == 2
350350
):
351351
return bounds
352352
else:
@@ -481,7 +481,7 @@ def _get_or_make_bounds(self, coordinate: xarray.DataArray) -> xarray.DataArray:
481481
len(bounds.dims) == 3
482482
and bounds.dims[0] == self.y_dimension
483483
and bounds.dims[1] == self.x_dimension
484-
and self.dataset.dims[bounds.dims[2]] == 4
484+
and self.dataset.sizes[bounds.dims[2]] == 4
485485
):
486486
return cast(xarray.DataArray, bounds)
487487
else:

src/emsarray/conventions/ugrid.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,10 @@ def two_dimension(self) -> Hashable:
890890
"""
891891
two = 'Two'
892892
# Check for the standard name
893-
if two in self.dataset.dims and self.dataset.dims[two] == 2:
893+
if two in self.dataset.sizes and self.dataset.sizes[two] == 2:
894894
return two
895895
# Check for any other dimension of size 2
896-
for name, size in self.dataset.dims.items():
896+
for name, size in self.dataset.sizes.items():
897897
if size == 2:
898898
return name
899899
# Make up a new dimension with the standard name
@@ -975,7 +975,7 @@ def max_node_dimension(self) -> Hashable:
975975
@property
976976
def node_count(self) -> int:
977977
"""The number of nodes in the dataset."""
978-
return self.dataset.dims[self.node_dimension]
978+
return self.dataset.sizes[self.node_dimension]
979979

980980
@property
981981
def edge_count(self) -> int:
@@ -986,7 +986,7 @@ def edge_count(self) -> int:
986986
# This dimension may not be defined, so ignore KeyErrors. We can
987987
# compute it below.
988988
with suppress(KeyError):
989-
return self.dataset.dims[self.edge_dimension]
989+
return self.dataset.sizes[self.edge_dimension]
990990

991991
# By computing the edge_node array we can determine how many edges exist
992992
return self.edge_node_array.shape[0]
@@ -996,12 +996,12 @@ def face_count(self) -> int:
996996
"""
997997
The number of faces in the dataset.
998998
"""
999-
return self.dataset.dims[self.face_dimension]
999+
return self.dataset.sizes[self.face_dimension]
10001000

10011001
@property
10021002
def max_node_count(self) -> int:
10031003
"""The maximum number of nodes / edges per face."""
1004-
return self.dataset.dims[self.max_node_dimension]
1004+
return self.dataset.sizes[self.max_node_dimension]
10051005

10061006

10071007
class UGridKind(str, enum.Enum):

src/emsarray/transect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def make_poly_collection(
422422
(distance_bounds[index, 1], depth_bounds[depth_index][0]),
423423
]
424424
for depth_index in range(transect_dataset.coords['depth'].size)
425-
for index in range(transect_dataset.dims['index'])
425+
for index in range(transect_dataset.sizes['index'])
426426
]
427427
return PolyCollection(vertices, **kwargs)
428428

@@ -474,7 +474,7 @@ def make_ocean_floor_poly_collection(
474474
(distance_bounds[index, 1], deepest),
475475
(distance_bounds[index, 1], bathymetry_values[linear_indices[index]]),
476476
]
477-
for index in range(transect_dataset.dims['index'])
477+
for index in range(transect_dataset.sizes['index'])
478478
]
479479
return PolyCollection(vertices, **kwargs)
480480

src/emsarray/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def check_data_array_dimensions_match(dataset: xarray.Dataset, data_array: xarra
454454
f"Data array has unknown dimension {dimension} of size {data_array_size}"
455455
)
456456

457-
dataset_size = dataset.dims[dimension]
457+
dataset_size = dataset.sizes[dimension]
458458
if data_array_size != dataset_size:
459459
raise ValueError(
460460
"Dimension mismatch between dataset and data array: "

tests/cli/commands/test_extract_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_extract_points(
3535
point_dataset = xarray.open_dataset(out_path)
3636

3737
assert 'point' in point_dataset.dims
38-
assert point_dataset.dims['point'] == num_points
38+
assert point_dataset.sizes['point'] == num_points
3939
assert_equal(points_df['name'], point_dataset['name'].values)
4040
assert_allclose(points_df['lon'], point_dataset['lon'].values)
4141
assert_allclose(points_df['lat'], point_dataset['lat'].values)

tests/conventions/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def test_select_index():
398398
ds_point = convention.select_index(SimpleGridIndex(y, x))
399399

400400
# The x and y dims should have been dropped, as they are now of size 1
401-
assert ds_point.dims == {'t': 5, 'z': 5}
401+
assert ds_point.sizes == {'t': 5, 'z': 5}
402402
# The x and y coords should be single values
403403
assert ds_point.coords['x'].values == x
404404
assert ds_point.coords['y'].values == y

tests/conventions/test_cfgrid1d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def test_drop_geometry(datasets: pathlib.Path):
348348
dataset = xarray.open_dataset(datasets / 'cfgrid1d.nc')
349349

350350
dropped = dataset.ems.drop_geometry()
351-
assert dropped.dims.keys() == {'lon', 'lat'}
351+
assert set(dropped.dims) == {'lon', 'lat'}
352352

353353
topology = dataset.ems.topology
354354
assert topology.longitude_name in dataset.variables
@@ -408,7 +408,7 @@ def test_make_clip_mask():
408408
assert_equal(mask.data_vars['cell_mask'].values, expected_cells)
409409

410410
assert mask.attrs == {'type': 'CFGrid mask'}
411-
assert mask.dims == {
411+
assert mask.sizes == {
412412
topology.longitude_name: topology.longitude.size,
413413
topology.latitude_name: topology.latitude.size,
414414
}
@@ -448,7 +448,7 @@ def test_apply_clip_mask(tmp_path):
448448
# Check that the variable and dimension keys were preserved
449449
assert set(dataset.data_vars.keys()) == set(clipped.data_vars.keys())
450450
assert set(dataset.coords.keys()) == set(clipped.coords.keys())
451-
assert set(dataset.dims.keys()) == set(clipped.dims.keys())
451+
assert set(dataset.dims) == set(clipped.dims)
452452

453453
# Check that the new topology seems reasonable
454454
assert clipped.ems.topology.longitude.size == 5

0 commit comments

Comments
 (0)