|
17 | 17 | from absl.testing import parameterized
|
18 | 18 | import dask.array as da
|
19 | 19 | import numpy as np
|
| 20 | +import pandas as pd |
20 | 21 | import xarray
|
21 | 22 | import xarray_beam as xbeam
|
22 | 23 | from xarray_beam._src import test_util
|
@@ -103,6 +104,86 @@ def test_make_template_from_chunked(self):
|
103 | 104 | self.assertEqual(template.foo.chunks, ((3,),))
|
104 | 105 | self.assertIsNone(template.bar.chunks)
|
105 | 106 |
|
| 107 | + def test_replace_template_dims_with_coords(self): |
| 108 | + source = xarray.Dataset( |
| 109 | + {'foo': (('x', 'y'), np.zeros((1, 2)))}, |
| 110 | + coords={'x': [0], 'y': [10, 20]}, |
| 111 | + ) |
| 112 | + template = xbeam.make_template(source) |
| 113 | + new_x_coords = pd.date_range('2000-01-01', periods=5) |
| 114 | + new_template = xbeam.replace_template_dims(template, x=new_x_coords) |
| 115 | + |
| 116 | + self.assertEqual(new_template.sizes, {'x': 5, 'y': 2}) |
| 117 | + expected_x_coord = xarray.DataArray( |
| 118 | + new_x_coords, dims='x', coords={'x': new_x_coords} |
| 119 | + ) |
| 120 | + xarray.testing.assert_equal(new_template.x, expected_x_coord) |
| 121 | + xarray.testing.assert_equal(new_template.y, source.y) # Unchanged coord |
| 122 | + self.assertEqual(new_template.foo.shape, (5, 2)) |
| 123 | + self.assertIsInstance(new_template.foo.data, da.Array) # Still lazy |
| 124 | + |
| 125 | + def test_replace_template_dims_with_size(self): |
| 126 | + source = xarray.Dataset( |
| 127 | + {'foo': (('x', 'y'), np.zeros((1, 2)))}, |
| 128 | + coords={'x': [0], 'y': [10, 20]}, |
| 129 | + ) |
| 130 | + template = xbeam.make_template(source) |
| 131 | + new_template = xbeam.replace_template_dims(template, x=10) |
| 132 | + |
| 133 | + self.assertEqual(new_template.sizes, {'x': 10, 'y': 2}) |
| 134 | + self.assertNotIn( |
| 135 | + 'x', new_template.coords |
| 136 | + ) # Coord is dropped when replaced by size |
| 137 | + xarray.testing.assert_equal(new_template.y, source.y) |
| 138 | + self.assertEqual(new_template.foo.shape, (10, 2)) |
| 139 | + self.assertIsInstance(new_template.foo.data, da.Array) |
| 140 | + |
| 141 | + def test_replace_template_dims_multiple(self): |
| 142 | + source = xarray.Dataset( |
| 143 | + {'foo': (('x', 'y'), np.zeros((1, 2)))}, |
| 144 | + coords={'x': [0], 'y': [10, 20]}, |
| 145 | + ) |
| 146 | + template = xbeam.make_template(source) |
| 147 | + new_x_coords = pd.date_range('2000-01-01', periods=5) |
| 148 | + new_template = xbeam.replace_template_dims(template, x=new_x_coords, y=3) |
| 149 | + |
| 150 | + self.assertEqual(new_template.sizes, {'x': 5, 'y': 3}) |
| 151 | + expected_x_coord = xarray.DataArray( |
| 152 | + new_x_coords, dims='x', coords={'x': new_x_coords} |
| 153 | + ) |
| 154 | + xarray.testing.assert_equal(new_template.x, expected_x_coord) |
| 155 | + self.assertNotIn('y', new_template.coords) |
| 156 | + self.assertEqual(new_template.foo.shape, (5, 3)) |
| 157 | + self.assertIsInstance(new_template.foo.data, da.Array) |
| 158 | + |
| 159 | + def test_replace_template_dims_multiple_vars(self): |
| 160 | + source = xarray.Dataset( |
| 161 | + { |
| 162 | + 'foo': (('x', 'y'), np.zeros((1, 2))), |
| 163 | + 'bar': ('x', np.zeros(1)), |
| 164 | + 'baz': ('z', np.zeros(3)), # Unrelated dim |
| 165 | + }, |
| 166 | + coords={'x': [0], 'y': [10, 20], 'z': [1, 2, 3]}, |
| 167 | + ) |
| 168 | + template = xbeam.make_template(source) |
| 169 | + new_template = xbeam.replace_template_dims(template, x=5) |
| 170 | + |
| 171 | + self.assertEqual(new_template.sizes, {'x': 5, 'y': 2, 'z': 3}) |
| 172 | + self.assertNotIn('x', new_template.coords) |
| 173 | + xarray.testing.assert_equal(new_template.y, source.y) |
| 174 | + xarray.testing.assert_equal(new_template.z, source.z) |
| 175 | + self.assertEqual(new_template.foo.shape, (5, 2)) |
| 176 | + self.assertEqual(new_template.bar.shape, (5,)) |
| 177 | + self.assertEqual(new_template.baz.shape, (3,)) # Unchanged var |
| 178 | + self.assertIsInstance(new_template.foo.data, da.Array) |
| 179 | + self.assertIsInstance(new_template.bar.data, da.Array) |
| 180 | + self.assertIsInstance(new_template.baz.data, da.Array) |
| 181 | + |
| 182 | + def test_replace_template_dims_error_on_non_template(self): |
| 183 | + source = xarray.Dataset({'foo': ('x', np.zeros(1))}) # Not a template |
| 184 | + with self.assertRaisesRegex(ValueError, 'is not chunked with Dask'): |
| 185 | + xbeam.replace_template_dims(source, x=5) |
| 186 | + |
106 | 187 | def test_chunks_to_zarr(self):
|
107 | 188 | dataset = xarray.Dataset(
|
108 | 189 | {'foo': ('x', np.arange(0, 60, 10))},
|
|
0 commit comments