-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
228 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Copyright (c) Microsoft Corporation. Licensed under the MIT license.""" | ||
|
||
import pickle | ||
from datetime import datetime | ||
from typing import Generator, TypedDict | ||
|
||
import numpy as np | ||
import pytest | ||
import torch | ||
from huggingface_hub import hf_hub_download | ||
|
||
from aurora import Batch, Metadata | ||
from aurora.batch import interpolate_numpy | ||
|
||
|
||
class SavedMetadata(TypedDict): | ||
"""Type of metadata of a saved test batch.""" | ||
|
||
lat: np.ndarray | ||
lon: np.ndarray | ||
time: list[datetime] | ||
atmos_levels: list[int | float] | ||
|
||
|
||
class SavedBatch(TypedDict): | ||
"""Type of a saved test batch.""" | ||
|
||
surf_vars: dict[str, np.ndarray] | ||
static_vars: dict[str, np.ndarray] | ||
atmos_vars: dict[str, np.ndarray] | ||
metadata: SavedMetadata | ||
|
||
|
||
@pytest.fixture() | ||
def test_input_output() -> Generator[tuple[Batch, SavedBatch], None, None]: | ||
# Load test input. | ||
path = hf_hub_download( | ||
repo_id="microsoft/aurora", | ||
filename="aurora-0.25-small-pretrained-test-input.pickle", | ||
) | ||
with open(path, "rb") as f: | ||
test_input: SavedBatch = pickle.load(f) | ||
|
||
# Load static variables. | ||
path = hf_hub_download( | ||
repo_id="microsoft/aurora", | ||
filename="aurora-0.25-static.pickle", | ||
) | ||
with open(path, "rb") as f: | ||
static_vars: dict[str, np.ndarray] = pickle.load(f) | ||
|
||
static_vars = { | ||
k: interpolate_numpy( | ||
v, | ||
np.linspace(90, -90, v.shape[0]), | ||
np.linspace(0, 360, v.shape[1], endpoint=False), | ||
test_input["metadata"]["lat"], | ||
test_input["metadata"]["lon"], | ||
) | ||
for k, v in static_vars.items() | ||
} | ||
|
||
# Construct a proper batch from the test input. | ||
batch = Batch( | ||
surf_vars={k: torch.from_numpy(v) for k, v in test_input["surf_vars"].items()}, | ||
static_vars={k: torch.from_numpy(v) for k, v in static_vars.items()}, | ||
atmos_vars={k: torch.from_numpy(v) for k, v in test_input["atmos_vars"].items()}, | ||
metadata=Metadata( | ||
lat=torch.from_numpy(test_input["metadata"]["lat"]), | ||
lon=torch.from_numpy(test_input["metadata"]["lon"]), | ||
atmos_levels=tuple(test_input["metadata"]["atmos_levels"]), | ||
time=tuple(test_input["metadata"]["time"]), | ||
), | ||
) | ||
|
||
# Load test output. | ||
path = hf_hub_download( | ||
repo_id="microsoft/aurora", | ||
filename="aurora-0.25-small-pretrained-test-output.pickle", | ||
) | ||
with open(path, "rb") as f: | ||
test_output: SavedBatch = pickle.load(f) | ||
|
||
yield batch, test_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Copyright (c) Microsoft Corporation. Licensed under the MIT license.""" | ||
|
||
import numpy as np | ||
|
||
from tests.conftest import SavedBatch | ||
|
||
from aurora import Batch | ||
|
||
|
||
def test_interpolation(test_input_output: tuple[Batch, SavedBatch]) -> None: | ||
batch, _ = test_input_output | ||
|
||
# Regridding to the same resolution shouldn't change the data. | ||
batch_regridded = batch.regrid(0.45) | ||
batch_regridded = batch_regridded.crop(4) # Regridding added the south pole. Remove it again. | ||
|
||
for k in batch.surf_vars: | ||
np.testing.assert_allclose( | ||
batch.surf_vars[k], | ||
batch_regridded.surf_vars[k], | ||
rtol=5e-6, | ||
) | ||
for k in batch.static_vars: | ||
np.testing.assert_allclose( | ||
batch.static_vars[k], | ||
batch_regridded.static_vars[k], | ||
atol=1e-7, | ||
) | ||
for k in batch.atmos_vars: | ||
np.testing.assert_allclose( | ||
batch.atmos_vars[k], | ||
batch_regridded.atmos_vars[k], | ||
rtol=5e-6, | ||
) | ||
|
||
np.testing.assert_allclose(batch.metadata.lat, batch_regridded.metadata.lat, atol=1e-10) | ||
np.testing.assert_allclose(batch.metadata.lon, batch_regridded.metadata.lon, atol=1e-10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters