-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_xarray.py
471 lines (397 loc) · 17.6 KB
/
test_xarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from collections.abc import Mapping
from unittest.mock import patch
import numpy as np
import pytest
import xarray as xr
import xarray.testing as xrt
from xarray.core.indexes import Index
from virtualizarr import open_virtual_dataset
from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.tests import has_astropy, has_tifffile, network, requires_s3fs
from virtualizarr.zarr import ZArray
def test_wrapping():
chunks = (5, 10)
shape = (5, 20)
dtype = np.dtype("int32")
zarray = ZArray(
chunks=chunks,
compressor="zlib",
dtype=dtype,
fill_value=0.0,
filters=None,
order="C",
shape=shape,
zarr_format=2,
)
chunks_dict = {
"0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "foo.nc", "offset": 200, "length": 100},
}
manifest = ChunkManifest(entries=chunks_dict)
marr = ManifestArray(zarray=zarray, chunkmanifest=manifest)
ds = xr.Dataset({"a": (["x", "y"], marr)})
assert isinstance(ds["a"].data, ManifestArray)
assert ds["a"].shape == shape
assert ds["a"].dtype == dtype
assert ds["a"].chunks == chunks
class TestEquals:
# regression test for GH29 https://github.com/TomNicholas/VirtualiZarr/issues/29
def test_equals(self):
chunks = (5, 10)
shape = (5, 20)
zarray = ZArray(
chunks=chunks,
compressor="zlib",
dtype=np.dtype("int32"),
fill_value=0.0,
filters=None,
order="C",
shape=shape,
zarr_format=2,
)
chunks_dict1 = {
"0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(zarray=zarray, chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
marr2 = ManifestArray(zarray=zarray, chunkmanifest=manifest1)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
assert ds1.equals(ds2)
chunks_dict3 = {
"0.0": {"path": "foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "foo.nc", "offset": 400, "length": 100},
}
manifest3 = ChunkManifest(entries=chunks_dict3)
marr3 = ManifestArray(zarray=zarray, chunkmanifest=manifest3)
ds3 = xr.Dataset({"a": (["x", "y"], marr3)})
assert not ds1.equals(ds3)
# TODO refactor these tests by making some fixtures
class TestConcat:
def test_concat_along_existing_dim(self):
# both manifest arrays in this example have the same zarray properties
zarray = ZArray(
chunks=(1, 10),
compressor="zlib",
dtype=np.dtype("int32"),
fill_value=0.0,
filters=None,
order="C",
shape=(1, 20),
zarr_format=2,
)
chunks_dict1 = {
"0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(zarray=zarray, chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
chunks_dict2 = {
"0.0": {"path": "foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(zarray=zarray, chunkmanifest=manifest2)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
result = xr.concat([ds1, ds2], dim="x")["a"]
assert result.indexes == {}
assert result.shape == (2, 20)
assert result.chunks == (1, 10)
assert result.data.manifest.dict() == {
"0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "foo.nc", "offset": 200, "length": 100},
"1.0": {"path": "foo.nc", "offset": 300, "length": 100},
"1.1": {"path": "foo.nc", "offset": 400, "length": 100},
}
assert result.data.zarray.compressor == zarray.compressor
assert result.data.zarray.filters == zarray.filters
assert result.data.zarray.fill_value == zarray.fill_value
assert result.data.zarray.order == zarray.order
assert result.data.zarray.zarr_format == zarray.zarr_format
def test_concat_along_new_dim(self):
# this calls np.stack internally
# both manifest arrays in this example have the same zarray properties
zarray = ZArray(
chunks=(5, 10),
compressor="zlib",
dtype=np.dtype("int32"),
fill_value=0.0,
filters=None,
order="C",
shape=(5, 20),
zarr_format=2,
)
chunks_dict1 = {
"0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(zarray=zarray, chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
chunks_dict2 = {
"0.0": {"path": "foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(zarray=zarray, chunkmanifest=manifest2)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
result = xr.concat([ds1, ds2], dim="z")["a"]
assert result.indexes == {}
# xarray.concat adds new dimensions along axis=0
assert result.shape == (2, 5, 20)
assert result.chunks == (1, 5, 10)
assert result.data.manifest.dict() == {
"0.0.0": {"path": "foo.nc", "offset": 100, "length": 100},
"0.0.1": {"path": "foo.nc", "offset": 200, "length": 100},
"1.0.0": {"path": "foo.nc", "offset": 300, "length": 100},
"1.0.1": {"path": "foo.nc", "offset": 400, "length": 100},
}
assert result.data.zarray.compressor == zarray.compressor
assert result.data.zarray.filters == zarray.filters
assert result.data.zarray.fill_value == zarray.fill_value
assert result.data.zarray.order == zarray.order
assert result.data.zarray.zarr_format == zarray.zarr_format
def test_concat_dim_coords_along_existing_dim(self):
# Tests that dimension coordinates don't automatically get new indexes on concat
# See https://github.com/pydata/xarray/issues/8871
# both manifest arrays in this example have the same zarray properties
zarray = ZArray(
chunks=(10,),
compressor="zlib",
dtype=np.dtype("int32"),
fill_value=0.0,
filters=None,
order="C",
shape=(20,),
zarr_format=2,
)
chunks_dict1 = {
"0": {"path": "foo.nc", "offset": 100, "length": 100},
"1": {"path": "foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(zarray=zarray, chunkmanifest=manifest1)
coords = xr.Coordinates({"t": (["t"], marr1)}, indexes={})
ds1 = xr.Dataset(coords=coords)
chunks_dict2 = {
"0": {"path": "foo.nc", "offset": 300, "length": 100},
"1": {"path": "foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(zarray=zarray, chunkmanifest=manifest2)
coords = xr.Coordinates({"t": (["t"], marr2)}, indexes={})
ds2 = xr.Dataset(coords=coords)
result = xr.concat([ds1, ds2], dim="t")["t"]
assert result.indexes == {}
assert result.shape == (40,)
assert result.chunks == (10,)
assert result.data.manifest.dict() == {
"0": {"path": "foo.nc", "offset": 100, "length": 100},
"1": {"path": "foo.nc", "offset": 200, "length": 100},
"2": {"path": "foo.nc", "offset": 300, "length": 100},
"3": {"path": "foo.nc", "offset": 400, "length": 100},
}
assert result.data.zarray.compressor == zarray.compressor
assert result.data.zarray.filters == zarray.filters
assert result.data.zarray.fill_value == zarray.fill_value
assert result.data.zarray.order == zarray.order
assert result.data.zarray.zarr_format == zarray.zarr_format
class TestOpenVirtualDatasetAttrs:
def test_drop_array_dimensions(self, netcdf4_file):
# regression test for GH issue #150
vds = open_virtual_dataset(netcdf4_file, indexes={})
assert "_ARRAY_DIMENSIONS" not in vds["air"].attrs
def test_coordinate_variable_attrs_preserved(self, netcdf4_file):
# regression test for GH issue #155
vds = open_virtual_dataset(netcdf4_file, indexes={})
assert vds["lat"].attrs == {
"standard_name": "latitude",
"long_name": "Latitude",
"units": "degrees_north",
"axis": "Y",
}
class TestOpenVirtualDatasetIndexes:
def test_no_indexes(self, netcdf4_file):
vds = open_virtual_dataset(netcdf4_file, indexes={})
assert vds.indexes == {}
def test_create_default_indexes(self, netcdf4_file):
with pytest.warns(UserWarning, match="will create in-memory pandas indexes"):
vds = open_virtual_dataset(netcdf4_file, indexes=None)
ds = xr.open_dataset(netcdf4_file, decode_times=False)
# TODO use xr.testing.assert_identical(vds.indexes, ds.indexes) instead once class supported by assertion comparison, see https://github.com/pydata/xarray/issues/5812
assert index_mappings_equal(vds.xindexes, ds.xindexes)
def index_mappings_equal(indexes1: Mapping[str, Index], indexes2: Mapping[str, Index]):
# Check if the mappings have the same keys
if set(indexes1.keys()) != set(indexes2.keys()):
return False
# Check if the values for each key are identical
for key in indexes1.keys():
index1 = indexes1[key]
index2 = indexes2[key]
if not index1.equals(index2):
return False
return True
class TestCombineUsingIndexes:
def test_combine_by_coords(self, netcdf4_files):
filepath1, filepath2 = netcdf4_files
with pytest.warns(UserWarning, match="will create in-memory pandas indexes"):
vds1 = open_virtual_dataset(filepath1)
with pytest.warns(UserWarning, match="will create in-memory pandas indexes"):
vds2 = open_virtual_dataset(filepath2)
combined_vds = xr.combine_by_coords(
[vds2, vds1],
)
assert combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing
@pytest.mark.xfail(reason="Not yet implemented, see issue #18")
def test_combine_by_coords_keeping_manifestarrays(self, netcdf4_files):
filepath1, filepath2 = netcdf4_files
with pytest.warns(UserWarning, match="will create in-memory pandas indexes"):
vds1 = open_virtual_dataset(filepath1)
with pytest.warns(UserWarning, match="will create in-memory pandas indexes"):
vds2 = open_virtual_dataset(filepath2)
combined_vds = xr.combine_by_coords(
[vds2, vds1],
)
assert isinstance(combined_vds["time"].data, ManifestArray)
assert isinstance(combined_vds["lat"].data, ManifestArray)
assert isinstance(combined_vds["lon"].data, ManifestArray)
@network
@requires_s3fs
class TestReadFromS3:
@pytest.mark.parametrize(
"filetype", ["netcdf4", None], ids=["netcdf4 filetype", "None filetype"]
)
@pytest.mark.parametrize(
"indexes", [None, {}], ids=["None index", "empty dict index"]
)
def test_anon_read_s3(self, filetype, indexes):
"""Parameterized tests for empty vs supplied indexes and filetypes."""
# TODO: Switch away from this s3 url after minIO is implemented.
fpath = "s3://carbonplan-share/virtualizarr/local.nc"
vds = open_virtual_dataset(fpath, filetype=filetype, indexes=indexes)
assert vds.dims == {"time": 2920, "lat": 25, "lon": 53}
for var in vds.variables:
assert isinstance(vds[var].data, ManifestArray), var
@network
class TestReadFromURL:
@pytest.mark.parametrize(
"filetype, url",
[
(
"grib",
"https://github.com/pydata/xarray-data/raw/master/era5-2mt-2019-03-uk.grib",
),
(
"netcdf3",
"https://github.com/pydata/xarray-data/raw/master/air_temperature.nc",
),
(
"netcdf4",
"https://github.com/pydata/xarray-data/raw/master/ROMS_example.nc",
),
(
"hdf4",
"https://github.com/corteva/rioxarray/raw/master/test/test_data/input/MOD09GA.A2008296.h14v17.006.2015181011753.hdf",
),
# https://github.com/zarr-developers/VirtualiZarr/issues/159
# ("hdf5", "https://github.com/fsspec/kerchunk/raw/main/kerchunk/tests/NEONDSTowerTemperatureData.hdf5"),
pytest.param(
"tiff",
"https://github.com/fsspec/kerchunk/raw/main/kerchunk/tests/lcmap_tiny_cog_2020.tif",
marks=pytest.mark.skipif(
not has_tifffile, reason="package tifffile is not available"
),
),
pytest.param(
"fits",
"https://fits.gsfc.nasa.gov/samples/WFPC2u5780205r_c0fx.fits",
marks=pytest.mark.skipif(
not has_astropy, reason="package astropy is not available"
),
),
(
"jpg",
"https://github.com/rasterio/rasterio/raw/main/tests/data/389225main_sw_1965_1024.jpg",
),
],
)
def test_read_from_url(self, filetype, url):
if filetype in ["grib", "jpg", "hdf4"]:
with pytest.raises(NotImplementedError):
vds = open_virtual_dataset(url, reader_options={}, indexes={})
else:
vds = open_virtual_dataset(url, reader_options={}, indexes={})
assert isinstance(vds, xr.Dataset)
class TestLoadVirtualDataset:
def test_loadable_variables(self, netcdf4_file):
vars_to_load = ["air", "time"]
vds = open_virtual_dataset(
netcdf4_file, loadable_variables=vars_to_load, indexes={}
)
for name in vds.variables:
if name in vars_to_load:
assert isinstance(vds[name].data, np.ndarray), name
else:
assert isinstance(vds[name].data, ManifestArray), name
full_ds = xr.open_dataset(netcdf4_file, decode_times=False)
for name in full_ds.variables:
if name in vars_to_load:
xrt.assert_identical(vds.variables[name], full_ds.variables[name])
def test_explicit_filetype(self, netcdf4_file):
with pytest.raises(ValueError):
open_virtual_dataset(netcdf4_file, filetype="unknown")
with pytest.raises(NotImplementedError):
open_virtual_dataset(netcdf4_file, filetype="grib")
@patch("virtualizarr.kerchunk.read_kerchunk_references_from_file")
def test_open_virtual_dataset_passes_expected_args(
self, mock_read_kerchunk, netcdf4_file
):
reader_options = {"option1": "value1", "option2": "value2"}
open_virtual_dataset(netcdf4_file, indexes={}, reader_options=reader_options)
args = {
"filepath": netcdf4_file,
"filetype": None,
"reader_options": reader_options,
}
mock_read_kerchunk.assert_called_once_with(**args)
class TestRenamePaths:
def test_rename_to_str(self, netcdf4_file):
vds = open_virtual_dataset(netcdf4_file, indexes={})
renamed_vds = vds.virtualize.rename_paths("s3://bucket/air.nc")
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
def test_rename_using_function(self, netcdf4_file):
vds = open_virtual_dataset(netcdf4_file, indexes={})
def local_to_s3_url(old_local_path: str) -> str:
from pathlib import Path
new_s3_bucket_url = "s3://bucket/"
filename = Path(old_local_path).name
return str(new_s3_bucket_url + filename)
renamed_vds = vds.virtualize.rename_paths(local_to_s3_url)
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
def test_invalid_type(self, netcdf4_file):
vds = open_virtual_dataset(netcdf4_file, indexes={})
with pytest.raises(TypeError):
vds.virtualize.rename_paths(["file1.nc", "file2.nc"])
def test_mixture_of_manifestarrays_and_numpy_arrays(self, netcdf4_file):
vds = open_virtual_dataset(
netcdf4_file, indexes={}, loadable_variables=["lat", "lon"]
)
renamed_vds = vds.virtualize.rename_paths("s3://bucket/air.nc")
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
assert isinstance(renamed_vds["lat"].data, np.ndarray)
def test_cftime_variables_must_be_in_loadable_variables(tmpdir):
ds = xr.Dataset(data_vars={"time": ["2024-06-21"]})
ds.to_netcdf(f"{tmpdir}/scalar.nc")
with pytest.raises(ValueError, match="'time' not in"):
open_virtual_dataset(f"{tmpdir}/scalar.nc", cftime_variables=["time"])