|
5 | 5 | import shutil |
6 | 6 | import unittest |
7 | 7 |
|
| 8 | +import numpy as np |
| 9 | +import pytest |
8 | 10 | import xarray as xr |
9 | 11 |
|
10 | 12 | from zappend.api import FileObj |
|
14 | 16 | from .helpers import make_test_dataset |
15 | 17 |
|
16 | 18 |
|
| 19 | +# noinspection PyMethodMayBeStatic |
17 | 20 | class ApiTest(unittest.TestCase): |
18 | 21 | def setUp(self): |
19 | 22 | clear_memory_fs() |
@@ -73,6 +76,140 @@ def process_slice(ctx, slice_ds: xr.Dataset) -> SliceSource: |
73 | 76 | self.assertEqual({"chl"}, set(ds.data_vars)) |
74 | 77 | self.assertEqual({"time", "y", "x"}, set(ds.coords)) |
75 | 78 |
|
| 79 | + def test_some_slices_with_inc_append_step(self): |
| 80 | + target_dir = "memory://target.zarr" |
| 81 | + slices = [ |
| 82 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 83 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 84 | + make_test_dataset(index=2, shape=(1, 50, 100)), |
| 85 | + ] |
| 86 | + zappend(slices, target_dir=target_dir, append_step="1D") |
| 87 | + ds = xr.open_zarr(target_dir) |
| 88 | + np.testing.assert_array_equal( |
| 89 | + ds.time.values, |
| 90 | + np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=np.datetime64), |
| 91 | + ) |
| 92 | + |
| 93 | + def test_some_slices_with_dec_append_step(self): |
| 94 | + target_dir = "memory://target.zarr" |
| 95 | + slices = [ |
| 96 | + make_test_dataset(index=2, shape=(1, 50, 100)), |
| 97 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 98 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 99 | + ] |
| 100 | + zappend(slices, target_dir=target_dir, append_step="-1D") |
| 101 | + ds = xr.open_zarr(target_dir) |
| 102 | + np.testing.assert_array_equal( |
| 103 | + ds.time.values, |
| 104 | + np.array(["2024-01-03", "2024-01-02", "2024-01-01"], dtype=np.datetime64), |
| 105 | + ) |
| 106 | + |
| 107 | + # # See https://github.com/bcdev/zappend/issues/21 |
| 108 | + # |
| 109 | + # def test_some_slices_with_one_missing_append_step(self): |
| 110 | + # target_dir = "memory://target.zarr" |
| 111 | + # slices = [ |
| 112 | + # make_test_dataset(index=0, shape=(1, 50, 100)), |
| 113 | + # make_test_dataset(index=2, shape=(1, 50, 100)), |
| 114 | + # ] |
| 115 | + # zappend(slices, target_dir=target_dir, append_step="1D") |
| 116 | + # ds = xr.open_zarr(target_dir) |
| 117 | + # np.testing.assert_array_equal( |
| 118 | + # ds.time.values, |
| 119 | + # np.array( |
| 120 | + # ["2024-01-01", "2024-01-02", "2024-01-03"], dtype="datetime64[ns]" |
| 121 | + # ), |
| 122 | + # ) |
| 123 | + |
| 124 | + # # See https://github.com/bcdev/zappend/issues/21 |
| 125 | + # |
| 126 | + # def test_some_slices_with_three_missing_append_steps(self): |
| 127 | + # target_dir = "memory://target.zarr" |
| 128 | + # slices = [ |
| 129 | + # make_test_dataset(index=0, shape=(1, 50, 100)), |
| 130 | + # make_test_dataset(index=4, shape=(1, 50, 100)), |
| 131 | + # ] |
| 132 | + # zappend(slices, target_dir=target_dir, append_step="1D") |
| 133 | + # ds = xr.open_zarr(target_dir) |
| 134 | + # np.testing.assert_array_equal( |
| 135 | + # ds.time.values, |
| 136 | + # np.array( |
| 137 | + # [ |
| 138 | + # "2024-01-01", |
| 139 | + # "2024-01-02", |
| 140 | + # "2024-01-03", |
| 141 | + # "2024-01-04", |
| 142 | + # "2024-01-05", |
| 143 | + # ], |
| 144 | + # dtype="datetime64[ns]", |
| 145 | + # ), |
| 146 | + # ) |
| 147 | + |
| 148 | + def test_it_raises_for_wrong_append_step(self): |
| 149 | + target_dir = "memory://target.zarr" |
| 150 | + slices = [ |
| 151 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 152 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 153 | + ] |
| 154 | + with pytest.raises( |
| 155 | + ValueError, |
| 156 | + match=( |
| 157 | + "Cannot append slice because this would result in" |
| 158 | + " an invalid step size." |
| 159 | + ), |
| 160 | + ): |
| 161 | + zappend(slices, target_dir=target_dir, append_step="2D") |
| 162 | + |
| 163 | + def test_some_slices_with_inc_append_labels(self): |
| 164 | + append_step = "+" |
| 165 | + |
| 166 | + target_dir = "memory://target.zarr" |
| 167 | + slices = [ |
| 168 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 169 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 170 | + make_test_dataset(index=2, shape=(1, 50, 100)), |
| 171 | + ] |
| 172 | + # OK! |
| 173 | + zappend(slices, target_dir=target_dir, append_step=append_step) |
| 174 | + |
| 175 | + target_dir = "memory://target.zarr" |
| 176 | + slices = [ |
| 177 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 178 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 179 | + ] |
| 180 | + with pytest.raises( |
| 181 | + ValueError, |
| 182 | + match=( |
| 183 | + "Cannot append slice because labels must be monotonically increasing" |
| 184 | + ), |
| 185 | + ): |
| 186 | + zappend(slices, target_dir=target_dir, append_step=append_step) |
| 187 | + |
| 188 | + def test_some_slices_with_dec_append_labels(self): |
| 189 | + append_step = "-" |
| 190 | + |
| 191 | + target_dir = "memory://target.zarr" |
| 192 | + slices = [ |
| 193 | + make_test_dataset(index=2, shape=(1, 50, 100)), |
| 194 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 195 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 196 | + ] |
| 197 | + # OK! |
| 198 | + zappend(slices, target_dir=target_dir, append_step=append_step) |
| 199 | + |
| 200 | + target_dir = "memory://target.zarr" |
| 201 | + slices = [ |
| 202 | + make_test_dataset(index=0, shape=(1, 50, 100)), |
| 203 | + make_test_dataset(index=1, shape=(1, 50, 100)), |
| 204 | + ] |
| 205 | + with pytest.raises( |
| 206 | + ValueError, |
| 207 | + match=( |
| 208 | + "Cannot append slice because labels must be monotonically decreasing" |
| 209 | + ), |
| 210 | + ): |
| 211 | + zappend(slices, target_dir=target_dir, append_step=append_step) |
| 212 | + |
76 | 213 | def test_some_slices_with_profiling(self): |
77 | 214 | target_dir = "memory://target.zarr" |
78 | 215 | slices = [ |
|
0 commit comments