|
8 | 8 | from itertools import chain, combinations
|
9 | 9 |
|
10 | 10 | import numpy as np
|
| 11 | +import pytest |
11 | 12 | from xarray import DataArray
|
12 | 13 | from xarray import concat as xr_concat
|
13 | 14 |
|
14 |
| -from pytensor.xtensor.shape import concat, stack, transpose, unstack |
| 15 | +from pytensor.xtensor.shape import ( |
| 16 | + concat, |
| 17 | + stack, |
| 18 | + transpose, |
| 19 | + unstack, |
| 20 | +) |
15 | 21 | from pytensor.xtensor.type import xtensor
|
16 | 22 | from tests.xtensor.util import (
|
17 | 23 | xr_arange_like,
|
|
21 | 27 | )
|
22 | 28 |
|
23 | 29 |
|
| 30 | +pytest.importorskip("xarray") |
| 31 | + |
| 32 | + |
24 | 33 | def powerset(iterable, min_group_size=0):
|
25 | 34 | "Subsequences of the iterable from shortest to longest."
|
26 | 35 | # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
|
@@ -253,3 +262,94 @@ def test_concat_scalar():
|
253 | 262 | res = fn(x1_test, x2_test)
|
254 | 263 | expected_res = xr_concat([x1_test, x2_test], dim="new_dim")
|
255 | 264 | xr_assert_allclose(res, expected_res)
|
| 265 | + |
| 266 | + |
| 267 | +def test_squeeze(): |
| 268 | + """Test squeeze.""" |
| 269 | + |
| 270 | + # Single dimension |
| 271 | + x1 = xtensor("x1", dims=("city", "country"), shape=(3, 1)) |
| 272 | + y1 = x1.squeeze("country") |
| 273 | + fn1 = xr_function([x1], y1) |
| 274 | + x1_test = xr_arange_like(x1) |
| 275 | + xr_assert_allclose(fn1(x1_test), x1_test.squeeze("country")) |
| 276 | + |
| 277 | + # Multiple dimensions and order independence |
| 278 | + x2 = xtensor("x2", dims=("a", "b", "c", "d"), shape=(2, 1, 1, 3)) |
| 279 | + y2a = x2.squeeze(["b", "c"]) |
| 280 | + y2b = x2.squeeze(["c", "b"]) # Test order independence |
| 281 | + y2c = x2.squeeze(["b", "b"]) # Test redundant dimensions |
| 282 | + y2d = x2.squeeze([]) # Test empty list (no-op) |
| 283 | + fn2a = xr_function([x2], y2a) |
| 284 | + fn2b = xr_function([x2], y2b) |
| 285 | + fn2c = xr_function([x2], y2c) |
| 286 | + fn2d = xr_function([x2], y2d) |
| 287 | + x2_test = xr_arange_like(x2) |
| 288 | + xr_assert_allclose(fn2a(x2_test), x2_test.squeeze(["b", "c"])) |
| 289 | + xr_assert_allclose(fn2b(x2_test), x2_test.squeeze(["c", "b"])) |
| 290 | + xr_assert_allclose(fn2c(x2_test), x2_test.squeeze(["b", "b"])) |
| 291 | + xr_assert_allclose(fn2d(x2_test), x2_test) |
| 292 | + |
| 293 | + # Unknown shapes |
| 294 | + x3 = xtensor("x3", dims=("a", "b", "c")) # shape unknown |
| 295 | + y3 = x3.squeeze("b") |
| 296 | + x3_test = xr_arange_like(xtensor(dims=x3.dims, shape=(2, 1, 3))) |
| 297 | + fn3 = xr_function([x3], y3) |
| 298 | + xr_assert_allclose(fn3(x3_test), x3_test.squeeze("b")) |
| 299 | + |
| 300 | + # Mixed known + unknown shapes |
| 301 | + x4 = xtensor("x4", dims=("a", "b", "c"), shape=(None, 1, 3)) |
| 302 | + y4 = x4.squeeze("b") |
| 303 | + x4_test = xr_arange_like(xtensor(dims=x4.dims, shape=(4, 1, 3))) |
| 304 | + fn4 = xr_function([x4], y4) |
| 305 | + xr_assert_allclose(fn4(x4_test), x4_test.squeeze("b")) |
| 306 | + |
| 307 | + # Test axis parameter |
| 308 | + x5 = xtensor("x5", dims=("a", "b", "c"), shape=(2, 1, 3)) |
| 309 | + y5 = x5.squeeze(axis=1) # squeeze dimension at index 1 (b) |
| 310 | + fn5 = xr_function([x5], y5) |
| 311 | + x5_test = xr_arange_like(x5) |
| 312 | + xr_assert_allclose(fn5(x5_test), x5_test.squeeze(axis=1)) |
| 313 | + |
| 314 | + # Test axis parameter with negative index |
| 315 | + y5 = x5.squeeze(axis=-1) # squeeze dimension at index -2 (b) |
| 316 | + fn5 = xr_function([x5], y5) |
| 317 | + x5_test = xr_arange_like(x5) |
| 318 | + xr_assert_allclose(fn5(x5_test), x5_test.squeeze(axis=-2)) |
| 319 | + |
| 320 | + # Test axis parameter with sequence of ints |
| 321 | + y6 = x2.squeeze(axis=[1, 2]) |
| 322 | + fn6 = xr_function([x2], y6) |
| 323 | + x2_test = xr_arange_like(x2) |
| 324 | + xr_assert_allclose(fn6(x2_test), x2_test.squeeze(axis=[1, 2])) |
| 325 | + |
| 326 | + # Test drop parameter warning |
| 327 | + x7 = xtensor("x7", dims=("a", "b"), shape=(2, 1)) |
| 328 | + with pytest.warns( |
| 329 | + UserWarning, match="drop parameter has no effect in pytensor.xtensor" |
| 330 | + ): |
| 331 | + y7 = x7.squeeze("b", drop=True) # squeeze and drop coordinate |
| 332 | + fn7 = xr_function([x7], y7) |
| 333 | + x7_test = xr_arange_like(x7) |
| 334 | + xr_assert_allclose(fn7(x7_test), x7_test.squeeze("b", drop=True)) |
| 335 | + |
| 336 | + |
| 337 | +def test_squeeze_errors(): |
| 338 | + """Test error cases for squeeze.""" |
| 339 | + |
| 340 | + # Non-existent dimension |
| 341 | + x1 = xtensor("x1", dims=("city", "country"), shape=(3, 1)) |
| 342 | + with pytest.raises(ValueError, match="Dimension .* not found"): |
| 343 | + x1.squeeze("time") |
| 344 | + |
| 345 | + # Dimension size > 1 |
| 346 | + with pytest.raises(ValueError, match="has static size .* not 1"): |
| 347 | + x1.squeeze("city") |
| 348 | + |
| 349 | + # Symbolic shape: dim is not 1 at runtime → should raise |
| 350 | + x2 = xtensor("x2", dims=("a", "b", "c")) # shape unknown |
| 351 | + y2 = x2.squeeze("b") |
| 352 | + x2_test = xr_arange_like(xtensor(dims=x2.dims, shape=(2, 2, 3))) |
| 353 | + fn2 = xr_function([x2], y2) |
| 354 | + with pytest.raises(Exception): |
| 355 | + fn2(x2_test) |
0 commit comments