|
| 1 | +import uxarray as ux |
| 2 | +import pytest |
| 3 | +import numpy as np |
| 4 | +from pathlib import Path |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def uxds_fixture(): |
| 9 | + """Fixture to load test dataset.""" |
| 10 | + current_path = Path(__file__).resolve().parent |
| 11 | + quad_hex_grid_path = current_path / 'meshfiles' / "ugrid" / "quad-hexagon" / 'grid.nc' |
| 12 | + quad_hex_data_path = current_path / 'meshfiles' / "ugrid" / "quad-hexagon" / 'data.nc' |
| 13 | + |
| 14 | + # Load the dataset |
| 15 | + ds = ux.open_dataset(quad_hex_grid_path, quad_hex_data_path) |
| 16 | + |
| 17 | + # Add a dummy coordinate |
| 18 | + if 'n_face' in ds.dims: |
| 19 | + n_face_size = ds.dims['n_face'] |
| 20 | + ds = ds.assign_coords(face_id=('n_face', np.arange(n_face_size))) |
| 21 | + |
| 22 | + return ds |
| 23 | + |
| 24 | +def test_isel(uxds_fixture): |
| 25 | + """Test that isel method preserves UxDataset type.""" |
| 26 | + result = uxds_fixture.isel(n_face=[1, 2]) |
| 27 | + assert isinstance(result, ux.UxDataset) |
| 28 | + assert hasattr(result, 'uxgrid') |
| 29 | + |
| 30 | +def test_where(uxds_fixture): |
| 31 | + """Test that where method preserves UxDataset type.""" |
| 32 | + result = uxds_fixture.where(uxds_fixture['t2m'] > uxds_fixture['t2m'].min()) |
| 33 | + assert isinstance(result, ux.UxDataset) |
| 34 | + assert hasattr(result, 'uxgrid') |
| 35 | + |
| 36 | +def test_assign(uxds_fixture): |
| 37 | + """Test that assign method preserves UxDataset type.""" |
| 38 | + # Create a new variable based on t2m |
| 39 | + new_var = xr.DataArray( |
| 40 | + np.ones_like(uxds_fixture['t2m']), |
| 41 | + dims=uxds_fixture['t2m'].dims |
| 42 | + ) |
| 43 | + result = uxds_fixture.assign(new_var=new_var) |
| 44 | + assert isinstance(result, ux.UxDataset) |
| 45 | + assert hasattr(result, 'uxgrid') |
| 46 | + assert result.uxgrid is uxds_fixture.uxgrid |
| 47 | + assert 'new_var' in result.data_vars |
| 48 | + |
| 49 | + |
| 50 | +def test_drop_vars(uxds_fixture): |
| 51 | + """Test that drop_vars method preserves UxDataset type.""" |
| 52 | + # Create a copy with a new variable so we can drop it |
| 53 | + ds_copy = uxds_fixture.copy(deep=True) |
| 54 | + ds_copy['t2m_copy'] = ds_copy['t2m'].copy() |
| 55 | + result = ds_copy.drop_vars('t2m_copy') |
| 56 | + assert isinstance(result, ux.UxDataset) |
| 57 | + assert hasattr(result, 'uxgrid') |
| 58 | + assert result.uxgrid is ds_copy.uxgrid |
| 59 | + assert 't2m_copy' not in result.data_vars |
| 60 | + |
| 61 | +def test_transpose(uxds_fixture): |
| 62 | + """Test that transpose method preserves UxDataset type.""" |
| 63 | + # Get all dimensions |
| 64 | + dims = list(uxds_fixture.dims) |
| 65 | + if len(dims) > 1: |
| 66 | + # Reverse dimensions for transpose |
| 67 | + reversed_dims = dims.copy() |
| 68 | + reversed_dims.reverse() |
| 69 | + result = uxds_fixture.transpose(*reversed_dims) |
| 70 | + assert isinstance(result, ux.UxDataset) |
| 71 | + assert hasattr(result, 'uxgrid') |
| 72 | + assert result.uxgrid is uxds_fixture.uxgrid |
| 73 | + |
| 74 | +def test_fillna(uxds_fixture): |
| 75 | + """Test that fillna method preserves UxDataset type.""" |
| 76 | + # Create a copy with some NaN values in t2m |
| 77 | + ds_with_nans = uxds_fixture.copy(deep=True) |
| 78 | + t2m_data = ds_with_nans['t2m'].values |
| 79 | + if t2m_data.size > 0: |
| 80 | + t2m_data.ravel()[0:2] = np.nan |
| 81 | + result = ds_with_nans.fillna(0) |
| 82 | + assert isinstance(result, ux.UxDataset) |
| 83 | + assert hasattr(result, 'uxgrid') |
| 84 | + # Verify NaNs were filled |
| 85 | + assert not np.isnan(result['t2m'].values).any() |
| 86 | + |
| 87 | +def test_rename(uxds_fixture): |
| 88 | + """Test that rename method preserves UxDataset type.""" |
| 89 | + result = uxds_fixture.rename({'t2m': 't2m_renamed'}) |
| 90 | + assert isinstance(result, ux.UxDataset) |
| 91 | + assert hasattr(result, 'uxgrid') |
| 92 | + assert result.uxgrid is uxds_fixture.uxgrid |
| 93 | + assert 't2m_renamed' in result.data_vars |
| 94 | + assert 't2m' not in result.data_vars |
| 95 | + |
| 96 | +def test_to_array(uxds_fixture): |
| 97 | + """Test that to_array method preserves UxDataArray type for its result.""" |
| 98 | + # Create a dataset with multiple variables to test to_array |
| 99 | + ds_multi = uxds_fixture.copy(deep=True) |
| 100 | + ds_multi['t2m_celsius'] = ds_multi['t2m'] |
| 101 | + ds_multi['t2m_kelvin'] = ds_multi['t2m'] + 273.15 |
| 102 | + |
| 103 | + result = ds_multi.to_array() |
| 104 | + assert isinstance(result, ux.UxDataArray) |
| 105 | + assert hasattr(result, 'uxgrid') |
| 106 | + assert result.uxgrid == uxds_fixture.uxgrid |
| 107 | + |
| 108 | +def test_arithmetic_operations(uxds_fixture): |
| 109 | + """Test arithmetic operations preserve UxDataset type.""" |
| 110 | + # Test addition |
| 111 | + result = uxds_fixture['t2m'] + 1 |
| 112 | + assert isinstance(result, ux.UxDataArray) |
| 113 | + assert hasattr(result, 'uxgrid') |
| 114 | + |
| 115 | + # Test dataset-level operations |
| 116 | + result = uxds_fixture * 2 |
| 117 | + assert isinstance(result, ux.UxDataset) |
| 118 | + assert hasattr(result, 'uxgrid') |
| 119 | + |
| 120 | + # Test more complex operations |
| 121 | + result = uxds_fixture.copy(deep=True) |
| 122 | + result['t2m_squared'] = uxds_fixture['t2m'] ** 2 |
| 123 | + assert isinstance(result, ux.UxDataset) |
| 124 | + assert hasattr(result, 'uxgrid') |
| 125 | + assert 't2m_squared' in result.data_vars |
| 126 | + |
| 127 | +def test_reduction_methods(uxds_fixture): |
| 128 | + """Test reduction methods preserve UxDataset type when dimensions remain.""" |
| 129 | + if len(uxds_fixture.dims) > 1: |
| 130 | + # Get a dimension to reduce over |
| 131 | + dim_to_reduce = list(uxds_fixture.dims)[0] |
| 132 | + |
| 133 | + # Test mean |
| 134 | + result = uxds_fixture.mean(dim=dim_to_reduce) |
| 135 | + assert isinstance(result, ux.UxDataset) |
| 136 | + assert hasattr(result, 'uxgrid') |
| 137 | + |
| 138 | + # Test sum on specific variable |
| 139 | + result = uxds_fixture['t2m'].sum(dim=dim_to_reduce) |
| 140 | + assert isinstance(result, ux.UxDataArray) |
| 141 | + assert hasattr(result, 'uxgrid') |
| 142 | + |
| 143 | +def test_groupby(uxds_fixture): |
| 144 | + """Test that groupby operations preserve UxDataset type.""" |
| 145 | + # Use face_id coordinate for grouping |
| 146 | + if 'face_id' in uxds_fixture.coords: |
| 147 | + # Create a discrete grouping variable |
| 148 | + grouper = uxds_fixture['face_id'] % 2 # Group by even/odd |
| 149 | + uxds_fixture = uxds_fixture.assign_coords(parity=grouper) |
| 150 | + groups = uxds_fixture.groupby('parity') |
| 151 | + result = groups.mean() |
| 152 | + assert isinstance(result, ux.UxDataset) |
| 153 | + assert hasattr(result, 'uxgrid') |
| 154 | + |
| 155 | + |
| 156 | +def test_assign_coords(uxds_fixture): |
| 157 | + """Test that assign_coords preserves UxDataset type.""" |
| 158 | + if 'n_face' in uxds_fixture.dims: |
| 159 | + dim = 'n_face' |
| 160 | + size = uxds_fixture.dims[dim] |
| 161 | + # Create a coordinate that's different from face_id |
| 162 | + new_coord = xr.DataArray(np.arange(size) * 10, dims=[dim]) |
| 163 | + result = uxds_fixture.assign_coords(scaled_id=new_coord) |
| 164 | + assert isinstance(result, ux.UxDataset) |
| 165 | + assert hasattr(result, 'uxgrid') |
| 166 | + assert 'scaled_id' in result.coords |
| 167 | + |
| 168 | + |
| 169 | +def test_expand_dims(uxds_fixture): |
| 170 | + """Test that expand_dims preserves UxDataset type.""" |
| 171 | + result = uxds_fixture.expand_dims(dim='time') |
| 172 | + assert isinstance(result, ux.UxDataset) |
| 173 | + assert hasattr(result, 'uxgrid') |
| 174 | + assert 'time' in result.dims |
| 175 | + assert result.dims['time'] == 1 |
| 176 | + |
| 177 | + # Verify data variable shape was updated correctly |
| 178 | + assert result['t2m'].shape[0] == 1 |
| 179 | + |
| 180 | +def test_method_chaining(uxds_fixture): |
| 181 | + """Test that methods can be chained while preserving UxDataset type.""" |
| 182 | + # Chain multiple operations |
| 183 | + result = (uxds_fixture |
| 184 | + .assign(t2m_kelvin=uxds_fixture['t2m'] + 273.15) |
| 185 | + .rename({'t2m': 't2m_celsius'}) |
| 186 | + .fillna(0)) |
| 187 | + assert isinstance(result, ux.UxDataset) |
| 188 | + assert hasattr(result, 'uxgrid') |
| 189 | + assert 't2m_celsius' in result.data_vars |
| 190 | + assert 't2m_kelvin' in result.data_vars |
| 191 | + |
| 192 | + |
| 193 | +def test_stack_unstack(uxds_fixture): |
| 194 | + """Test that stack and unstack preserve UxDataset type.""" |
| 195 | + if len(uxds_fixture.dims) >= 2: |
| 196 | + # Get two dimensions to stack |
| 197 | + dims = list(uxds_fixture.dims)[:2] |
| 198 | + # Stack the dimensions |
| 199 | + stacked_name = f"{dims[0]}_{dims[1]}" |
| 200 | + stacked = uxds_fixture.stack({stacked_name: dims}) |
| 201 | + assert isinstance(stacked, ux.UxDataset) |
| 202 | + assert hasattr(stacked, 'uxgrid') |
| 203 | + |
| 204 | + # Unstack them |
| 205 | + unstacked = stacked.unstack(stacked_name) |
| 206 | + assert isinstance(unstacked, ux.UxDataset) |
| 207 | + assert hasattr(unstacked, 'uxgrid') |
| 208 | + |
| 209 | + |
| 210 | +def test_sortby(uxds_fixture): |
| 211 | + """Test that sortby preserves UxDataset type.""" |
| 212 | + if 'face_id' in uxds_fixture.coords: |
| 213 | + # Create a reverse sorted coordinate |
| 214 | + size = len(uxds_fixture.face_id) |
| 215 | + uxds_fixture = uxds_fixture.assign_coords(reverse_id=('n_face', np.arange(size)[::-1])) |
| 216 | + |
| 217 | + # Sort by this new coordinate |
| 218 | + result = uxds_fixture.sortby('reverse_id') |
| 219 | + assert isinstance(result, ux.UxDataset) |
| 220 | + assert hasattr(result, 'uxgrid') |
| 221 | + # Verify sorting changed the order |
| 222 | + assert np.array_equal(result.face_id.values, np.sort(uxds_fixture.face_id.values)[::-1]) |
| 223 | + |
| 224 | +def test_shift(uxds_fixture): |
| 225 | + """Test that shift preserves UxDataset type.""" |
| 226 | + if 'n_face' in uxds_fixture.dims: |
| 227 | + result = uxds_fixture.shift(n_face=1) |
| 228 | + assert isinstance(result, ux.UxDataset) |
| 229 | + assert hasattr(result, 'uxgrid') |
| 230 | + # Verify data has shifted (first element now NaN) |
| 231 | + assert np.isnan(result['t2m'].isel(n_face=0).values.item()) |
0 commit comments