|
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 | + squeeze, |
| 18 | + stack, |
| 19 | + transpose, |
| 20 | + unstack, |
| 21 | +) |
15 | 22 | from pytensor.xtensor.type import xtensor
|
16 | 23 | from tests.xtensor.util import (
|
17 | 24 | xr_arange_like,
|
|
21 | 28 | )
|
22 | 29 |
|
23 | 30 |
|
| 31 | +pytest.importorskip("xarray") |
| 32 | + |
| 33 | + |
24 | 34 | def powerset(iterable, min_group_size=0):
|
25 | 35 | "Subsequences of the iterable from shortest to longest."
|
26 | 36 | # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
|
@@ -253,3 +263,109 @@ def test_concat_scalar():
|
253 | 263 | res = fn(x1_test, x2_test)
|
254 | 264 | expected_res = xr_concat([x1_test, x2_test], dim="new_dim")
|
255 | 265 | xr_assert_allclose(res, expected_res)
|
| 266 | + |
| 267 | + |
| 268 | +def test_squeeze_explicit_dims(): |
| 269 | + """Test squeeze with explicit dimension(s).""" |
| 270 | + |
| 271 | + # Single dimension |
| 272 | + x1 = xtensor("x1", dims=("city", "country"), shape=(3, 1)) |
| 273 | + y1 = squeeze(x1, "country") |
| 274 | + fn1 = xr_function([x1], y1) |
| 275 | + x1_test = xr_arange_like(x1) |
| 276 | + xr_assert_allclose(fn1(x1_test), x1_test.squeeze("country")) |
| 277 | + |
| 278 | + # Multiple dimensions |
| 279 | + x2 = xtensor("x2", dims=("a", "b", "c", "d"), shape=(2, 1, 1, 3)) |
| 280 | + y2 = squeeze(x2, ["b", "c"]) |
| 281 | + fn2 = xr_function([x2], y2) |
| 282 | + x2_test = xr_arange_like(x2) |
| 283 | + xr_assert_allclose(fn2(x2_test), x2_test.squeeze(["b", "c"])) |
| 284 | + |
| 285 | + # Order independence |
| 286 | + x3 = xtensor("x3", dims=("a", "b", "c"), shape=(2, 1, 1)) |
| 287 | + y3a = squeeze(x3, ["b", "c"]) |
| 288 | + y3b = squeeze(x3, ["c", "b"]) |
| 289 | + fn3a = xr_function([x3], y3a) |
| 290 | + fn3b = xr_function([x3], y3b) |
| 291 | + x3_test = xr_arange_like(x3) |
| 292 | + xr_assert_allclose(fn3a(x3_test), fn3b(x3_test)) |
| 293 | + |
| 294 | + # Redundant dimensions |
| 295 | + y3c = squeeze(x3, ["b", "b"]) |
| 296 | + fn3c = xr_function([x3], y3c) |
| 297 | + xr_assert_allclose(fn3c(x3_test), x3_test.squeeze(["b", "b"])) |
| 298 | + |
| 299 | + # Empty list = no-op |
| 300 | + y3d = squeeze(x3, []) |
| 301 | + fn3d = xr_function([x3], y3d) |
| 302 | + xr_assert_allclose(fn3d(x3_test), x3_test) |
| 303 | + |
| 304 | + |
| 305 | +def test_squeeze_implicit_dims(): |
| 306 | + """Test squeeze with implicit dim=None (all size-1 dimensions).""" |
| 307 | + |
| 308 | + # All dimensions size 1 |
| 309 | + x1 = xtensor("x1", dims=("a", "b"), shape=(1, 1)) |
| 310 | + y1 = squeeze(x1) |
| 311 | + fn1 = xr_function([x1], y1) |
| 312 | + x1_test = xr_arange_like(x1) |
| 313 | + xr_assert_allclose(fn1(x1_test), x1_test.squeeze()) |
| 314 | + |
| 315 | + # No dimensions size 1 = no-op |
| 316 | + x2 = xtensor("x2", dims=("row", "col", "batch"), shape=(2, 3, 4)) |
| 317 | + y2 = squeeze(x2) |
| 318 | + fn2 = xr_function([x2], y2) |
| 319 | + x2_test = xr_arange_like(x2) |
| 320 | + xr_assert_allclose(fn2(x2_test), x2_test) |
| 321 | + |
| 322 | + # Symbolic shape where runtime shape is 1 → should squeeze |
| 323 | + x3 = xtensor("x3", dims=("a", "b", "c")) # shape unknown |
| 324 | + y3 = squeeze(x3, "b") |
| 325 | + x3_test = xr_arange_like(xtensor(dims=x3.dims, shape=(2, 1, 3))) |
| 326 | + fn3 = xr_function([x3], y3) |
| 327 | + xr_assert_allclose(fn3(x3_test), x3_test.squeeze("b")) |
| 328 | + |
| 329 | + # Mixed static + symbolic shapes, where symbolic shape is 1 |
| 330 | + x4 = xtensor("x4", dims=("a", "b", "c"), shape=(None, 1, 3)) |
| 331 | + y4 = squeeze(x4, "b") |
| 332 | + x4_test = xr_arange_like(xtensor(dims=x4.dims, shape=(4, 1, 3))) |
| 333 | + fn4 = xr_function([x4], y4) |
| 334 | + xr_assert_allclose(fn4(x4_test), x4_test.squeeze("b")) |
| 335 | + |
| 336 | + """ |
| 337 | + This test documents that we intentionally don't squeeze dimensions with symbolic shapes |
| 338 | + (static_shape=None) even when they are 1 at runtime, while xarray does squeeze them. |
| 339 | + """ |
| 340 | + # Create a tensor with a symbolic dimension that will be 1 at runtime |
| 341 | + x = xtensor("x", dims=("a", "b", "c")) # shape unknown |
| 342 | + y = squeeze(x) # implicit dim=None should not squeeze symbolic dimensions |
| 343 | + x_test = xr_arange_like(xtensor(dims=x.dims, shape=(2, 1, 3))) |
| 344 | + fn = xr_function([x], y) |
| 345 | + res = fn(x_test) |
| 346 | + |
| 347 | + # Our implementation should not squeeze the symbolic dimension |
| 348 | + assert "b" in res.dims |
| 349 | + # While xarray would squeeze it |
| 350 | + assert "b" not in x_test.squeeze().dims |
| 351 | + |
| 352 | + |
| 353 | +def test_squeeze_errors(): |
| 354 | + """Test error cases for squeeze.""" |
| 355 | + |
| 356 | + # Non-existent dimension |
| 357 | + x1 = xtensor("x1", dims=("city", "country"), shape=(3, 1)) |
| 358 | + with pytest.raises(ValueError, match="Dimension .* not found"): |
| 359 | + squeeze(x1, "time") |
| 360 | + |
| 361 | + # Dimension size > 1 |
| 362 | + with pytest.raises(ValueError, match="has static size .* not 1"): |
| 363 | + squeeze(x1, "city") |
| 364 | + |
| 365 | + # Symbolic shape: dim is not 1 at runtime → should raise |
| 366 | + x2 = xtensor("x2", dims=("a", "b", "c")) # shape unknown |
| 367 | + y2 = squeeze(x2, "b") |
| 368 | + x2_test = xr_arange_like(xtensor(dims=x2.dims, shape=(2, 2, 3))) |
| 369 | + fn2 = xr_function([x2], y2) |
| 370 | + with pytest.raises(Exception): |
| 371 | + fn2(x2_test) |
0 commit comments