()
| 47 | |
| 48 | @requires_scipy |
| 49 | def test_tree_index_sel() -> None: |
| 50 | xx, yy = np.meshgrid([1.0, 2.0], [3.0, 4.0]) |
| 51 | ds = xr.Dataset(coords={"xx": (("y", "x"), xx), "yy": (("y", "x"), yy)}).set_xindex( |
| 52 | ("xx", "yy"), NDPointIndex |
| 53 | ) |
| 54 | |
| 55 | # 1-dimensional labels |
| 56 | actual = ds.sel( |
| 57 | xx=xr.Variable("u", [1.1, 1.1, 1.1]), |
| 58 | yy=xr.Variable("u", [3.1, 3.1, 3.1]), |
| 59 | method="nearest", |
| 60 | ) |
| 61 | expected = xr.Dataset( |
| 62 | coords={"xx": ("u", [1.0, 1.0, 1.0]), "yy": ("u", [3.0, 3.0, 3.0])} |
| 63 | ) |
| 64 | assert_identical(actual, expected) |
| 65 | |
| 66 | # 2-dimensional labels |
| 67 | actual = ds.sel( |
| 68 | xx=xr.Variable(("u", "v"), [[1.1, 1.1, 1.1], [1.9, 1.9, 1.9]]), |
| 69 | yy=xr.Variable(("u", "v"), [[3.1, 3.1, 3.1], [3.9, 3.9, 3.9]]), |
| 70 | method="nearest", |
| 71 | ) |
| 72 | expected = xr.Dataset( |
| 73 | coords={ |
| 74 | "xx": (("u", "v"), [[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), |
| 75 | "yy": (("u", "v"), [[3.0, 3.0, 3.0], [4.0, 4.0, 4.0]]), |
| 76 | }, |
| 77 | ) |
| 78 | assert_identical(actual, expected) |
| 79 | |
| 80 | # all scalar labels |
| 81 | actual = ds.sel(xx=1.1, yy=3.1, method="nearest") |
| 82 | expected = xr.Dataset(coords={"xx": 1.0, "yy": 3.0}) |
| 83 | assert_identical(actual, expected) |
| 84 | |
| 85 | # broadcast scalar to label shape and dimensions |
| 86 | actual = ds.sel(xx=1.1, yy=xr.Variable("u", [3.1, 3.1, 3.1]), method="nearest") |
| 87 | expected = ds.sel( |
| 88 | xx=xr.Variable("u", [1.1, 1.1, 1.1]), |
| 89 | yy=xr.Variable("u", [3.1, 3.1, 3.1]), |
| 90 | method="nearest", |
| 91 | ) |
| 92 | assert_identical(actual, expected) |
| 93 | |
| 94 | # broadcast orthogonal 1-dimensional labels |
| 95 | actual = ds.sel( |
| 96 | xx=xr.Variable("u", [1.1, 1.1]), |
| 97 | yy=xr.Variable("v", [3.1, 3.1]), |
| 98 | method="nearest", |
| 99 | ) |
| 100 | expected = xr.Dataset( |
| 101 | coords={ |
| 102 | "xx": (("u", "v"), [[1.0, 1.0], [1.0, 1.0]]), |
| 103 | "yy": (("u", "v"), [[3.0, 3.0], [3.0, 3.0]]), |
| 104 | }, |
| 105 | ) |
| 106 | assert_identical(actual, expected) |
nothing calls this directly
no test coverage detected
searching dependent graphs…