|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 | import re |
15 | | -import textwrap |
16 | 15 |
|
17 | 16 | from absl.testing import absltest |
18 | 17 | from absl.testing import parameterized |
@@ -185,10 +184,10 @@ def test_map_blocks_new_vars_and_dims(self): |
185 | 184 | source = xarray.Dataset({'foo': ('x', np.arange(10))}) |
186 | 185 | source_ds = xbeam.Dataset.from_xarray(source, {'x': 5}) |
187 | 186 | mapped_ds = source_ds.map_blocks( |
188 | | - lambda ds: ds.assign(bar=2*ds.foo.expand_dims('y')) |
| 187 | + lambda ds: ds.assign(bar=2 * ds.foo.expand_dims('y')) |
189 | 188 | ) |
190 | 189 | self.assertEqual(mapped_ds.chunks, {'x': 5, 'y': 1}) |
191 | | - expected = source.assign(bar=2*source.foo.expand_dims('y')) |
| 190 | + expected = source.assign(bar=2 * source.foo.expand_dims('y')) |
192 | 191 | actual = mapped_ds.collect_with_direct_runner() |
193 | 192 | xarray.testing.assert_identical(actual, expected) |
194 | 193 |
|
@@ -240,5 +239,61 @@ def test_map_blocks_explicit_template(self): |
240 | 239 | xarray.testing.assert_identical(actual, source) |
241 | 240 |
|
242 | 241 |
|
| 242 | +class RechunkingTest(test_util.TestCase): |
| 243 | + |
| 244 | + def test_split_variables(self): |
| 245 | + source = xarray.Dataset( |
| 246 | + {'foo': ('x', np.arange(10)), 'bar': ('x', np.arange(10))} |
| 247 | + ) |
| 248 | + beam_ds = xbeam.Dataset.from_xarray(source, {'x': 5}, split_vars=False) |
| 249 | + self.assertFalse(beam_ds.split_vars) |
| 250 | + split_ds = beam_ds.split_variables() |
| 251 | + self.assertTrue(split_ds.split_vars) |
| 252 | + self.assertRegex( |
| 253 | + split_ds.ptransform.label, r'^from_xarray_\d+\|split_vars_\d+$' |
| 254 | + ) |
| 255 | + actual = split_ds.collect_with_direct_runner() |
| 256 | + xarray.testing.assert_identical(actual, source) |
| 257 | + |
| 258 | + def test_consolidate_variables(self): |
| 259 | + source = xarray.Dataset( |
| 260 | + {'foo': ('x', np.arange(10)), 'bar': ('x', np.arange(10))} |
| 261 | + ) |
| 262 | + beam_ds = xbeam.Dataset.from_xarray(source, {'x': 5}, split_vars=True) |
| 263 | + self.assertTrue(beam_ds.split_vars) |
| 264 | + consolidated_ds = beam_ds.consolidate_variables() |
| 265 | + self.assertFalse(consolidated_ds.split_vars) |
| 266 | + self.assertRegex( |
| 267 | + consolidated_ds.ptransform.label, |
| 268 | + r'^from_xarray_\d+\|consolidate_vars_\d+$', |
| 269 | + ) |
| 270 | + actual = consolidated_ds.collect_with_direct_runner() |
| 271 | + xarray.testing.assert_identical(actual, source) |
| 272 | + |
| 273 | + def test_rechunk(self): |
| 274 | + source_chunks = {'x': 5, 'y': 1} |
| 275 | + target_chunks = {'x': 2, 'y': -1} |
| 276 | + source = xarray.Dataset({'foo': (('x', 'y'), np.arange(40).reshape(10, 4))}) |
| 277 | + beam_ds = xbeam.Dataset.from_xarray(source, source_chunks) |
| 278 | + rechunked_ds = beam_ds.rechunk(target_chunks) |
| 279 | + |
| 280 | + self.assertEqual(rechunked_ds.chunks, {'x': 2, 'y': 4}) |
| 281 | + actual = rechunked_ds.collect_with_direct_runner() |
| 282 | + xarray.testing.assert_identical(actual, source) |
| 283 | + |
| 284 | + def test_rechunk_split_vars(self): |
| 285 | + source = xarray.Dataset({ |
| 286 | + 'foo': (('x', 'y'), np.arange(20).reshape(10, 2)), |
| 287 | + 'bar': ('x', np.arange(10)), |
| 288 | + }) |
| 289 | + beam_ds = xbeam.Dataset.from_xarray( |
| 290 | + source, {'x': 5, 'y': 2}, split_vars=True |
| 291 | + ) |
| 292 | + rechunked_ds = beam_ds.rechunk({'x': 2, 'y': 1}) |
| 293 | + self.assertEqual(rechunked_ds.chunks, {'x': 2, 'y': 1}) |
| 294 | + actual = rechunked_ds.collect_with_direct_runner() |
| 295 | + xarray.testing.assert_identical(actual, source) |
| 296 | + |
| 297 | + |
243 | 298 | if __name__ == '__main__': |
244 | 299 | absltest.main() |
0 commit comments